/* * ll1_parser.go: Parser for the Grammar grammar. * Generated by the ll1 tool from ll1.ll1 at 2020-08-28 18:00:14.138349709 +0200 CEST m=+0.001367333. * Based on template: ll1.parser.go.lined.tpl * Uses a scanner * * Available definition keys at template expansion: * [Grammar Import InName LexerType OutName Package Parser Prefix Templates] * * DO NOT EDIT. */ package main import "text/scanner" import "unicode" import "io" import "os" import "fmt" // Ll1Value is the lexical value of a lexer token. // This is based on strings as a default. //line ll1.parser.go.tpl:43 type Ll1Value = string // Ll1Position is a position within a source file. Since the lexer is based on // text/scanner, we use that package's Position. //line ll1.parser.go.tpl:51 type Ll1Position = scanner.Position // Ll1TokenKind is the kind or type of a token. // This has rune as the underlying type so one-character tokens can be easily // supported. EOF will be 65535 (I.e, -1 cast to rune). Non-character token // kinds will start from 65533 down (i.e -3, -4, -5, etc). //line ll1.parser.go.tpl:69 type Ll1TokenKind rune // NoLl1TokenKind means "no token kind" i.e. no token. //line ll1.parser.go.tpl:74 const NoLl1TokenKind Ll1TokenKind = Ll1TokenKind(0) // Ll1TokenKindEOF means the end of the input. //line ll1.parser.go.tpl:77 const Ll1TokenKindEOF Ll1TokenKind = Ll1TokenKind(-1) // Ll1TokenKindError means a parsing or lexing error was encountered. //line ll1.parser.go.tpl:80 const Ll1TokenKindError Ll1TokenKind = Ll1TokenKind(-2) // Convert token kind to a string representation //line ll1.parser.go.tpl:86 func (tk Ll1TokenKind) String() string { return scanner.TokenString(rune(tk)) } // Ll1Token is the result of a single lexical analysis step by the lexer. //line ll1.parser.go.tpl:109 type Ll1Token struct { Ll1Position // Position in the source where the token was found. Ll1TokenKind // Type of the token Ll1Value // Value of the token } // MakeLl1Token makes a token with the given position, type and value. //line ll1.parser.go.tpl:118 func MakeLl1Token(pos Ll1Position, typ Ll1TokenKind, val Ll1Value) Ll1Token { return Ll1Token{ pos, typ, val} } // Ll1Lexer performs the lexical analysis of the input. //line ll1.parser.go.tpl:124 type Ll1Lexer struct { // Embed scanner.Scanner scanner.Scanner Filename string } // NewLl1LexerFromReader creates a new lexer for the given parser and input. //line ll1.parser.go.tpl:133 func NewLl1LexerFromReader(parser *Ll1Parser, reader io.Reader, filename string) *Ll1Lexer { lexer := &Ll1Lexer{} lexer.Filename = filename lexer.Scanner.Init(reader) lexer.Scanner.Mode = scanner.GoTokens lexer.Scanner.Error = func (s *scanner.Scanner, msg string) { parser.Panicf("%s: scanner error: %s, %s", s.Position, s.TokenText(), msg) } // XXX: needs to be generated from the identifier rule in the syntax! lexer.Scanner.IsIdentRune = func(ch rune, i int) bool { if i == 0 { return unicode.IsLetter(ch) } return unicode.IsLetter(ch) || unicode.IsNumber(ch) || ch == '_' || ch == '-' } return lexer } //line ll1.parser.go.tpl:155 func (lex *Ll1Lexer) Lex() Ll1Token { scanned := lex.Scanner.Scan() pos := lex.Scanner.Position pos.Filename = lex.Filename value := lex.Scanner.TokenText() // Get rid of the quotes if scanned == scanner.Char || scanned == scanner.String || scanned == scanner.RawString { value = value[1:len(value) - 1] } token := Ll1Token { Ll1TokenKind: Ll1TokenKind(scanned), Ll1Value: value, Ll1Position: pos, } return token } // Ll1Parser parses the input and returns a parse tree, // based on the rules in ll1.ll1 //line ll1.parser.go.tpl:188 type Ll1Parser struct { reader io.Reader lexer *Ll1Lexer current Ll1Token Errors []Ll1ParserError Filename string Debug io.Writer } //line ll1.parser.go.tpl:198 func NewLl1ParserFromReader(reader io.Reader, filename string, debug bool) *Ll1Parser { parser := &Ll1Parser{} parser.lexer = NewLl1LexerFromReader(parser, reader, filename) parser.Filename = filename parser.current.Ll1TokenKind = NoLl1TokenKind parser.Debug = nil if debug { parser.Debug = os.Stderr } return parser } // Advances the parser. Returns the current token /after/ advancing. //line ll1.parser.go.tpl:214 func (p *Ll1Parser) Advance() Ll1Token { token := p.lexer.Lex() p.Debugf("Lexed token: %v", token) p.current = token return token } // Ll1ParserError is an error encountered during parsing or lexing. // The parser may panic with this type on errors that would prevent the parser // from making progress. //line ll1.parser.go.tpl:225 type Ll1ParserError struct { *Ll1Parser // Parser that had the error. *Ll1Token // Token at which the error was found Chain error // underlying error } //line ll1.parser.go.tpl:232 func (pe Ll1ParserError) Error() string { // XXX will need to be improved return pe.Chain.Error() } //line ll1.parser.go.tpl:238 func (parser *Ll1Parser) Errorf(message string, args ...interface{}) Ll1ParserError { err := fmt.Errorf(message, args...) pe := Ll1ParserError { Ll1Parser: parser, Ll1Token: &parser.current, Chain: err, } parser.Errors = append(parser.Errors, pe) return pe } //line ll1.parser.go.tpl:250 func (parser *Ll1Parser) Panicf(message string, args ...interface{}) { pe := parser.Errorf(message, args...) panic(pe) } //line ll1.parser.go.tpl:257 func (p *Ll1Parser) Debugf(message string, args ...interface{}) { if p.Debug != nil { fmt.Fprintf(p.Debug, message, args) } } /* Looks at the current token and advances the lexer if the token is of any of the token kinds given in kinds. In this case it will return the accepted token and advance the parser. Otherwise, it will call parser.Panicf.*/ //line ll1.parser.go.tpl:267 func (parser *Ll1Parser) Require(kinds ...Ll1TokenKind) Ll1Token { parser.Debugf("Require: %v\n", kinds) if parser.current.Ll1TokenKind == Ll1TokenKind(0) { parser.Advance() } expected := "" sep := "" for _, kind := range kinds { if kind == parser.current.Ll1TokenKind { accepted := parser.current parser.Advance() return accepted } expected = fmt.Sprintf("%s%s%s", expected, sep, kind.String()) } parser.Panicf("error: expected one of the following: %s", expected) return Ll1Token{} } //line ll1.parser.go.tpl:288 func (parser Ll1Parser) NextIs(kinds ...Ll1TokenKind) bool { parser.Debugf("NextIs: %v\n", kinds) if (parser.current.Ll1TokenKind == 0) { parser.Advance() } for _, kind := range kinds { if kind == parser.current.Ll1TokenKind { return true } } return false } //line ll1.parser.go.tpl:324 type Ll1Grammar struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Grammar() (Ll1Grammar, error) { result := Ll1Grammar {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Rules struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Rules() (Ll1Rules, error) { result := Ll1Rules {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1OptRules struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1OptRules() (Ll1OptRules, error) { result := Ll1OptRules {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Rule struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Rule() (Ll1Rule, error) { result := Ll1Rule {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Name struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Name() (Ll1Name, error) { result := Ll1Name {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Template struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Template() (Ll1Template, error) { result := Ll1Template {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Definition struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Definition() (Ll1Definition, error) { result := Ll1Definition {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Alternates struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Alternates() (Ll1Alternates, error) { result := Ll1Alternates {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1OptSequences struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1OptSequences() (Ll1OptSequences, error) { result := Ll1OptSequences {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Sequence struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Sequence() (Ll1Sequence, error) { result := Ll1Sequence {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1OptElements struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1OptElements() (Ll1OptElements, error) { result := Ll1OptElements {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Element struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Element() (Ll1Element, error) { result := Ll1Element {} return result, nil } //line ll1.parser.go.tpl:324 type Ll1Parenthesis struct { } //line ll1.parser.go.tpl:328 func ( *Ll1Parser) ParseLl1Parenthesis() (Ll1Parenthesis, error) { result := Ll1Parenthesis {} return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKinddot Ll1TokenKind = Ll1TokenKind(-2) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKinddot() (Ll1TokenKind, error) { result := Ll1TokenKinddot return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindor Ll1TokenKind = Ll1TokenKind(-3) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindor() (Ll1TokenKind, error) { result := Ll1TokenKindor return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindidentifier Ll1TokenKind = Ll1TokenKind(-4) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindidentifier() (Ll1TokenKind, error) { result := Ll1TokenKindidentifier return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindruleName Ll1TokenKind = Ll1TokenKind(-5) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindruleName() (Ll1TokenKind, error) { result := Ll1TokenKindruleName return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindterminalName Ll1TokenKind = Ll1TokenKind(-6) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindterminalName() (Ll1TokenKind, error) { result := Ll1TokenKindterminalName return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindepsilon Ll1TokenKind = Ll1TokenKind(-7) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindepsilon() (Ll1TokenKind, error) { result := Ll1TokenKindepsilon return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindarrow Ll1TokenKind = Ll1TokenKind(-8) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindarrow() (Ll1TokenKind, error) { result := Ll1TokenKindarrow return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindliteral Ll1TokenKind = Ll1TokenKind(-9) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindliteral() (Ll1TokenKind, error) { result := Ll1TokenKindliteral return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindstringLiteral Ll1TokenKind = Ll1TokenKind(-10) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindstringLiteral() (Ll1TokenKind, error) { result := Ll1TokenKindstringLiteral return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindcharLiteral Ll1TokenKind = Ll1TokenKind(-11) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindcharLiteral() (Ll1TokenKind, error) { result := Ll1TokenKindcharLiteral return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindrawString Ll1TokenKind = Ll1TokenKind(-12) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindrawString() (Ll1TokenKind, error) { result := Ll1TokenKindrawString return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindwhiteSpace Ll1TokenKind = Ll1TokenKind(-13) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindwhiteSpace() (Ll1TokenKind, error) { result := Ll1TokenKindwhiteSpace return result, nil } //line ll1.parser.go.tpl:313 const Ll1TokenKindlineComment Ll1TokenKind = Ll1TokenKind(-14) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindlineComment() (Ll1TokenKind, error) { result := Ll1TokenKindlineComment return result, nil } // Expanded from template of rule handCoded // Not implemented //line ll1.parser.go.tpl:313 const Ll1TokenKindhandCoded Ll1TokenKind = Ll1TokenKind(-15) //line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindhandCoded() (Ll1TokenKind, error) { result := Ll1TokenKindhandCoded return result, nil }