diff options
Diffstat (limited to 'src/pkg/ebnf/parser.go')
-rw-r--r-- | src/pkg/ebnf/parser.go | 128 |
1 files changed, 64 insertions, 64 deletions
diff --git a/src/pkg/ebnf/parser.go b/src/pkg/ebnf/parser.go index 5193f8b26..649587879 100644 --- a/src/pkg/ebnf/parser.go +++ b/src/pkg/ebnf/parser.go @@ -5,25 +5,25 @@ package ebnf import ( - "container/vector"; - "go/scanner"; - "go/token"; - "os"; - "strconv"; + "container/vector" + "go/scanner" + "go/token" + "os" + "strconv" ) type parser struct { - scanner.ErrorVector; - scanner scanner.Scanner; - pos token.Position; // token position - tok token.Token; // one token look-ahead - lit []byte; // token literal + scanner.ErrorVector + scanner scanner.Scanner + pos token.Position // token position + tok token.Token // one token look-ahead + lit []byte // token literal } func (p *parser) next() { - p.pos, p.tok, p.lit = p.scanner.Scan(); + p.pos, p.tok, p.lit = p.scanner.Scan() if p.tok.IsKeyword() { // TODO Should keyword mapping always happen outside scanner? // Or should there be a flag to scanner to enable keyword mapping? @@ -33,90 +33,90 @@ func (p *parser) next() { func (p *parser) errorExpected(pos token.Position, msg string) { - msg = "expected " + msg; + msg = "expected " + msg if pos.Offset == p.pos.Offset { // the error happened at the current position; // make the error message more specific - msg += ", found '" + p.tok.String() + "'"; + msg += ", found '" + p.tok.String() + "'" if p.tok.IsLiteral() { msg += " " + string(p.lit) } } - p.Error(pos, msg); + p.Error(pos, msg) } func (p *parser) expect(tok token.Token) token.Position { - pos := p.pos; + pos := p.pos if p.tok != tok { p.errorExpected(pos, "'"+tok.String()+"'") } - p.next(); // make progress in any case - return pos; + p.next() // make progress in any case + return pos } func (p *parser) parseIdentifier() *Name { - pos := p.pos; - name := string(p.lit); - p.expect(token.IDENT); - return &Name{pos, name}; + pos := p.pos + name := string(p.lit) + p.expect(token.IDENT) + return &Name{pos, name} } func (p *parser) parseToken() *Token { - pos := p.pos; - value := ""; + pos := p.pos + value := "" if p.tok == token.STRING { - value, _ = strconv.Unquote(string(p.lit)); + value, _ = strconv.Unquote(string(p.lit)) // Unquote may fail with an error, but only if the scanner found // an illegal string in the first place. In this case the error // has already been reported. - p.next(); + p.next() } else { p.expect(token.STRING) } - return &Token{pos, value}; + return &Token{pos, value} } func (p *parser) parseTerm() (x Expression) { - pos := p.pos; + pos := p.pos switch p.tok { case token.IDENT: x = p.parseIdentifier() case token.STRING: - tok := p.parseToken(); - x = tok; + tok := p.parseToken() + x = tok if p.tok == token.ELLIPSIS { - p.next(); - x = &Range{tok, p.parseToken()}; + p.next() + x = &Range{tok, p.parseToken()} } case token.LPAREN: - p.next(); - x = &Group{pos, p.parseExpression()}; - p.expect(token.RPAREN); + p.next() + x = &Group{pos, p.parseExpression()} + p.expect(token.RPAREN) case token.LBRACK: - p.next(); - x = &Option{pos, p.parseExpression()}; - p.expect(token.RBRACK); + p.next() + x = &Option{pos, p.parseExpression()} + p.expect(token.RBRACK) case token.LBRACE: - p.next(); - x = &Repetition{pos, p.parseExpression()}; - p.expect(token.RBRACE); + p.next() + x = &Repetition{pos, p.parseExpression()} + p.expect(token.RBRACE) } - return x; + return x } func (p *parser) parseSequence() Expression { - var list vector.Vector; + var list vector.Vector for x := p.parseTerm(); x != nil; x = p.parseTerm() { list.Push(x) @@ -131,26 +131,26 @@ func (p *parser) parseSequence() Expression { } // convert list into a sequence - seq := make(Sequence, list.Len()); + seq := make(Sequence, list.Len()) for i := 0; i < list.Len(); i++ { seq[i] = list.At(i).(Expression) } - return seq; + return seq } func (p *parser) parseExpression() Expression { - var list vector.Vector; + var list vector.Vector for { - x := p.parseSequence(); + x := p.parseSequence() if x != nil { list.Push(x) } if p.tok != token.OR { break } - p.next(); + p.next() } // no need for an Alternative node if list.Len() < 2 @@ -162,33 +162,33 @@ func (p *parser) parseExpression() Expression { } // convert list into an Alternative node - alt := make(Alternative, list.Len()); + alt := make(Alternative, list.Len()) for i := 0; i < list.Len(); i++ { alt[i] = list.At(i).(Expression) } - return alt; + return alt } func (p *parser) parseProduction() *Production { - name := p.parseIdentifier(); - p.expect(token.ASSIGN); - expr := p.parseExpression(); - p.expect(token.PERIOD); - return &Production{name, expr}; + name := p.parseIdentifier() + p.expect(token.ASSIGN) + expr := p.parseExpression() + p.expect(token.PERIOD) + return &Production{name, expr} } func (p *parser) parse(filename string, src []byte) Grammar { // initialize parser - p.ErrorVector.Reset(); - p.scanner.Init(filename, src, p, 0); - p.next(); // initializes pos, tok, lit + p.ErrorVector.Reset() + p.scanner.Init(filename, src, p, 0) + p.next() // initializes pos, tok, lit - grammar := make(Grammar); + grammar := make(Grammar) for p.tok != token.EOF { - prod := p.parseProduction(); - name := prod.Name.String; + prod := p.parseProduction() + name := prod.Name.String if _, found := grammar[name]; !found { grammar[name] = prod } else { @@ -196,7 +196,7 @@ func (p *parser) parse(filename string, src []byte) Grammar { } } - return grammar; + return grammar } @@ -206,7 +206,7 @@ func (p *parser) parse(filename string, src []byte) Grammar { // more than once. // func Parse(filename string, src []byte) (Grammar, os.Error) { - var p parser; - grammar := p.parse(filename, src); - return grammar, p.GetError(scanner.Sorted); + var p parser + grammar := p.parse(filename, src) + return grammar, p.GetError(scanner.Sorted) } |