Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Nested classes | Public static fields | Properties | Protected Member Functions | Static Protected Member Functions | Protected fields | List of all members
Loyc.Syntax.BaseParser< Token, MatchType > Class Template Referenceabstract

An base class designed for parsers that use LLLPG (Loyc LL(k) Parser Generator). Note: this is the old (harder to use) base class design. You should use BaseParserForList{Token,MatchType} instead. More...


Source file:
Inheritance diagram for Loyc.Syntax.BaseParser< Token, MatchType >:
Loyc.Syntax.BaseParser< Token > Loyc.Syntax.BaseParser< Token, MatchType >.SavePosition Loyc.Syntax.BaseParserForList< Token, MatchType, List > Loyc.Ecs.Parser.EcsParser Loyc.Syntax.BaseParserNoBacktracking< Token, Enumerator > Loyc.Syntax.BaseParserForList< Token, MatchType > Loyc.Syntax.ParserSource< Token, MatchType, List > Loyc.Syntax.BaseParserNoBacktracking< Token > Loyc.Syntax.Les.Les2Parser Loyc.Syntax.Les.Les3Parser Loyc.Syntax.ParserSource< Token, MatchType > Loyc.Syntax.ParserSource< Token >

Remarks

An base class designed for parsers that use LLLPG (Loyc LL(k) Parser Generator). Note: this is the old (harder to use) base class design. You should use BaseParserForList{Token,MatchType} instead.

Type Constraints
MatchType :IEquatable<MatchType> 

Nested classes

struct  SavePosition
 A helper class used by LLLPG for backtracking. More...
 

Public static fields

static readonly IMessageSink LogExceptionErrorSink
 Throws LogException when it receives an error. Non-errors are sent to MessageSink.Default. More...
 
static readonly IMessageSink FormatExceptionErrorSink
 

Properties

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

 BaseParser (ISourceFile?file=null, int startIndex=0)
 Initializes the base class. More...
 
abstract MatchType EofInt ()
 Returns the value used for EOF (normally 0) More...
 
abstract 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...
 
abstract string ToString (MatchType tokenType)
 Returns a string representation of the specified token type. These strings are used in error messages. 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="")
 

Static Protected Member Functions

static HashSet< MatchType > NewSet (params MatchType[] items)
 

Protected fields

ISourceFile _sourceFile
 
Token _lt0 = default!
 
int _inputPosition
 
MatchType EOF
 

Constructor & Destructor Documentation

Loyc.Syntax.BaseParser< Token, MatchType >.BaseParser ( ISourceFile file = null,
int  startIndex = 0 
)
inlineprotected

Initializes the base class.

Parameters
fileA 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.
startIndexThe initial value of InputPosition.

Member Function Documentation

abstract MatchType Loyc.Syntax.BaseParser< Token, MatchType >.EofInt ( )
protectedpure virtual
virtual void Loyc.Syntax.BaseParser< Token, MatchType >.Error ( int  lookaheadIndex,
string  message 
)
inlineprotectedvirtual

Records an error or throws an exception.

Parameters
lookaheadIndexLocation of the error relative to the current InputPosition. When called by BaseParser, lookaheadIndex is always equal to 0.

The default implementation throws a FormatException. When overriding this method, you can convert the lookaheadIndex to a SourcePos using the expression SourceFile.IndexToLine(LT(lookaheadIndex).StartIndex). This only works if an ISourceFile object was provided to the constructor of this class, and Token implements ISimpleToken.

Reimplemented in Loyc.Ecs.Parser.EcsParser.

virtual int Loyc.Syntax.BaseParser< Token, MatchType >.LaIndexToCharIndex ( int  lookaheadIndex)
inlineprotectedvirtual

Converts a lookahead token index to a character index (used for error reporting).

The default implementation does this by trying to cast LT(lookaheadIndex) to ISimpleToken{MatchType}. Returns -1 on failure.

The StartIndex reported by an EOF token is assumed not to be trustworthy (since it is allowed to be a "dummy" token): this method will ensure that the character index returned for EOF is at least as large as SourceFile.Text.Count if a SourceFile was provided, or, otherwise, at least as large as the last token in the file, by scanning backward to find the last token in the file.

virtual object Loyc.Syntax.BaseParser< Token, MatchType >.LaIndexToMsgContext ( int  lookaheadIndex)
inlineprotectedvirtual

Converts a lookahead token index to a SourceRange (or to a string if SourceFile was initialized to null.)

The base class can only return a zero-width SourceRange.

abstract Token Loyc.Syntax.BaseParser< Token, MatchType >.LT ( int  i)
protectedpure virtual

Returns the token at lookahead i (e.g. Source[InputPosition + i] if the tokens come from a list called Source)

Implemented in Loyc.Ecs.Parser.EcsParser, Loyc.Syntax.BaseParserForList< Token, MatchType, List >, Loyc.Syntax.BaseParserNoBacktracking< Token, Enumerator >, and Loyc.Syntax.ParserSource< Token, MatchType, List >.

virtual void Loyc.Syntax.BaseParser< Token, MatchType >.MatchError ( bool  inverted,
IEnumerable< MatchType >  expected 
)
inlineprotectedvirtual

Handles an error that occurs during Match() or MatchExcept()

Parameters
invertedSet inversion flag. If true, then expected is actually a list of things that were NOT expected.
expectedList of items that were expected (or unexpected, if inverted)

References Loyc.Localize.Localized().

abstract string Loyc.Syntax.BaseParser< Token, MatchType >.ToString ( MatchType  tokenType)
protectedpure virtual

Returns a string representation of the specified token type. These strings are used in error messages.

Implemented in Loyc.Syntax.BaseParserForList< Token, MatchType, List >, and Loyc.Syntax.ParserSource< Token, MatchType, List >.

Member Data Documentation

readonly IMessageSink Loyc.Syntax.BaseParser< Token, MatchType >.FormatExceptionErrorSink
static
Initial value:
= MessageSink.FromDelegate(
(sev, location, fmt, args) =>
{
if (sev >= Severity.Error)
throw new FormatException(MessageSink.ContextToString(location) + ": " + Localize.Localized(fmt, args));
else
MessageSink.Default.Write(sev, location, fmt, args);
})
readonly IMessageSink Loyc.Syntax.BaseParser< Token, MatchType >.LogExceptionErrorSink
static
Initial value:
= MessageSink.FromDelegate(
(sev, location, fmt, args) =>
{
LogMessage msg = new LogMessage(sev, location, fmt, args);
if (sev >= Severity.Error)
throw new LogException(msg);
else
msg.WriteTo(MessageSink.Default);
})

Throws LogException when it receives an error. Non-errors are sent to MessageSink.Default.

Property Documentation

IMessageSink Loyc.Syntax.BaseParser< Token, MatchType >.ErrorSink
getset

Gets or sets the object to which error messages are sent. The default object is LogExceptionErrorSink, which throws an exception if an error occurs.

int Loyc.Syntax.BaseParser< Token, MatchType >.InputPosition
getsetprotected

Current position of the next token to be parsed.

abstract MatchType Loyc.Syntax.BaseParser< Token, MatchType >.LA0Int
getprotected

Returns the token type of _lt0 (normally _lt0.TypeInt)

Token Loyc.Syntax.BaseParser< Token, MatchType >.LT0
get

Next token to parse (cached; is set to LT(0) whenever InputPosition is changed).

ISourceFile Loyc.Syntax.BaseParser< Token, MatchType >.SourceFile
getprotected

The ISourceFile object that was provided to the constructor, if any.