In a recent school software engineering module, I needed to developed a static program analyzer. Given a ficticious programming language source code, I need to analyze it, then the user can query about the program, eg. what statements uses this variable, which modifies, statements affected by another etc.
Possible link with IDEs
At first, it sounded very useless... it was a software engineering module but in reality more like some theoretical programming language module instead. However, some of these functionality could be what goes into the IDE's we use. So I was wondering how might some common IDE functionality be implemented.
Static Program Analyzer (how to make it dynamic/realtime)
In the project, we designed the analyzer for a static program ... but who needs that? In the IDEs we use, everything is near realtime, as we type, syntax highlighting is done, the IDE's analyzes the code for errors, variables that are not used, etc. How is it done so efficiently?
In the static analyzer, there is a parser that parses the source, builds AST & CFG. This can then be used to see if a variable is used by some statement etc. In reality what can be cached are cached. There are many areas that can be optimized, but I am not sure any (I can think of) can make it realtime.
Some problems I can see are:
- As we type, things are incomplete, this will lead to a parsing error, but a real IDE somehow is smart enough to know where to continue parsing.
- As we type, things change very rapidly, we cannot parse the entire program. Even if I make the parser smart enough and parse only the changed line, I need to update my AST/CFG/expire relevant cache etc.
In a real IDE, I don't see that CPU usage spikes as we type etc. How is it done?
Some might suggest looking at the source of some open source IDE eg. Eclipse or Netbeans. But I expect it to be really hard to understand such a big app by just reading code. What I hope to get is a high level idea of how might a simple IDE (and some of the commonly used features) be implemented, a simple paragraph or some pseudocode etc. Some common features I can think of now are
- Refactoring (something like a contextual find and replace)
- Find usages
- Go to definition
- Auto complete (find all possible classes, variables, methods in context)
- Syntax highlighting
For static code, some might be trivial to implement, but an IDE is (near) realtime