This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_err_test.go
More file actions
71 lines (63 loc) · 1.36 KB
/
parse_err_test.go
File metadata and controls
71 lines (63 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package parsekit
import (
"testing"
"unicode/utf8"
)
func TestErrMessage(t *testing.T) {
const txt = `option "color" "red"
opton "destination" "Turin"
option "time" "3h"
`
p := Init[O](ReadString(txt), WithLexer(lexOpts), SynchronizeAt("option"))
parseOptions(p)
_, err := p.Finish()
if err == nil || err.Error() != `at <input>:2:1: expected the option keyword, got "opton" instead` {
t.Error("invalid error returned", err)
}
}
type O struct {
opts map[string]string
}
func parseOptions(p *Parser[O]) {
defer p.Synchronize()
for p.More() {
p.Expect(OptionToken, "the option keyword")
p.Expect(StringToken, "an option name, e.g. color")
p.Expect(StringToken, "an option value, e.g. red")
}
}
const (
OptionToken = ScanToken - iota
StringToken
)
func lexOpts(sc *Scanner) Token {
switch r := sc.Peek(); r {
default:
sc.Advance()
return Token{Lexeme: string(r)}
case ' ', '\n', '\t':
sc.Advance()
return Ignore
case 'o':
sc.Advance()
rest := "ption"
for len(rest) > 0 {
if sc.Advance() != rune(rest[0]) {
// read the full word to help with error message
for sc.Peek() != ' ' {
sc.Advance()
}
return Token{}
}
rest = rest[1:]
}
return Const(OptionToken)
case '"':
sc.Advance()
for sc.Peek() != '"' && sc.Peek() != utf8.RuneError {
sc.Advance()
}
sc.Advance()
return Auto[string](StringToken, sc)
}
}