|
Enhanced C#
Loyc library documentation
|
An base class designed for parsers that use LLLPG (Loyc LL(k) Parser Generator) and receive tokens from any IEnumerator{Token}. (Potentially also useful for parsers written by hand.) More...
An base class designed for parsers that use LLLPG (Loyc LL(k) Parser Generator) and receive tokens from any IEnumerator{Token}. (Potentially also useful for parsers written by hand.)
The compiler will ensure that you use this base class correctly. All you have to do is call the base class constructor and override the abstract method ToString(MatchType).
This version of BaseParserForList has List (a token list) as a generic parameter. Compared to using IList{Token} directly, this can increase performance in case the List is a value type (e.g. InternalList<Token>).
| Token | Data type of complete tokens in the token list. A token contains the type of a "word" in the program (string, identifier, plus sign, etc.), a value (e.g. the name of an identifier), and a range of characters in the source file. See ISimpleToken{MatchType}. Note: Token is usually a small struct; this class assumes that it will never be null. |
| MatchType | A data type, usually int, that represents a token type (identifier, operator, etc.) and implements IEquatable{T} so it can be compared for equality with other token types; this is also the type of the ISimpleToken{Matchtype}.Type property. Unfortunately, MatchType cannot be an enum because, strangely, an enum does not implement IEquatable{T}. So if your token type is an enum, as it usually is, set this to int and use matchType: int when you invoke LLLPG: LLLPG(parser(matchType: int, laType: TokenEnum));. |
| List | Data type of the list that contains the tokens (one often uses IList{Token}, but one could use Loyc.Collections.Impl.InternalList{T} for potentially higher performance.) |
| Token | : | ISimpleToken<MatchType> | |
| MatchType | : | IEquatable<MatchType> | |
| List | : | IList<Token> |
Properties | |
| Token | EofToken [get] |
| List | TokenList [get] |
| The IList{Token} that was provided to the constructor, if any. More... | |
| sealed override MatchType | LA0Int [get] |
| MatchType | LA0 [get] |
| new int | InputPosition [get, set] |
Properties inherited from Loyc.Syntax.BaseParser< Token, MatchType > | |
| IMessageSink | ErrorSink [get, set] |
| Gets or sets the object to which error messages are sent. The default object is LogExceptionErrorSink, which throws an exception if an error occurs. More... | |
| ISourceFile | SourceFile [get] |
| The ISourceFile object that was provided to the constructor, if any. More... | |
| Token | LT0 [get] |
| Next token to parse (cached; is set to LT(0) whenever InputPosition is changed). More... | |
| int | InputPosition [get, set] |
| Current position of the next token to be parsed. More... | |
| abstract MatchType | LA0Int [get] |
| Returns the token type of _lt0 (normally _lt0.TypeInt) More... | |
Protected Member Functions | |
| BaseParserForList (List list, Func< Token, Token > getEofToken, MatchType eof, ISourceFile file, int startIndex=0) | |
| Initializes this object to begin parsing the specified tokens. More... | |
| BaseParserForList (List list, Token getEofToken, ISourceFile file, int startIndex=0) | |
| virtual void | Reset (List list, Token eofToken, ISourceFile file, int startIndex=0) |
| Reinitializes the object. This method is called by the constructor. More... | |
| virtual void | Reset (List list, Func< Token, Token > getEofToken, MatchType eof, ISourceFile file, int startIndex=0) |
| Reinitializes the object. This method is called by the constructor. More... | |
| void | Reset () |
| sealed override MatchType | EofInt () |
| Returns the value used for EOF (normally 0) More... | |
| sealed override Token | LT (int i) |
Returns the token at lookahead i (e.g. Source[InputPosition + i] if the tokens come from a list called Source) More... | |
| MatchType | LA (int i) |
| abstract override string | ToString (MatchType tokenType) |
| Returns a string representation of the specified token type. These strings are used in error messages. More... | |
| bool | Down (List children) |
| Switches to parsing the specified token list at position zero (typically the value of Loyc.Syntax.Lexing.Token.Children in a token tree produced by TokensToTree.) The original token list and the original InputPosition are placed on a stack, so you can restore the old list by calling Up(). More... | |
| void | Up () |
| Returns to the old token list saved by Down. More... | |
| T | Up< T > (T value) |
Calls Up() and returns value. More... | |
Protected Member Functions inherited from Loyc.Syntax.BaseParser< Token, MatchType > | |
| BaseParser (ISourceFile?file=null, int startIndex=0) | |
| Initializes the base class. More... | |
| virtual int | LaIndexToCharIndex (int lookaheadIndex) |
| Converts a lookahead token index to a character index (used for error reporting). More... | |
| virtual object | LaIndexToMsgContext (int lookaheadIndex) |
| Converts a lookahead token index to a SourceRange (or to a string if SourceFile was initialized to null.) More... | |
| virtual void | Error (int lookaheadIndex, string message) |
| Records an error or throws an exception. More... | |
| virtual void | Error (int lookaheadIndex, string format, params object?[] args) |
| void | Skip () |
| Token | MatchAny () |
| Token | Match (HashSet< MatchType > set, bool inverted=false) |
| Token | Match (MatchType a) |
| Token | Match (MatchType a, MatchType b) |
| Token | Match (MatchType a, MatchType b, MatchType c) |
| Token | Match (MatchType a, MatchType b, MatchType c, MatchType d) |
| Token | MatchExcept () |
| Token | MatchExcept (MatchType a) |
| Token | MatchExcept (MatchType a, MatchType b) |
| Token | MatchExcept (MatchType a, MatchType b, MatchType c) |
| Token | MatchExcept (MatchType a, MatchType b, MatchType c, MatchType d) |
| Token | MatchExcept (HashSet< MatchType > set) |
| bool | TryMatch (HashSet< MatchType > set, bool inverted=false) |
| bool | TryMatch (MatchType a) |
| bool | TryMatch (MatchType a, MatchType b) |
| bool | TryMatch (MatchType a, MatchType b, MatchType c) |
| bool | TryMatch (MatchType a, MatchType b, MatchType c, MatchType d) |
| bool | TryMatchExcept () |
| bool | TryMatchExcept (MatchType a) |
| bool | TryMatchExcept (MatchType a, MatchType b) |
| bool | TryMatchExcept (MatchType a, MatchType b, MatchType c) |
| bool | TryMatchExcept (MatchType a, MatchType b, MatchType c, MatchType d) |
| bool | TryMatchExcept (HashSet< MatchType > set) |
| void | MatchError (bool inverted, params MatchType[] expected) |
| virtual void | MatchError (bool inverted, IEnumerable< MatchType > expected) |
| Handles an error that occurs during Match() or MatchExcept() More... | |
| virtual string | ToString (bool inverted, IEnumerable< MatchType > expected) |
| virtual void | Check (bool expectation, string expectedDescr="") |
Protected fields | |
| Func< Token, Token > | _getEofToken |
| List | _tokenList |
Protected fields inherited from Loyc.Syntax.BaseParser< Token, MatchType > | |
| ISourceFile | _sourceFile |
| Token | _lt0 = default! |
| int | _inputPosition |
| MatchType | EOF |
Additional Inherited Members | |
Public static fields inherited from Loyc.Syntax.BaseParser< Token, MatchType > | |
| static readonly IMessageSink | LogExceptionErrorSink |
| Throws LogException when it receives an error. Non-errors are sent to MessageSink.Default. More... | |
| static readonly IMessageSink | FormatExceptionErrorSink |
Static Protected Member Functions inherited from Loyc.Syntax.BaseParser< Token, MatchType > | |
| static HashSet< MatchType > | NewSet (params MatchType[] items) |
|
inlineprotected |
Initializes this object to begin parsing the specified tokens.
| list | A list of tokens that the derived class will parse. |
| getEofToken | This is either a token value to return when the input position reaches the end of the token list, or a function that computes a "synthetic" EOF token given the final token in the file. Ideally the StartIndex of the EOF token contains the position of EOF for proper error handling; if your grammar has expects a certain token, it's usually possible to receive a different token or EOF instead, which may not have a valid Value but should have correct position info so that you can construct your syntax tree. If your token stream is lazy- loaded then you don't know the EOF position yet, which is why you may wish to pass a method instead of just a token. |
In any case, BaseParser{Tok,MT}.LaIndexToCharIndex does not trust this value, and will ensure that the character index returned for EOF is at least as large as the character index of the last token in the file, so that error messages involving EOF won't be overly inaccurate.
| eof | The value of the EOF property used by LLLPG to detect EOF. |
| file | A source file object that will be returned by the SourceFile property. By default, this object is used to get the file name, line number and column number shown in parser errors. If your lexer uses BaseLexer or LexerSource, you can get this object from the BaseLexer{C}.SourceFile property. The SourceFile property (in this class) will return this value. If this parameter is null, then by default, error messages will only show the character index instead of the file, line number and column number. |
| startIndex | The initial index from which to start reading tokens from the list (normally 0). |
|
inlineprotected |
Switches to parsing the specified token list at position zero (typically the value of Loyc.Syntax.Lexing.Token.Children in a token tree produced by TokensToTree.) The original token list and the original InputPosition are placed on a stack, so you can restore the old list by calling Up().
children is null.
|
inlineprotectedvirtual |
Returns the value used for EOF (normally 0)
Implements Loyc.Syntax.BaseParser< Token, MatchType >.
|
inlineprotectedvirtual |
Returns the token at lookahead i (e.g. Source[InputPosition + i] if the tokens come from a list called Source)
Implements Loyc.Syntax.BaseParser< Token, MatchType >.
Reimplemented in Loyc.Syntax.ParserSource< Token, MatchType, List >.
|
protectedvirtual |
Reinitializes the object. This method is called by the constructor.
See the constructor for documentation of the parameters.
Reimplemented in Loyc.Syntax.ParserSource< Token, MatchType, List >.
|
inlineprotectedvirtual |
Reinitializes the object. This method is called by the constructor.
See the constructor for documentation of the parameters.
|
protectedpure virtual |
Returns a string representation of the specified token type. These strings are used in error messages.
Implements Loyc.Syntax.BaseParser< Token, MatchType >.
Implemented in Loyc.Syntax.ParserSource< Token, MatchType, List >.
Returns to the old token list saved by Down.
Calls Up() and returns value.
1.8.7