Context:
When I learned about parsers, the process of compiling code (say, C++) was explained like this:
- Write code in a file and save it.
- Put the file in a compiler.
- The compiler first parses the code into an abstract syntax tree,
- then generates machine code.
- Run the code to test it.
- Repeat.
Bret Victor wanted a kind of programming environment that evaluates the code as you type. ( http://worrydream.com/#!/InventingOnPrinciple )
I guess he wasn't the first, that there might be some conceptual problems for translating that concept to general purpose programming beyond 2D game programming, and I know that there are some systems that already do something similar: E.g. preadsheets (like Excel), Smalltalk.
That's not what I want to discuss.
Question: (kind of broad, sorry - the main question is in bold)
How could parsing text while editing work? I had the idea, that whenever the editor sends an event indicating that some portion of text is changed, only a portion of the AST gets reevaluated and values that are affected by this portion of the AST also get reevaluated.
I thought about writing a parser generator that takes a grammar like usual, but produces a parser that processes incremental changes to a text, instead of a whole text.
1. Is this a reasonable concept? (For whatever obscure programming language/environment. Something "functional reactive" maybe. Or simply html.)
(2.) Is it maybe even used yet?
(3.) Is parsing the whole file fast enough to make the complicated approach unnecessary?
(4.) Do syntax highlighter or type-checker in IDEs such as Eclipse work like this? How do they work instead? I think they aren't as powerful as compiler-parsers to make them work fast enough, is that right?
(5.) Here in Stackoverflow there is a live preview of the styled text. Does it parse the whole question after every keystroke? Does it have some limitations that "my" concept would solve?