Parsing and Parse Data
The Visual Basic language uses an advanced parser that has been constructed using the Actipro LL(*) Parser Framework, and provides functionality for building ASTs, reporting parse errors, and more.
The ILLParseData results returned by this parser are consumed by multiple other features of the language, such as code outlining, parse error display, etc.
AST Construction
A complete AST of parsed documents is generated by the parser, comprised of the various type-specific AST nodes created for this language. It is passed back in the ILLParseData.Ast property.
The AST node classes are all defined in the ActiproSoftware.Text.Languages.DotNet.Ast.Implementation namespace, while related enumerations and intefaces are defined in the ActiproSoftware.Text.Languages.DotNet.Ast namespace.
Parse Errors
Parse errors are reported from the parser. They are passed back in the ILLParseData.Errors property.
The parser reports errors as it performs syntax and semantic analysis of document text.
When the language is used in a SyntaxEditor control, the parse errors will have squiggle lines under them and mouse hovering over the squiggle lines will show IntelliPrompt quick info tips with error descriptions.
Parse errors can also be consumed by applications that wish to show the parse errors in a tool window.
Retrieving Parse Data
As long as an ambient parse request dispatcher has been set up, all parsing and validation is performed in a worker thread. The processing occurs after any text changes are made to documents using the language. Since the work occurs in a worker thread, the results are returned asynchronously and may not be in perfect sync with the document text. Although since the parse data includes the ITextSnapshot instance that was parsed, if known, you can easily translate offsets from the parse data snapshot to the current snapshot in a SyntaxEditor view or document.
Once a parsing operation completes, the ILLParseData results are placed into the ParseData property of the document that was parsed and its ParseDataChanged event fires. The latest parse data results can be obtained at any time from that property.
See the Parsers topic for more detailed information on the parsing workflow and parse data.
Parsing a Text String
See the Parsers for details on how to easily parse a text string using a language's parser, such as this one.
The VBParser class also has a special helper static method named ParseExpression that takes a string containing a Visual Basic expression and returns the IDotNetParseData parse results.
Grammar Options
The Visual Basic grammar (VBGrammar class) constructor can be passed an IVBGrammarOptions object that indicates options for the grammar's creation. The VBGrammarOptions class implements this interface.
Root Non-Terminal
The default options indicate that compilation unit should be the root non-terminal for the grammar. However this can be altered via the IVBGrammarOptions.RootNonTerminal property. The DotNetGrammarRootNonTerminal enumeration has a number of options for a root non-terminal, everything from expressions to statements.
Use this code to alter the root non-terminal for the grammar so that it tries to parse a single expression instead of a compilation unit:
VBSyntaxLanguage language = new VBSyntaxLanguage();
VBGrammarOptions grammarOptions = new VBGrammarOptions();
grammarOptions.RootNonTerminal = DotNetGrammarRootNonTerminal.Expression;
VBGrammar grammar = new VBGrammar(grammarOptions);
language.RegisterParser(new VBParser(grammar));
Note
The SyntaxLanguageExtensions.RegisterParser method in the code snippet above is a helper extension method that gets added to ISyntaxLanguage objects when the ActiproSoftware.Text
namespace is imported. See the Service Locator Architecture topic for details on registering and retrieving various service object instances, both via extension methods and generically, as there are some additional requirements for using the extension methods.