diff --git a/.travis.yml b/.travis.yml index 62dad5b8..4c330e0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_install: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls # Required for dot parser checks. - - ./.travis/install-gocc.sh 2454fdc + - ./.travis/install-gocc.sh cfc6e12 go_import_path: gonum.org/v1/gonum diff --git a/graph/formats/dot/internal/errors/errors.go b/graph/formats/dot/internal/errors/errors.go index 1843da8e..44f8825e 100644 --- a/graph/formats/dot/internal/errors/errors.go +++ b/graph/formats/dot/internal/errors/errors.go @@ -30,22 +30,22 @@ type Error struct { StackTop int } -func (E *Error) String() string { +func (e *Error) String() string { w := new(bytes.Buffer) fmt.Fprintf(w, "Error") - if E.Err != nil { - fmt.Fprintf(w, " %s\n", E.Err) + if e.Err != nil { + fmt.Fprintf(w, " %s\n", e.Err) } else { fmt.Fprintf(w, "\n") } - 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, "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, "Expected one of: ") - for _, sym := range E.ExpectedTokens { + for _, sym := range e.ExpectedTokens { fmt.Fprintf(w, "%s ", sym) } fmt.Fprintf(w, "ErrorSymbol:\n") - for _, sym := range E.ErrorSymbols { + for _, sym := range e.ErrorSymbols { fmt.Fprintf(w, "%v\n", sym) } return w.String() diff --git a/graph/formats/dot/internal/lexer/acttab.go b/graph/formats/dot/internal/lexer/acttab.go index d63702ed..34c9d4dd 100644 --- a/graph/formats/dot/internal/lexer/acttab.go +++ b/graph/formats/dot/internal/lexer/acttab.go @@ -25,8 +25,8 @@ type ActionRow struct { Ignore string } -func (this ActionRow) String() string { - return fmt.Sprintf("Accept=%d, Ignore=%s", this.Accept, this.Ignore) +func (a ActionRow) String() string { + return fmt.Sprintf("Accept=%d, Ignore=%s", a.Accept, a.Ignore) } var ActTab = ActionTable{ diff --git a/graph/formats/dot/internal/lexer/lexer.go b/graph/formats/dot/internal/lexer/lexer.go index dcd95e84..caa9c461 100644 --- a/graph/formats/dot/internal/lexer/lexer.go +++ b/graph/formats/dot/internal/lexer/lexer.go @@ -13,11 +13,9 @@ package lexer import ( - // "fmt" "io/ioutil" "unicode/utf8" - // "gonum.org/v1/gonum/graph/formats/dot/internal/util" "gonum.org/v1/gonum/graph/formats/dot/internal/token" ) @@ -52,94 +50,75 @@ func NewLexerFile(fpath string) (*Lexer, error) { return NewLexer(src), nil } -func (this *Lexer) Scan() (tok *token.Token) { - // fmt.Printf("Lexer.Scan() pos=%d\n", this.pos) +func (l *Lexer) Scan() (tok *token.Token) { tok = new(token.Token) - if this.pos >= len(this.src) { + if l.pos >= len(l.src) { 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 } - 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 state, rune1, size := 0, rune(-1), 0 for state != -1 { - // fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state) - if this.pos >= len(this.src) { + if l.pos >= len(l.src) { rune1 = -1 } else { - rune1, size = utf8.DecodeRune(this.src[this.pos:]) - this.pos += size + rune1, size = utf8.DecodeRune(l.src[l.pos:]) + l.pos += size } - // Production start - if rune1 != -1 { - state = TransTab[state](rune1) - } else { - state = -1 - } - // Production end - // Debug start - // nextState := -1 - // if rune1 != -1 { - // nextState = TransTab[state](rune1) - // } - // 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 + nextState := -1 + if rune1 != -1 { + nextState = TransTab[state](rune1) + } + state = nextState if state != -1 { switch rune1 { case '\n': - this.line++ - this.column = 1 + l.line++ + l.column = 1 case '\r': - this.column = 1 + l.column = 1 case '\t': - this.column += 4 + l.column += 4 default: - this.column++ + l.column++ } switch { case ActTab[state].Accept != -1: tok.Type = ActTab[state].Accept - // fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok) - end = this.pos + end = l.pos case ActTab[state].Ignore != "": - // fmt.Printf("\t Ignore(%s)\n", string(act)) - start, startLine, startColumn = this.pos, this.line, this.column + start, startLine, startColumn = l.pos, l.line, l.column state = 0 - if start >= len(this.src) { + if start >= len(l.src) { tok.Type = token.EOF } } } else { if tok.Type == token.INVALID { - end = this.pos + end = l.pos } } } if end > start { - this.pos = end - tok.Lit = this.src[start:end] + l.pos = end + tok.Lit = l.src[start:end] } else { tok.Lit = []byte{} } 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 } -func (this *Lexer) Reset() { - this.pos = 0 +func (l *Lexer) Reset() { + l.pos = 0 } /* diff --git a/graph/formats/dot/internal/parser/parser.go b/graph/formats/dot/internal/parser/parser.go index 949ac3b8..18e146e0 100644 --- a/graph/formats/dot/internal/parser/parser.go +++ b/graph/formats/dot/internal/parser/parser.go @@ -42,48 +42,48 @@ func newStack() *stack { } } -func (this *stack) reset() { - this.state = this.state[0:0] - this.attrib = this.attrib[0:0] +func (s *stack) reset() { + s.state = s.state[:0] + s.attrib = s.attrib[:0] } -func (this *stack) push(s int, a Attrib) { - this.state = append(this.state, s) - this.attrib = append(this.attrib, a) +func (s *stack) push(state int, a Attrib) { + s.state = append(s.state, state) + s.attrib = append(s.attrib, a) } -func (this *stack) top() int { - return this.state[len(this.state)-1] +func (s *stack) top() int { + return s.state[len(s.state)-1] } -func (this *stack) peek(pos int) int { - return this.state[pos] +func (s *stack) peek(pos int) int { + return s.state[pos] } -func (this *stack) topIndex() int { - return len(this.state) - 1 +func (s *stack) topIndex() int { + return len(s.state) - 1 } -func (this *stack) popN(items int) []Attrib { - lo, hi := len(this.state)-items, len(this.state) +func (s *stack) popN(items int) []Attrib { + lo, hi := len(s.state)-items, len(s.state) - attrib := this.attrib[lo:hi] + attrib := s.attrib[lo:hi] - this.state = this.state[:lo] - this.attrib = this.attrib[:lo] + s.state = s.state[:lo] + s.attrib = s.attrib[:lo] return attrib } -func (S *stack) String() string { +func (s *stack) String() string { w := new(bytes.Buffer) 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) - if S.attrib[i] == nil { + if s.attrib[i] == nil { fmt.Fprintf(w, "nil") } else { - fmt.Fprintf(w, "%v", S.attrib[i]) + fmt.Fprintf(w, "%v", s.attrib[i]) } fmt.Fprintf(w, "\n") } @@ -108,36 +108,36 @@ func NewParser() *Parser { return p } -func (P *Parser) Reset() { - P.stack.reset() - P.stack.push(0, nil) +func (p *Parser) Reset() { + p.stack.reset() + 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{ Err: err, - ErrorToken: P.nextToken, - ErrorSymbols: P.popNonRecoveryStates(), + ErrorToken: p.nextToken, + ErrorSymbols: p.popNonRecoveryStates(), 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 { errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, token.TokMap.Id(token.Type(t))) } } - 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 + 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 } else { 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 } - for !recovered && P.nextToken.Type != token.EOF { - P.nextToken = scanner.Scan() - if action := actionTab[P.stack.top()].actions[P.nextToken.Type]; action != nil { + for !recovered && p.nextToken.Type != token.EOF { + p.nextToken = scanner.Scan() + if action := actionTab[p.stack.top()].actions[p.nextToken.Type]; action != nil { recovered = true } } @@ -145,9 +145,9 @@ func (P *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib return } -func (P *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol) { - if rs, ok := P.firstRecoveryState(); ok { - errorSymbols := P.stack.popN(int(P.stack.topIndex() - rs)) +func (p *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol) { + if rs, ok := p.firstRecoveryState(); ok { + errorSymbols := p.stack.popN(int(p.stack.topIndex() - rs)) removedAttribs = make([]parseError.ErrorSymbol, len(errorSymbols)) for i, e := range errorSymbols { 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 -func (P *Parser) firstRecoveryState() (recoveryState int, canRecover bool) { - recoveryState, canRecover = P.stack.topIndex(), actionTab[P.stack.top()].canRecover +func (p *Parser) firstRecoveryState() (recoveryState int, canRecover bool) { + recoveryState, canRecover = p.stack.topIndex(), actionTab[p.stack.top()].canRecover for recoveryState > 0 && !canRecover { recoveryState-- - canRecover = actionTab[P.stack.peek(recoveryState)].canRecover + canRecover = actionTab[p.stack.peek(recoveryState)].canRecover } return } -func (P *Parser) newError(err error) error { +func (p *Parser) newError(err error) error { e := &parseError.Error{ Err: err, - StackTop: P.stack.top(), - ErrorToken: P.nextToken, + StackTop: p.stack.top(), + ErrorToken: p.nextToken, } - actRow := actionTab[P.stack.top()] + actRow := actionTab[p.stack.top()] for i, t := range actRow.actions { if t != nil { e.ExpectedTokens = append(e.ExpectedTokens, token.TokMap.Id(token.Type(i))) @@ -183,36 +183,35 @@ func (P *Parser) newError(err error) error { return e } -func (this *Parser) Parse(scanner Scanner) (res interface{}, err error) { - this.Reset() - this.nextToken = scanner.Scan() +func (p *Parser) Parse(scanner Scanner) (res interface{}, err error) { + p.Reset() + p.nextToken = scanner.Scan() 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 recovered, errAttrib := this.Error(nil, scanner); !recovered { - this.nextToken = errAttrib.ErrorToken - return nil, this.newError(nil) + if recovered, errAttrib := p.Error(nil, scanner); !recovered { + p.nextToken = errAttrib.ErrorToken + 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") } } - // fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String()) switch act := action.(type) { case accept: - res = this.stack.popN(1)[0] + res = p.stack.popN(1)[0] acc = true case shift: - this.stack.push(int(act), this.nextToken) - this.nextToken = scanner.Scan() + p.stack.push(int(act), p.nextToken) + p.nextToken = scanner.Scan() case reduce: 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 { - return nil, this.newError(err) + return nil, p.newError(err) } else { - this.stack.push(gotoTab[this.stack.top()][prod.NTType], attrib) + p.stack.push(gotoTab[p.stack.top()][prod.NTType], attrib) } default: panic("unknown action: " + action.String()) diff --git a/graph/formats/dot/internal/token/token.go b/graph/formats/dot/internal/token/token.go index 31db6035..1492df0d 100644 --- a/graph/formats/dot/internal/token/token.go +++ b/graph/formats/dot/internal/token/token.go @@ -35,8 +35,8 @@ type Pos struct { Column int } -func (this Pos) String() string { - return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", this.Offset, this.Line, this.Column) +func (p Pos) String() string { + return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column) } type TokenMap struct { @@ -44,27 +44,27 @@ type TokenMap struct { idMap map[string]Type } -func (this TokenMap) Id(tok Type) string { - if int(tok) < len(this.typeMap) { - return this.typeMap[tok] +func (m TokenMap) Id(tok Type) string { + if int(tok) < len(m.typeMap) { + return m.typeMap[tok] } return "unknown" } -func (this TokenMap) Type(tok string) Type { - if typ, exist := this.idMap[tok]; exist { +func (m TokenMap) Type(tok string) Type { + if typ, exist := m.idMap[tok]; exist { return typ } return INVALID } -func (this TokenMap) TokenString(tok *Token) string { +func (m TokenMap) TokenString(tok *Token) string { //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 { - return fmt.Sprintf("%s(%d)", this.Id(typ), typ) +func (m TokenMap) StringType(typ Type) string { + return fmt.Sprintf("%s(%d)", m.Id(typ), typ) } var TokMap = TokenMap{