/* * ll1_parser.go: Parser for the Grammar grammar. * Generated by the ll1 tool from ll1.ll1 at 2020-09-07 15:48:11.467763157 +0200 CEST m=+0.001425957. * 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. //no-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. //no-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). //no-line ll1.parser.go.tpl:69 type Ll1TokenKind rune // NoLl1TokenKind means "no token kind" i.e. no token. //no-line ll1.parser.go.tpl:74 const NoLl1TokenKind Ll1TokenKind = Ll1TokenKind(0) // Ll1TokenKindEOF means the end of the input. //no-line ll1.parser.go.tpl:77 const Ll1TokenKindEOF Ll1TokenKind = Ll1TokenKind(-1) // Ll1TokenKindError means a parsing or lexing error was encountered. //no-line ll1.parser.go.tpl:80 const Ll1TokenKindError Ll1TokenKind = Ll1TokenKind(-2) // Convert token kind to a string representation //no-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. //no-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. //no-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. //no-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. //no-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 } //no-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 //no-line ll1.parser.go.tpl:188 type Ll1Parser struct { reader io.Reader lexer *Ll1Lexer current Ll1Token Errors []Ll1ParserError Filename string Debug io.Writer } //no-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. //no-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. //no-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 } //no-line ll1.parser.go.tpl:232 func (pe Ll1ParserError) Error() string { // XXX will need to be improved return pe.Chain.Error() } //no-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 } //no-line ll1.parser.go.tpl:250 func (parser *Ll1Parser) Panicf(message string, args ...interface{}) { pe := parser.Errorf(message, args...) panic(pe) } //no-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.*/ //no-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{} } //no-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 } //no-line ll1.parser.go.tpl:324 type Ll1Grammar struct { Ll1Token *Ll1Rules } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Grammar() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Grammar() (*Ll1Grammar, error) { var err error result := &Ll1Grammar { Ll1Token: p.current } if p.IsLl1Rules() { result.Ll1Rules, err = p.ParseLl1Rules() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Rules struct { Ll1Token *Ll1Rule *Ll1OptRules } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Rules() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Rules() (*Ll1Rules, error) { var err error result := &Ll1Rules { Ll1Token: p.current } if p.IsLl1Rule() { result.Ll1Rule, err = p.ParseLl1Rule() if err != nil { return result, err } } if p.IsLl1OptRules() { result.Ll1OptRules, err = p.ParseLl1OptRules() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1OptRules struct { Ll1Token *Ll1Rules } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1OptRules() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1OptRules() (*Ll1OptRules, error) { var err error result := &Ll1OptRules { Ll1Token: p.current } if p.IsLl1Rules() { result.Ll1Rules, err = p.ParseLl1Rules() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Rule struct { Ll1Token *Ll1Name *Ll1Definition *Ll1Template } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Rule() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Rule() (*Ll1Rule, error) { var err error result := &Ll1Rule { Ll1Token: p.current } if p.IsLl1Name() { result.Ll1Name, err = p.ParseLl1Name() if err != nil { return result, err } } if p.IsLl1Definition() { result.Ll1Definition, err = p.ParseLl1Definition() if err != nil { return result, err } } if p.IsLl1Template() { result.Ll1Template, err = p.ParseLl1Template() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Name struct { Ll1Token } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Name() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Name() (*Ll1Name, error) { var err error result := &Ll1Name { Ll1Token: p.current } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Template struct { Ll1Token } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Template() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Template() (*Ll1Template, error) { var err error result := &Ll1Template { Ll1Token: p.current } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Definition struct { Ll1Token *Ll1Alternates } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Definition() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Definition() (*Ll1Definition, error) { var err error result := &Ll1Definition { Ll1Token: p.current } if p.IsLl1Alternates() { result.Ll1Alternates, err = p.ParseLl1Alternates() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Alternates struct { Ll1Token *Ll1Sequence *Ll1OptSequences } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Alternates() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Alternates() (*Ll1Alternates, error) { var err error result := &Ll1Alternates { Ll1Token: p.current } if p.IsLl1Sequence() { result.Ll1Sequence, err = p.ParseLl1Sequence() if err != nil { return result, err } } if p.IsLl1OptSequences() { result.Ll1OptSequences, err = p.ParseLl1OptSequences() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1OptSequences struct { Ll1Token *Ll1Alternates } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1OptSequences() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1OptSequences() (*Ll1OptSequences, error) { var err error result := &Ll1OptSequences { Ll1Token: p.current } if p.IsLl1Alternates() { result.Ll1Alternates, err = p.ParseLl1Alternates() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Sequence struct { Ll1Token *Ll1Element *Ll1OptElements } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Sequence() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Sequence() (*Ll1Sequence, error) { var err error result := &Ll1Sequence { Ll1Token: p.current } if p.IsLl1Element() { result.Ll1Element, err = p.ParseLl1Element() if err != nil { return result, err } } if p.IsLl1OptElements() { result.Ll1OptElements, err = p.ParseLl1OptElements() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1OptElements struct { Ll1Token *Ll1Element *Ll1OptElements } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1OptElements() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1OptElements() (*Ll1OptElements, error) { var err error result := &Ll1OptElements { Ll1Token: p.current } if p.IsLl1Element() { result.Ll1Element, err = p.ParseLl1Element() if err != nil { return result, err } } if p.IsLl1OptElements() { result.Ll1OptElements, err = p.ParseLl1OptElements() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Element struct { Ll1Token *Ll1Parenthesis *Ll1Name } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Element() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Element() (*Ll1Element, error) { var err error result := &Ll1Element { Ll1Token: p.current } if p.IsLl1Parenthesis() { result.Ll1Parenthesis, err = p.ParseLl1Parenthesis() if err != nil { return result, err } } if p.IsLl1Name() { result.Ll1Name, err = p.ParseLl1Name() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:324 type Ll1Parenthesis struct { Ll1Token *Ll1Definition } //no-line ll1.parser.go.tpl:335 func (p *Ll1Parser) IsLl1Parenthesis() (bool) { return false // return p.NextIs() } //no-line ll1.parser.go.tpl:340 func (p *Ll1Parser) ParseLl1Parenthesis() (*Ll1Parenthesis, error) { var err error result := &Ll1Parenthesis { Ll1Token: p.current } if p.IsLl1Definition() { result.Ll1Definition, err = p.ParseLl1Definition() if err != nil { return result, err } } return result, err } //no-line ll1.parser.go.tpl:313 const Ll1TokenKinddot Ll1TokenKind = Ll1TokenKind(-2) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKinddot() (Ll1TokenKind, error) { result := Ll1TokenKinddot return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindor Ll1TokenKind = Ll1TokenKind(-3) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindor() (Ll1TokenKind, error) { result := Ll1TokenKindor return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindidentifier Ll1TokenKind = Ll1TokenKind(-4) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindidentifier() (Ll1TokenKind, error) { result := Ll1TokenKindidentifier return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindruleName Ll1TokenKind = Ll1TokenKind(-5) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindruleName() (Ll1TokenKind, error) { result := Ll1TokenKindruleName return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindterminalName Ll1TokenKind = Ll1TokenKind(-6) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindterminalName() (Ll1TokenKind, error) { result := Ll1TokenKindterminalName return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindepsilon Ll1TokenKind = Ll1TokenKind(-7) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindepsilon() (Ll1TokenKind, error) { result := Ll1TokenKindepsilon return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindarrow Ll1TokenKind = Ll1TokenKind(-8) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindarrow() (Ll1TokenKind, error) { result := Ll1TokenKindarrow return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindliteral Ll1TokenKind = Ll1TokenKind(-9) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindliteral() (Ll1TokenKind, error) { result := Ll1TokenKindliteral return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindstringLiteral Ll1TokenKind = Ll1TokenKind(-10) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindstringLiteral() (Ll1TokenKind, error) { result := Ll1TokenKindstringLiteral return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindcharLiteral Ll1TokenKind = Ll1TokenKind(-11) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindcharLiteral() (Ll1TokenKind, error) { result := Ll1TokenKindcharLiteral return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindrawString Ll1TokenKind = Ll1TokenKind(-12) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindrawString() (Ll1TokenKind, error) { result := Ll1TokenKindrawString return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindwhiteSpace Ll1TokenKind = Ll1TokenKind(-13) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindwhiteSpace() (Ll1TokenKind, error) { result := Ll1TokenKindwhiteSpace return result, nil } //no-line ll1.parser.go.tpl:313 const Ll1TokenKindlineComment Ll1TokenKind = Ll1TokenKind(-14) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindlineComment() (Ll1TokenKind, error) { result := Ll1TokenKindlineComment return result, nil } // Expanded from template of rule handCoded // Not implemented //no-line ll1.parser.go.tpl:313 const Ll1TokenKindhandCoded Ll1TokenKind = Ll1TokenKind(-15) //no-line ll1.parser.go.tpl:316 func ( *Ll1Lexer) LexLl1TokenKindhandCoded() (Ll1TokenKind, error) { result := Ll1TokenKindhandCoded return result, nil }