-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.rl
More file actions
171 lines (154 loc) · 3.88 KB
/
lexer.rl
File metadata and controls
171 lines (154 loc) · 3.88 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package goeval
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"sync"
"strings"
"github.com/tidwall/gjson"
)
%%{
machine expression;
write data;
access lex.;
variable p lex.p;
variable pe lex.pe;
}%%
type lexer struct {
data []byte
p, pe, cs int
ts, te, act int
answer Value
kv map[string]any
err error
tokens []string
fns map[string]Func
once sync.Once
json gjson.Result
}
func (lex *lexer) token() string {
return string(lex.data[lex.ts:lex.te])
}
func newLexer(data []byte, kv map[string]any, fns map[string]Func) *lexer {
lex := &lexer{
data: data,
pe: len(data),
kv: kv,
fns: fns,
}
%% write init;
return lex
}
func (lex *lexer) Lex(out *yySymType) int {
eof := lex.pe
tok := 0
%%{
action Bool {
tok = VALUE
out.val = NewValue("", lex.token() == "true")
fbreak;
}
action JsonPath {
tok = VALUE
path := string(lex.data[lex.ts+3:lex.te-2])
lex.once.Do(func() {
bs, err := json.Marshal(lex.kv)
if err != nil {
panic(fmt.Errorf("parameter json marshal failed, %s", err))
}
lex.json = gjson.ParseBytes(bs)
})
res := lex.json.Get(path)
out.val = NewValue(path, res)
fbreak;
}
action Identifier {
tok = IDENTIFIER
out.name = lex.token()
fbreak;
}
action Special {
tok = IDENTIFIER
out.name = string(lex.data[lex.ts+4:lex.te-2])
fbreak;
}
action SubPath {
tok = IDENTIFIER
out.name = string(lex.data[lex.ts+1:lex.te])
fbreak;
}
action SpecialSubPath {
tok = IDENTIFIER
out.name = string(lex.data[lex.ts+5:lex.te-2])
fbreak;
}
action Float {
tok = VALUE
n, err := strconv.ParseFloat(lex.token(), 64)
if err != nil {
panic(err)
}
out.val = NewValue("", n)
fbreak;
}
action String {
tok = VALUE
val := string(lex.token())
val = strings.ReplaceAll(val, "\\\\", "\\")
if val[0] == '"' {
val = strings.ReplaceAll(val, "\\\"", "\"")
}else if val[0] == '\'' {
val = strings.ReplaceAll(val, "\\'", "'")
}else if val[0] == '`' {
val = strings.ReplaceAll(val, "\\`", "`")
}
out.val = NewValue("", val[1:len(val)-1])
fbreak;
}
identifier = (alpha | '_')+ (alnum | '_')* ;
string = '"' ((any - '"') | '\\\"')* '"' | "'" ((any - "'") | "\\\'")* "'" | '`' ((any - "`") | "\\\`")* "`" ;
int = '-'? digit+ ;
float = '-'? digit+ ('.' digit+)? ;
jsonpath = "$[" string ']' ;
special = "$.[" string ']' ;
letter = '+' | '-' | '*' | '/' | '%' | '<' | '>' | '?' | ':' | '=' | '!' | '(' | ')' | ',' | '[' | ']';
sub_path = '.' (alnum | '_')+ ;
special_sub_path = '.' special ;
main := |*
# values
string => String;
(int | float) => Float;
("true" | "false") => Bool;
"nil" => { tok = VALUE; out.val = nilValue; fbreak; };
# relations
"==" => { tok = EQ; fbreak; };
"!=" => { tok = NEQ; fbreak; };
">=" => { tok = GTE; fbreak; };
"<=" => { tok = LTE; fbreak; };
"=~" => { tok = RE; fbreak; };
"!~" => { tok = NRE; fbreak; };
"&&" => { tok = AND; fbreak; };
"||" => { tok = OR; fbreak; };
"??" => { tok = NC; fbreak; };
# keywords
"in" => { tok = IN; fbreak; };
identifier => Identifier;
sub_path => SubPath;
special_sub_path => SpecialSubPath;
jsonpath => JsonPath;
special => Special;
space+;
letter => { tok = int(lex.data[lex.ts]); fbreak; };
any => { panic(errors.New("unexpected character: " + string(lex.data[lex.ts]))) };
*|;
write exec;
}%%
if tok != 0 {
lex.tokens = append(lex.tokens, lex.token())
}
return tok
}
func (lex *lexer) Error(e string) {
lex.err = errors.New(e)
}