Commit 7eb5c9a5 authored by Robert Griesemer's avatar Robert Griesemer

ebnf: use append

R=r, rsc
CC=golang-dev
https://golang.org/cl/2799041
parent 75855a8f
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
package ebnf package ebnf
import ( import (
"container/vector"
"go/scanner" "go/scanner"
"go/token" "go/token"
"os" "os"
...@@ -123,7 +122,7 @@ func isLexical(name string) bool { ...@@ -123,7 +122,7 @@ func isLexical(name string) bool {
type verifier struct { type verifier struct {
scanner.ErrorVector scanner.ErrorVector
worklist vector.Vector worklist []*Production
reached Grammar // set of productions reached from (and including) the root production reached Grammar // set of productions reached from (and including) the root production
grammar Grammar grammar Grammar
} }
...@@ -132,7 +131,7 @@ type verifier struct { ...@@ -132,7 +131,7 @@ type verifier struct {
func (v *verifier) push(prod *Production) { func (v *verifier) push(prod *Production) {
name := prod.Name.String name := prod.Name.String
if _, found := v.reached[name]; !found { if _, found := v.reached[name]; !found {
v.worklist.Push(prod) v.worklist = append(v.worklist, prod)
v.reached[name] = prod v.reached[name] = prod
} }
} }
...@@ -205,14 +204,19 @@ func (v *verifier) verify(grammar Grammar, start string) { ...@@ -205,14 +204,19 @@ func (v *verifier) verify(grammar Grammar, start string) {
// initialize verifier // initialize verifier
v.ErrorVector.Reset() v.ErrorVector.Reset()
v.worklist.Resize(0, 0) v.worklist = v.worklist[0:0]
v.reached = make(Grammar) v.reached = make(Grammar)
v.grammar = grammar v.grammar = grammar
// work through the worklist // work through the worklist
v.push(root) v.push(root)
for v.worklist.Len() > 0 { for {
prod := v.worklist.Pop().(*Production) n := len(v.worklist) - 1
if n < 0 {
break
}
prod := v.worklist[n]
v.worklist = v.worklist[0:n]
v.verifyExpr(prod.Expr, isLexical(prod.Name.String)) v.verifyExpr(prod.Expr, isLexical(prod.Name.String))
} }
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package ebnf package ebnf
import ( import (
"container/vector"
"go/scanner" "go/scanner"
"go/token" "go/token"
"os" "os"
...@@ -116,36 +115,30 @@ func (p *parser) parseTerm() (x Expression) { ...@@ -116,36 +115,30 @@ func (p *parser) parseTerm() (x Expression) {
func (p *parser) parseSequence() Expression { func (p *parser) parseSequence() Expression {
var list vector.Vector var list Sequence
for x := p.parseTerm(); x != nil; x = p.parseTerm() { for x := p.parseTerm(); x != nil; x = p.parseTerm() {
list.Push(x) list = append(list, x)
} }
// no need for a sequence if list.Len() < 2 // no need for a sequence if list.Len() < 2
switch list.Len() { switch len(list) {
case 0: case 0:
return nil return nil
case 1: case 1:
return list.At(0).(Expression) return list[0]
} }
// convert list into a sequence return list
seq := make(Sequence, list.Len())
for i := 0; i < list.Len(); i++ {
seq[i] = list.At(i).(Expression)
}
return seq
} }
func (p *parser) parseExpression() Expression { func (p *parser) parseExpression() Expression {
var list vector.Vector var list Alternative
for { for {
x := p.parseSequence() if x := p.parseSequence(); x != nil {
if x != nil { list = append(list, x)
list.Push(x)
} }
if p.tok != token.OR { if p.tok != token.OR {
break break
...@@ -154,19 +147,14 @@ func (p *parser) parseExpression() Expression { ...@@ -154,19 +147,14 @@ func (p *parser) parseExpression() Expression {
} }
// no need for an Alternative node if list.Len() < 2 // no need for an Alternative node if list.Len() < 2
switch list.Len() { switch len(list) {
case 0: case 0:
return nil return nil
case 1: case 1:
return list.At(0).(Expression) return list[0]
} }
// convert list into an Alternative node return list
alt := make(Alternative, list.Len())
for i := 0; i < list.Len(); i++ {
alt[i] = list.At(i).(Expression)
}
return alt
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment