-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
expressionparser.go
236 lines (201 loc) · 5.67 KB
/
expressionparser.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package parser
import (
"strings"
"github.com/a-h/parse"
)
// StripType takes the parser and throws away the return value.
func StripType[T any](p parse.Parser[T]) parse.Parser[any] {
return parse.Func(func(in *parse.Input) (out any, ok bool, err error) {
return p.Parse(in)
})
}
func ExpressionOf(p parse.Parser[string]) parse.Parser[Expression] {
return parse.Func(func(in *parse.Input) (out Expression, ok bool, err error) {
from := in.Position()
var exp string
if exp, ok, err = p.Parse(in); err != nil || !ok {
return
}
return NewExpression(exp, from, in.Position()), true, nil
})
}
var lt = parse.Rune('<')
var gt = parse.Rune('>')
var openBrace = parse.String("{")
var optionalSpaces = parse.StringFrom(parse.Optional(
parse.AtLeast(1, parse.Rune(' '))))
var openBraceWithPadding = parse.StringFrom(optionalSpaces,
openBrace,
optionalSpaces)
var openBraceWithOptionalPadding = parse.Any(openBraceWithPadding, openBrace)
var closeBrace = parse.String("}")
var closeBraceWithOptionalPadding = parse.StringFrom(optionalSpaces, closeBrace)
var openBracket = parse.String("(")
var closeBracket = parse.String(")")
var stringUntilNewLine = parse.StringUntil[string](parse.NewLine)
var newLineOrEOF = parse.Or(parse.NewLine, parse.EOF[string]())
var stringUntilNewLineOrEOF = parse.StringUntil(newLineOrEOF)
var jsOrGoSingleLineComment = parse.StringFrom(parse.String("//"), parse.StringUntil(parse.Any(parse.NewLine, parse.EOF[string]())))
var jsOrGoMultiLineComment = parse.StringFrom(parse.String("/*"), parse.StringUntil(parse.String("*/")))
var exp = expressionParser{
startBraceCount: 1,
}
type expressionParser struct {
startBraceCount int
}
func (p expressionParser) Parse(pi *parse.Input) (s Expression, ok bool, err error) {
from := pi.Position()
braceCount := p.startBraceCount
sb := new(strings.Builder)
loop:
for {
var result string
// Try to parse a single line comment.
if result, ok, err = jsOrGoSingleLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try to parse a multi-line comment.
if result, ok, err = jsOrGoMultiLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try to read a string literal.
if result, ok, err = string_lit.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Also try for a rune literal.
if result, ok, err = rune_lit.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try opener.
if result, ok, err = openBrace.Parse(pi); err != nil {
return
}
if ok {
braceCount++
sb.WriteString(result)
continue
}
// Try closer.
startOfCloseBrace := pi.Index()
if result, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil {
return
}
if ok {
braceCount--
if braceCount < 0 {
err = parse.Error("expression: too many closing braces", pi.Position())
return
}
if braceCount == 0 {
pi.Seek(startOfCloseBrace)
break loop
}
sb.WriteString(result)
continue
}
// Read anything else.
var c string
c, ok = pi.Take(1)
if !ok {
break loop
}
if rune(c[0]) == 65533 { // Invalid Unicode.
break loop
}
sb.WriteString(c)
}
if braceCount != 0 {
err = parse.Error("expression: unexpected brace count", pi.Position())
return
}
return NewExpression(sb.String(), from, pi.Position()), true, nil
}
// Letters and digits
var octal_digit = parse.RuneIn("01234567")
var hex_digit = parse.RuneIn("0123456789ABCDEFabcdef")
// https://go.dev/ref/spec#Rune_literals
var rune_lit = parse.StringFrom(
parse.Rune('\''),
parse.StringFrom(parse.Until(
parse.Any(unicode_value_rune, byte_value),
parse.Rune('\''),
)),
parse.Rune('\''),
)
var unicode_value_rune = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("'"))
// byte_value = octal_byte_value | hex_byte_value .
var byte_value = parse.Any(octal_byte_value, hex_byte_value)
// octal_byte_value = `\` octal_digit octal_digit octal_digit .
var octal_byte_value = parse.StringFrom(
parse.String(`\`),
octal_digit, octal_digit, octal_digit,
)
// hex_byte_value = `\` "x" hex_digit hex_digit .
var hex_byte_value = parse.StringFrom(
parse.String(`\x`),
hex_digit, hex_digit,
)
// little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
var little_u_value = parse.StringFrom(
parse.String(`\u`),
hex_digit, hex_digit,
hex_digit, hex_digit,
)
// big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
var big_u_value = parse.StringFrom(
parse.String(`\U`),
hex_digit, hex_digit, hex_digit, hex_digit,
hex_digit, hex_digit, hex_digit, hex_digit,
)
// escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
var escaped_char = parse.StringFrom(
parse.Rune('\\'),
parse.Any(
parse.Rune('a'),
parse.Rune('b'),
parse.Rune('f'),
parse.Rune('n'),
parse.Rune('r'),
parse.Rune('t'),
parse.Rune('v'),
parse.Rune('\\'),
parse.Rune('\''),
parse.Rune('"'),
),
)
// https://go.dev/ref/spec#String_literals
var string_lit = parse.Any(parse.String(`""`), parse.String(`''`), interpreted_string_lit, raw_string_lit)
var interpreted_string_lit = parse.StringFrom(
parse.Rune('"'),
parse.StringFrom(parse.Until(
parse.Any(unicode_value_interpreted, byte_value),
parse.Rune('"'),
)),
parse.Rune('"'),
)
var unicode_value_interpreted = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("\n\""))
var raw_string_lit = parse.StringFrom(
parse.Rune('`'),
parse.StringFrom(parse.Until(
unicode_value_raw,
parse.Rune('`'),
)),
parse.Rune('`'),
)
var unicode_value_raw = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("`"))