0

Imagine programming something in Java or C++. You have a file, which is about 5000 lines worth of pure code. Every time you type a single letter, and pause for a split second (think Eclipse or Visual C++), the IDE will show a bunch of errors throughout your file (or maybe only the same line, depending on the editor). What I'm wondering is, how does an IDE do this? I can only imagine that an IDE which reads the entire text file every single time you type a letter, would be majorly inefficient and would probably start slowing down real fast when someone hits a certain quantity of characters.

I'm not looking for a complete algorithmic solution or something like that. I'm looking to write a very simple editor for fun, and I'm wondering how I should incorporate the whole scanning of a file thing. Even if I want to have a different color for a keyword (this, class, static, extern, transient, public, private, protected, etc), I'd have to be able to know how I should implement this scanning "algorithm".

ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • What is your knowledge of grammars/parsers/compilers? Because that is what you are going to do for this. Alternatively, identifying keywords is easier, although if you do not use a grammar you will have trouble with identifiers in comments or literals (v.g., marking the `if` in `String a = "this if a";` as a keyword). – SJuan76 Jun 17 '13 at 01:08
  • I have 0 knowledge on the things you mentioned, but even so, starting out with only keywords shouldn't be impossible. It's not even the way in which it is done that interests me, it's the frequency and the quantity of code scanned every time a character is written. – ZimZim Jun 17 '13 at 06:00
  • keyword identification is so trivial that it can be done at rendering time. Grammar parsing probably relies in storing the file structure in memory and modifying only the part altered by your modifications (maybe in a separate thread). – SJuan76 Jun 17 '13 at 08:57

1 Answers1

0

There is no single algorithm as such, but as usual divide and conquer. Just like large XML files can be manipulated using DOM structure, similarly source code can be kept in something called AST (Abstract Syntax Tree). You can read more on Wikipedia and other places, the following are few links which might be good start here

http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

Community
  • 1
  • 1
Hemant
  • 4,537
  • 8
  • 41
  • 43