1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 scanner scanner.Scanner
17 pos scanner.Position // token position
18 tok int // one token look-ahead
19 lit string // token literal
22 func (p *parser) next() {
23 p.tok = p.scanner.Scan()
24 p.pos = p.scanner.Position
25 p.lit = p.scanner.TokenText()
28 func (p *parser) error(pos scanner.Position, msg string) {
29 p.errors = append(p.errors, newError(pos, msg))
32 func (p *parser) errorExpected(pos scanner.Position, msg string) {
33 msg = `expected "` + msg + `"`
34 if pos.Offset == p.pos.Offset {
35 // the error happened at the current position;
36 // make the error message more specific
37 msg += ", found " + scanner.TokenString(p.tok)
45 func (p *parser) expect(tok int) scanner.Position {
48 p.errorExpected(pos, scanner.TokenString(tok))
50 p.next() // make progress in any case
54 func (p *parser) parseIdentifier() *Name {
57 p.expect(scanner.Ident)
58 return &Name{pos, name}
61 func (p *parser) parseToken() *Token {
64 if p.tok == scanner.String {
65 value, _ = strconv.Unquote(p.lit)
66 // Unquote may fail with an error, but only if the scanner found
67 // an illegal string in the first place. In this case the error
68 // has already been reported.
71 p.expect(scanner.String)
73 return &Token{pos, value}
76 // ParseTerm returns nil if no term was found.
77 func (p *parser) parseTerm() (x Expression) {
82 x = p.parseIdentifier()
87 const ellipsis = '…' // U+2026, the horizontal ellipsis character
88 if p.tok == ellipsis {
90 x = &Range{tok, p.parseToken()}
95 x = &Group{pos, p.parseExpression()}
100 x = &Option{pos, p.parseExpression()}
105 x = &Repetition{pos, p.parseExpression()}
112 func (p *parser) parseSequence() Expression {
115 for x := p.parseTerm(); x != nil; x = p.parseTerm() {
116 list = append(list, x)
119 // no need for a sequence if list.Len() < 2
122 p.errorExpected(p.pos, "term")
123 return &Bad{p.pos, "term expected"}
131 func (p *parser) parseExpression() Expression {
135 list = append(list, p.parseSequence())
143 // no need for an Alternative node if list.Len() < 2
151 func (p *parser) parseProduction() *Production {
152 name := p.parseIdentifier()
156 expr = p.parseExpression()
159 return &Production{name, expr}
162 func (p *parser) parse(filename string, src io.Reader) Grammar {
164 p.scanner.Filename = filename
165 p.next() // initializes pos, tok, lit
167 grammar := make(Grammar)
168 for p.tok != scanner.EOF {
169 prod := p.parseProduction()
170 name := prod.Name.String
171 if _, found := grammar[name]; !found {
174 p.error(prod.Pos(), name+" declared already")
181 // Parse parses a set of EBNF productions from source src.
182 // It returns a set of productions. Errors are reported
183 // for incorrect syntax and if a production is declared
184 // more than once; the filename is used only for error
187 func Parse(filename string, src io.Reader) (Grammar, os.Error) {
189 grammar := p.parse(filename, src)
190 return grammar, p.errors.Error()