Skip to content

Commit

Permalink
Ignore extra error tokens.
Browse files Browse the repository at this point in the history
An alternative that fixes #116.

This is essentially what @pborman proposed in
#117 (comment).

I wasn't able to reproduce test failing as mentioned by @andaru in
#117 (comment) in
the current codebase, so pborman's suggestion now makes more sense.

The reason seems to be that error tokens are always skipped, so they
don't seem to affect `Location` as Andrew mentioned:
https://github.com/openconfig/goyang/blob/1fb7893798e04830b4d7fbbcd6672b95668fcf73/pkg/yang/parse.go#L207-L214
  • Loading branch information
wenovus committed Aug 24, 2021
1 parent 1fb7893 commit a22c8a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/yang/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func newLexer(input, path string) *lexer {
file: path,
input: input,
line: 1, // humans start with 1
items: make(chan *token, 3),
items: make(chan *token, maxErrors),
state: lexGround,
errout: os.Stderr,
}
Expand Down Expand Up @@ -187,12 +187,15 @@ func (l *lexer) emitText(c code, text string) {
if l.debug {
fmt.Fprintf(os.Stderr, "%v: %q\n", c, text)
}
l.items <- &token{
select {
case l.items <- &token{
code: c,
Text: text,
File: l.file,
Line: l.sline,
Col: l.scol + 1,
}:
default:
}
l.consume()
}
Expand Down Expand Up @@ -330,11 +333,14 @@ func (l *lexer) ErrorfAt(line, col int, f string, v ...interface{}) {
// If more than maxErrors are encountered, a "too many errors" message is
// displayed and processing stops (by clearing the input).
func (l *lexer) adderror(err []byte) {
if l.errcnt >= maxErrors {
if l.errcnt == maxErrors {
l.pos = 0
l.start = 0
l.input = ""
l.errout.Write([]byte(tooMany))
l.errcnt++
return
} else if l.errcnt == maxErrors+1 {
return
}
l.errout.Write(err)
Expand Down
14 changes: 14 additions & 0 deletions pkg/yang/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ without an ending.
`test.yang:1:1: missing closing */
`,
},
{line(),
// Two errors too many.
`yang-version 1.1;description "\/\/\/\/\/\/\/\/\/\/";`,
9,
`test.yang:1:31: invalid escape sequence: \/
test.yang:1:33: invalid escape sequence: \/
test.yang:1:35: invalid escape sequence: \/
test.yang:1:37: invalid escape sequence: \/
test.yang:1:39: invalid escape sequence: \/
test.yang:1:41: invalid escape sequence: \/
test.yang:1:43: invalid escape sequence: \/
test.yang:1:45: invalid escape sequence: \/
` + tooMany,
},
} {
l := newLexer(tt.in, "test.yang")
errbuf := &bytes.Buffer{}
Expand Down

0 comments on commit a22c8a0

Please sign in to comment.