Skip to content

Commit e98c1f4

Browse files
authored
Merge pull request #16 from luyuhuang/pos-to
add a new field to suggest the end position of the expression
2 parents 20181f2 + f921bec commit e98c1f4

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

lua-parser/parser.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,23 @@ local function kw (str)
186186
return token(P(str) * -V"IdRest")
187187
end
188188

189+
local function dec(n)
190+
return n - 1
191+
end
192+
189193
local function tagC (tag, patt)
190-
return Ct(Cg(Cp(), "pos") * Cg(Cc(tag), "tag") * patt)
194+
return Ct(Cg(Cp(), "pos") * Cg(Cc(tag), "tag") * patt * Cg(Cp() / dec, "end_pos"))
191195
end
192196

193197
local function unaryOp (op, e)
194-
return { tag = "Op", pos = e.pos, [1] = op, [2] = e }
198+
return { tag = "Op", pos = e.pos, end_pos = e.end_pos, [1] = op, [2] = e }
195199
end
196200

197201
local function binaryOp (e1, op, e2)
198202
if not op then
199203
return e1
200204
else
201-
return { tag = "Op", pos = e1.pos, [1] = op, [2] = e1, [3] = e2 }
205+
return { tag = "Op", pos = e1.pos, end_pos = e2.end_pos, [1] = op, [2] = e1, [3] = e2 }
202206
end
203207
end
204208

@@ -236,25 +240,25 @@ local function addDots (params, dots)
236240
end
237241

238242
local function insertIndex (t, index)
239-
return { tag = "Index", pos = t.pos, [1] = t, [2] = index }
243+
return { tag = "Index", pos = t.pos, end_pos = index.end_pos, [1] = t, [2] = index }
240244
end
241245

242246
local function markMethod(t, method)
243247
if method then
244-
return { tag = "Index", pos = t.pos, is_method = true, [1] = t, [2] = method }
248+
return { tag = "Index", pos = t.pos, end_pos = method.end_pos, is_method = true, [1] = t, [2] = method }
245249
end
246250
return t
247251
end
248252

249253
local function makeIndexOrCall (t1, t2)
250254
if t2.tag == "Call" or t2.tag == "Invoke" then
251-
local t = { tag = t2.tag, pos = t1.pos, [1] = t1 }
255+
local t = { tag = t2.tag, pos = t1.pos, end_pos = t2.end_pos, [1] = t1 }
252256
for k, v in ipairs(t2) do
253257
table.insert(t, v)
254258
end
255259
return t
256260
end
257-
return { tag = "Index", pos = t1.pos, [1] = t1, [2] = t2[1] }
261+
return { tag = "Index", pos = t1.pos, end_pos = t2.end_pos, [1] = t1, [2] = t2[1] }
258262
end
259263

260264
-- grammar

0 commit comments

Comments
 (0)