Skip to content

Commit 7d44399

Browse files
authored
Merge pull request #45 from PaulSonOfLars/exposeErrors
Expose error data to external tools.
2 parents d364597 + b71252a commit 7d44399

23 files changed

+369
-211
lines changed

internal/lexer/error.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package lexer
22

33
import (
4-
"fmt"
54
"runtime"
5+
6+
"github.com/yoheimuta/go-protoparser/parser/meta"
67
)
78

89
func (lex *Lexer) unexpected(found, expected string) error {
9-
debug := ""
10+
err := &meta.Error{
11+
Pos: lex.Pos.Position,
12+
Expected: expected,
13+
Found: lex.Text,
14+
}
1015
if lex.debug {
1116
_, file, line, _ := runtime.Caller(1)
12-
debug = fmt.Sprintf(" at %s:%d", file, line)
17+
err.SetOccured(file, line)
1318
}
14-
return fmt.Errorf("found %q but expected [%s]%s", found, expected, debug)
19+
return err
1520
}

internal/lexer/scanner/error.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package scanner
22

33
import (
4-
"fmt"
54
"runtime"
5+
6+
"github.com/yoheimuta/go-protoparser/parser/meta"
67
)
78

89
func (s *Scanner) unexpected(found rune, expected string) error {
910
_, file, line, _ := runtime.Caller(1)
10-
message := fmt.Sprintf(" at %s:%d", file, line)
11-
return fmt.Errorf("found %q but expected [%s]%s", found, expected, message)
11+
err := &meta.Error{
12+
Pos: s.pos.Position,
13+
Expected: expected,
14+
Found: string(found),
15+
}
16+
err.SetOccured(file, line)
17+
return err
1218
}

internal/lexer/scanner/position.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package scanner
22

33
import (
4-
"fmt"
54
"unicode/utf8"
5+
6+
"github.com/yoheimuta/go-protoparser/parser/meta"
67
)
78

89
// Position represents a source position.
910
type Position struct {
10-
// Filename is a name of file, if any
11-
Filename string
12-
// Offset is a byte offset, starting at 0
13-
Offset int
14-
// Line is a line number, starting at 1
15-
Line int
16-
// Column is a column number, starting at 1 (character count per line)
17-
Column int
11+
meta.Position
1812

1913
// columns is a map which the key is a line number and the value is a column number.
2014
columns map[int]int
@@ -23,21 +17,18 @@ type Position struct {
2317
// NewPosition creates a new Position.
2418
func NewPosition() *Position {
2519
return &Position{
26-
Offset: 0,
27-
Line: 1,
28-
Column: 1,
20+
Position: meta.Position{
21+
Offset: 0,
22+
Line: 1,
23+
Column: 1,
24+
},
2925
columns: make(map[int]int),
3026
}
3127
}
3228

3329
// String stringify the position.
3430
func (pos Position) String() string {
35-
s := pos.Filename
36-
if s == "" {
37-
s = "<input>"
38-
}
39-
s += fmt.Sprintf(":%d:%d", pos.Line, pos.Column)
40-
return s
31+
return pos.Position.String()
4132
}
4233

4334
// Advance advances the position value.

0 commit comments

Comments
 (0)