-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparser.lua
253 lines (221 loc) · 6.48 KB
/
parser.lua
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
local combinators = require 'snippets.combinators'
local sym = combinators.sym
local pattern = combinators.pattern
local map = combinators.map
local any = combinators.any
local seq = combinators.seq
local many = combinators.many
local take_until = combinators.take_until
local separated = combinators.separated
local lazy = combinators.lazy
local SKIP = combinators.SKIP
local C = require 'snippets.common'
local format = string.format
local concat = table.concat
local insert = table.insert
local function vscode_parser()
local dollar = sym("$")
local open = sym("{")
local close = sym("}")
local colon = sym(":")
local slash = sym("/")
local comma = sym(",")
local pipe = sym("|")
local var = pattern("[_a-zA-Z][_a-zA-Z0-9]*")
local int = map(pattern("%d+"), function(v) return tonumber(v) end)
-- TODO: opt so we can avoid swallowing the close here
local regex = map(
seq(slash, take_until("/"), slash, take_until("/"), slash, any(take_until("}"), close)),
function(v) return { type = "regex", value = v[1], format = v[2], options = v[3]} end
)
local tabstop, placeholder, choice, variable, anything
-- need to make lazy so that tabstop/placeholder/variable aren't nil at
-- declaration time because of mutual recursion.
anything = lazy(function() return any(
tabstop,
placeholder,
choice,
variable
-- -- text: we do this on a per usecase basis
) end)
tabstop = map(
any(
seq(dollar, int),
seq(dollar, open, int, close)
),
function(v) return { type = "tabstop", id = v[1] } end
)
placeholder = map(
-- match until $ or }
seq(dollar, open, int, colon, many(any(anything, take_until("[%$}]"))), close),
function(v) return { type = "placeholder", id = v[1], value = v[2] } end
)
choice = map(
-- match until , or |
seq(dollar, open, int, pipe, separated(comma, take_until("[,|]")), pipe, close),
function(v) return { type = "choice", id = v[1], value = v[2] } end
)
variable = any(
map(
seq(dollar, var),
function(v) return { type = "variable", name = v[1] } end
),
map(
seq(dollar, open, var, colon, many(any(anything, take_until("}"))), close),
function(v) return { type = "variable", name = v[1], default = v[2] } end
),
map(
seq(dollar, open, var, regex), -- regex already eats the close
function(v) return { type = "variable", name = v[1], regex = v[2] } end
)
)
-- toplevel take_until matches until $
local parse = many(any(anything, take_until("%$")))
end
local function lazy_any(array)
local cached
return lazy(function()
if not cached then
cached = any(unpack(array))
end
return cached
end)
end
local function ashkan_parser()
local dollar = sym("$")
local open = sym("{")
local close = sym("}")
local colon = sym(":")
local slash = sym("/")
local comma = sym(",")
local pipe = sym("|")
local equals = sym("=")
local var = pattern("[_a-zA-Z][_a-zA-Z0-9]*")
local int = map(pattern("%-?%d+"), function(v) return tonumber(v) end)
local function mkslash_escaped(inner)
return map(
pattern("\\."),
function(v)
return v:sub(2):match(inner) or v
-- return v:sub(2):match(inner) or SKIP
-- return v:sub(2):match(inner) or error(format("Invalid escape sequence %q. Expected %s", v, inner))
end
)
end
local escaped = mkslash_escaped("[\\$]")
local transform_escaped = mkslash_escaped("[\\|]")
local variants = {}
-- need to make lazy so that variants aren't empty at
-- declaration time because of mutual recursion.
local anything = lazy_any(variants)
variants[#variants+1] = escaped
variants[#variants+1] = map(
seq(dollar, open, equals, take_until("}"), close),
function(v)
local expression = C.make_lambda(v[1], v[1])
return C.structure_variable(false, nil, expression, -1, nil)
end
)
variants[#variants+1] = map(
seq(dollar, open, pipe, take_until("}"), close),
function(v)
local transform = C.make_lambda(v[1], v[1])
return C.structure_variable(false, nil, nil, math.huge, transform)
end
)
-- Basic style
variants[#variants+1] = map(
any(
seq(dollar, int),
seq(dollar, open, int, close)
),
function(v)
local id = v[1]
return C.structure_variable(id > 0, id, "", id, nil)
end
)
local transform = map(
seq(pipe, take_until("}")),
function(v)
return C.make_lambda(v[1], v[1])
end
)
-- Placeholder style
variants[#variants+1] = map(
-- match until $ or }
any(
seq(dollar, open, int, colon, many(any(transform_escaped, anything, take_until("[\\$}|]"))), transform, close),
seq(dollar, open, int, colon, many(any(anything, take_until("[\\$}]"))), close)
),
function(v)
local id = v[1]
local placeholder = v[2]
if #placeholder > 1 or type(placeholder[1]) ~= 'string' then
local evaluator = C.evaluate_snippet(placeholder)
placeholder = function(context)
local inputs = {}
for i, v in ipairs(evaluator.inputs) do
-- TODO(ashkan, Tue 18 Aug 2020 09:37:37 AM JST) ignore the v.default here?
inputs[i] = context[v.id]
end
return concat(evaluator.evaluate_structure(inputs))
end
else
placeholder = placeholder[1]
end
return C.structure_variable(id > 0, id, placeholder, id, v[3])
end
)
-- Expression style
variants[#variants+1] = map(
-- match until $ or }
any(
seq(dollar, open, int, equals, many(any(transform_escaped, take_until("[\\|}]"))), transform, close),
seq(dollar, open, int, equals, take_until("}"), close)
),
function(v)
local id = v[1]
local expression = v[2]
if type(expression) == 'table' then
expression = concat(expression)
end
expression = C.make_lambda(expression, expression)
return C.structure_variable(id > 0, id, expression, id, v[3])
end
)
-- Transform style
variants[#variants+1] = map(
seq(dollar, open, int, transform, close),
function(v)
local id = v[1]
return C.structure_variable(id > 0, id, "", id, v[2])
end
)
-- toplevel take_until matches until $
return map(
many(any(anything, take_until("[\\$]"))),
C.make_snippet
)
end
local lazily_initialized_parser
return {
make_ashkan_parser = ashkan_parser;
make_vscode_parser = vscode_parser;
parse_snippet = function(s)
if not lazily_initialized_parser then
lazily_initialized_parser = ashkan_parser()
end
if s == "" then
return {""}
end
local ok, snippet, pos = lazily_initialized_parser(s, 1)
if not ok then
error(format("Failed to parse snippet: %q", s))
end
if pos ~= #s+1 then
error(format("Failed to parse snippet fully: %d != %d\n%s\n%s^", pos, #s+1, s, (" "):rep(pos-1)))
end
return snippet
end;
}
-- vim:noet sw=3 ts=3