Skip to content

Commit

Permalink
Follow golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nihei9 committed Mar 27, 2022
1 parent 5212b7f commit bb9bf49
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 103 deletions.
5 changes: 1 addition & 4 deletions cmd/vartan/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
"github.com/spf13/cobra"
)

var showFlags = struct {
}{}

func init() {
cmd := &cobra.Command{
Use: "show",
Expand Down Expand Up @@ -150,7 +147,7 @@ func writeDescription(w io.Writer, desc *spec.Description) error {
}

if count == 1 {
return fmt.Sprintf("1 conflict was detected.")
return "1 conflict was detected."
} else if count > 1 {
return fmt.Sprintf("%v conflicts were detected.", count)
}
Expand Down
60 changes: 4 additions & 56 deletions grammar/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,6 @@ func (flw *followSet) find(sym symbol) (*followEntry, error) {
return e, nil
}

type followComContext struct {
prods *productionSet
first *firstSet
follow *followSet
}

func newFollowComContext(prods *productionSet, first *firstSet) *followComContext {
return &followComContext{
prods: prods,
first: first,
follow: newFollow(prods),
}
}

func genFollowSet(prods *productionSet, first *firstSet) (*followSet, error) {
ntsyms := map[symbol]struct{}{}
for _, prod := range prods.getAllProductions() {
Expand All @@ -110,11 +96,11 @@ func genFollowSet(prods *productionSet, first *firstSet) (*followSet, error) {
ntsyms[prod.lhs] = struct{}{}
}

cc := newFollowComContext(prods, first)
follow := newFollow(prods)
for {
more := false
for ntsym := range ntsyms {
e, err := cc.follow.find(ntsym)
e, err := follow.find(ntsym)
if err != nil {
return nil, err
}
Expand All @@ -138,7 +124,7 @@ func genFollowSet(prods *productionSet, first *firstSet) (*followSet, error) {
more = true
}
if fst.empty {
flw, err := cc.follow.find(prod.lhs)
flw, err := follow.find(prod.lhs)
if err != nil {
return nil, err
}
Expand All @@ -155,43 +141,5 @@ func genFollowSet(prods *productionSet, first *firstSet) (*followSet, error) {
}
}

return cc.follow, nil
}

func genFollowEntry(cc *followComContext, acc *followEntry, ntsym symbol) (bool, error) {
changed := false

if ntsym.isStart() {
added := acc.addEOF()
if added {
changed = true
}
}
for _, prod := range cc.prods.getAllProductions() {
for i, sym := range prod.rhs {
if sym != ntsym {
continue
}
fst, err := cc.first.find(prod, i+1)
if err != nil {
return false, err
}
added := acc.merge(fst, nil)
if added {
changed = true
}
if fst.empty {
flw, err := cc.follow.find(prod.lhs)
if err != nil {
return false, err
}
added := acc.merge(nil, flw)
if added {
changed = true
}
}
}
}

return changed, nil
return follow, nil
}
3 changes: 3 additions & 0 deletions grammar/follow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func genActualFollow(t *testing.T, src string) (*followSet, *Grammar) {
t.Fatal(err)
}
flw, err := genFollowSet(gram.productionSet, fst)
if err != nil {
t.Fatal(err)
}
if flw == nil {
t.Fatal("genFollow returned nil without any error")
}
Expand Down
25 changes: 14 additions & 11 deletions grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (b *GrammarBuilder) Build() (*Grammar, error) {
if len(md.Parameters) != 1 || md.Parameters[0].ID == "" {
b.errs = append(b.errs, &verr.SpecError{
Cause: semErrMDInvalidParam,
Detail: fmt.Sprintf("'name' takes just one ID parameter"),
Detail: "'name' takes just one ID parameter",
Row: md.Pos.Row,
Col: md.Pos.Col,
})
Expand Down Expand Up @@ -480,7 +480,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if len(dir.Parameters) == 0 {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'mode' directive needs an ID parameter"),
Detail: "'mode' directive needs an ID parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
Expand All @@ -489,7 +489,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if param.ID == "" {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'mode' directive needs an ID parameter"),
Detail: "'mode' directive needs an ID parameter",
Row: param.Pos.Row,
Col: param.Pos.Col,
}, nil
Expand Down Expand Up @@ -528,7 +528,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if len(dir.Parameters) > 0 {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'skip' directive needs no parameter"),
Detail: "'skip' directive needs no parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
Expand All @@ -538,7 +538,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if len(dir.Parameters) != 1 || dir.Parameters[0].ID == "" {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'push' directive needs an ID parameter"),
Detail: "'push' directive needs an ID parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
Expand All @@ -548,7 +548,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if len(dir.Parameters) > 0 {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'pop' directive needs no parameter"),
Detail: "'pop' directive needs no parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
Expand All @@ -558,7 +558,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, string, *ve
if len(dir.Parameters) != 1 || dir.Parameters[0].String == "" {
return nil, false, "", &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'alias' directive needs a string parameter"),
Detail: "'alias' directive needs a string parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
Expand Down Expand Up @@ -762,7 +762,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
if len(dir.Parameters) != 1 || dir.Parameters[0].Tree == nil {
b.errs = append(b.errs, &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'ast' directive needs a tree parameter"),
Detail: "'ast' directive needs a tree parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
})
Expand Down Expand Up @@ -829,7 +829,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
if len(dir.Parameters) != 1 || dir.Parameters[0].ID == "" {
b.errs = append(b.errs, &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'prec' directive needs an ID parameter"),
Detail: "'prec' directive needs an ID parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
})
Expand Down Expand Up @@ -859,7 +859,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
if len(dir.Parameters) > 0 {
b.errs = append(b.errs, &verr.SpecError{
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'recover' directive needs no parameter"),
Detail: "'recover' directive needs no parameter",
Row: dir.Pos.Row,
Col: dir.Pos.Col,
})
Expand Down Expand Up @@ -1140,7 +1140,10 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error
return nil, err
}

f.Write(d)
_, err = f.Write(d)
if err != nil {
return nil, fmt.Errorf("failed to write a description file: %w", err)
}
}

if len(b.conflicts) > 0 {
Expand Down
4 changes: 0 additions & 4 deletions grammar/parsing_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ func newGoToEntry(state stateNum) goToEntry {
return goToEntry(state)
}

func (e goToEntry) isEmpty() bool {
return e == goToEntryEmpty
}

func (e goToEntry) describe() (GoToType, stateNum) {
if e == goToEntryEmpty {
return GoToTypeError, stateNumInitial
Expand Down
4 changes: 0 additions & 4 deletions grammar/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func newProduction(lhs symbol, rhs []symbol) (*production, error) {
}, nil
}

func (p *production) equals(q *production) bool {
return q.id == p.id
}

func (p *production) isEmpty() bool {
return p.rhsLen == 0
}
Expand Down
5 changes: 1 addition & 4 deletions grammar/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ func (s symbol) isNonTerminal() bool {
return false
}
kind, _, _, _ := s.describe()
if kind == symbolKindNonTerminal {
return true
}
return false
return kind == symbolKindNonTerminal
}

func (s symbol) isTerminal() bool {
Expand Down
18 changes: 9 additions & 9 deletions grammar/symbol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import "testing"

func TestSymbol(t *testing.T) {
tab := newSymbolTable()
tab.registerStartSymbol("expr'")
tab.registerNonTerminalSymbol("expr")
tab.registerNonTerminalSymbol("term")
tab.registerNonTerminalSymbol("factor")
tab.registerTerminalSymbol("id")
tab.registerTerminalSymbol("add")
tab.registerTerminalSymbol("mul")
tab.registerTerminalSymbol("l_paren")
tab.registerTerminalSymbol("r_paren")
_, _ = tab.registerStartSymbol("expr'")
_, _ = tab.registerNonTerminalSymbol("expr")
_, _ = tab.registerNonTerminalSymbol("term")
_, _ = tab.registerNonTerminalSymbol("factor")
_, _ = tab.registerTerminalSymbol("id")
_, _ = tab.registerTerminalSymbol("add")
_, _ = tab.registerTerminalSymbol("mul")
_, _ = tab.registerTerminalSymbol("l_paren")
_, _ = tab.registerTerminalSymbol("r_paren")

nonTermTexts := []string{
"", // Nil
Expand Down
8 changes: 4 additions & 4 deletions spec/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (l *lexer) lexAndSkipWSs() (*token, error) {
// The escape sequences in a pattern string are interpreted by the lexer, except for the \".
// We must interpret the \" before passing them to the lexer because they are delimiters for
// the pattern strings.
fmt.Fprintf(&b, strings.ReplaceAll(string(tok.Lexeme), `\"`, `"`))
fmt.Fprint(&b, strings.ReplaceAll(string(tok.Lexeme), `\"`, `"`))
case KindIDEscapeSymbol:
return nil, &verr.SpecError{
Cause: synErrIncompletedEscSeq,
Expand Down Expand Up @@ -240,13 +240,13 @@ func (l *lexer) lexAndSkipWSs() (*token, error) {
}
switch tok.KindID {
case KindIDCharSeq:
fmt.Fprintf(&b, string(tok.Lexeme))
fmt.Fprint(&b, string(tok.Lexeme))
case KindIDEscapedQuot:
// Remove '\' character.
fmt.Fprintf(&b, `'`)
fmt.Fprint(&b, `'`)
case KindIDEscapedBackSlash:
// Remove '\' character.
fmt.Fprintf(&b, `\`)
fmt.Fprint(&b, `\`)
case KindIDEscapeSymbol:
return nil, &verr.SpecError{
Cause: synErrIncompletedEscSeq,
Expand Down
6 changes: 0 additions & 6 deletions spec/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ func (p *parser) parseMetaData() *DirectiveNode {

p.errs = append(p.errs, specErr)
p.skipOverTo(tokenKindNewline)

return
}()

p.consume(tokenKindNewline)
Expand Down Expand Up @@ -241,8 +239,6 @@ func (p *parser) parseFragment() *FragmentNode {

p.errs = append(p.errs, specErr)
p.skipOverTo(tokenKindSemicolon)

return
}()

p.consume(tokenKindNewline)
Expand Down Expand Up @@ -308,8 +304,6 @@ func (p *parser) parseProduction() *ProductionNode {

p.errs = append(p.errs, specErr)
p.skipOverTo(tokenKindSemicolon)

return
}()

p.consume(tokenKindNewline)
Expand Down
1 change: 0 additions & 1 deletion spec/syntax_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var (
synErrAutoGenID = newSyntaxError("you cannot define an identifier beginning with an underscore")
synErrUnclosedTerminal = newSyntaxError("unclosed terminal")
synErrUnclosedString = newSyntaxError("unclosed string")
synErrInvalidEscSeq = newSyntaxError("invalid escape sequence")
synErrIncompletedEscSeq = newSyntaxError("incompleted escape sequence; unexpected EOF following a backslash")
synErrEmptyPattern = newSyntaxError("a pattern must include at least one character")
synErrEmptyString = newSyntaxError("a string must include at least one character")
Expand Down

0 comments on commit bb9bf49

Please sign in to comment.