-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.go
83 lines (59 loc) · 1021 Bytes
/
action.go
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
72
73
74
75
76
77
78
79
80
81
82
83
package ulexer
import (
"fmt"
"runtime"
)
func Expect(lex *Lexer, m Matcher) *Token {
tk := lex.Read(m)
if tk == nil {
var target string
if s, ok := m.(fmt.Stringer); ok {
target = s.String()
} else {
target = m.TokenType()
}
lex.Error("expect '%s'", target)
}
return tk
}
func Ignore(lex *Lexer, m Matcher) {
state := lex.State
tk := lex.Read(m)
if tk == nil && !lex.EOF() {
lex.State = state
}
}
func Is(lex *Lexer, m Matcher, refToken **Token) bool {
tk := lex.Read(m)
if tk != nil {
*refToken = tk
return true
}
return false
}
func Try(lex *Lexer, callback func(lex *Lexer)) (retErr error) {
defer func() {
switch raw := recover().(type) {
case runtime.Error:
panic(raw)
case nil:
case error:
if raw != ErrEOF {
retErr = raw
}
default:
panic(raw)
}
}()
callback(lex)
return
}
func Select(lex *Lexer, mlist ...Matcher) *Token {
for _, m := range mlist {
tk := lex.Read(m)
if tk != nil {
return tk
}
}
return nil
}