Exception | Description |
---|---|
System.ArgumentNullException | Occurs when symbol is null. |
System.InvalidOperationException | Occurs when the IsMutable value of the owning Grammar is False. |
System.ArgumentException | Occurs when symbol is not from the same Grammar as the NonTerminalSymbol. |
When parsing a document, if an unexpected token is encountered from the lexical analyzer, the parse must attempt to recover from the error. It uses a number of strategies to do this, each of which handles different kinds of errors. One of these strategies will take the non-terminals which are currently in construction when the error occurred and see if the unexpected token from the lexical analyzer should have actually come later in one of those non-terminals. If so, it will insert the missing structures up to the unexpected token, thereby making the token "expected" and the parse can continue. However, it is possible that this type of error recovery is incorrect for certain non-terminals.
For example, a class declaration in C# can have an optional semicolon token after its close brace. If an erroneous semicolon is typed within the class members section, like so: "class X{ ; }", the error recovery routine will assume the semicolon is the optional semicolon after the closing brace. It will then insert the missing closing brace into the parse tree. When it reaches the actual closing brace, it will be skipped and marked as an error for being unexpected. One could argue that this is actually a correct reporting of errors, but things get much worse when there are class members declared after the semicolon. All tokens representing those members could be skipped if they cannot exist after the end of a class declaration.
To solve this issue, certain non-terminals may need to know that an erroneous token should really be skipped rather than used in the error recovery process. In the example above, semicolons are only optional at the end of the class. Intuitively, one can see that the close brace should have priority over the semicolon to signal the end of the class. Therefore, the semicolon in the a class declaration should not be considered in the error recovery routine. So the TerminalSymbol representing the semicolon should be passed to the SuppressErrorRecoveryForSymbol method on the NonTerminalSymbol representing the class declaration.
Note: The suppression will only work if the specified symbol is directly referenced by the NonTerminalSymbol in its rule hierarchy. If the symbol is indirectly referenced through a reference to another non-terminal symbol, this call will have no effect on the error recovery process.
Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2