Colorization of user defined types

Nov 8, 2012 at 10:09 AM


Syntax highlighting - in Visual Studio - is done by repeatetly calling to

IScanner.ScanTokenAndProvideInfoAboutIt(TokenInfo, ref int)

So far so good, but all identifiers are colored the same way. Suppose I want to use a different color for, e.g., Types, Functions, Variables, etc., how would I do that?

The parser already works, so I actually have a list of types, functions, variables available. The problem is, however, that I cannot simply perform a string comparison, because the scope is important. A local variable defined in a function must not be highlighted as variable outside this very function... But in the ScanTokenAndProvideInfoAboutInt, there is no way of telling the current scope. This is OK I think, because the linescanner shall be QUICK for syntax highlighting. Anyway, I'm missing the connection to coloring user defined types depending on the current scope.



Nov 8, 2012 at 2:44 PM

The only way I figured out so far is to keep track of the scope while parsing, thus generating some kind of "scope tree" for the current file. Later, for syntax-highlighting, one can look up the scope of the current token and retrieve the respective symbols for this very scope.

Nov 9, 2012 at 5:11 PM

Associating names with variables in scopes is part of "semantics", semantic analysis that is a separate step that works with the entire parse tree. So when scanning at line level, the proper way is to hook to previously prepared semantic info on the entire text in the editor (which should be refreshed regularly). How to do this in details - have no immediate detailed recipe, sorry.

Nov 9, 2012 at 6:31 PM


Thanks for the quick reply. I just wanted some confirmation that this is the correct way to go, and that I'm not missing some already existing link.

BTW, when I was working on a respective solution for semantic-based coloring, I came about this issue:
If you want to incorporate semantics at LineScanning-level, you need the correct location of the currently processed token, in order to retrieve the current context's scope (for symbol visibility).