graph/formats/dot/internal: update gocc version to cfc6e12

This commit is contained in:
kortschak
2017-08-16 09:10:31 +09:30
committed by Dan Kortschak
parent 620965de6d
commit 159a37d657
6 changed files with 104 additions and 126 deletions

View File

@@ -21,7 +21,7 @@ before_install:
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls - go get github.com/mattn/goveralls
# Required for dot parser checks. # Required for dot parser checks.
- ./.travis/install-gocc.sh 2454fdc - ./.travis/install-gocc.sh cfc6e12
go_import_path: gonum.org/v1/gonum go_import_path: gonum.org/v1/gonum

View File

@@ -30,22 +30,22 @@ type Error struct {
StackTop int StackTop int
} }
func (E *Error) String() string { func (e *Error) String() string {
w := new(bytes.Buffer) w := new(bytes.Buffer)
fmt.Fprintf(w, "Error") fmt.Fprintf(w, "Error")
if E.Err != nil { if e.Err != nil {
fmt.Fprintf(w, " %s\n", E.Err) fmt.Fprintf(w, " %s\n", e.Err)
} else { } else {
fmt.Fprintf(w, "\n") fmt.Fprintf(w, "\n")
} }
fmt.Fprintf(w, "Token: type=%d, lit=%s\n", E.ErrorToken.Type, E.ErrorToken.Lit) fmt.Fprintf(w, "Token: type=%d, lit=%s\n", e.ErrorToken.Type, e.ErrorToken.Lit)
fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", E.ErrorToken.Pos.Offset, E.ErrorToken.Pos.Line, E.ErrorToken.Pos.Column) fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", e.ErrorToken.Pos.Offset, e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
fmt.Fprintf(w, "Expected one of: ") fmt.Fprintf(w, "Expected one of: ")
for _, sym := range E.ExpectedTokens { for _, sym := range e.ExpectedTokens {
fmt.Fprintf(w, "%s ", sym) fmt.Fprintf(w, "%s ", sym)
} }
fmt.Fprintf(w, "ErrorSymbol:\n") fmt.Fprintf(w, "ErrorSymbol:\n")
for _, sym := range E.ErrorSymbols { for _, sym := range e.ErrorSymbols {
fmt.Fprintf(w, "%v\n", sym) fmt.Fprintf(w, "%v\n", sym)
} }
return w.String() return w.String()

View File

@@ -25,8 +25,8 @@ type ActionRow struct {
Ignore string Ignore string
} }
func (this ActionRow) String() string { func (a ActionRow) String() string {
return fmt.Sprintf("Accept=%d, Ignore=%s", this.Accept, this.Ignore) return fmt.Sprintf("Accept=%d, Ignore=%s", a.Accept, a.Ignore)
} }
var ActTab = ActionTable{ var ActTab = ActionTable{

View File

@@ -13,11 +13,9 @@
package lexer package lexer
import ( import (
// "fmt"
"io/ioutil" "io/ioutil"
"unicode/utf8" "unicode/utf8"
// "gonum.org/v1/gonum/graph/formats/dot/internal/util"
"gonum.org/v1/gonum/graph/formats/dot/internal/token" "gonum.org/v1/gonum/graph/formats/dot/internal/token"
) )
@@ -52,94 +50,75 @@ func NewLexerFile(fpath string) (*Lexer, error) {
return NewLexer(src), nil return NewLexer(src), nil
} }
func (this *Lexer) Scan() (tok *token.Token) { func (l *Lexer) Scan() (tok *token.Token) {
// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
tok = new(token.Token) tok = new(token.Token)
if this.pos >= len(this.src) { if l.pos >= len(l.src) {
tok.Type = token.EOF tok.Type = token.EOF
tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = this.pos, this.line, this.column tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column
return return
} }
start, startLine, startColumn, end := this.pos, this.line, this.column, 0 start, startLine, startColumn, end := l.pos, l.line, l.column, 0
tok.Type = token.INVALID tok.Type = token.INVALID
state, rune1, size := 0, rune(-1), 0 state, rune1, size := 0, rune(-1), 0
for state != -1 { for state != -1 {
// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state) if l.pos >= len(l.src) {
if this.pos >= len(this.src) {
rune1 = -1 rune1 = -1
} else { } else {
rune1, size = utf8.DecodeRune(this.src[this.pos:]) rune1, size = utf8.DecodeRune(l.src[l.pos:])
this.pos += size l.pos += size
} }
// Production start
if rune1 != -1 {
state = TransTab[state](rune1)
} else {
state = -1
}
// Production end
// Debug start nextState := -1
// nextState := -1 if rune1 != -1 {
// if rune1 != -1 { nextState = TransTab[state](rune1)
// nextState = TransTab[state](rune1) }
// } state = nextState
// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
// if nextState != -1 {
// fmt.Printf("\t\taction:%s\n", ActTab[nextState].String())
// }
// state = nextState
// Debug end
if state != -1 { if state != -1 {
switch rune1 { switch rune1 {
case '\n': case '\n':
this.line++ l.line++
this.column = 1 l.column = 1
case '\r': case '\r':
this.column = 1 l.column = 1
case '\t': case '\t':
this.column += 4 l.column += 4
default: default:
this.column++ l.column++
} }
switch { switch {
case ActTab[state].Accept != -1: case ActTab[state].Accept != -1:
tok.Type = ActTab[state].Accept tok.Type = ActTab[state].Accept
// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok) end = l.pos
end = this.pos
case ActTab[state].Ignore != "": case ActTab[state].Ignore != "":
// fmt.Printf("\t Ignore(%s)\n", string(act)) start, startLine, startColumn = l.pos, l.line, l.column
start, startLine, startColumn = this.pos, this.line, this.column
state = 0 state = 0
if start >= len(this.src) { if start >= len(l.src) {
tok.Type = token.EOF tok.Type = token.EOF
} }
} }
} else { } else {
if tok.Type == token.INVALID { if tok.Type == token.INVALID {
end = this.pos end = l.pos
} }
} }
} }
if end > start { if end > start {
this.pos = end l.pos = end
tok.Lit = this.src[start:end] tok.Lit = l.src[start:end]
} else { } else {
tok.Lit = []byte{} tok.Lit = []byte{}
} }
tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn
// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
return return
} }
func (this *Lexer) Reset() { func (l *Lexer) Reset() {
this.pos = 0 l.pos = 0
} }
/* /*

View File

@@ -42,48 +42,48 @@ func newStack() *stack {
} }
} }
func (this *stack) reset() { func (s *stack) reset() {
this.state = this.state[0:0] s.state = s.state[:0]
this.attrib = this.attrib[0:0] s.attrib = s.attrib[:0]
} }
func (this *stack) push(s int, a Attrib) { func (s *stack) push(state int, a Attrib) {
this.state = append(this.state, s) s.state = append(s.state, state)
this.attrib = append(this.attrib, a) s.attrib = append(s.attrib, a)
} }
func (this *stack) top() int { func (s *stack) top() int {
return this.state[len(this.state)-1] return s.state[len(s.state)-1]
} }
func (this *stack) peek(pos int) int { func (s *stack) peek(pos int) int {
return this.state[pos] return s.state[pos]
} }
func (this *stack) topIndex() int { func (s *stack) topIndex() int {
return len(this.state) - 1 return len(s.state) - 1
} }
func (this *stack) popN(items int) []Attrib { func (s *stack) popN(items int) []Attrib {
lo, hi := len(this.state)-items, len(this.state) lo, hi := len(s.state)-items, len(s.state)
attrib := this.attrib[lo:hi] attrib := s.attrib[lo:hi]
this.state = this.state[:lo] s.state = s.state[:lo]
this.attrib = this.attrib[:lo] s.attrib = s.attrib[:lo]
return attrib return attrib
} }
func (S *stack) String() string { func (s *stack) String() string {
w := new(bytes.Buffer) w := new(bytes.Buffer)
fmt.Fprintf(w, "stack:\n") fmt.Fprintf(w, "stack:\n")
for i, st := range S.state { for i, st := range s.state {
fmt.Fprintf(w, "\t%d:%d , ", i, st) fmt.Fprintf(w, "\t%d:%d , ", i, st)
if S.attrib[i] == nil { if s.attrib[i] == nil {
fmt.Fprintf(w, "nil") fmt.Fprintf(w, "nil")
} else { } else {
fmt.Fprintf(w, "%v", S.attrib[i]) fmt.Fprintf(w, "%v", s.attrib[i])
} }
fmt.Fprintf(w, "\n") fmt.Fprintf(w, "\n")
} }
@@ -108,36 +108,36 @@ func NewParser() *Parser {
return p return p
} }
func (P *Parser) Reset() { func (p *Parser) Reset() {
P.stack.reset() p.stack.reset()
P.stack.push(0, nil) p.stack.push(0, nil)
} }
func (P *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib *parseError.Error) { func (p *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib *parseError.Error) {
errorAttrib = &parseError.Error{ errorAttrib = &parseError.Error{
Err: err, Err: err,
ErrorToken: P.nextToken, ErrorToken: p.nextToken,
ErrorSymbols: P.popNonRecoveryStates(), ErrorSymbols: p.popNonRecoveryStates(),
ExpectedTokens: make([]string, 0, 8), ExpectedTokens: make([]string, 0, 8),
} }
for t, action := range actionTab[P.stack.top()].actions { for t, action := range actionTab[p.stack.top()].actions {
if action != nil { if action != nil {
errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, token.TokMap.Id(token.Type(t))) errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, token.TokMap.Id(token.Type(t)))
} }
} }
if action := actionTab[P.stack.top()].actions[token.TokMap.Type("error")]; action != nil { if action := actionTab[p.stack.top()].actions[token.TokMap.Type("error")]; action != nil {
P.stack.push(int(action.(shift)), errorAttrib) // action can only be shift p.stack.push(int(action.(shift)), errorAttrib) // action can only be shift
} else { } else {
return return
} }
if action := actionTab[P.stack.top()].actions[P.nextToken.Type]; action != nil { if action := actionTab[p.stack.top()].actions[p.nextToken.Type]; action != nil {
recovered = true recovered = true
} }
for !recovered && P.nextToken.Type != token.EOF { for !recovered && p.nextToken.Type != token.EOF {
P.nextToken = scanner.Scan() p.nextToken = scanner.Scan()
if action := actionTab[P.stack.top()].actions[P.nextToken.Type]; action != nil { if action := actionTab[p.stack.top()].actions[p.nextToken.Type]; action != nil {
recovered = true recovered = true
} }
} }
@@ -145,9 +145,9 @@ func (P *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib
return return
} }
func (P *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol) { func (p *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol) {
if rs, ok := P.firstRecoveryState(); ok { if rs, ok := p.firstRecoveryState(); ok {
errorSymbols := P.stack.popN(int(P.stack.topIndex() - rs)) errorSymbols := p.stack.popN(int(p.stack.topIndex() - rs))
removedAttribs = make([]parseError.ErrorSymbol, len(errorSymbols)) removedAttribs = make([]parseError.ErrorSymbol, len(errorSymbols))
for i, e := range errorSymbols { for i, e := range errorSymbols {
removedAttribs[i] = e removedAttribs[i] = e
@@ -159,22 +159,22 @@ func (P *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol
} }
// recoveryState points to the highest state on the stack, which can recover // recoveryState points to the highest state on the stack, which can recover
func (P *Parser) firstRecoveryState() (recoveryState int, canRecover bool) { func (p *Parser) firstRecoveryState() (recoveryState int, canRecover bool) {
recoveryState, canRecover = P.stack.topIndex(), actionTab[P.stack.top()].canRecover recoveryState, canRecover = p.stack.topIndex(), actionTab[p.stack.top()].canRecover
for recoveryState > 0 && !canRecover { for recoveryState > 0 && !canRecover {
recoveryState-- recoveryState--
canRecover = actionTab[P.stack.peek(recoveryState)].canRecover canRecover = actionTab[p.stack.peek(recoveryState)].canRecover
} }
return return
} }
func (P *Parser) newError(err error) error { func (p *Parser) newError(err error) error {
e := &parseError.Error{ e := &parseError.Error{
Err: err, Err: err,
StackTop: P.stack.top(), StackTop: p.stack.top(),
ErrorToken: P.nextToken, ErrorToken: p.nextToken,
} }
actRow := actionTab[P.stack.top()] actRow := actionTab[p.stack.top()]
for i, t := range actRow.actions { for i, t := range actRow.actions {
if t != nil { if t != nil {
e.ExpectedTokens = append(e.ExpectedTokens, token.TokMap.Id(token.Type(i))) e.ExpectedTokens = append(e.ExpectedTokens, token.TokMap.Id(token.Type(i)))
@@ -183,36 +183,35 @@ func (P *Parser) newError(err error) error {
return e return e
} }
func (this *Parser) Parse(scanner Scanner) (res interface{}, err error) { func (p *Parser) Parse(scanner Scanner) (res interface{}, err error) {
this.Reset() p.Reset()
this.nextToken = scanner.Scan() p.nextToken = scanner.Scan()
for acc := false; !acc; { for acc := false; !acc; {
action := actionTab[this.stack.top()].actions[this.nextToken.Type] action := actionTab[p.stack.top()].actions[p.nextToken.Type]
if action == nil { if action == nil {
if recovered, errAttrib := this.Error(nil, scanner); !recovered { if recovered, errAttrib := p.Error(nil, scanner); !recovered {
this.nextToken = errAttrib.ErrorToken p.nextToken = errAttrib.ErrorToken
return nil, this.newError(nil) return nil, p.newError(nil)
} }
if action = actionTab[this.stack.top()].actions[this.nextToken.Type]; action == nil { if action = actionTab[p.stack.top()].actions[p.nextToken.Type]; action == nil {
panic("Error recovery led to invalid action") panic("Error recovery led to invalid action")
} }
} }
// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
switch act := action.(type) { switch act := action.(type) {
case accept: case accept:
res = this.stack.popN(1)[0] res = p.stack.popN(1)[0]
acc = true acc = true
case shift: case shift:
this.stack.push(int(act), this.nextToken) p.stack.push(int(act), p.nextToken)
this.nextToken = scanner.Scan() p.nextToken = scanner.Scan()
case reduce: case reduce:
prod := productionsTable[int(act)] prod := productionsTable[int(act)]
attrib, err := prod.ReduceFunc(this.stack.popN(prod.NumSymbols)) attrib, err := prod.ReduceFunc(p.stack.popN(prod.NumSymbols))
if err != nil { if err != nil {
return nil, this.newError(err) return nil, p.newError(err)
} else { } else {
this.stack.push(gotoTab[this.stack.top()][prod.NTType], attrib) p.stack.push(gotoTab[p.stack.top()][prod.NTType], attrib)
} }
default: default:
panic("unknown action: " + action.String()) panic("unknown action: " + action.String())

View File

@@ -35,8 +35,8 @@ type Pos struct {
Column int Column int
} }
func (this Pos) String() string { func (p Pos) String() string {
return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", this.Offset, this.Line, this.Column) return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
} }
type TokenMap struct { type TokenMap struct {
@@ -44,27 +44,27 @@ type TokenMap struct {
idMap map[string]Type idMap map[string]Type
} }
func (this TokenMap) Id(tok Type) string { func (m TokenMap) Id(tok Type) string {
if int(tok) < len(this.typeMap) { if int(tok) < len(m.typeMap) {
return this.typeMap[tok] return m.typeMap[tok]
} }
return "unknown" return "unknown"
} }
func (this TokenMap) Type(tok string) Type { func (m TokenMap) Type(tok string) Type {
if typ, exist := this.idMap[tok]; exist { if typ, exist := m.idMap[tok]; exist {
return typ return typ
} }
return INVALID return INVALID
} }
func (this TokenMap) TokenString(tok *Token) string { func (m TokenMap) TokenString(tok *Token) string {
//TODO: refactor to print pos & token string properly //TODO: refactor to print pos & token string properly
return fmt.Sprintf("%s(%d,%s)", this.Id(tok.Type), tok.Type, tok.Lit) return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
} }
func (this TokenMap) StringType(typ Type) string { func (m TokenMap) StringType(typ Type) string {
return fmt.Sprintf("%s(%d)", this.Id(typ), typ) return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
} }
var TokMap = TokenMap{ var TokMap = TokenMap{