Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idl: add column numbers to parse error positions #507

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@

package ast

import "strconv"

// Position represents a position in the parsed document.
// Line and column numbers are 1-based.
type Position struct {
Line int
Line int
Column int
}

func (p Position) String() string {
s := strconv.Itoa(p.Line)
if c := p.Column; c > 0 {
s += ":" + strconv.Itoa(c)
}
return s
}

// Pos attempts to return the position of a Node in the parsed document.
Expand Down
2 changes: 1 addition & 1 deletion idl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (pe *ParseError) Error() string {
var buffer bytes.Buffer
buffer.WriteString("parse error\n")
for _, pe := range pe.Errors {
buffer.WriteString(fmt.Sprintf(" line %d: %s\n", pe.Pos.Line, pe.Err))
buffer.WriteString(fmt.Sprintf(" line %s: %s\n", pe.Pos, pe.Err))
}
return buffer.String()
}
48 changes: 44 additions & 4 deletions idl/internal/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ const thrift_en_main int = 19


type lexer struct {
line int
program *ast.Program

line int
lineStart int

docstringStart int
lastDocstring string
linesSinceDocstring int
nodePositions NodePositions

nodePositions NodePositions

errors []ParseError
parseFailed bool
Expand Down Expand Up @@ -1365,6 +1368,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr29:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

lex.te = (lex.p) + 1
Expand Down Expand Up @@ -2094,6 +2098,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr13:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st10
Expand Down Expand Up @@ -2143,6 +2148,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr18:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st13
Expand Down Expand Up @@ -2481,6 +2487,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr73:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st33
Expand Down Expand Up @@ -4369,6 +4376,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr134:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st93
Expand Down Expand Up @@ -4490,6 +4498,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr139:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st97
Expand Down Expand Up @@ -4704,6 +4713,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr146:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st104
Expand Down Expand Up @@ -5116,6 +5126,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr162:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st117
Expand Down Expand Up @@ -5545,6 +5556,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr176:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st131
Expand Down Expand Up @@ -6073,6 +6085,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr194:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st147
Expand Down Expand Up @@ -6985,6 +6998,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr223:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st175
Expand Down Expand Up @@ -7274,6 +7288,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr234:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st184
Expand Down Expand Up @@ -7457,6 +7472,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr241:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st190
Expand Down Expand Up @@ -7650,6 +7666,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr252:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st196
Expand Down Expand Up @@ -8438,6 +8455,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr278:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st221
Expand Down Expand Up @@ -8528,6 +8546,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr282:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st224
Expand Down Expand Up @@ -8618,6 +8637,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr286:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st227
Expand Down Expand Up @@ -8677,6 +8697,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr289:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st229
Expand Down Expand Up @@ -9125,6 +9146,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr306:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st243
Expand Down Expand Up @@ -9775,6 +9797,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr325:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st264
Expand Down Expand Up @@ -9898,6 +9921,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr331:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st268
Expand Down Expand Up @@ -10275,6 +10299,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr345:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st280
Expand Down Expand Up @@ -10588,6 +10613,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr355:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st290
Expand Down Expand Up @@ -10833,6 +10859,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr364:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st298
Expand Down Expand Up @@ -11811,6 +11838,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr397:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st329
Expand Down Expand Up @@ -12165,6 +12193,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr412:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st340
Expand Down Expand Up @@ -12224,6 +12253,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr415:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st342
Expand Down Expand Up @@ -12473,6 +12503,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr424:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st350
Expand Down Expand Up @@ -12594,6 +12625,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr429:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st354
Expand Down Expand Up @@ -13104,6 +13136,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr446:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st370
Expand Down Expand Up @@ -13384,6 +13417,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr456:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st379
Expand Down Expand Up @@ -13598,6 +13632,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr464:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st386
Expand Down Expand Up @@ -13822,6 +13857,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr475:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st393
Expand Down Expand Up @@ -14228,6 +14264,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tr487:

lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++

goto st406
Expand Down Expand Up @@ -16596,7 +16633,7 @@ func (lex *lexer) Lex(out *yySymType) int {


if lex.cs == thrift_error {
lex.AppendError(fmt.Errorf("unknown token at index %d", lex.p))
lex.Error("unknown token")
}
return tok
}
Expand All @@ -16607,7 +16644,10 @@ func (lex *lexer) Error(e string) {

func (lex *lexer) AppendError(err error) {
lex.parseFailed = true
lex.errors = append(lex.errors, ParseError{Pos: ast.Position{Line: lex.line}, Err: err})
lex.errors = append(lex.errors, ParseError{
Pos: ast.Position{Line: lex.line, Column: lex.ts - lex.lineStart + 1},
Err: err,
})
}

func (lex *lexer) RecordPosition(n ast.Node) {
Expand Down
15 changes: 11 additions & 4 deletions idl/internal/lex.rl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ variable pe lex.pe;
}%%

type lexer struct {
line int
program *ast.Program

line int
lineStart int

docstringStart int
lastDocstring string
linesSinceDocstring int

nodePositions NodePositions

errors []ParseError
Expand Down Expand Up @@ -74,6 +77,7 @@ func (lex *lexer) Lex(out *yySymType) int {
# number tracking.
newline = '\n' >{
lex.line++
lex.lineStart = lex.p + 1
lex.linesSinceDocstring++
};

Expand Down Expand Up @@ -340,7 +344,7 @@ func (lex *lexer) Lex(out *yySymType) int {
}%%

if lex.cs == thrift_error {
lex.AppendError(fmt.Errorf("unknown token at index %d", lex.p))
lex.Error("unknown token")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

}
return tok
}
Expand All @@ -350,8 +354,11 @@ func (lex *lexer) Error(e string) {
}

func (lex *lexer) AppendError(err error) {
lex.parseFailed = true
lex.errors = append(lex.errors, ParseError{Pos: ast.Position{Line: lex.line}, Err: err})
lex.parseFailed = true
lex.errors = append(lex.errors, ParseError{
Pos: ast.Position{Line: lex.line, Column: lex.ts - lex.lineStart + 1},
Err: err,
})
}

func (lex* lexer) RecordPosition(n ast.Node) {
Expand Down
Loading