public static string Consume(this AbstractLexer lexer, TokenKind kind, string text = null) { lexer.VerifyToken(kind, text); var result = lexer.Token.Text; lexer.Advance(); return(result); }
public static string Consume(this AbstractLexer lexer, params TokenKind[] kinds) { lexer.VerifyToken(kinds); var result = lexer.Token.Text; lexer.Advance(); return(result); }
public static void VerifyToken(this AbstractLexer lexer, TokenKind kind, string text = null) { if (lexer.Token.Kind != kind) { throw new ParserException($"Expecting token kind {kind}, but found {lexer.Token.Kind}: {lexer.Token.Text}.", lexer.Token); } if ((text != null) && (lexer.Token.Text != text)) { throw new ParserException($"Expecting {kind} token to have text '{text}', but found '{lexer.Token.Text}'.", lexer.Token); } }
public static void VerifyToken(this AbstractLexer lexer, params TokenKind[] kinds) { foreach (var kind in kinds) { if (lexer.Token.Kind == kind) { return; } } var list = string.Join(", ", kinds); throw new ParserException($"Expecting token to be one of ({list}), but found {lexer.Token.Kind}.", lexer.Token); }
public TrackerContainer(AbstractLexer lx) { assocLexer = lx; }
public static void Advance(this AbstractLexer lexer) { lexer.LexToken(); }
public Token Create(AbstractLexer lexer, SStreamReader reader) { long startPos = reader.Position; int length = 0; int currentChar = lexer.Next(); length++; var sourcePos = lexer.CurrentSourcePosition; lexer.CheckMatch(StartCharacter); currentChar = lexer.Next(); length++; while (true) { if (!((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= '0' && currentChar <= '9') || currentChar == '_')) { length--; break; } currentChar = lexer.Next(); length++; } if (currentChar == -1) { throw new MonkeyspeakException("Unexpected end of file", sourcePos); } #region Variable Table Handling if (lexer.LookAhead(1) == '[') { while (((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= '0' && currentChar <= '9'))) { if (currentChar == -1) { throw new MonkeyspeakException("Unexpected end of file", lexer.CurrentSourcePosition); } currentChar = lexer.Next(); length++; if (currentChar == ']') { break; } if (!((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= '0' && currentChar <= '9'))) { throw new MonkeyspeakException($"Invalid character in variable list index delcaration '{currentChar}'", lexer.CurrentSourcePosition); } } } #endregion Variable Table Handling return(new Token(TokenType.VARIABLE, startPos, length, lexer.CurrentSourcePosition)); }
public abstract IEnumerable <Trigger> Parse(AbstractLexer lexer);