IncludeUrl

Untitled

Sep 25th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.18 KB | None | 0 0
  1. local c=require("30log")local function d(...)local e={}for t,n in ipairs({...})do
  2. e[n]=true
  3. end
  4. return e
  5. end
  6. local n=c("Lexer")function n:init(t,n,e)self.pos=1
  7. self.col=1
  8. self.line=1
  9. self.source=t
  10. self.file=n or"<unnamed>"self.colored=type(e)=="boolean"and e or true
  11. self.highlight={}end
  12. function n:peek(e)e=e or 0
  13. return self.source:sub(self.pos+e,(utf8.offset(self.source,2,self.pos+e)or(self.pos+e+1))-1)end
  14. function n:next()if self.nsaved then
  15. self.nsaved=nil
  16. return"\n"end
  17. local e=self.source:sub(self.pos,utf8.offset(self.source,2,self.pos)-1)self.pos=self.pos+#e
  18. self.col=self.col+1
  19. if e=="\r"or e=="\n"then
  20. if e=="\r"and self:peek()=="\n"then
  21. self.pos=self.pos+1
  22. self.nsaved=true
  23. end
  24. self.col=1
  25. self.line=self.line+1
  26. end
  27. return e
  28. end
  29. function n:pmatch(e)local e={self.source:sub(self.pos):find("^"..e)}if#e==0 then return end
  30. local t,n=table.remove(e,1),table.remove(e,1)return self.source:sub(self.pos+t-1,self.pos+n-1),table.unpack(e)end
  31. function n:match(e)local e={self:pmatch(e)}if#e>0 then
  32. self.pos=self.pos+#e[1]self.col=self.col+#e[1]return table.unpack(e)end
  33. end
  34. function n:getline(n)if n<1 then return end
  35. local e=1
  36. for t in self.source:gmatch("([^\r\n]*)\r?\n?")do
  37. if e==n then
  38. return t
  39. end
  40. e=e+1
  41. end
  42. end
  43. function n:showSlice(e,n)if self.colored then
  44. for e in self:lex()do end
  45. end
  46. local l=math.ceil(math.log(math.max(e[1],e[3]),10))if l==0 then l=1 end
  47. if self.colored then
  48. print(("\27[1;34m%s-->\27[0m %s:%d:%d"):format((" "):rep(l),self.file,e[1],e[2]))else
  49. print(("%s--> %s:%d:%d"):format((" "):rep(l),self.file,e[1],e[2]))end
  50. for n=e[1]-2,e[3]+2 do
  51. local t=self:getline(n)if not t then goto continue end
  52. if self.colored then
  53. io.write(("\27[1;34m%"..l.."d |\27[0m "):format(n))local e=1
  54. while#t>0 do
  55. local l=t:sub(1,utf8.offset(t,2)-1)if self.highlight[n]and self.highlight[n][e]then
  56. io.write("\27[0m"..self.highlight[n][e])end
  57. io.write(l)e=e+1
  58. t=t:sub(utf8.offset(t,2))end
  59. print("\27[0m")end
  60. if n>=e[1]and n<=e[3]then
  61. if self.colored then
  62. io.write("\27[1;34m")end
  63. io.write((" "):rep(l+1).."| ")if self.colored then
  64. io.write("\27[31m")end
  65. io.write((" "):rep(n==e[1]and(e[2]-1)or 0))io.write(("~"):rep((n==e[3]and e[4]or(utf8.len(t)+1))-(n==e[1]and e[2]or 1)))io.write((n==e[3]and"^"or""))if self.colored then
  66. io.write("\27[0m")end
  67. io.write("\n")end::continue::end
  68. return l
  69. end
  70. function n:error(e,t)coroutine.yield("error",e,t)end
  71. n.COLORS={keyword="\27[1;36m",punct="\27[1;37m",number="\27[33m",string="\27[32m",name="",comment="\27[37m"}function n:pushcolor(e,t)if not self.colored then
  72. return
  73. end
  74. if n.KEYWORDS[e]then
  75. e="keyword"end
  76. if n.OPS[3][e]then
  77. e="punct"end
  78. local n=n.COLORS[e]for e=t[1],t[3]do
  79. if not self.highlight[e]then
  80. self.highlight[e]={}end
  81. self.highlight[e][e==t[1]and t[2]or 1]=n
  82. end
  83. end
  84. function n:token(e,l,t)if not self.colored then
  85. coroutine.yield(e,l,t)return
  86. end
  87. self:pushcolor(e,t)coroutine.yield(e,l,t)end
  88. n.WHITESPACE=d(" ","\t","\n","\v","\f","\r")n.KEYWORDS=d("and","break","do","else","elseif","end","false","for","function","goto","if","in","local","nil","not","or","repeat","return","then","true","until","while")n.OPS={}n.OPS[3]=d("+","-","*","/","%","^","#","&","~","|","<<",">>","//","==","~=","<=",">=","<",">","=","(",")","{","}","[","]","::",";",":",",",".","..","...")n.OPS[1]={}do
  89. for e in pairs(n.OPS[3])do
  90. n.OPS[1][e:sub(1,1)]=true
  91. end
  92. end
  93. n.OPS[2]={}do
  94. for e in pairs(n.OPS[3])do
  95. n.OPS[2][e:sub(1,2)]=true
  96. end
  97. end
  98. function n.isnamehead(e)return(e>="a"and e<="z")or(e>="A"and e<="Z")or e=="_"end
  99. function n.isdigit(e)return(e>="0"and e<="9")end
  100. function n:lexc()local e=""while self:peek()~=""do
  101. local t=self:peek()if self:pmatch("%-%-")then
  102. self:comment()goto continue
  103. end
  104. if self:pmatch("%[=*%[")then
  105. self:longstring()goto continue
  106. end
  107. if#e==0 and n.OPS[1][t]then
  108. self.broken=nil
  109. e=e..t
  110. self:next()goto continue
  111. elseif#e==1 and n.OPS[2][e..t]and not self.broken then
  112. e=e..t
  113. self:next()goto continue
  114. elseif#e==2 and n.OPS[3][e..t]and not self.broken then
  115. e=e..t
  116. self:next()goto continue
  117. elseif#e>0 then
  118. self:token(e,nil,{self.line,self.col-#e,self.line,self.col-1})e=""self.broken=nil
  119. end
  120. if#e==0 and n.OPS[1][t]then
  121. self.broken=nil
  122. e=e..t
  123. self:next()goto continue
  124. elseif#e==1 and n.OPS[2][e..t]and not self.broken then
  125. e=e..t
  126. self:next()goto continue
  127. elseif#e==2 and n.OPS[3][e..t]and not self.broken then
  128. e=e..t
  129. self:next()goto continue
  130. end
  131. if n.WHITESPACE[t]then
  132. self:next()elseif t=="'"or t=='"'then
  133. self:string()elseif n.isnamehead(t)then
  134. self:name()elseif n.isdigit(t)or(t=="."and n.isdigit((self:peek(1))))then
  135. self:number()else
  136. self:error(("unexpected symbol `%s`"):format(t),{self.line,self.col,self.line,self.col})end::continue::end
  137. if#e>0 then
  138. self:token(e,nil,{self.line,self.col-1,self.line,self.col-1})end
  139. end
  140. function n:name()local t,l=self.line,self.col
  141. local e=self:match("[a-zA-Z_][a-zA-Z0-9_]*")if n.KEYWORDS[e]then
  142. self:token(e,nil,{t,l,self.line,self.col-1})else
  143. self:token("name",e,{t,l,self.line,self.col-1})end
  144. end
  145. function n:number()local t,l=self.line,self.col
  146. local e
  147. e=self:match("0[xX]%x+%.?%x*[eEpP][+-]?%d+")e=e or self:match("0[xX]%x+%.?%x*")e=e or self:match("0[xX]%x*%.?%x+[eEpP][+-]?%d+")e=e or self:match("0[xX]%x*%.?%x+")e=e or self:match("%d+%.?%d*[eE][+-]?%d+")e=e or self:match("%d+%.?%d*")e=e or self:match("%d*%.?%d+[eE][+-]?%d+")e=e or self:match("%d*%.?%d+")if n.isnamehead(self:peek())then
  148. e=nil
  149. end
  150. if e then
  151. self:token("number",tonumber(e),{t,l,self.line,self.col-1})else
  152. self:error("malformed number",{t,l,t,l})end
  153. end
  154. function n:comment()self.broken=true
  155. local e,t=self.line,self.col
  156. self:next()self:next()local o,l=self:match("%[(=*)%[")if not l then
  157. self:match("[^\r\n]*")self:pushcolor("comment",{e,t,self.line,self.col})return
  158. else
  159. local n=self:match("(.-)%]"..l.."%]")self:pushcolor("comment",{e,t,self.line,self.col})if n then return end
  160. end
  161. self:error("unfinished long comment",{e,t,e,t+3+#l})end
  162. n.CESCAPES={["a"]="\a",["b"]="\b",["f"]="\f",["n"]="\n",["r"]="\r",["t"]="\t",["v"]="\v",["\\"]="\\",['"']='"',["'"]="'",}function n:longstring()local n,e=self.line,self.col
  163. local l,t=self:match("%[(=*)%[")local o,l=self:match("(.-)%]"..t.."%]")if l then
  164. self:token("string",l,{n,e,self.line,self.col})else
  165. self:error("unfinished long string",{n,e,n,e+1+#t})end
  166. end
  167. function n:string()local l,t=self.line,self.col
  168. local e=1
  169. local d=self:next()local r=false
  170. local i=""while true do
  171. local o=self:peek()if o==""then
  172. self:error(("`%s` expected to close the string, found EOF"):format(d),{l,t+e,l,t})end
  173. if not r then
  174. if o=="\\"then
  175. r=true
  176. elseif o==d then
  177. self:next()self:token("string",i,{l,t,self.line,self.col-1})return
  178. elseif o=="\n"or o=="\r"then
  179. self:error(("`%s` expected to close the string, found EOL"):format(d),{l,t+e,l,t+e})else
  180. i=i..o
  181. end
  182. elseif n.CESCAPES[o]then
  183. i=i..n.CESCAPES[o]r=false
  184. elseif o=="z"then
  185. self:next()e=e+1
  186. while n.WHITESPACE[self:peek()]do
  187. self:next()e=e+1
  188. end
  189. r=false
  190. goto continue
  191. elseif o=="x"then
  192. self:next()e=e+1
  193. local n=self:match("%x%x")if not n then
  194. self:error("invalid hexadecimal escape sequence",{l,t+e-2,l,t+e-1})end
  195. e=e+2
  196. i=i..string.char(tonumber(n,16))r=false
  197. goto continue
  198. elseif n.isdigit(o)then
  199. local n=self:match("%d%d?%d?")if tonumber(n)>255 then
  200. self:error("invalid decimal escape sequence (too large)",{l,t+e-1,l,t+e+#n-1})end
  201. e=e+#n
  202. i=i..string.char(tonumber(n))r=false
  203. goto continue
  204. elseif o=="u"then
  205. self:next()e=e+1
  206. local n,o=self:match("{(%x+)}")if not n then
  207. self:error("invalid UTF-8 escape sequence",{l,t+e-2,l,t+e-1})end
  208. if not pcall(utf8.char,tonumber(o,16))then
  209. self:error("invalid UTF-8 escape sequence (out of range)",{l,t+e-2,l,t+e+#n-1})end
  210. e=e+#n
  211. i=i..utf8.char(tonumber(o,16))r=false
  212. goto continue
  213. else
  214. self:error("invalid escape sequence",{l,t+e-1,l,t+e-1})end
  215. self:next()e=e+1::continue::end
  216. end
  217. function n:lex()local e=coroutine.create(function()return self:lexc()end)local n=false
  218. return function()local e={coroutine.resume(e)}if not n and table.remove(e,1)then
  219. if e[1]=="error"then
  220. n=true
  221. end
  222. return e[1],e[2],e[3]end
  223. end
  224. end
  225. local e=c("Parser")function e:init(e)self.l=e
  226. self.tokens=self.l:lex()self.saved={}self.pos=1
  227. end
  228. function e:peek()if not self.saved[self.pos]then
  229. self.saved[self.pos]={self.tokens()}end
  230. return table.unpack(self.saved[self.pos])end
  231. function e:next()local e={self:peek()}if self.saving then
  232. self.pos=self.pos+1
  233. self.previous=self.saved[self.pos-2]else
  234. self.previous=table.remove(self.saved,self.pos)end
  235. return table.unpack(e)end
  236. function e:save()self.saving=true
  237. end
  238. function e:restore()self.saving=false
  239. self.pos=1
  240. end
  241. function e:commit()self.saved={self.saved[self.pos]}self.saving=false
  242. self.pos=1
  243. end
  244. function e.node(e,n,t)return{ty=e,data=n,slice=t}end
  245. local function t(e)return e:gsub("\\","\\\\"):gsub("\n","\\n"):gsub("\r","\\r"):gsub("'","\\'"):gsub("\t","\\t"):gsub("\v","\\v"):gsub("\f","\\f"):gsub("\a","\\a"):gsub("\b","\\b")end
  246. function e:showNode(o)local function n(e)if e.ty=="name"then
  247. return e.data
  248. elseif e.ty=="number"then
  249. return tostring(e.data)elseif e.ty=="nil"then
  250. return"nil"elseif e.ty=="boolean"then
  251. return tostring(e.data)elseif e.ty=="string"then
  252. return("('%s')"):format(t(e.data))elseif e.ty=="dots"then
  253. return"..."elseif e.ty=="binop"then
  254. return"("..n(e.data.lhs).." "..e.data.op.." "..n(e.data.rhs)..")"elseif e.ty=="unop"then
  255. return e.data.op.." "..n(e.data.node)elseif e.ty=="index"then
  256. return n(e.data.indexing).."["..n(e.data.index).."]"elseif e.ty=="call"then
  257. local t=n(e.data.callable).."("for l,e in ipairs(e.data.args)do
  258. if l>1 then
  259. t=t..", "end
  260. t=t..n(e)end
  261. return t..")"elseif e.ty=="methodcall"then
  262. local t=n(e.data.callable)..":"..n(e.data.method).."("for l,e in ipairs(e.data.args)do
  263. if l>1 then
  264. t=t..", "end
  265. t=t..n(e)end
  266. return t..")"elseif e.ty=="table"then
  267. local t="{"local l={}for e,l in ipairs(e.data.seq)do
  268. if e>1 then
  269. t=t..", "end
  270. t=t..n(l)end
  271. for e,l in pairs(e.data.map)do
  272. t=t.."["..n(e).."] = "..n(l)..", "end
  273. return t.."}"elseif e.ty=="emptystat"then
  274. return";"elseif e.ty=="do"then
  275. local t="do"for l,e in ipairs(e.data)do
  276. t=t.."\n"..n(e)end
  277. return t.."\nend"elseif e.ty=="if"then
  278. local t="if "..n(e.data.cond).." then"for l,e in ipairs(e.data.block)do
  279. t=t.."\n"..n(e)end
  280. for l,e in ipairs(e.data.pelseif)do
  281. t=t.."\nelseif "..n(e.cond).." then"for l,e in ipairs(e.block)do
  282. t=t.."\n"..n(e)end
  283. end
  284. if e.data.pelse then
  285. t=t.."\nelse"for l,e in ipairs(e.data.pelse)do
  286. t=t.."\n"..n(e)end
  287. end
  288. return t.."\nend"elseif e.ty=="while"then
  289. local t="while "..n(e.data.cond).." do"for l,e in ipairs(e.data.block)do
  290. t=t.."\n"..n(e)end
  291. return t.."\nend"elseif e.ty=="repeat"then
  292. local t="repeat"for l,e in ipairs(e.data.block)do
  293. t=t.."\n"..n(e)end
  294. return t.."\nuntil "..n(e.data.cond)elseif e.ty=="goto"then
  295. return"goto "..n(e.data)elseif e.ty=="label"then
  296. return"::"..n(e.data).."::"elseif e.ty=="break"then
  297. return"break"elseif e.ty=="return"then
  298. local t="return "for e,l in ipairs(e.data)do
  299. if e>1 then
  300. t=t..", "end
  301. t=t..n(l)end
  302. return t
  303. elseif e.ty=="for"then
  304. local t="for "if e.data.counter then
  305. t=t..n(e.data.counter).." = "..n(e.data.init)..", "..n(e.data.limit)..(e.data.step
  306. and(", "..n(e.data.step))or"")else
  307. for e,l in ipairs(e.data.names)do
  308. if e>1 then
  309. t=t..", "end
  310. t=t..n(l)end
  311. t=t.." in "for l,e in ipairs(e.data.exps)do
  312. if l>1 then
  313. t=t..", "end
  314. t=t..n(e)end
  315. end
  316. t=t.." do"for l,e in ipairs(e.data.block)do
  317. t=t.."\n"..n(e)end
  318. return t.."\nend"elseif e.ty=="assign"then
  319. local t=""for e,l in ipairs(e.data.vars)do
  320. if e>1 then
  321. t=t..", "end
  322. t=t..n(l)end
  323. t=t.." = "for l,e in ipairs(e.data.exps)do
  324. if l>1 then
  325. t=t..", "end
  326. t=t..n(e)end
  327. return t
  328. elseif e.ty=="decl"then
  329. local t="local "for e,l in ipairs(e.data.names)do
  330. if e>1 then
  331. t=t..", "end
  332. t=t..n(l)end
  333. if e.data.exps then
  334. t=t.." = "for l,e in ipairs(e.data.exps)do
  335. if l>1 then
  336. t=t..", "end
  337. t=t..n(e)end
  338. end
  339. return t
  340. elseif e.ty=="funcbody"then
  341. local t="function("for l,e in ipairs(e.data.pars)do
  342. if l>1 then
  343. t=t..", "end
  344. t=t..n(e)end
  345. t=t..")"for l,e in ipairs(e.data.block)do
  346. t=t.."\n"..n(e)end
  347. return t.."\nend"end
  348. end
  349. print(n(o))end
  350. function e:expected(l,t,e,o)if e=="error"then
  351. return nil,o,t
  352. end
  353. if n.KEYWORDS[e]then
  354. return nil,("%s expected, found keyword `%s`"):format(l,e),t
  355. end
  356. if n.OPS[3][e]then
  357. return nil,("%s expected, found `%s`"):format(l,e),t
  358. end
  359. if e=="number"then
  360. return nil,("%s expected, found number literal"):format(l),t
  361. end
  362. if e=="string"then
  363. return nil,("%s expected, found string literal"):format(l),t
  364. end
  365. if e=="EOF"then
  366. return nil,("%s expected, found EOF"):format(l),t
  367. end
  368. if e=="name"then
  369. return nil,("%s expected, found identifier"):format(l),t
  370. end
  371. return nil,("%s expected"):format(l),t
  372. end
  373. function e:unexpected(n,e)return nil,("unexpected %s"):format(n),e.slice
  374. end
  375. function e:name()local l,i=self.l.line,self.l.col
  376. local n,o,t=self:peek()if n and n=="name"then
  377. self:next()return e.node(n,o,t)else
  378. return self:expected("name",t or{l,i,l,i},n,o)end
  379. end
  380. function e:number()local t,l=self.l.line,self.l.col
  381. local n,o,i=self:peek()if n and n=="number"then
  382. self:next()return e.node(n,o,i)else
  383. return self:expected("number",i or{t,l,t,l},n,o)end
  384. end
  385. function e:string()local i,o=self.l.line,self.l.col
  386. local n,l,t=self:peek()if n and n=="string"then
  387. self:next()return e.node(n,l,t)else
  388. return self:expected("string",t or{i,o,i,o},n,l)end
  389. end
  390. function e:litntf()local l,o=self.l.line,self.l.col
  391. local n,i,t=self:peek()if n and n=="nil"then
  392. self:next()return e.node(n,i,t)elseif n and(n=="true"or n=="false")then
  393. self:next()return e.node("boolean",n=="true",t)else
  394. return self:expected("expression",t or{l,o,l,o},n,i)end
  395. end
  396. function e:expr()return self:l0()end
  397. function e:explist()local e,t,n=self:expr()if not e then return nil,t,n end
  398. local e={e}while true do
  399. if self:peek()==","then
  400. self:next()else
  401. break
  402. end
  403. local n,t,l=self:expr()if not n then return nil,t,l end
  404. table.insert(e,n)end
  405. return e
  406. end
  407. function e:namelist()local e,n,t=self:name()if not e then return nil,n,t end
  408. local e={e}while true do
  409. if self:peek()==","then
  410. self:save()self:next()else
  411. break
  412. end
  413. local n,t,t=self:name()if not n then
  414. self:restore()return e
  415. end
  416. self:commit()table.insert(e,n)end
  417. return e
  418. end
  419. function e:varlist()local e,n,t=self:var()if not e then return nil,n,t end
  420. local e={e}while true do
  421. if self:peek()==","then
  422. self:next()else
  423. break
  424. end
  425. local n,l,t=self:var()if not n then return nil,l,t end
  426. table.insert(e,n)end
  427. return e
  428. end
  429. function e:table()local t,l,n=self:peek()if t~="{"then
  430. return self:expected("`{`",n or{startLine,startCol,startLine,startCol},t,l)end
  431. self:next()local s,a=n[1],n[2]local t,l=s,a
  432. local c={map={},seq={}}local d=true
  433. while true do
  434. if d and self:peek()=="}"then
  435. break
  436. end
  437. if self:peek()=="error"then
  438. local t,e,n=self:peek()return nil,e,n
  439. end
  440. if not d then
  441. local e,n,n=self:peek()if e~=","and e~=";"then
  442. break
  443. else
  444. self:next()end
  445. end
  446. if self:peek()=="}"then
  447. break
  448. end
  449. local n,e,i,o,a,r
  450. e,i,o=self:expr()if self:peek()=="error"then
  451. local t,n,e=self:peek()return nil,n,e
  452. end
  453. if self:peek()=="["then
  454. self:next()n,i,o=self:expr()if not n then return nil,i,o end
  455. local e,t,n=self:next()if e~="]"then
  456. return self:expected("`]`",n or{self.l.line,self.l.col,self.l.line,self.l.col},e,t)end
  457. a=true
  458. elseif e then
  459. n=e
  460. e=nil
  461. r=true
  462. else
  463. return nil,i,o
  464. end
  465. if a or self:peek()=="="then
  466. if r and n.ty~="name"then
  467. return self:expected("table index",n.slice,"expression")end
  468. if r then
  469. n.ty="string"end
  470. local o,l,t=self:next()if o~="="then
  471. return self:expected("`=`",t or{self.l.line,self.l.col,self.l.line,self.l.col},o,l)end
  472. e,l,t=self:expr()if not e then return nil,l,t end
  473. c.map[n]=e
  474. else
  475. e=n
  476. table.insert(c.seq,e)end
  477. t,l=e.slice[3],e.slice[4]d=false
  478. end
  479. if self:peek()==","or self:peek()==";"then
  480. local e=select(3,self:peek())t,l=e[3],e[4]self:next()end
  481. local o,i,n=self:next()if o~="}"then
  482. return self:expected("`}`",n or{t,l+1,t,l+1},o,i)end
  483. return e.node("table",c,{s,a,n[3],n[4]})end
  484. function e:args()local n,t=self.l.line,self.l.col
  485. if self:peek()=="("then
  486. self:next()if self:peek()==")"then
  487. local e=select(3,self:next())return{},nil,{n,t,e[3],e[4]}end
  488. local e,l,o=self:explist()if not e then return nil,l,o end
  489. local l=e[#e].slice
  490. local i,r,o=self:next()if i~=")"then
  491. return self:expected("`)`",o or{l[3],l[4]+1,l[3],l[4]+1},i,r)end
  492. return e,nil,{n,t,o[3],o[4]}end
  493. local e,l
  494. e,_,l=self:string()if not e then
  495. e,_,l=self:table()end
  496. if e then
  497. return{e},nil,{n,t,e.slice[3],e.slice[4]}end
  498. return self:expected("function arguments",{n,t,n,t})end
  499. function e:prefixexp()local n
  500. if self:peek()=="("then
  501. self:next()local e,t,l=self:expr()if not e then return nil,t,l end
  502. local t,l,o=self:next()if t~=")"then
  503. return self:expected("`)`",o or{e.slice[3],e.slice[4]+1,e.slice[3],e.slice[4]+1},t,l)end
  504. n=e
  505. else
  506. n,_,slice=self:name()end
  507. if not n then
  508. return self:expected("expression",slice,select(1,self:peek()),select(2,self:peek()))end
  509. while true do
  510. local t=self:peek()if t=="."then
  511. self:next()local t,o,l=self:name()if not t then return o,l end
  512. t.ty="string"n=e.node("index",{indexing=n,index=t},{n.slice[1],n.slice[2],t.slice[3],t.slice[4]})elseif t=="["then
  513. local t,t
  514. self:next()local o,t,l=self:expr()if not o then return nil,t,l end
  515. local l,i,t=self:next()if l~="]"then
  516. return self:expected("a closing square bracket",t or{self.l.line,self.l.col,self.l.line,self.l.col},l,i)end
  517. n=e.node("index",{indexing=n,index=o},{n.slice[1],n.slice[2],t[3],t[4]})elseif t=="("or t=="{"or t=="string"then
  518. local l,o,t=self:args()if not l then return nil,o,t end
  519. n=e.node("call",{callable=n,args=l},{n.slice[1],n.slice[2],t[3],t[4]})elseif t==":"then
  520. self:next()local o,l,t=self:name()if not o then return l,t end
  521. local l,i,t=self:args()if not l then return nil,i,t end
  522. n=e.node("methodcall",{callable=n,method=o,args=l},{n.slice[1],n.slice[2],t[3],t[4]})else
  523. break
  524. end
  525. end
  526. return n
  527. end
  528. function e:var()local e,n,t=self:prefixexp()if not e then return nil,n,t end
  529. if e.ty=="call"or e.ty=="methodcall"then
  530. return nil,"function call is not a variable",e.slice
  531. end
  532. return e
  533. end
  534. function e:parlist()if self:peek()=="..."then
  535. local n=select(3,self:peek())self:next()return{e.node("dots",nil,n)}end
  536. local t,n,l=self:namelist()if not t then return nil,n,l end
  537. if self:peek()==","then
  538. local n=select(3,self:next())if self:peek()~="..."then
  539. return self:expected("expression or `...`",{n[1],n[2]+1,n[3],n[4]+1})end
  540. local n=select(3,self:next())table.insert(t,e.node("dots",nil,n))end
  541. return t
  542. end
  543. function e:funcbody(s)local t,l,n=self:peek()if t~="("then
  544. return self:expected("`(`",n or{self.l.line,self.l.col,self.l.line,self.l.col},t,l)end
  545. self:next()local r,c=n[1],n[2]local l,o=r,c
  546. local n=self:parlist()or{}l,o=n[#n]and n[#n].slice[3]or l,n[#n]and n[#n].slice[4]or o
  547. local t,d,i=self:next()if t~=")"then
  548. return self:expected("`)`",i or{self.l.line,self.l.col,self.l.line,self.l.col},t,d)end
  549. local t,d,i=self:block()if not t then return nil,d,i end
  550. l,o=t[#t]and t[#t].slice[3]or l,t[#t]and t[#t].slice[4]or o
  551. local d,a,i=self:peek()if d~="end"then
  552. return self:expected("`end`",i or{l,o+1,l,o+1},d,a)end
  553. self:next()if s then
  554. table.insert(n,1,e.node("name","self"))end
  555. return e.node("funcbody",{pars=n,block=t},{r,c,i[3],i[4]})end
  556. function e:functiondef()local e,t,n=self:peek()if e~="function"then
  557. return self:expected("`function`",n or{self.l.line,self.l.col,self.l.line,self.l.col},e,t)end
  558. self:next()return self:funcbody()end
  559. function e:term()local n,n,n,n,n
  560. if not self:peek()then
  561. local e=self.previous and self.previous[3]return self:expected("expression",e and{e[3],e[4]+1,e[3],e[4]+1}or{self.l.line,self.l.col,self.l.line,self.l.col},"EOF")end
  562. if self:peek()=="error"then
  563. local e={self:peek()}e[1]=nil
  564. return table.unpack(e)end
  565. local n=self:peek()if n=="number"then
  566. return self:number()elseif n=="nil"or n=="true"or n=="false"then
  567. return self:litntf()elseif n=="string"then
  568. return self:string()elseif n=="{"then
  569. return self:table()elseif n=="function"then
  570. return self:functiondef()elseif n=="..."then
  571. return e.node("dots",nil,select(3,self:next()))else
  572. return self:prefixexp()end
  573. end
  574. function e.lassoc(o,...)local i=d(...)return function(n)local d,r=n.l.line,n.l.col
  575. local t,l,c=o(n)if not t then
  576. return nil,l,c
  577. end
  578. local t=t
  579. while true do
  580. local l=n:peek()if i[l]then
  581. n:next()local n,i,o=o(n)if not n then
  582. return nil,i,o
  583. end
  584. t=e.node("binop",{op=l,lhs=t,rhs=n},{d,r,n.slice[3],n.slice[4]})else
  585. break
  586. end
  587. end
  588. return t
  589. end
  590. end
  591. function e.rassoc(n,...)local r=d(...)local function o(t)local n,l,i=n(t)if not n then
  592. return n,l,i
  593. end
  594. local l=t:peek()if not r[l]then
  595. return n
  596. end
  597. t:next()local t=o(t)if t then
  598. return e.node("binop",{op=l,lhs=n,rhs=t},{n.slice[1],n.slice[2],t.slice[3],t.slice[4]})else
  599. return n
  600. end
  601. end
  602. return o
  603. end
  604. function e.unop(o,...)local i=d(...)local function t(n)local d,r=n.l.line,n.l.col
  605. local l=n:peek()if i[l]then
  606. n:next()else
  607. return o(n)end
  608. local t,o,i=t(n)if not t then return nil,o,i end
  609. return e.node("unop",{op=l,node=t},{d,r,n.l.line,n.l.col-1})end
  610. return t
  611. end
  612. e.l11=e.rassoc(e.term,"^")e.l10=e.unop(e.l11,"not","#","-","~")e.l9=e.lassoc(e.l10,"*","/","//","%")e.l8=e.lassoc(e.l9,"+","-")e.l7=e.rassoc(e.l8,"..")e.l6=e.lassoc(e.l7,"<<",">>")e.l5=e.lassoc(e.l6,"&")e.l4=e.lassoc(e.l5,"~")e.l3=e.lassoc(e.l4,"|")e.l2=e.lassoc(e.l3,"<",">","<=",">=","~=","==")e.l1=e.lassoc(e.l2,"and")e.l0=e.lassoc(e.l1,"or")function e:block()local n={}local e,t,l
  613. while true do
  614. e,t,l=self:stat()if t then return nil,t,l end
  615. if not e then break end
  616. table.insert(n,e)end
  617. if self:peek()and self:peek()=="return"then
  618. table.insert(n,self:returnstat())end
  619. local e,l,t=self:peek()if e and e~="end"and e~="until"and e~="else"and e~="elseif"then
  620. return self:expected("statement",t,e,l)end
  621. return n,l,t
  622. end
  623. function e:funcname()local n,t,l=self:name()if not n then return nil,t,l end
  624. local l
  625. while true do
  626. if self:peek()~="."then
  627. break
  628. end
  629. self:next()local t,o,l=self:name()if not t then return nil,o,l end
  630. t.ty="string"n=e.node("index",{indexing=n,index=t},{n.slice[1],n.slice[2],t.slice[3],t.slice[4]})end
  631. if self:peek()==":"then
  632. self:next()local t,o,i=self:name()if not t then return nil,o,i end
  633. t.ty="string"n=e.node("index",{indexing=n,index=t},{n.slice[1],n.slice[2],t.slice[3],t.slice[4]})l=true
  634. end
  635. return n,l
  636. end
  637. function e:stat()if self.savedstat then
  638. local e=self.savedstat
  639. self.savedstat=nil
  640. return e
  641. end
  642. local n,l,t=self:peek()if not n then
  643. return
  644. end
  645. if n=="error"then
  646. return nil,l,t
  647. end
  648. if n=="return"then return end
  649. if n==";"then
  650. local n=select(3,self:peek())self:next()return e.node("emptystat",nil,n)elseif n=="do"then
  651. return self:dostat()elseif n=="while"then
  652. return self:whilestat()elseif n=="repeat"then
  653. return self:repeatstat()elseif n=="if"then
  654. return self:ifstat()elseif n=="goto"then
  655. return self:gotostat()elseif n=="::"then
  656. return self:labelstat()elseif n=="break"then
  657. local n=select(3,self:peek())self:next()return e.node("break",nil,n)elseif n=="for"then
  658. return self:forstat()end
  659. if n=="local"then
  660. self:next()local n=self:namelist()if n and self:peek()=="="then
  661. self:next()local l,o,i=self:explist()if not l then return nil,o,i end
  662. return e.node("decl",{names=n,exps=l},{t[1],t[2],l[#l].slice[3],l[#l].slice[4]})elseif n then
  663. return e.node("decl",{names=n},{t[1],t[2],n[#n].slice[3],n[#n].slice[4]})elseif self:peek()=="function"then
  664. self:next()local n,o,l=self:name()if not n then return nil,o,l end
  665. local l,o,i=self:funcbody()if not l then return nil,o,i end
  666. self.savedstat=e.node("assign",{vars={n},exps={l}},{t[1],t[2],l.slice[3],l.slice[4]})return e.node("decl",{names={n}},{t[1],t[2],n.slice[3],n.slice[4]})end
  667. end
  668. if n=="function"then
  669. self:next()local l,n,o=self:funcname()if not l then return nil,n,o end
  670. local n,o,i=self:funcbody(n)if not n then return nil,o,i end
  671. return e.node("assign",{vars={l},exps={n}},{t[1],t[2],n.slice[3],n.slice[4]})end
  672. local t,n,n=self:prefixexp()if t and(t.ty=="methodcall"or t.ty=="call")then
  673. return t
  674. end
  675. local n={}if t then
  676. table.insert(n,t)end
  677. if self:peek()==","then
  678. self:next()local e,l,t=self:varlist()if not e then return nil,l,t end
  679. for t,e in ipairs(e)do
  680. table.insert(n,e)end
  681. end
  682. if#n>0 then
  683. local t,l,o=self:next()if t~="="then
  684. return self:expected("`=`",o or{n[#n].slice[3],n[#n].slice[4]+1,n[#n].slice[3],n[#n].slice[4]+1},t,l)end
  685. local t,o,l=self:explist()if not t then return nil,o,l end
  686. return e.node("assign",{vars=n,exps=t},{n[1].slice[1],n[1].slice[2],t[#t].slice[3],t[#t].slice[4]})end
  687. end
  688. function e:dostat()local n,t,l=self:next()if n~="do"then
  689. return self:expected("`do`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,t)end
  690. local n,t,o=self:block()if not n then return nil,t,o end
  691. local o,i,t=self:next()if o~="end"then
  692. return self:expected("`end`",t or{n[#n].slice[3],n[#n].slice[4]+1,n[#n].slice[3],n[#n].slice[4]+1},o,i)end
  693. return e.node("do",n,{l[1],l[2],t[3],t[4]})end
  694. function e:whilestat()local n,t,l=self:next()if n~="while"then
  695. return self:expected("`while`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,t)end
  696. local o,t,n=self:expr()if not o then return nil,t,n end
  697. local n,i,t=self:next()if n~="do"then
  698. return self:expected("`do`",t or{self.l.line,self.l.col,self.l.line,self.l.col},n,i)end
  699. local n,t,i=self:block()if not n then return nil,t,i end
  700. local i,r,t=self:next()if i~="end"then
  701. return self:expected("`end`",t or{n[#n].slice[3],n[#n].slice[4]+1,n[#n].slice[3],n[#n].slice[4]+1},i,r)end
  702. return e.node("while",{cond=o,block=n},{l[1],l[2],t[3],t[4]})end
  703. function e:repeatstat()local n,t,l=self:next()if n~="repeat"then
  704. return self:expected("`repeat`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,t)end
  705. local n,o,t=self:block()if not n then return nil,o,t end
  706. local t,i,o=self:next()if t~="until"then
  707. return self:expected("`until`",o or{n[#n].slice[3],n[#n].slice[4]+1,n[#n].slice[3],n[#n].slice[4]+1},t,i)end
  708. local t,o,i=self:expr()if not t then return nil,o,i end
  709. return e.node("repeat",{cond=t,block=n},{l[1],l[2],t.slice[3],t.slice[4]})end
  710. function e:ifstat()local t,l
  711. local n,o,a=self:next()if n~="if"then
  712. return self:expected("`if`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,o)end
  713. local i,n,o=self:expr()if not i then return nil,n,o end
  714. local o,r,n=self:next()if o~="then"then
  715. return self:expected("`then`",n or{i.slice[3],i.slice[4]+1,i.slice[3],i.slice[4]+1},o,r)end
  716. t,l=n[3],n[4]local o,r,n=self:block()if not o then return nil,r,n end
  717. local c={}local n
  718. if#o>0 then
  719. t,l=o[#o].slice[3],o[#o].slice[4]end
  720. while true do
  721. local e,n,n=self:peek()if e=="elseif"then
  722. self:next()else
  723. break
  724. end
  725. local n,e,o=self:expr()if not n then return nil,e,o end
  726. local o,i,e=self:next()if o~="then"then
  727. return self:expected("`then`",e or{n.slice[3],n.slice[4]+1,n.slice[3],n.slice[4]+1},o,i)end
  728. t,l=e[3],e[4]local e,i,o=self:block()if not e then return nil,i,o end
  729. if#e>0 then
  730. t,l=e[#e].slice[3],e[#e].slice[4]end
  731. table.insert(c,{cond=n,block=e})end
  732. if self:peek()=="else"then
  733. local e=select(3,self:peek())t,l=e[3],e[4]self:next()n,r,e=self:block()if not n then return nil,r,e end
  734. if#n>0 then
  735. t,l=n[#n].slice[3],n[#n].slice[4]end
  736. end
  737. local d,s,r=self:next()if d~="end"then
  738. return self:expected("`end`",r or{t,l+1,t,l+1},d,s)end
  739. return e.node("if",{cond=i,block=o,pelseif=c,pelse=n},{a[1],a[2],r[3],r[4]})end
  740. function e:gotostat()local n,l,t=self:next()if n~="goto"then
  741. return self:expected("`goto`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,l)end
  742. local n,o,l=self:name()if not n then return nil,o,l end
  743. return e.node("goto",n,{t[1],t[2],n.slice[3],n.slice[4]})end
  744. function e:labelstat()local n,t,o=self:next()if n~="::"then
  745. return self:expected("`::`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,t)end
  746. local l,i,t=self:name()if not l then return nil,i,t end
  747. local i,r,t=self:next()if i~="::"then
  748. return self:expected("`::`",t or{self.l.line,self.l.col,self.l.line,self.l.col},n,r)end
  749. return e.node("label",l,{o[1],o[2],t[3],t[4]})end
  750. function e:returnstat()local n,l,t=self:peek()if n~="return"then
  751. return self:expected("`return`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,l)end
  752. self:next()local n=self:explist()if not n then
  753. n={}end
  754. local l=n[#n]and n[#n].slice or t
  755. return e.node("return",n,{t[1],t[2],l[3],l[4]})end
  756. function e:forstat()local n,t,c=self:next()if n~="for"then
  757. return self:expected("`for`",slice or{self.l.line,self.l.col,self.l.line,self.l.col},n,t)end
  758. local l,n,t=self:name()if not l then return nil,n,t end
  759. local d,r,n,t,i
  760. if self:peek()=="="then
  761. local e=select(3,self:peek())self:next()d,r,step,other=table.unpack(self:explist())if not d then
  762. return self:expected("expression (for-loop initializer)",{e[3],e[4]+1,e[3],e[4]+1})end
  763. if not r then
  764. return self:expected("expression (for-loop limit)",{e[3],e[4]+1,e[3],e[4]+1})end
  765. if other then
  766. return self:unexpected("expression",other)end
  767. else
  768. local n,e
  769. t={}if self:peek()==","then
  770. self:next()t,n,e=self:namelist()if not t then return nil,n,e end
  771. end
  772. table.insert(t,1,l)local t,n,e=self:next()if t~="in"then
  773. return self:expected("`in`",e or{self.l.line,self.l.col,self.l.line,self.l.col},t,n)end
  774. i,n,e=self:explist()if not i then return nil,n,e end
  775. l=nil
  776. end
  777. local n,a,o=self:next()if n~="do"then
  778. return self:expected("`do`",o or{self.l.line,self.l.col,self.l.line,self.l.col},n,a)end
  779. local n,o,a=self:block()if not n then return nil,o,a end
  780. local a,s,o=self:next()if a~="end"then
  781. return self:expected("`end`",o or{n[#n].slice[3],n[#n].slice[4]+1,n[#n].slice[3],n[#n].slice[4]+1},a,s)end
  782. return e.node("for",{counter=l,names=t,exps=i,block=n,init=d,limit=r,step=step},{c[1],c[2],o[3],o[4]})end
  783. return{Lexer=n,Parser=e}
Advertisement
Add Comment
Please, Sign In to add comment