Skip to content

Commit

Permalink
Add a column number to an error message
Browse files Browse the repository at this point in the history
  • Loading branch information
nihei9 committed Aug 22, 2021
1 parent 4d879b9 commit 02674d7
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 77 deletions.
5 changes: 3 additions & 2 deletions error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ type SpecError struct {
FilePath string
SourceName string
Row int
Col int
}

func (e *SpecError) Error() string {
var b strings.Builder
if e.SourceName != "" {
fmt.Fprintf(&b, "%v: ", e.SourceName)
}
if e.Row != 0 {
fmt.Fprintf(&b, "%v: ", e.Row)
if e.Row != 0 && e.Col != 0 {
fmt.Fprintf(&b, "%v:%v: ", e.Row, e.Col)
}
fmt.Fprintf(&b, "error: %v", e.Cause)
if e.Detail != "" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/nihei9/vartan
go 1.16

require (
github.com/nihei9/maleeni v0.3.0
github.com/nihei9/maleeni v0.4.0
github.com/spf13/cobra v1.1.3
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nihei9/maleeni v0.3.0 h1:jNKxnHcoegf7iy1K253dl3nUlrV0BvZDr8a5pp+AF6E=
github.com/nihei9/maleeni v0.3.0/go.mod h1:BWTzKvhWqb4xDo5koZCQJ2eLdkgKL2WY7KlXa7J0tSg=
github.com/nihei9/maleeni v0.4.0 h1:ca1V9U7lZuf5c01Ls4HqlYf7eyhDWdGBv+uyBkChdf0=
github.com/nihei9/maleeni v0.4.0/go.mod h1:d5x5jHHuema6IUi+aDPczMZQ4AlNokcKbEgB5T+70dI=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
28 changes: 28 additions & 0 deletions grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (b *GrammarBuilder) Build() (*Grammar, error) {
Cause: semErrTermCannotBeSkipped,
Detail: sym,
Row: prod.Pos.Row,
Col: prod.Pos.Col,
})
continue
}
Expand All @@ -128,6 +129,7 @@ func (b *GrammarBuilder) Build() (*Grammar, error) {
Cause: semErrUnusedProduction,
Detail: sym,
Row: prod.Pos.Row,
Col: prod.Pos.Col,
})
}

Expand All @@ -136,6 +138,7 @@ func (b *GrammarBuilder) Build() (*Grammar, error) {
Cause: semErrUnusedTerminal,
Detail: sym,
Row: prod.Pos.Row,
Col: prod.Pos.Col,
})
}

Expand Down Expand Up @@ -310,6 +313,7 @@ func (b *GrammarBuilder) genSymbolTableAndLexSpec(root *spec.RootNode) (*symbolT
Cause: semErrDuplicateTerminal,
Detail: prod.LHS,
Row: prod.Pos.Row,
Col: prod.Pos.Col,
})
continue
}
Expand Down Expand Up @@ -341,6 +345,7 @@ func (b *GrammarBuilder) genSymbolTableAndLexSpec(root *spec.RootNode) (*symbolT
Cause: semErrDuplicateTerminal,
Detail: fragment.LHS,
Row: fragment.Pos.Row,
Col: fragment.Pos.Col,
})
continue
}
Expand Down Expand Up @@ -376,6 +381,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'mode' directive needs an ID parameter"),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
for _, param := range dir.Parameters {
Expand All @@ -384,6 +390,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'mode' directive needs an ID parameter"),
Row: param.Pos.Row,
Col: param.Pos.Col,
}, nil
}
modes = append(modes, mlspec.LexModeName(param.ID))
Expand All @@ -393,6 +400,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidName,
Detail: dir.Name,
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
}
Expand All @@ -410,6 +418,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'skip' directive needs no parameter"),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
skip = true
Expand All @@ -419,6 +428,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'push' directive needs an ID parameter"),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
push = mlspec.LexModeName(dir.Parameters[0].ID)
Expand All @@ -428,6 +438,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'pop' directive needs no parameter"),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
pop = true
Expand All @@ -436,6 +447,7 @@ func genLexEntry(prod *spec.ProductionNode) (*mlspec.LexEntry, bool, *verr.SpecE
Cause: semErrDirInvalidName,
Detail: dir.Name,
Row: dir.Pos.Row,
Col: dir.Pos.Col,
}, nil
}
}
Expand Down Expand Up @@ -499,6 +511,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDuplicateName,
Detail: prod.LHS,
Row: prod.Pos.Row,
Col: prod.Pos.Col,
})
}
}
Expand Down Expand Up @@ -530,6 +543,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrUndefinedSym,
Detail: elem.ID,
Row: elem.Pos.Row,
Col: elem.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -545,10 +559,13 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
// Report the line number of a duplicate alternative.
// When the alternative is empty, we report the position of its LHS.
var row int
var col int
if len(alt.Elements) > 0 {
row = alt.Elements[0].Pos.Row
col = alt.Elements[0].Pos.Col
} else {
row = prod.Pos.Row
col = prod.Pos.Col
}

var detail string
Expand All @@ -574,6 +591,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDuplicateProduction,
Detail: detail,
Row: row,
Col: col,
})
continue LOOP_RHS
}
Expand All @@ -588,6 +606,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("'ast' directive needs a tree parameter"),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -598,6 +617,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("a name of a tree structure must be the same ID as an LHS of a production; LHS: %v", lhsText),
Row: param.Pos.Row,
Col: param.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -608,6 +628,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("a position must be less than or equal to the length of an alternativ (%v)", len(alt.Elements)),
Row: c.Pos.Row,
Col: c.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -620,6 +641,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("the expansion symbol cannot be applied to a pattern ($%v: %v)", c.Position, elem.Pattern),
Row: c.Pos.Row,
Col: c.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -633,6 +655,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidParam,
Detail: fmt.Sprintf("the expansion symbol cannot be applied to a terminal symbol ($%v: %v)", c.Position, elem.ID),
Row: c.Pos.Row,
Col: c.Pos.Col,
})
continue LOOP_RHS
}
Expand All @@ -649,6 +672,7 @@ func (b *GrammarBuilder) genProductionsAndActions(root *spec.RootNode, symTabAnd
Cause: semErrDirInvalidName,
Detail: fmt.Sprintf("invalid directive name '%v'", dir.Name),
Row: dir.Pos.Row,
Col: dir.Pos.Col,
})
continue LOOP_RHS
}
Expand Down Expand Up @@ -679,6 +703,7 @@ func (b *GrammarBuilder) genPrecAndAssoc(symTab *symbolTable, prods *productionS
return nil, &verr.SpecError{
Cause: semErrMDInvalidName,
Row: md.Pos.Row,
Col: md.Pos.Col,
}
}

Expand All @@ -687,6 +712,7 @@ func (b *GrammarBuilder) genPrecAndAssoc(symTab *symbolTable, prods *productionS
Cause: semErrMDInvalidParam,
Detail: "associativity needs at least one symbol",
Row: md.Pos.Row,
Col: md.Pos.Col,
}
}

Expand All @@ -697,13 +723,15 @@ func (b *GrammarBuilder) genPrecAndAssoc(symTab *symbolTable, prods *productionS
Cause: semErrMDInvalidParam,
Detail: fmt.Sprintf("'%v' is undefined", p.ID),
Row: p.Pos.Row,
Col: p.Pos.Col,
}
}
if !sym.isTerminal() {
return nil, &verr.SpecError{
Cause: semErrMDInvalidParam,
Detail: fmt.Sprintf("associativity can take only terminal symbol ('%v' is a non-terminal)", p.ID),
Row: p.Pos.Row,
Col: p.Pos.Col,
}
}

Expand Down
Loading

0 comments on commit 02674d7

Please sign in to comment.