Getting a non-null root to the AST when status is Error

Aug 18, 2010 at 9:33 PM

Hello,

Thanks for the tool, very helpful.

I am integrating a language into VS2010. I am working on support for outlining. As long as the code is syntactically correct, outlining works perfect. However, if it is not, the Parser.Parse() method returns a ParseTree with a null Root, which forces me to continue to re-parse until I get a correct AST that I can then feed to the regions computation. Additionally, if I am opening the file for the first time and it has a syntax error I can't display any outlining until it is fixed. I was wondering if I could get some help on modifying the behavior of the Parse() method to return a ParseTree with a non-null root of the tree even when the parse status is Error. I am assuming that when the parser recovers, in the end a parse tree with a non-null ParseTreeNode root must be there somewhere but not exposed. Any help with this is greatly appreciated.

 

Mohamed

Coordinator
Aug 19, 2010 at 7:24 AM

Are you using error recovery rules? There's ErrorRule property in NonTerminal, similar to Rule; in ErrorRule you specify how to recover from errors to continue parsing. Have a look at c# grammar and how it parses; it has ErrorRule set in some key NonTerminals (like Statement), so parser is able to recover and continue, so even incorrect source is colorized

Sep 8, 2010 at 9:55 PM

Thanks a lot, Roman, for the suggestion.

I am using error rules and syntax coloring has been working fine. However, outlining (code regions) does not seem to work. Specifically, if I open the file after it was saved with syntax errors the outliner fails to find the regions in the code because it cannot get a tree with a non-null root to traverse. Once I correct the code, outlining works perfect. Any ideas here would be greatly appreciated.

Mohamed

Coordinator
Sep 8, 2010 at 10:04 PM

well, try to figure out what's different about parsing after opening the file, compared to parsing on-the-fly while typing... it's hard to say from here what can be wrong. It might be some bug in recovery code, it's possible, so try to trace it through.

Roman

Sep 14, 2010 at 4:15 PM

You were right. A couple of error rules were a little off. Thanks for lending the insight, Roman!

 

Mohamed