Fri, 05 May 2017 09:47:52 +0100
squish: Optionally add in a 5.2-compatible module() function (5.2's own compat function is broken)
--module-compat or --no-module-compat, default is --module-compat if running under 5.2+ (detected at runtime).
When compiled with the appropriate flags, Lua 5.2 provides a module() function for backwards compatibility with
5.1. However 5.1's version of the function changed function environments, while 5.2's version changes the global
environment, which breaks through squish's per-module sandbox functions.
1 | 1 | --[[-------------------------------------------------------------------- |
2 | ||
3 | lparser.lua: Lua 5.1 parser in Lua | |
4 | This file is part of LuaSrcDiet, based on Yueliang material. | |
5 | ||
6 | Copyright (c) 2008 Kein-Hong Man <khman@users.sf.net> | |
7 | The COPYRIGHT file describes the conditions | |
8 | under which this software may be distributed. | |
9 | ||
10 | See the ChangeLog for more information. | |
11 | ||
12 | ----------------------------------------------------------------------]] | |
13 | ||
14 | --[[-------------------------------------------------------------------- | |
15 | -- NOTES: | |
16 | -- * This is a version of the native 5.1.x parser from Yueliang 0.4.0, | |
17 | -- with significant modifications to handle LuaSrcDiet's needs: | |
18 | -- (1) needs pre-built token tables instead of a module.method | |
19 | -- (2) lparser.error is an optional error handler (from llex) | |
20 | -- (3) not full parsing, currently fakes raw/unlexed constants | |
21 | -- (4) parser() returns globalinfo, localinfo tables | |
22 | -- * Please read technotes.txt for more technical details. | |
23 | -- * NO support for 'arg' vararg functions (LUA_COMPAT_VARARG) | |
24 | -- * A lot of the parser is unused, but might later be useful for | |
25 | -- full-on parsing and analysis for a few measly bytes saved. | |
26 | ----------------------------------------------------------------------]] | |
27 | ||
28 | local base = _G | |
29 | local string = require "string" | |
30 | module "lparser" | |
31 | ||
32 | --[[-------------------------------------------------------------------- | |
33 | -- variable and data structure initialization | |
34 | ----------------------------------------------------------------------]] | |
35 | ||
36 | ---------------------------------------------------------------------- | |
37 | -- initialization: main variables | |
38 | ---------------------------------------------------------------------- | |
39 | ||
40 | local toklist, -- grammar-only token tables (token table, | |
41 | seminfolist, -- semantic information table, line number | |
42 | toklnlist, -- table, cross-reference table) | |
43 | xreflist, | |
44 | tpos, -- token position | |
45 | ||
46 | line, -- start line # for error messages | |
47 | lastln, -- last line # for ambiguous syntax chk | |
48 | tok, seminfo, ln, xref, -- token, semantic info, line | |
49 | nameref, -- proper position of <name> token | |
50 | fs, -- current function state | |
51 | top_fs, -- top-level function state | |
52 | ||
53 | globalinfo, -- global variable information table | |
54 | globallookup, -- global variable name lookup table | |
55 | localinfo, -- local variable information table | |
56 | ilocalinfo, -- inactive locals (prior to activation) | |
57 | ilocalrefs -- corresponding references to activate | |
58 | ||
59 | -- forward references for local functions | |
60 | local explist1, expr, block, exp1, body, chunk | |
61 | ||
62 | ---------------------------------------------------------------------- | |
63 | -- initialization: data structures | |
64 | ---------------------------------------------------------------------- | |
65 | ||
66 | local gmatch = string.gmatch | |
67 | ||
68 | local block_follow = {} -- lookahead check in chunk(), returnstat() | |
69 | for v in gmatch("else elseif end until <eof>", "%S+") do | |
70 | block_follow[v] = true | |
71 | end | |
72 | ||
73 | local stat_call = {} -- lookup for calls in stat() | |
74 | for v in gmatch("if while do for repeat function local return break", "%S+") do | |
75 | stat_call[v] = v.."_stat" | |
76 | end | |
77 | ||
78 | local binopr_left = {} -- binary operators, left priority | |
79 | local binopr_right = {} -- binary operators, right priority | |
80 | for op, lt, rt in gmatch([[ | |
81 | {+ 6 6}{- 6 6}{* 7 7}{/ 7 7}{% 7 7} | |
82 | {^ 10 9}{.. 5 4} | |
83 | {~= 3 3}{== 3 3} | |
84 | {< 3 3}{<= 3 3}{> 3 3}{>= 3 3} | |
85 | {and 2 2}{or 1 1} | |
86 | ]], "{(%S+)%s(%d+)%s(%d+)}") do | |
87 | binopr_left[op] = lt + 0 | |
88 | binopr_right[op] = rt + 0 | |
89 | end | |
90 | ||
91 | local unopr = { ["not"] = true, ["-"] = true, | |
92 | ["#"] = true, } -- unary operators | |
93 | local UNARY_PRIORITY = 8 -- priority for unary operators | |
94 | ||
95 | --[[-------------------------------------------------------------------- | |
96 | -- support functions | |
97 | ----------------------------------------------------------------------]] | |
98 | ||
99 | ---------------------------------------------------------------------- | |
100 | -- formats error message and throws error (duplicated from llex) | |
101 | -- * a simplified version, does not report what token was responsible | |
102 | ---------------------------------------------------------------------- | |
103 | ||
104 | local function errorline(s, line) | |
105 | local e = error or base.error | |
106 | e(string.format("(source):%d: %s", line or ln, s)) | |
107 | end | |
108 | ||
109 | ---------------------------------------------------------------------- | |
110 | -- handles incoming token, semantic information pairs | |
111 | -- * NOTE: 'nextt' is named 'next' originally | |
112 | ---------------------------------------------------------------------- | |
113 | ||
114 | -- reads in next token | |
115 | local function nextt() | |
116 | lastln = toklnlist[tpos] | |
117 | tok, seminfo, ln, xref | |
118 | = toklist[tpos], seminfolist[tpos], toklnlist[tpos], xreflist[tpos] | |
119 | tpos = tpos + 1 | |
120 | end | |
121 | ||
122 | -- peek at next token (single lookahead for table constructor) | |
123 | local function lookahead() | |
124 | return toklist[tpos] | |
125 | end | |
126 | ||
127 | ---------------------------------------------------------------------- | |
128 | -- throws a syntax error, or if token expected is not there | |
129 | ---------------------------------------------------------------------- | |
130 | ||
131 | local function syntaxerror(msg) | |
132 | local tok = tok | |
133 | if tok ~= "<number>" and tok ~= "<string>" then | |
134 | if tok == "<name>" then tok = seminfo end | |
135 | tok = "'"..tok.."'" | |
136 | end | |
137 | errorline(msg.." near "..tok) | |
138 | end | |
139 | ||
140 | local function error_expected(token) | |
141 | syntaxerror("'"..token.."' expected") | |
142 | end | |
143 | ||
144 | ---------------------------------------------------------------------- | |
145 | -- tests for a token, returns outcome | |
146 | -- * return value changed to boolean | |
147 | ---------------------------------------------------------------------- | |
148 | ||
149 | local function testnext(c) | |
150 | if tok == c then nextt(); return true end | |
151 | end | |
152 | ||
153 | ---------------------------------------------------------------------- | |
154 | -- check for existence of a token, throws error if not found | |
155 | ---------------------------------------------------------------------- | |
156 | ||
157 | local function check(c) | |
158 | if tok ~= c then error_expected(c) end | |
159 | end | |
160 | ||
161 | ---------------------------------------------------------------------- | |
162 | -- verify existence of a token, then skip it | |
163 | ---------------------------------------------------------------------- | |
164 | ||
165 | local function checknext(c) | |
166 | check(c); nextt() | |
167 | end | |
168 | ||
169 | ---------------------------------------------------------------------- | |
170 | -- throws error if condition not matched | |
171 | ---------------------------------------------------------------------- | |
172 | ||
173 | local function check_condition(c, msg) | |
174 | if not c then syntaxerror(msg) end | |
175 | end | |
176 | ||
177 | ---------------------------------------------------------------------- | |
178 | -- verifies token conditions are met or else throw error | |
179 | ---------------------------------------------------------------------- | |
180 | ||
181 | local function check_match(what, who, where) | |
182 | if not testnext(what) then | |
183 | if where == ln then | |
184 | error_expected(what) | |
185 | else | |
186 | syntaxerror("'"..what.."' expected (to close '"..who.."' at line "..where..")") | |
187 | end | |
188 | end | |
189 | end | |
190 | ||
191 | ---------------------------------------------------------------------- | |
192 | -- expect that token is a name, return the name | |
193 | ---------------------------------------------------------------------- | |
194 | ||
195 | local function str_checkname() | |
196 | check("<name>") | |
197 | local ts = seminfo | |
198 | nameref = xref | |
199 | nextt() | |
200 | return ts | |
201 | end | |
202 | ||
203 | ---------------------------------------------------------------------- | |
204 | -- adds given string s in string pool, sets e as VK | |
205 | ---------------------------------------------------------------------- | |
206 | ||
207 | local function codestring(e, s) | |
208 | e.k = "VK" | |
209 | end | |
210 | ||
211 | ---------------------------------------------------------------------- | |
212 | -- consume a name token, adds it to string pool | |
213 | ---------------------------------------------------------------------- | |
214 | ||
215 | local function checkname(e) | |
216 | codestring(e, str_checkname()) | |
217 | end | |
218 | ||
219 | --[[-------------------------------------------------------------------- | |
220 | -- variable (global|local|upvalue) handling | |
221 | -- * to track locals and globals, we can extend Yueliang's minimal | |
222 | -- variable management code with little trouble | |
223 | -- * entry point is singlevar() for variable lookups | |
224 | -- * lookup tables (bl.locallist) are maintained awkwardly in the basic | |
225 | -- block data structures, PLUS the function data structure (this is | |
226 | -- an inelegant hack, since bl is nil for the top level of a function) | |
227 | ----------------------------------------------------------------------]] | |
228 | ||
229 | ---------------------------------------------------------------------- | |
230 | -- register a local variable, create local variable object, set in | |
231 | -- to-activate variable list | |
232 | -- * used in new_localvarliteral(), parlist(), fornum(), forlist(), | |
233 | -- localfunc(), localstat() | |
234 | ---------------------------------------------------------------------- | |
235 | ||
236 | local function new_localvar(name, special) | |
237 | local bl = fs.bl | |
238 | local locallist | |
239 | -- locate locallist in current block object or function root object | |
240 | if bl then | |
241 | locallist = bl.locallist | |
242 | else | |
243 | locallist = fs.locallist | |
244 | end | |
245 | -- build local variable information object and set localinfo | |
246 | local id = #localinfo + 1 | |
247 | localinfo[id] = { -- new local variable object | |
248 | name = name, -- local variable name | |
249 | xref = { nameref }, -- xref, first value is declaration | |
250 | decl = nameref, -- location of declaration, = xref[1] | |
251 | } | |
94
cf209451be5f
minify: Preserve _ENV, as it's lexically significant in 5.2+
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
252 | if special or name == "_ENV" then -- "self" must be not be changed |
cf209451be5f
minify: Preserve _ENV, as it's lexically significant in 5.2+
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
253 | localinfo[id].preserve = true |
1 | 254 | end |
255 | -- this can override a local with the same name in the same scope | |
256 | -- but first, keep it inactive until it gets activated | |
257 | local i = #ilocalinfo + 1 | |
258 | ilocalinfo[i] = id | |
259 | ilocalrefs[i] = locallist | |
260 | end | |
261 | ||
262 | ---------------------------------------------------------------------- | |
263 | -- actually activate the variables so that they are visible | |
264 | -- * remember Lua semantics, e.g. RHS is evaluated first, then LHS | |
265 | -- * used in parlist(), forbody(), localfunc(), localstat(), body() | |
266 | ---------------------------------------------------------------------- | |
267 | ||
268 | local function adjustlocalvars(nvars) | |
269 | local sz = #ilocalinfo | |
270 | -- i goes from left to right, in order of local allocation, because | |
271 | -- of something like: local a,a,a = 1,2,3 which gives a = 3 | |
272 | while nvars > 0 do | |
273 | nvars = nvars - 1 | |
274 | local i = sz - nvars | |
275 | local id = ilocalinfo[i] -- local's id | |
276 | local obj = localinfo[id] | |
277 | local name = obj.name -- name of local | |
278 | obj.act = xref -- set activation location | |
279 | ilocalinfo[i] = nil | |
280 | local locallist = ilocalrefs[i] -- ref to lookup table to update | |
281 | ilocalrefs[i] = nil | |
282 | local existing = locallist[name] -- if existing, remove old first! | |
283 | if existing then -- do not overlap, set special | |
284 | obj = localinfo[existing] -- form of rem, as -id | |
285 | obj.rem = -id | |
286 | end | |
287 | locallist[name] = id -- activate, now visible to Lua | |
288 | end | |
289 | end | |
290 | ||
291 | ---------------------------------------------------------------------- | |
292 | -- remove (deactivate) variables in current scope (before scope exits) | |
293 | -- * zap entire locallist tables since we are not allocating registers | |
294 | -- * used in leaveblock(), close_func() | |
295 | ---------------------------------------------------------------------- | |
296 | ||
297 | local function removevars() | |
298 | local bl = fs.bl | |
299 | local locallist | |
300 | -- locate locallist in current block object or function root object | |
301 | if bl then | |
302 | locallist = bl.locallist | |
303 | else | |
304 | locallist = fs.locallist | |
305 | end | |
306 | -- enumerate the local list at current scope and deactivate 'em | |
307 | for name, id in base.pairs(locallist) do | |
308 | local obj = localinfo[id] | |
309 | obj.rem = xref -- set deactivation location | |
310 | end | |
311 | end | |
312 | ||
313 | ---------------------------------------------------------------------- | |
314 | -- creates a new local variable given a name | |
315 | -- * skips internal locals (those starting with '('), so internal | |
316 | -- locals never needs a corresponding adjustlocalvars() call | |
317 | -- * special is true for "self" which must not be optimized | |
318 | -- * used in fornum(), forlist(), parlist(), body() | |
319 | ---------------------------------------------------------------------- | |
320 | ||
321 | local function new_localvarliteral(name, special) | |
322 | if string.sub(name, 1, 1) == "(" then -- can skip internal locals | |
323 | return | |
324 | end | |
325 | new_localvar(name, special) | |
326 | end | |
327 | ||
328 | ---------------------------------------------------------------------- | |
329 | -- search the local variable namespace of the given fs for a match | |
330 | -- * returns localinfo index | |
331 | -- * used only in singlevaraux() | |
332 | ---------------------------------------------------------------------- | |
333 | ||
334 | local function searchvar(fs, n) | |
335 | local bl = fs.bl | |
336 | local locallist | |
337 | if bl then | |
338 | locallist = bl.locallist | |
339 | while locallist do | |
340 | if locallist[n] then return locallist[n] end -- found | |
341 | bl = bl.prev | |
342 | locallist = bl and bl.locallist | |
343 | end | |
344 | end | |
345 | locallist = fs.locallist | |
346 | return locallist[n] or -1 -- found or not found (-1) | |
347 | end | |
348 | ||
349 | ---------------------------------------------------------------------- | |
350 | -- handle locals, globals and upvalues and related processing | |
351 | -- * search mechanism is recursive, calls itself to search parents | |
352 | -- * used only in singlevar() | |
353 | ---------------------------------------------------------------------- | |
354 | ||
355 | local function singlevaraux(fs, n, var) | |
356 | if fs == nil then -- no more levels? | |
357 | var.k = "VGLOBAL" -- default is global variable | |
358 | return "VGLOBAL" | |
359 | else | |
360 | local v = searchvar(fs, n) -- look up at current level | |
361 | if v >= 0 then | |
362 | var.k = "VLOCAL" | |
363 | var.id = v | |
364 | -- codegen may need to deal with upvalue here | |
365 | return "VLOCAL" | |
366 | else -- not found at current level; try upper one | |
367 | if singlevaraux(fs.prev, n, var) == "VGLOBAL" then | |
368 | return "VGLOBAL" | |
369 | end | |
370 | -- else was LOCAL or UPVAL, handle here | |
371 | var.k = "VUPVAL" -- upvalue in this level | |
372 | return "VUPVAL" | |
373 | end--if v | |
374 | end--if fs | |
375 | end | |
376 | ||
377 | ---------------------------------------------------------------------- | |
378 | -- consume a name token, creates a variable (global|local|upvalue) | |
379 | -- * used in prefixexp(), funcname() | |
380 | ---------------------------------------------------------------------- | |
381 | ||
382 | local function singlevar(v) | |
383 | local name = str_checkname() | |
384 | singlevaraux(fs, name, v) | |
385 | ------------------------------------------------------------------ | |
386 | -- variable tracking | |
387 | ------------------------------------------------------------------ | |
388 | if v.k == "VGLOBAL" then | |
389 | -- if global being accessed, keep track of it by creating an object | |
390 | local id = globallookup[name] | |
391 | if not id then | |
392 | id = #globalinfo + 1 | |
393 | globalinfo[id] = { -- new global variable object | |
394 | name = name, -- global variable name | |
395 | xref = { nameref }, -- xref, first value is declaration | |
396 | } | |
397 | globallookup[name] = id -- remember it | |
398 | else | |
399 | local obj = globalinfo[id].xref | |
400 | obj[#obj + 1] = nameref -- add xref | |
401 | end | |
402 | else | |
403 | -- local/upvalue is being accessed, keep track of it | |
404 | local id = v.id | |
405 | local obj = localinfo[id].xref | |
406 | obj[#obj + 1] = nameref -- add xref | |
407 | end | |
408 | end | |
409 | ||
410 | --[[-------------------------------------------------------------------- | |
411 | -- state management functions with open/close pairs | |
412 | ----------------------------------------------------------------------]] | |
413 | ||
414 | ---------------------------------------------------------------------- | |
415 | -- enters a code unit, initializes elements | |
416 | ---------------------------------------------------------------------- | |
417 | ||
418 | local function enterblock(isbreakable) | |
419 | local bl = {} -- per-block state | |
420 | bl.isbreakable = isbreakable | |
421 | bl.prev = fs.bl | |
422 | bl.locallist = {} | |
423 | fs.bl = bl | |
424 | end | |
425 | ||
426 | ---------------------------------------------------------------------- | |
427 | -- leaves a code unit, close any upvalues | |
428 | ---------------------------------------------------------------------- | |
429 | ||
430 | local function leaveblock() | |
431 | local bl = fs.bl | |
432 | removevars() | |
433 | fs.bl = bl.prev | |
434 | end | |
435 | ||
436 | ---------------------------------------------------------------------- | |
437 | -- opening of a function | |
438 | -- * top_fs is only for anchoring the top fs, so that parser() can | |
439 | -- return it to the caller function along with useful output | |
440 | -- * used in parser() and body() | |
441 | ---------------------------------------------------------------------- | |
442 | ||
443 | local function open_func() | |
444 | local new_fs -- per-function state | |
445 | if not fs then -- top_fs is created early | |
446 | new_fs = top_fs | |
447 | else | |
448 | new_fs = {} | |
449 | end | |
450 | new_fs.prev = fs -- linked list of function states | |
451 | new_fs.bl = nil | |
452 | new_fs.locallist = {} | |
453 | fs = new_fs | |
454 | end | |
455 | ||
456 | ---------------------------------------------------------------------- | |
457 | -- closing of a function | |
458 | -- * used in parser() and body() | |
459 | ---------------------------------------------------------------------- | |
460 | ||
461 | local function close_func() | |
462 | removevars() | |
463 | fs = fs.prev | |
464 | end | |
465 | ||
466 | --[[-------------------------------------------------------------------- | |
467 | -- other parsing functions | |
468 | -- * for table constructor, parameter list, argument list | |
469 | ----------------------------------------------------------------------]] | |
470 | ||
471 | ---------------------------------------------------------------------- | |
472 | -- parse a function name suffix, for function call specifications | |
473 | -- * used in primaryexp(), funcname() | |
474 | ---------------------------------------------------------------------- | |
475 | ||
476 | local function field(v) | |
477 | -- field -> ['.' | ':'] NAME | |
478 | local key = {} | |
479 | nextt() -- skip the dot or colon | |
480 | checkname(key) | |
481 | v.k = "VINDEXED" | |
482 | end | |
483 | ||
484 | ---------------------------------------------------------------------- | |
485 | -- parse a table indexing suffix, for constructors, expressions | |
486 | -- * used in recfield(), primaryexp() | |
487 | ---------------------------------------------------------------------- | |
488 | ||
489 | local function yindex(v) | |
490 | -- index -> '[' expr ']' | |
491 | nextt() -- skip the '[' | |
492 | expr(v) | |
493 | checknext("]") | |
494 | end | |
495 | ||
496 | ---------------------------------------------------------------------- | |
497 | -- parse a table record (hash) field | |
498 | -- * used in constructor() | |
499 | ---------------------------------------------------------------------- | |
500 | ||
501 | local function recfield(cc) | |
502 | -- recfield -> (NAME | '['exp1']') = exp1 | |
503 | local key, val = {}, {} | |
504 | if tok == "<name>" then | |
505 | checkname(key) | |
506 | else-- tok == '[' | |
507 | yindex(key) | |
508 | end | |
509 | checknext("=") | |
510 | expr(val) | |
511 | end | |
512 | ||
513 | ---------------------------------------------------------------------- | |
514 | -- emit a set list instruction if enough elements (LFIELDS_PER_FLUSH) | |
515 | -- * note: retained in this skeleton because it modifies cc.v.k | |
516 | -- * used in constructor() | |
517 | ---------------------------------------------------------------------- | |
518 | ||
519 | local function closelistfield(cc) | |
520 | if cc.v.k == "VVOID" then return end -- there is no list item | |
521 | cc.v.k = "VVOID" | |
522 | end | |
523 | ||
524 | ---------------------------------------------------------------------- | |
525 | -- parse a table list (array) field | |
526 | -- * used in constructor() | |
527 | ---------------------------------------------------------------------- | |
528 | ||
529 | local function listfield(cc) | |
530 | expr(cc.v) | |
531 | end | |
532 | ||
533 | ---------------------------------------------------------------------- | |
534 | -- parse a table constructor | |
535 | -- * used in funcargs(), simpleexp() | |
536 | ---------------------------------------------------------------------- | |
537 | ||
538 | local function constructor(t) | |
539 | -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}' | |
540 | -- field -> recfield | listfield | |
541 | -- fieldsep -> ',' | ';' | |
542 | local line = ln | |
543 | local cc = {} | |
544 | cc.v = {} | |
545 | cc.t = t | |
546 | t.k = "VRELOCABLE" | |
547 | cc.v.k = "VVOID" | |
548 | checknext("{") | |
549 | repeat | |
550 | if tok == "}" then break end | |
551 | -- closelistfield(cc) here | |
552 | local c = tok | |
553 | if c == "<name>" then -- may be listfields or recfields | |
554 | if lookahead() ~= "=" then -- look ahead: expression? | |
555 | listfield(cc) | |
556 | else | |
557 | recfield(cc) | |
558 | end | |
559 | elseif c == "[" then -- constructor_item -> recfield | |
560 | recfield(cc) | |
561 | else -- constructor_part -> listfield | |
562 | listfield(cc) | |
563 | end | |
564 | until not testnext(",") and not testnext(";") | |
565 | check_match("}", "{", line) | |
566 | -- lastlistfield(cc) here | |
567 | end | |
568 | ||
569 | ---------------------------------------------------------------------- | |
570 | -- parse the arguments (parameters) of a function declaration | |
571 | -- * used in body() | |
572 | ---------------------------------------------------------------------- | |
573 | ||
574 | local function parlist() | |
575 | -- parlist -> [ param { ',' param } ] | |
576 | local nparams = 0 | |
577 | if tok ~= ")" then -- is 'parlist' not empty? | |
578 | repeat | |
579 | local c = tok | |
580 | if c == "<name>" then -- param -> NAME | |
581 | new_localvar(str_checkname()) | |
582 | nparams = nparams + 1 | |
583 | elseif c == "..." then | |
584 | nextt() | |
585 | fs.is_vararg = true | |
586 | else | |
587 | syntaxerror("<name> or '...' expected") | |
588 | end | |
589 | until fs.is_vararg or not testnext(",") | |
590 | end--if | |
591 | adjustlocalvars(nparams) | |
592 | end | |
593 | ||
594 | ---------------------------------------------------------------------- | |
595 | -- parse the parameters of a function call | |
596 | -- * contrast with parlist(), used in function declarations | |
597 | -- * used in primaryexp() | |
598 | ---------------------------------------------------------------------- | |
599 | ||
600 | local function funcargs(f) | |
601 | local args = {} | |
602 | local line = ln | |
603 | local c = tok | |
604 | if c == "(" then -- funcargs -> '(' [ explist1 ] ')' | |
605 | if line ~= lastln then | |
606 | syntaxerror("ambiguous syntax (function call x new statement)") | |
607 | end | |
608 | nextt() | |
609 | if tok == ")" then -- arg list is empty? | |
610 | args.k = "VVOID" | |
611 | else | |
612 | explist1(args) | |
613 | end | |
614 | check_match(")", "(", line) | |
615 | elseif c == "{" then -- funcargs -> constructor | |
616 | constructor(args) | |
617 | elseif c == "<string>" then -- funcargs -> STRING | |
618 | codestring(args, seminfo) | |
619 | nextt() -- must use 'seminfo' before 'next' | |
620 | else | |
621 | syntaxerror("function arguments expected") | |
622 | return | |
623 | end--if c | |
624 | f.k = "VCALL" | |
625 | end | |
626 | ||
627 | --[[-------------------------------------------------------------------- | |
628 | -- mostly expression functions | |
629 | ----------------------------------------------------------------------]] | |
630 | ||
631 | ---------------------------------------------------------------------- | |
632 | -- parses an expression in parentheses or a single variable | |
633 | -- * used in primaryexp() | |
634 | ---------------------------------------------------------------------- | |
635 | ||
636 | local function prefixexp(v) | |
637 | -- prefixexp -> NAME | '(' expr ')' | |
638 | local c = tok | |
639 | if c == "(" then | |
640 | local line = ln | |
641 | nextt() | |
642 | expr(v) | |
643 | check_match(")", "(", line) | |
644 | elseif c == "<name>" then | |
645 | singlevar(v) | |
646 | else | |
647 | syntaxerror("unexpected symbol") | |
648 | end--if c | |
649 | end | |
650 | ||
651 | ---------------------------------------------------------------------- | |
652 | -- parses a prefixexp (an expression in parentheses or a single | |
653 | -- variable) or a function call specification | |
654 | -- * used in simpleexp(), assignment(), expr_stat() | |
655 | ---------------------------------------------------------------------- | |
656 | ||
657 | local function primaryexp(v) | |
658 | -- primaryexp -> | |
659 | -- prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } | |
660 | prefixexp(v) | |
661 | while true do | |
662 | local c = tok | |
663 | if c == "." then -- field | |
664 | field(v) | |
665 | elseif c == "[" then -- '[' exp1 ']' | |
666 | local key = {} | |
667 | yindex(key) | |
668 | elseif c == ":" then -- ':' NAME funcargs | |
669 | local key = {} | |
670 | nextt() | |
671 | checkname(key) | |
672 | funcargs(v) | |
673 | elseif c == "(" or c == "<string>" or c == "{" then -- funcargs | |
674 | funcargs(v) | |
675 | else | |
676 | return | |
677 | end--if c | |
678 | end--while | |
679 | end | |
680 | ||
681 | ---------------------------------------------------------------------- | |
682 | -- parses general expression types, constants handled here | |
683 | -- * used in subexpr() | |
684 | ---------------------------------------------------------------------- | |
685 | ||
686 | local function simpleexp(v) | |
687 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | | |
688 | -- constructor | FUNCTION body | primaryexp | |
689 | local c = tok | |
690 | if c == "<number>" then | |
691 | v.k = "VKNUM" | |
692 | elseif c == "<string>" then | |
693 | codestring(v, seminfo) | |
694 | elseif c == "nil" then | |
695 | v.k = "VNIL" | |
696 | elseif c == "true" then | |
697 | v.k = "VTRUE" | |
698 | elseif c == "false" then | |
699 | v.k = "VFALSE" | |
700 | elseif c == "..." then -- vararg | |
701 | check_condition(fs.is_vararg == true, | |
702 | "cannot use '...' outside a vararg function"); | |
703 | v.k = "VVARARG" | |
704 | elseif c == "{" then -- constructor | |
705 | constructor(v) | |
706 | return | |
707 | elseif c == "function" then | |
708 | nextt() | |
709 | body(v, false, ln) | |
710 | return | |
711 | else | |
712 | primaryexp(v) | |
713 | return | |
714 | end--if c | |
715 | nextt() | |
716 | end | |
717 | ||
718 | ------------------------------------------------------------------------ | |
719 | -- Parse subexpressions. Includes handling of unary operators and binary | |
720 | -- operators. A subexpr is given the rhs priority level of the operator | |
721 | -- immediately left of it, if any (limit is -1 if none,) and if a binop | |
722 | -- is found, limit is compared with the lhs priority level of the binop | |
723 | -- in order to determine which executes first. | |
724 | -- * recursively called | |
725 | -- * used in expr() | |
726 | ------------------------------------------------------------------------ | |
727 | ||
728 | local function subexpr(v, limit) | |
729 | -- subexpr -> (simpleexp | unop subexpr) { binop subexpr } | |
730 | -- * where 'binop' is any binary operator with a priority | |
731 | -- higher than 'limit' | |
732 | local op = tok | |
733 | local uop = unopr[op] | |
734 | if uop then | |
735 | nextt() | |
736 | subexpr(v, UNARY_PRIORITY) | |
737 | else | |
738 | simpleexp(v) | |
739 | end | |
740 | -- expand while operators have priorities higher than 'limit' | |
741 | op = tok | |
742 | local binop = binopr_left[op] | |
743 | while binop and binop > limit do | |
744 | local v2 = {} | |
745 | nextt() | |
746 | -- read sub-expression with higher priority | |
747 | local nextop = subexpr(v2, binopr_right[op]) | |
748 | op = nextop | |
749 | binop = binopr_left[op] | |
750 | end | |
751 | return op -- return first untreated operator | |
752 | end | |
753 | ||
754 | ---------------------------------------------------------------------- | |
755 | -- Expression parsing starts here. Function subexpr is entered with the | |
756 | -- left operator (which is non-existent) priority of -1, which is lower | |
757 | -- than all actual operators. Expr information is returned in parm v. | |
758 | -- * used in cond(), explist1(), index(), recfield(), listfield(), | |
759 | -- prefixexp(), while_stat(), exp1() | |
760 | ---------------------------------------------------------------------- | |
761 | ||
762 | -- this is a forward-referenced local | |
763 | function expr(v) | |
764 | -- expr -> subexpr | |
765 | subexpr(v, 0) | |
766 | end | |
767 | ||
768 | --[[-------------------------------------------------------------------- | |
769 | -- third level parsing functions | |
770 | ----------------------------------------------------------------------]] | |
771 | ||
772 | ------------------------------------------------------------------------ | |
773 | -- parse a variable assignment sequence | |
774 | -- * recursively called | |
775 | -- * used in expr_stat() | |
776 | ------------------------------------------------------------------------ | |
777 | ||
778 | local function assignment(v) | |
779 | local e = {} | |
780 | local c = v.v.k | |
781 | check_condition(c == "VLOCAL" or c == "VUPVAL" or c == "VGLOBAL" | |
782 | or c == "VINDEXED", "syntax error") | |
783 | if testnext(",") then -- assignment -> ',' primaryexp assignment | |
784 | local nv = {} -- expdesc | |
785 | nv.v = {} | |
786 | primaryexp(nv.v) | |
787 | -- lparser.c deals with some register usage conflict here | |
788 | assignment(nv) | |
789 | else -- assignment -> '=' explist1 | |
790 | checknext("=") | |
791 | explist1(e) | |
792 | return -- avoid default | |
793 | end | |
794 | e.k = "VNONRELOC" | |
795 | end | |
796 | ||
797 | ---------------------------------------------------------------------- | |
798 | -- parse a for loop body for both versions of the for loop | |
799 | -- * used in fornum(), forlist() | |
800 | ---------------------------------------------------------------------- | |
801 | ||
802 | local function forbody(nvars, isnum) | |
803 | -- forbody -> DO block | |
804 | checknext("do") | |
805 | enterblock(false) -- scope for declared variables | |
806 | adjustlocalvars(nvars) | |
807 | block() | |
808 | leaveblock() -- end of scope for declared variables | |
809 | end | |
810 | ||
811 | ---------------------------------------------------------------------- | |
812 | -- parse a numerical for loop, calls forbody() | |
813 | -- * used in for_stat() | |
814 | ---------------------------------------------------------------------- | |
815 | ||
816 | local function fornum(varname) | |
817 | -- fornum -> NAME = exp1, exp1 [, exp1] DO body | |
818 | local line = line | |
819 | new_localvarliteral("(for index)") | |
820 | new_localvarliteral("(for limit)") | |
821 | new_localvarliteral("(for step)") | |
822 | new_localvar(varname) | |
823 | checknext("=") | |
824 | exp1() -- initial value | |
825 | checknext(",") | |
826 | exp1() -- limit | |
827 | if testnext(",") then | |
828 | exp1() -- optional step | |
829 | else | |
830 | -- default step = 1 | |
831 | end | |
832 | forbody(1, true) | |
833 | end | |
834 | ||
835 | ---------------------------------------------------------------------- | |
836 | -- parse a generic for loop, calls forbody() | |
837 | -- * used in for_stat() | |
838 | ---------------------------------------------------------------------- | |
839 | ||
840 | local function forlist(indexname) | |
841 | -- forlist -> NAME {, NAME} IN explist1 DO body | |
842 | local e = {} | |
843 | -- create control variables | |
844 | new_localvarliteral("(for generator)") | |
845 | new_localvarliteral("(for state)") | |
846 | new_localvarliteral("(for control)") | |
847 | -- create declared variables | |
848 | new_localvar(indexname) | |
849 | local nvars = 1 | |
850 | while testnext(",") do | |
851 | new_localvar(str_checkname()) | |
852 | nvars = nvars + 1 | |
853 | end | |
854 | checknext("in") | |
855 | local line = line | |
856 | explist1(e) | |
857 | forbody(nvars, false) | |
858 | end | |
859 | ||
860 | ---------------------------------------------------------------------- | |
861 | -- parse a function name specification | |
862 | -- * used in func_stat() | |
863 | ---------------------------------------------------------------------- | |
864 | ||
865 | local function funcname(v) | |
866 | -- funcname -> NAME {field} [':' NAME] | |
867 | local needself = false | |
868 | singlevar(v) | |
869 | while tok == "." do | |
870 | field(v) | |
871 | end | |
872 | if tok == ":" then | |
873 | needself = true | |
874 | field(v) | |
875 | end | |
876 | return needself | |
877 | end | |
878 | ||
879 | ---------------------------------------------------------------------- | |
880 | -- parse the single expressions needed in numerical for loops | |
881 | -- * used in fornum() | |
882 | ---------------------------------------------------------------------- | |
883 | ||
884 | -- this is a forward-referenced local | |
885 | function exp1() | |
886 | -- exp1 -> expr | |
887 | local e = {} | |
888 | expr(e) | |
889 | end | |
890 | ||
891 | ---------------------------------------------------------------------- | |
892 | -- parse condition in a repeat statement or an if control structure | |
893 | -- * used in repeat_stat(), test_then_block() | |
894 | ---------------------------------------------------------------------- | |
895 | ||
896 | local function cond() | |
897 | -- cond -> expr | |
898 | local v = {} | |
899 | expr(v) -- read condition | |
900 | end | |
901 | ||
902 | ---------------------------------------------------------------------- | |
903 | -- parse part of an if control structure, including the condition | |
904 | -- * used in if_stat() | |
905 | ---------------------------------------------------------------------- | |
906 | ||
907 | local function test_then_block() | |
908 | -- test_then_block -> [IF | ELSEIF] cond THEN block | |
909 | nextt() -- skip IF or ELSEIF | |
910 | cond() | |
911 | checknext("then") | |
912 | block() -- 'then' part | |
913 | end | |
914 | ||
915 | ---------------------------------------------------------------------- | |
916 | -- parse a local function statement | |
917 | -- * used in local_stat() | |
918 | ---------------------------------------------------------------------- | |
919 | ||
920 | local function localfunc() | |
921 | -- localfunc -> NAME body | |
922 | local v, b = {} | |
923 | new_localvar(str_checkname()) | |
924 | v.k = "VLOCAL" | |
925 | adjustlocalvars(1) | |
926 | body(b, false, ln) | |
927 | end | |
928 | ||
929 | ---------------------------------------------------------------------- | |
930 | -- parse a local variable declaration statement | |
931 | -- * used in local_stat() | |
932 | ---------------------------------------------------------------------- | |
933 | ||
934 | local function localstat() | |
935 | -- localstat -> NAME {',' NAME} ['=' explist1] | |
936 | local nvars = 0 | |
937 | local e = {} | |
938 | repeat | |
939 | new_localvar(str_checkname()) | |
940 | nvars = nvars + 1 | |
941 | until not testnext(",") | |
942 | if testnext("=") then | |
943 | explist1(e) | |
944 | else | |
945 | e.k = "VVOID" | |
946 | end | |
947 | adjustlocalvars(nvars) | |
948 | end | |
949 | ||
950 | ---------------------------------------------------------------------- | |
951 | -- parse a list of comma-separated expressions | |
952 | -- * used in return_stat(), localstat(), funcargs(), assignment(), | |
953 | -- forlist() | |
954 | ---------------------------------------------------------------------- | |
955 | ||
956 | -- this is a forward-referenced local | |
957 | function explist1(e) | |
958 | -- explist1 -> expr { ',' expr } | |
959 | expr(e) | |
960 | while testnext(",") do | |
961 | expr(e) | |
962 | end | |
963 | end | |
964 | ||
965 | ---------------------------------------------------------------------- | |
966 | -- parse function declaration body | |
967 | -- * used in simpleexp(), localfunc(), func_stat() | |
968 | ---------------------------------------------------------------------- | |
969 | ||
970 | -- this is a forward-referenced local | |
971 | function body(e, needself, line) | |
972 | -- body -> '(' parlist ')' chunk END | |
973 | open_func() | |
974 | checknext("(") | |
975 | if needself then | |
976 | new_localvarliteral("self", true) | |
977 | adjustlocalvars(1) | |
978 | end | |
979 | parlist() | |
980 | checknext(")") | |
981 | chunk() | |
982 | check_match("end", "function", line) | |
983 | close_func() | |
984 | end | |
985 | ||
986 | ---------------------------------------------------------------------- | |
987 | -- parse a code block or unit | |
988 | -- * used in do_stat(), while_stat(), forbody(), test_then_block(), | |
989 | -- if_stat() | |
990 | ---------------------------------------------------------------------- | |
991 | ||
992 | -- this is a forward-referenced local | |
993 | function block() | |
994 | -- block -> chunk | |
995 | enterblock(false) | |
996 | chunk() | |
997 | leaveblock() | |
998 | end | |
999 | ||
1000 | --[[-------------------------------------------------------------------- | |
1001 | -- second level parsing functions, all with '_stat' suffix | |
1002 | -- * since they are called via a table lookup, they cannot be local | |
1003 | -- functions (a lookup table of local functions might be smaller...) | |
1004 | -- * stat() -> *_stat() | |
1005 | ----------------------------------------------------------------------]] | |
1006 | ||
1007 | ---------------------------------------------------------------------- | |
1008 | -- initial parsing for a for loop, calls fornum() or forlist() | |
1009 | -- * removed 'line' parameter (used to set debug information only) | |
1010 | -- * used in stat() | |
1011 | ---------------------------------------------------------------------- | |
1012 | ||
1013 | function for_stat() | |
1014 | -- stat -> for_stat -> FOR (fornum | forlist) END | |
1015 | local line = line | |
1016 | enterblock(true) -- scope for loop and control variables | |
1017 | nextt() -- skip 'for' | |
1018 | local varname = str_checkname() -- first variable name | |
1019 | local c = tok | |
1020 | if c == "=" then | |
1021 | fornum(varname) | |
1022 | elseif c == "," or c == "in" then | |
1023 | forlist(varname) | |
1024 | else | |
1025 | syntaxerror("'=' or 'in' expected") | |
1026 | end | |
1027 | check_match("end", "for", line) | |
1028 | leaveblock() -- loop scope (`break' jumps to this point) | |
1029 | end | |
1030 | ||
1031 | ---------------------------------------------------------------------- | |
1032 | -- parse a while-do control structure, body processed by block() | |
1033 | -- * used in stat() | |
1034 | ---------------------------------------------------------------------- | |
1035 | ||
1036 | function while_stat() | |
1037 | -- stat -> while_stat -> WHILE cond DO block END | |
1038 | local line = line | |
1039 | nextt() -- skip WHILE | |
1040 | cond() -- parse condition | |
1041 | enterblock(true) | |
1042 | checknext("do") | |
1043 | block() | |
1044 | check_match("end", "while", line) | |
1045 | leaveblock() | |
1046 | end | |
1047 | ||
1048 | ---------------------------------------------------------------------- | |
1049 | -- parse a repeat-until control structure, body parsed by chunk() | |
1050 | -- * originally, repeatstat() calls breakstat() too if there is an | |
1051 | -- upvalue in the scope block; nothing is actually lexed, it is | |
1052 | -- actually the common code in breakstat() for closing of upvalues | |
1053 | -- * used in stat() | |
1054 | ---------------------------------------------------------------------- | |
1055 | ||
1056 | function repeat_stat() | |
1057 | -- stat -> repeat_stat -> REPEAT block UNTIL cond | |
1058 | local line = line | |
1059 | enterblock(true) -- loop block | |
1060 | enterblock(false) -- scope block | |
1061 | nextt() -- skip REPEAT | |
1062 | chunk() | |
1063 | check_match("until", "repeat", line) | |
1064 | cond() | |
1065 | -- close upvalues at scope level below | |
1066 | leaveblock() -- finish scope | |
1067 | leaveblock() -- finish loop | |
1068 | end | |
1069 | ||
1070 | ---------------------------------------------------------------------- | |
1071 | -- parse an if control structure | |
1072 | -- * used in stat() | |
1073 | ---------------------------------------------------------------------- | |
1074 | ||
1075 | function if_stat() | |
1076 | -- stat -> if_stat -> IF cond THEN block | |
1077 | -- {ELSEIF cond THEN block} [ELSE block] END | |
1078 | local line = line | |
1079 | local v = {} | |
1080 | test_then_block() -- IF cond THEN block | |
1081 | while tok == "elseif" do | |
1082 | test_then_block() -- ELSEIF cond THEN block | |
1083 | end | |
1084 | if tok == "else" then | |
1085 | nextt() -- skip ELSE | |
1086 | block() -- 'else' part | |
1087 | end | |
1088 | check_match("end", "if", line) | |
1089 | end | |
1090 | ||
1091 | ---------------------------------------------------------------------- | |
1092 | -- parse a return statement | |
1093 | -- * used in stat() | |
1094 | ---------------------------------------------------------------------- | |
1095 | ||
1096 | function return_stat() | |
1097 | -- stat -> return_stat -> RETURN explist | |
1098 | local e = {} | |
1099 | nextt() -- skip RETURN | |
1100 | local c = tok | |
1101 | if block_follow[c] or c == ";" then | |
1102 | -- return no values | |
1103 | else | |
1104 | explist1(e) -- optional return values | |
1105 | end | |
1106 | end | |
1107 | ||
1108 | ---------------------------------------------------------------------- | |
1109 | -- parse a break statement | |
1110 | -- * used in stat() | |
1111 | ---------------------------------------------------------------------- | |
1112 | ||
1113 | function break_stat() | |
1114 | -- stat -> break_stat -> BREAK | |
1115 | local bl = fs.bl | |
1116 | nextt() -- skip BREAK | |
1117 | while bl and not bl.isbreakable do -- find a breakable block | |
1118 | bl = bl.prev | |
1119 | end | |
1120 | if not bl then | |
1121 | syntaxerror("no loop to break") | |
1122 | end | |
1123 | end | |
1124 | ||
1125 | ---------------------------------------------------------------------- | |
1126 | -- parse a function call with no returns or an assignment statement | |
1127 | -- * the struct with .prev is used for name searching in lparse.c, | |
1128 | -- so it is retained for now; present in assignment() also | |
1129 | -- * used in stat() | |
1130 | ---------------------------------------------------------------------- | |
1131 | ||
1132 | function expr_stat() | |
1133 | -- stat -> expr_stat -> func | assignment | |
1134 | local v = {} | |
1135 | v.v = {} | |
1136 | primaryexp(v.v) | |
1137 | if v.v.k == "VCALL" then -- stat -> func | |
1138 | -- call statement uses no results | |
1139 | else -- stat -> assignment | |
1140 | v.prev = nil | |
1141 | assignment(v) | |
1142 | end | |
1143 | end | |
1144 | ||
1145 | ---------------------------------------------------------------------- | |
1146 | -- parse a function statement | |
1147 | -- * used in stat() | |
1148 | ---------------------------------------------------------------------- | |
1149 | ||
1150 | function function_stat() | |
1151 | -- stat -> function_stat -> FUNCTION funcname body | |
1152 | local line = line | |
1153 | local v, b = {}, {} | |
1154 | nextt() -- skip FUNCTION | |
1155 | local needself = funcname(v) | |
1156 | body(b, needself, line) | |
1157 | end | |
1158 | ||
1159 | ---------------------------------------------------------------------- | |
1160 | -- parse a simple block enclosed by a DO..END pair | |
1161 | -- * used in stat() | |
1162 | ---------------------------------------------------------------------- | |
1163 | ||
1164 | function do_stat() | |
1165 | -- stat -> do_stat -> DO block END | |
1166 | local line = line | |
1167 | nextt() -- skip DO | |
1168 | block() | |
1169 | check_match("end", "do", line) | |
1170 | end | |
1171 | ||
1172 | ---------------------------------------------------------------------- | |
1173 | -- parse a statement starting with LOCAL | |
1174 | -- * used in stat() | |
1175 | ---------------------------------------------------------------------- | |
1176 | ||
1177 | function local_stat() | |
1178 | -- stat -> local_stat -> LOCAL FUNCTION localfunc | |
1179 | -- -> LOCAL localstat | |
1180 | nextt() -- skip LOCAL | |
1181 | if testnext("function") then -- local function? | |
1182 | localfunc() | |
1183 | else | |
1184 | localstat() | |
1185 | end | |
1186 | end | |
1187 | ||
1188 | --[[-------------------------------------------------------------------- | |
1189 | -- main functions, top level parsing functions | |
1190 | -- * accessible functions are: init(lexer), parser() | |
1191 | -- * [entry] -> parser() -> chunk() -> stat() | |
1192 | ----------------------------------------------------------------------]] | |
1193 | ||
1194 | ---------------------------------------------------------------------- | |
1195 | -- initial parsing for statements, calls '_stat' suffixed functions | |
1196 | -- * used in chunk() | |
1197 | ---------------------------------------------------------------------- | |
1198 | ||
1199 | local function stat() | |
1200 | -- stat -> if_stat while_stat do_stat for_stat repeat_stat | |
1201 | -- function_stat local_stat return_stat break_stat | |
1202 | -- expr_stat | |
1203 | line = ln -- may be needed for error messages | |
1204 | local c = tok | |
1205 | local fn = stat_call[c] | |
1206 | -- handles: if while do for repeat function local return break | |
1207 | if fn then | |
85
347ac1733354
Remove uses of getfenv to work around Lua/LuaJIT inconsistency, and as a step towards 5.2 compatibility (thanks mva, Zash)
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
1208 | _M[fn]() |
1 | 1209 | -- return or break must be last statement |
1210 | if c == "return" or c == "break" then return true end | |
1211 | else | |
1212 | expr_stat() | |
1213 | end | |
1214 | return false | |
1215 | end | |
1216 | ||
1217 | ---------------------------------------------------------------------- | |
1218 | -- parse a chunk, which consists of a bunch of statements | |
1219 | -- * used in parser(), body(), block(), repeat_stat() | |
1220 | ---------------------------------------------------------------------- | |
1221 | ||
1222 | -- this is a forward-referenced local | |
1223 | function chunk() | |
1224 | -- chunk -> { stat [';'] } | |
1225 | local islast = false | |
1226 | while not islast and not block_follow[tok] do | |
1227 | islast = stat() | |
1228 | testnext(";") | |
1229 | end | |
1230 | end | |
1231 | ||
1232 | ---------------------------------------------------------------------- | |
1233 | -- performs parsing, returns parsed data structure | |
1234 | ---------------------------------------------------------------------- | |
1235 | ||
1236 | function parser() | |
1237 | open_func() | |
1238 | fs.is_vararg = true -- main func. is always vararg | |
1239 | nextt() -- read first token | |
1240 | chunk() | |
1241 | check("<eof>") | |
1242 | close_func() | |
1243 | return globalinfo, localinfo | |
1244 | end | |
1245 | ||
1246 | ---------------------------------------------------------------------- | |
1247 | -- initialization function | |
1248 | ---------------------------------------------------------------------- | |
1249 | ||
1250 | function init(tokorig, seminfoorig, toklnorig) | |
1251 | tpos = 1 -- token position | |
1252 | top_fs = {} -- reset top level function state | |
1253 | ------------------------------------------------------------------ | |
1254 | -- set up grammar-only token tables; impedance-matching... | |
1255 | -- note that constants returned by the lexer is source-level, so | |
1256 | -- for now, fake(!) constant tokens (TK_NUMBER|TK_STRING|TK_LSTRING) | |
1257 | ------------------------------------------------------------------ | |
1258 | local j = 1 | |
1259 | toklist, seminfolist, toklnlist, xreflist = {}, {}, {}, {} | |
1260 | for i = 1, #tokorig do | |
1261 | local tok = tokorig[i] | |
1262 | local yep = true | |
1263 | if tok == "TK_KEYWORD" or tok == "TK_OP" then | |
1264 | tok = seminfoorig[i] | |
1265 | elseif tok == "TK_NAME" then | |
1266 | tok = "<name>" | |
1267 | seminfolist[j] = seminfoorig[i] | |
1268 | elseif tok == "TK_NUMBER" then | |
1269 | tok = "<number>" | |
1270 | seminfolist[j] = 0 -- fake! | |
1271 | elseif tok == "TK_STRING" or tok == "TK_LSTRING" then | |
1272 | tok = "<string>" | |
1273 | seminfolist[j] = "" -- fake! | |
1274 | elseif tok == "TK_EOS" then | |
1275 | tok = "<eof>" | |
1276 | else | |
1277 | -- non-grammar tokens; ignore them | |
1278 | yep = false | |
1279 | end | |
1280 | if yep then -- set rest of the information | |
1281 | toklist[j] = tok | |
1282 | toklnlist[j] = toklnorig[i] | |
1283 | xreflist[j] = i | |
1284 | j = j + 1 | |
1285 | end | |
1286 | end--for | |
1287 | ------------------------------------------------------------------ | |
1288 | -- initialize data structures for variable tracking | |
1289 | ------------------------------------------------------------------ | |
1290 | globalinfo, globallookup, localinfo = {}, {}, {} | |
1291 | ilocalinfo, ilocalrefs = {}, {} | |
1292 | end | |
1293 | ||
85
347ac1733354
Remove uses of getfenv to work around Lua/LuaJIT inconsistency, and as a step towards 5.2 compatibility (thanks mva, Zash)
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
1294 | return _M |