lparser.lua 41 KB

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