PostgreSQL native library for Tarantool programming language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpeg.lua 87KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. -- LuLPeg, a pure Lua port of LPeg, Roberto Ierusalimschy's
  2. -- Parsing Expression Grammars library.
  3. --
  4. -- Copyright (C) Pierre-Yves Gerardy.
  5. -- Released under the Romantic WTF Public License (cf. the LICENSE
  6. -- file or the end of this file, whichever is present).
  7. --
  8. -- See http://www.inf.puc-rio.br/~roberto/lpeg/ for the original.
  9. --
  10. -- The re.lua module and the test suite (tests/lpeg.*.*.tests.lua)
  11. -- are part of the original LPeg distribution.
  12. local _ENV, loaded, packages, release, require_
  13. = _ENV or _G, {}, {}, true, require
  14. local function require(...)
  15. local lib = ...
  16. -- is it a private file?
  17. if loaded[lib] then
  18. return loaded[lib]
  19. elseif packages[lib] then
  20. loaded[lib] = packages[lib](lib)
  21. return loaded[lib]
  22. else
  23. return require_(lib)
  24. end
  25. end
  26. --=============================================================================
  27. do local _ENV = _ENV
  28. packages['analizer'] = function (...)
  29. local u = require"util"
  30. local nop, weakkey = u.nop, u.weakkey
  31. local hasVcache, hasCmtcache , lengthcache
  32. = weakkey{}, weakkey{}, weakkey{}
  33. return {
  34. hasV = nop,
  35. hasCmt = nop,
  36. length = nop,
  37. hasCapture = nop
  38. }
  39. end
  40. end
  41. --=============================================================================
  42. do local _ENV = _ENV
  43. packages['compiler'] = function (...)
  44. local assert, error, pairs, print, rawset, select, setmetatable, tostring, type
  45. = assert, error, pairs, print, rawset, select, setmetatable, tostring, type
  46. local s, t, u = require"string", require"table", require"util"
  47. local _ENV = u.noglobals() ----------------------------------------------------
  48. local s_byte, s_sub, t_concat, t_insert, t_remove, t_unpack
  49. = s.byte, s.sub, t.concat, t.insert, t.remove, u.unpack
  50. local load, map, map_all, t_pack
  51. = u.load, u.map, u.map_all, u.pack
  52. local expose = u.expose
  53. return function(Builder, LL)
  54. local evaluate, LL_ispattern = LL.evaluate, LL.ispattern
  55. local charset = Builder.charset
  56. local compilers = {}
  57. local
  58. function compile(pt, ccache)
  59. if not LL_ispattern(pt) then
  60. error("pattern expected")
  61. end
  62. local typ = pt.pkind
  63. if typ == "grammar" then
  64. ccache = {}
  65. elseif typ == "ref" or typ == "choice" or typ == "sequence" then
  66. if not ccache[pt] then
  67. ccache[pt] = compilers[typ](pt, ccache)
  68. end
  69. return ccache[pt]
  70. end
  71. if not pt.compiled then
  72. pt.compiled = compilers[pt.pkind](pt, ccache)
  73. end
  74. return pt.compiled
  75. end
  76. LL.compile = compile
  77. local
  78. function clear_captures(ary, ci)
  79. for i = ci, #ary do ary[i] = nil end
  80. end
  81. local LL_compile, LL_evaluate, LL_P
  82. = LL.compile, LL.evaluate, LL.P
  83. local function computeidex(i, len)
  84. if i == 0 or i == 1 or i == nil then return 1
  85. elseif type(i) ~= "number" then error"number or nil expected for the stating index"
  86. elseif i > 0 then return i > len and len + 1 or i
  87. else return len + i < 0 and 1 or len + i + 1
  88. end
  89. end
  90. local function newcaps()
  91. return {
  92. kind = {},
  93. bounds = {},
  94. openclose = {},
  95. aux = -- [[DBG]] dbgcaps
  96. {}
  97. }
  98. end
  99. local
  100. function _match(dbg, pt, sbj, si, ...)
  101. if dbg then -------------
  102. print("@!!! Match !!!@", pt)
  103. end ---------------------
  104. pt = LL_P(pt)
  105. assert(type(sbj) == "string", "string expected for the match subject")
  106. si = computeidex(si, #sbj)
  107. if dbg then -------------
  108. print(("-"):rep(30))
  109. print(pt.pkind)
  110. LL.pprint(pt)
  111. end ---------------------
  112. local matcher = compile(pt, {})
  113. local caps = newcaps()
  114. local matcher_state = {grammars = {}, args = {n = select('#',...),...}, tags = {}}
  115. local success, final_si, ci = matcher(sbj, si, caps, 1, matcher_state)
  116. if dbg then -------------
  117. print("!!! Done Matching !!! success: ", success,
  118. "final position", final_si, "final cap index", ci,
  119. "#caps", #caps.openclose)
  120. end----------------------
  121. if success then
  122. clear_captures(caps.kind, ci)
  123. clear_captures(caps.aux, ci)
  124. if dbg then -------------
  125. print("trimmed cap index = ", #caps + 1)
  126. LL.cprint(caps, sbj, 1)
  127. end ---------------------
  128. local values, _, vi = LL_evaluate(caps, sbj, 1, 1)
  129. if dbg then -------------
  130. print("#values", vi)
  131. expose(values)
  132. end ---------------------
  133. if vi == 0
  134. then return final_si
  135. else return t_unpack(values, 1, vi) end
  136. else
  137. if dbg then print("Failed") end
  138. return nil
  139. end
  140. end
  141. function LL.match(...)
  142. return _match(false, ...)
  143. end
  144. function LL.dmatch(...)
  145. return _match(true, ...)
  146. end
  147. for _, v in pairs{
  148. "C", "Cf", "Cg", "Cs", "Ct", "Clb",
  149. "div_string", "div_table", "div_number", "div_function"
  150. } do
  151. compilers[v] = load(([=[
  152. local compile, expose, type, LL = ...
  153. return function (pt, ccache)
  154. local matcher, this_aux = compile(pt.pattern, ccache), pt.aux
  155. return function (sbj, si, caps, ci, state)
  156. local ref_ci = ci
  157. local kind, bounds, openclose, aux
  158. = caps.kind, caps.bounds, caps.openclose, caps.aux
  159. kind [ci] = "XXXX"
  160. bounds [ci] = si
  161. openclose [ci] = 0
  162. caps.aux [ci] = (this_aux or false)
  163. local success
  164. success, si, ci
  165. = matcher(sbj, si, caps, ci + 1, state)
  166. if success then
  167. if ci == ref_ci + 1 then
  168. caps.openclose[ref_ci] = si
  169. else
  170. kind [ci] = "XXXX"
  171. bounds [ci] = si
  172. openclose [ci] = ref_ci - ci
  173. aux [ci] = this_aux or false
  174. ci = ci + 1
  175. end
  176. else
  177. ci = ci - 1
  178. end
  179. return success, si, ci
  180. end
  181. end]=]):gsub("XXXX", v), v.." compiler")(compile, expose, type, LL)
  182. end
  183. compilers["Carg"] = function (pt, ccache)
  184. local n = pt.aux
  185. return function (sbj, si, caps, ci, state)
  186. if state.args.n < n then error("reference to absent argument #"..n) end
  187. caps.kind [ci] = "value"
  188. caps.bounds [ci] = si
  189. if state.args[n] == nil then
  190. caps.openclose [ci] = 1/0
  191. caps.aux [ci] = 1/0
  192. else
  193. caps.openclose [ci] = si
  194. caps.aux [ci] = state.args[n]
  195. end
  196. return true, si, ci + 1
  197. end
  198. end
  199. for _, v in pairs{
  200. "Cb", "Cc", "Cp"
  201. } do
  202. compilers[v] = load(([=[
  203. return function (pt, ccache)
  204. local this_aux = pt.aux
  205. return function (sbj, si, caps, ci, state)
  206. caps.kind [ci] = "XXXX"
  207. caps.bounds [ci] = si
  208. caps.openclose [ci] = si
  209. caps.aux [ci] = this_aux or false
  210. return true, si, ci + 1
  211. end
  212. end]=]):gsub("XXXX", v), v.." compiler")(expose)
  213. end
  214. compilers["/zero"] = function (pt, ccache)
  215. local matcher = compile(pt.pattern, ccache)
  216. return function (sbj, si, caps, ci, state)
  217. local success, nsi = matcher(sbj, si, caps, ci, state)
  218. clear_captures(caps.aux, ci)
  219. return success, nsi, ci
  220. end
  221. end
  222. local function pack_Cmt_caps(i,...) return i, t_pack(...) end
  223. compilers["Cmt"] = function (pt, ccache)
  224. local matcher, func = compile(pt.pattern, ccache), pt.aux
  225. return function (sbj, si, caps, ci, state)
  226. local success, Cmt_si, Cmt_ci = matcher(sbj, si, caps, ci, state)
  227. if not success then
  228. clear_captures(caps.aux, ci)
  229. return false, si, ci
  230. end
  231. local final_si, values
  232. if Cmt_ci == ci then
  233. final_si, values = pack_Cmt_caps(
  234. func(sbj, Cmt_si, s_sub(sbj, si, Cmt_si - 1))
  235. )
  236. else
  237. clear_captures(caps.aux, Cmt_ci)
  238. clear_captures(caps.kind, Cmt_ci)
  239. local cps, _, nn = evaluate(caps, sbj, ci)
  240. final_si, values = pack_Cmt_caps(
  241. func(sbj, Cmt_si, t_unpack(cps, 1, nn))
  242. )
  243. end
  244. if not final_si then
  245. return false, si, ci
  246. end
  247. if final_si == true then final_si = Cmt_si end
  248. if type(final_si) == "number"
  249. and si <= final_si
  250. and final_si <= #sbj + 1
  251. then
  252. local kind, bounds, openclose, aux
  253. = caps.kind, caps.bounds, caps.openclose, caps.aux
  254. for i = 1, values.n do
  255. kind [ci] = "value"
  256. bounds [ci] = si
  257. if values[i] == nil then
  258. caps.openclose [ci] = 1/0
  259. caps.aux [ci] = 1/0
  260. else
  261. caps.openclose [ci] = final_si
  262. caps.aux [ci] = values[i]
  263. end
  264. ci = ci + 1
  265. end
  266. elseif type(final_si) == "number" then
  267. error"Index out of bounds returned by match-time capture."
  268. else
  269. error("Match time capture must return a number, a boolean or nil"
  270. .." as first argument, or nothing at all.")
  271. end
  272. return true, final_si, ci
  273. end
  274. end
  275. compilers["string"] = function (pt, ccache)
  276. local S = pt.aux
  277. local N = #S
  278. return function(sbj, si, caps, ci, state)
  279. local in_1 = si - 1
  280. for i = 1, N do
  281. local c
  282. c = s_byte(sbj,in_1 + i)
  283. if c ~= S[i] then
  284. return false, si, ci
  285. end
  286. end
  287. return true, si + N, ci
  288. end
  289. end
  290. compilers["char"] = function (pt, ccache)
  291. return load(([=[
  292. local s_byte, s_char = ...
  293. return function(sbj, si, caps, ci, state)
  294. local c, nsi = s_byte(sbj, si), si + 1
  295. if c ~= __C0__ then
  296. return false, si, ci
  297. end
  298. return true, nsi, ci
  299. end]=]):gsub("__C0__", tostring(pt.aux)))(s_byte, ("").char)
  300. end
  301. local
  302. function truecompiled (sbj, si, caps, ci, state)
  303. return true, si, ci
  304. end
  305. compilers["true"] = function (pt)
  306. return truecompiled
  307. end
  308. local
  309. function falsecompiled (sbj, si, caps, ci, state)
  310. return false, si, ci
  311. end
  312. compilers["false"] = function (pt)
  313. return falsecompiled
  314. end
  315. local
  316. function eoscompiled (sbj, si, caps, ci, state)
  317. return si > #sbj, si, ci
  318. end
  319. compilers["eos"] = function (pt)
  320. return eoscompiled
  321. end
  322. local
  323. function onecompiled (sbj, si, caps, ci, state)
  324. local char, _ = s_byte(sbj, si), si + 1
  325. if char
  326. then return true, si + 1, ci
  327. else return false, si, ci end
  328. end
  329. compilers["one"] = function (pt)
  330. return onecompiled
  331. end
  332. compilers["any"] = function (pt)
  333. local N = pt.aux
  334. if N == 1 then
  335. return onecompiled
  336. else
  337. N = pt.aux - 1
  338. return function (sbj, si, caps, ci, state)
  339. local n = si + N
  340. if n <= #sbj then
  341. return true, n + 1, ci
  342. else
  343. return false, si, ci
  344. end
  345. end
  346. end
  347. end
  348. do
  349. local function checkpatterns(g)
  350. for k,v in pairs(g.aux) do
  351. if not LL_ispattern(v) then
  352. error(("rule 'A' is not a pattern"):gsub("A", tostring(k)))
  353. end
  354. end
  355. end
  356. compilers["grammar"] = function (pt, ccache)
  357. checkpatterns(pt)
  358. local gram = map_all(pt.aux, compile, ccache)
  359. local start = gram[1]
  360. return function (sbj, si, caps, ci, state)
  361. t_insert(state.grammars, gram)
  362. local success, nsi, ci = start(sbj, si, caps, ci, state)
  363. t_remove(state.grammars)
  364. return success, nsi, ci
  365. end
  366. end
  367. end
  368. local dummy_acc = {kind={}, bounds={}, openclose={}, aux={}}
  369. compilers["behind"] = function (pt, ccache)
  370. local matcher, N = compile(pt.pattern, ccache), pt.aux
  371. return function (sbj, si, caps, ci, state)
  372. if si <= N then return false, si, ci end
  373. local success = matcher(sbj, si - N, dummy_acc, ci, state)
  374. dummy_acc.aux = {}
  375. return success, si, ci
  376. end
  377. end
  378. compilers["range"] = function (pt)
  379. local ranges = pt.aux
  380. return function (sbj, si, caps, ci, state)
  381. local char, nsi = s_byte(sbj, si), si + 1
  382. for i = 1, #ranges do
  383. local r = ranges[i]
  384. if char and r[char]
  385. then return true, nsi, ci end
  386. end
  387. return false, si, ci
  388. end
  389. end
  390. compilers["set"] = function (pt)
  391. local s = pt.aux
  392. return function (sbj, si, caps, ci, state)
  393. local char, nsi = s_byte(sbj, si), si + 1
  394. if s[char]
  395. then return true, nsi, ci
  396. else return false, si, ci end
  397. end
  398. end
  399. compilers["range"] = compilers.set
  400. compilers["ref"] = function (pt, ccache)
  401. local name = pt.aux
  402. local ref
  403. return function (sbj, si, caps, ci, state)
  404. if not ref then
  405. if #state.grammars == 0 then
  406. error(("rule 'XXXX' used outside a grammar"):gsub("XXXX", tostring(name)))
  407. elseif not state.grammars[#state.grammars][name] then
  408. error(("rule 'XXXX' undefined in given grammar"):gsub("XXXX", tostring(name)))
  409. end
  410. ref = state.grammars[#state.grammars][name]
  411. end
  412. local success, nsi, nci = ref(sbj, si, caps, ci, state)
  413. return success, nsi, nci
  414. end
  415. end
  416. local choice_tpl = [=[
  417. success, si, ci = XXXX(sbj, si, caps, ci, state)
  418. if success then
  419. return true, si, ci
  420. else
  421. end]=]
  422. local function flatten(kind, pt, ccache)
  423. if pt[2].pkind == kind then
  424. return compile(pt[1], ccache), flatten(kind, pt[2], ccache)
  425. else
  426. return compile(pt[1], ccache), compile(pt[2], ccache)
  427. end
  428. end
  429. compilers["choice"] = function (pt, ccache)
  430. local choices = {flatten("choice", pt, ccache)}
  431. local names, chunks = {}, {}
  432. for i = 1, #choices do
  433. local m = "ch"..i
  434. names[#names + 1] = m
  435. chunks[ #names ] = choice_tpl:gsub("XXXX", m)
  436. end
  437. names[#names + 1] = "clear_captures"
  438. choices[ #names ] = clear_captures
  439. local compiled = t_concat{
  440. "local ", t_concat(names, ", "), [=[ = ...
  441. return function (sbj, si, caps, ci, state)
  442. local aux, success = caps.aux, false
  443. ]=],
  444. t_concat(chunks,"\n"),[=[--
  445. return false, si, ci
  446. end]=]
  447. }
  448. return load(compiled, "Choice")(t_unpack(choices))
  449. end
  450. local sequence_tpl = [=[
  451. success, si, ci = XXXX(sbj, si, caps, ci, state)
  452. if not success then
  453. return false, ref_si, ref_ci
  454. end]=]
  455. compilers["sequence"] = function (pt, ccache)
  456. local sequence = {flatten("sequence", pt, ccache)}
  457. local names, chunks = {}, {}
  458. for i = 1, #sequence do
  459. local m = "seq"..i
  460. names[#names + 1] = m
  461. chunks[ #names ] = sequence_tpl:gsub("XXXX", m)
  462. end
  463. names[#names + 1] = "clear_captures"
  464. sequence[ #names ] = clear_captures
  465. local compiled = t_concat{
  466. "local ", t_concat(names, ", "), [=[ = ...
  467. return function (sbj, si, caps, ci, state)
  468. local ref_si, ref_ci, success = si, ci
  469. ]=],
  470. t_concat(chunks,"\n"),[=[
  471. return true, si, ci
  472. end]=]
  473. }
  474. return load(compiled, "Sequence")(t_unpack(sequence))
  475. end
  476. compilers["at most"] = function (pt, ccache)
  477. local matcher, n = compile(pt.pattern, ccache), pt.aux
  478. n = -n
  479. return function (sbj, si, caps, ci, state)
  480. local success = true
  481. for i = 1, n do
  482. success, si, ci = matcher(sbj, si, caps, ci, state)
  483. if not success then
  484. break
  485. end
  486. end
  487. return true, si, ci
  488. end
  489. end
  490. compilers["at least"] = function (pt, ccache)
  491. local matcher, n = compile(pt.pattern, ccache), pt.aux
  492. if n == 0 then
  493. return function (sbj, si, caps, ci, state)
  494. local last_si, last_ci
  495. while true do
  496. local success
  497. last_si, last_ci = si, ci
  498. success, si, ci = matcher(sbj, si, caps, ci, state)
  499. if not success then
  500. si, ci = last_si, last_ci
  501. break
  502. end
  503. end
  504. return true, si, ci
  505. end
  506. elseif n == 1 then
  507. return function (sbj, si, caps, ci, state)
  508. local last_si, last_ci
  509. local success = true
  510. success, si, ci = matcher(sbj, si, caps, ci, state)
  511. if not success then
  512. return false, si, ci
  513. end
  514. while true do
  515. local success
  516. last_si, last_ci = si, ci
  517. success, si, ci = matcher(sbj, si, caps, ci, state)
  518. if not success then
  519. si, ci = last_si, last_ci
  520. break
  521. end
  522. end
  523. return true, si, ci
  524. end
  525. else
  526. return function (sbj, si, caps, ci, state)
  527. local last_si, last_ci
  528. local success = true
  529. for _ = 1, n do
  530. success, si, ci = matcher(sbj, si, caps, ci, state)
  531. if not success then
  532. return false, si, ci
  533. end
  534. end
  535. while true do
  536. local success
  537. last_si, last_ci = si, ci
  538. success, si, ci = matcher(sbj, si, caps, ci, state)
  539. if not success then
  540. si, ci = last_si, last_ci
  541. break
  542. end
  543. end
  544. return true, si, ci
  545. end
  546. end
  547. end
  548. compilers["unm"] = function (pt, ccache)
  549. if pt.pkind == "any" and pt.aux == 1 then
  550. return eoscompiled
  551. end
  552. local matcher = compile(pt.pattern, ccache)
  553. return function (sbj, si, caps, ci, state)
  554. local success, _, _ = matcher(sbj, si, caps, ci, state)
  555. return not success, si, ci
  556. end
  557. end
  558. compilers["lookahead"] = function (pt, ccache)
  559. local matcher = compile(pt.pattern, ccache)
  560. return function (sbj, si, caps, ci, state)
  561. local success, _, _ = matcher(sbj, si, caps, ci, state)
  562. return success, si, ci
  563. end
  564. end
  565. end
  566. end
  567. end
  568. --=============================================================================
  569. do local _ENV = _ENV
  570. packages['datastructures'] = function (...)
  571. local getmetatable, pairs, setmetatable, type
  572. = getmetatable, pairs, setmetatable, type
  573. local m, t , u = require"math", require"table", require"util"
  574. local compat = require"compat"
  575. local ffi if compat.luajit then
  576. ffi = require"ffi"
  577. end
  578. local _ENV = u.noglobals() ----------------------------------------------------
  579. local extend, load, u_max
  580. = u.extend, u.load, u.max
  581. local m_max, t_concat, t_insert, t_sort
  582. = m.max, t.concat, t.insert, t.sort
  583. local structfor = {}
  584. local byteset_new, isboolset, isbyteset
  585. local byteset_mt = {}
  586. local
  587. function byteset_constructor (upper)
  588. local set = setmetatable(load(t_concat{
  589. "return{ [0]=false",
  590. (", false"):rep(upper),
  591. " }"
  592. })(),
  593. byteset_mt)
  594. return set
  595. end
  596. if compat.jit then
  597. local struct, boolset_constructor = {v={}}
  598. function byteset_mt.__index(s,i)
  599. if i == nil or i > s.upper then return nil end
  600. return s.v[i]
  601. end
  602. function byteset_mt.__len(s)
  603. return s.upper
  604. end
  605. function byteset_mt.__newindex(s,i,v)
  606. s.v[i] = v
  607. end
  608. boolset_constructor = ffi.metatype('struct { int upper; bool v[?]; }', byteset_mt)
  609. function byteset_new (t)
  610. if type(t) == "number" then
  611. local res = boolset_constructor(t+1)
  612. res.upper = t
  613. return res
  614. end
  615. local upper = u_max(t)
  616. struct.upper = upper
  617. if upper > 255 then error"bool_set overflow" end
  618. local set = boolset_constructor(upper+1)
  619. set.upper = upper
  620. for i = 1, #t do set[t[i]] = true end
  621. return set
  622. end
  623. function isboolset(s) return type(s)=="cdata" and ffi.istype(s, boolset_constructor) end
  624. isbyteset = isboolset
  625. else
  626. function byteset_new (t)
  627. if type(t) == "number" then return byteset_constructor(t) end
  628. local set = byteset_constructor(u_max(t))
  629. for i = 1, #t do set[t[i]] = true end
  630. return set
  631. end
  632. function isboolset(s) return false end
  633. function isbyteset (s)
  634. return getmetatable(s) == byteset_mt
  635. end
  636. end
  637. local
  638. function byterange_new (low, high)
  639. high = ( low <= high ) and high or -1
  640. local set = byteset_new(high)
  641. for i = low, high do
  642. set[i] = true
  643. end
  644. return set
  645. end
  646. local tmpa, tmpb ={}, {}
  647. local
  648. function set_if_not_yet (s, dest)
  649. if type(s) == "number" then
  650. dest[s] = true
  651. return dest
  652. else
  653. return s
  654. end
  655. end
  656. local
  657. function clean_ab (a,b)
  658. tmpa[a] = nil
  659. tmpb[b] = nil
  660. end
  661. local
  662. function byteset_union (a ,b)
  663. local upper = m_max(
  664. type(a) == "number" and a or #a,
  665. type(b) == "number" and b or #b
  666. )
  667. local A, B
  668. = set_if_not_yet(a, tmpa)
  669. , set_if_not_yet(b, tmpb)
  670. local res = byteset_new(upper)
  671. for i = 0, upper do
  672. res[i] = A[i] or B[i] or false
  673. end
  674. clean_ab(a,b)
  675. return res
  676. end
  677. local
  678. function byteset_difference (a, b)
  679. local res = {}
  680. for i = 0, 255 do
  681. res[i] = a[i] and not b[i]
  682. end
  683. return res
  684. end
  685. local
  686. function byteset_tostring (s)
  687. local list = {}
  688. for i = 0, 255 do
  689. list[#list+1] = (s[i] == true) and i or nil
  690. end
  691. return t_concat(list,", ")
  692. end
  693. structfor.binary = {
  694. set ={
  695. new = byteset_new,
  696. union = byteset_union,
  697. difference = byteset_difference,
  698. tostring = byteset_tostring
  699. },
  700. Range = byterange_new,
  701. isboolset = isboolset,
  702. isbyteset = isbyteset,
  703. isset = isbyteset
  704. }
  705. local set_mt = {}
  706. local
  707. function set_new (t)
  708. local set = setmetatable({}, set_mt)
  709. for i = 1, #t do set[t[i]] = true end
  710. return set
  711. end
  712. local -- helper for the union code.
  713. function add_elements(a, res)
  714. for k in pairs(a) do res[k] = true end
  715. return res
  716. end
  717. local
  718. function set_union (a, b)
  719. a, b = (type(a) == "number") and set_new{a} or a
  720. , (type(b) == "number") and set_new{b} or b
  721. local res = set_new{}
  722. add_elements(a, res)
  723. add_elements(b, res)
  724. return res
  725. end
  726. local
  727. function set_difference(a, b)
  728. local list = {}
  729. a, b = (type(a) == "number") and set_new{a} or a
  730. , (type(b) == "number") and set_new{b} or b
  731. for el in pairs(a) do
  732. if a[el] and not b[el] then
  733. list[#list+1] = el
  734. end
  735. end
  736. return set_new(list)
  737. end
  738. local
  739. function set_tostring (s)
  740. local list = {}
  741. for el in pairs(s) do
  742. t_insert(list,el)
  743. end
  744. t_sort(list)
  745. return t_concat(list, ",")
  746. end
  747. local
  748. function isset (s)
  749. return (getmetatable(s) == set_mt)
  750. end
  751. local
  752. function range_new (start, finish)
  753. local list = {}
  754. for i = start, finish do
  755. list[#list + 1] = i
  756. end
  757. return set_new(list)
  758. end
  759. structfor.other = {
  760. set = {
  761. new = set_new,
  762. union = set_union,
  763. tostring = set_tostring,
  764. difference = set_difference,
  765. },
  766. Range = range_new,
  767. isboolset = isboolset,
  768. isbyteset = isbyteset,
  769. isset = isset,
  770. isrange = function(a) return false end
  771. }
  772. return function(Builder, LL)
  773. local cs = (Builder.options or {}).charset or "binary"
  774. if type(cs) == "string" then
  775. cs = (cs == "binary") and "binary" or "other"
  776. else
  777. cs = cs.binary and "binary" or "other"
  778. end
  779. return extend(Builder, structfor[cs])
  780. end
  781. end
  782. end
  783. --=============================================================================
  784. do local _ENV = _ENV
  785. packages['charsets'] = function (...)
  786. local s, t, u = require"string", require"table", require"util"
  787. local _ENV = u.noglobals() ----------------------------------------------------
  788. local copy = u.copy
  789. local s_char, s_sub, s_byte, t_concat, t_insert
  790. = s.char, s.sub, s.byte, t.concat, t.insert
  791. local
  792. function utf8_offset (byte)
  793. if byte < 128 then return 0, byte
  794. elseif byte < 192 then
  795. error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence")
  796. elseif byte < 224 then return 1, byte - 192
  797. elseif byte < 240 then return 2, byte - 224
  798. elseif byte < 248 then return 3, byte - 240
  799. elseif byte < 252 then return 4, byte - 248
  800. elseif byte < 254 then return 5, byte - 252
  801. else
  802. error("Byte values between 0xFE and OxFF cannot start a multibyte sequence")
  803. end
  804. end
  805. local
  806. function utf8_validate (subject, start, finish)
  807. start = start or 1
  808. finish = finish or #subject
  809. local offset, char
  810. = 0
  811. for i = start,finish do
  812. local b = s_byte(subject,i)
  813. if offset == 0 then
  814. char = i
  815. success, offset = pcall(utf8_offset, b)
  816. if not success then return false, char - 1 end
  817. else
  818. if not (127 < b and b < 192) then
  819. return false, char - 1
  820. end
  821. offset = offset -1
  822. end
  823. end
  824. if offset ~= 0 then return nil, char - 1 end -- Incomplete input.
  825. return true, finish
  826. end
  827. local
  828. function utf8_next_int (subject, i)
  829. i = i and i+1 or 1
  830. if i > #subject then return end
  831. local c = s_byte(subject, i)
  832. local offset, val = utf8_offset(c)
  833. for i = i+1, i+offset do
  834. c = s_byte(subject, i)
  835. val = val * 64 + (c-128)
  836. end
  837. return i + offset, i, val
  838. end
  839. local
  840. function utf8_next_char (subject, i)
  841. i = i and i+1 or 1
  842. if i > #subject then return end
  843. local offset = utf8_offset(s_byte(subject,i))
  844. return i + offset, i, s_sub(subject, i, i + offset)
  845. end
  846. local
  847. function utf8_split_int (subject)
  848. local chars = {}
  849. for _, _, c in utf8_next_int, subject do
  850. t_insert(chars,c)
  851. end
  852. return chars
  853. end
  854. local
  855. function utf8_split_char (subject)
  856. local chars = {}
  857. for _, _, c in utf8_next_char, subject do
  858. t_insert(chars,c)
  859. end
  860. return chars
  861. end
  862. local
  863. function utf8_get_int(subject, i)
  864. if i > #subject then return end
  865. local c = s_byte(subject, i)
  866. local offset, val = utf8_offset(c)
  867. for i = i+1, i+offset do
  868. c = s_byte(subject, i)
  869. val = val * 64 + ( c - 128 )
  870. end
  871. return val, i + offset + 1
  872. end
  873. local
  874. function split_generator (get)
  875. if not get then return end
  876. return function(subject)
  877. local res = {}
  878. local o, i = true
  879. while o do
  880. o,i = get(subject, i)
  881. res[#res] = o
  882. end
  883. return res
  884. end
  885. end
  886. local
  887. function merge_generator (char)
  888. if not char then return end
  889. return function(ary)
  890. local res = {}
  891. for i = 1, #ary do
  892. t_insert(res,char(ary[i]))
  893. end
  894. return t_concat(res)
  895. end
  896. end
  897. local
  898. function utf8_get_int2 (subject, i)
  899. local byte, b5, b4, b3, b2, b1 = s_byte(subject, i)
  900. if byte < 128 then return byte, i + 1
  901. elseif byte < 192 then
  902. error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence")
  903. elseif byte < 224 then
  904. return (byte - 192)*64 + s_byte(subject, i+1), i+2
  905. elseif byte < 240 then
  906. b2, b1 = s_byte(subject, i+1, i+2)
  907. return (byte-224)*4096 + b2%64*64 + b1%64, i+3
  908. elseif byte < 248 then
  909. b3, b2, b1 = s_byte(subject, i+1, i+2, 1+3)
  910. return (byte-240)*262144 + b3%64*4096 + b2%64*64 + b1%64, i+4
  911. elseif byte < 252 then
  912. b4, b3, b2, b1 = s_byte(subject, i+1, i+2, 1+3, i+4)
  913. return (byte-248)*16777216 + b4%64*262144 + b3%64*4096 + b2%64*64 + b1%64, i+5
  914. elseif byte < 254 then
  915. b5, b4, b3, b2, b1 = s_byte(subject, i+1, i+2, 1+3, i+4, i+5)
  916. return (byte-252)*1073741824 + b5%64*16777216 + b4%64*262144 + b3%64*4096 + b2%64*64 + b1%64, i+6
  917. else
  918. error("Byte values between 0xFE and OxFF cannot start a multibyte sequence")
  919. end
  920. end
  921. local
  922. function utf8_get_char(subject, i)
  923. if i > #subject then return end
  924. local offset = utf8_offset(s_byte(subject,i))
  925. return s_sub(subject, i, i + offset), i + offset + 1
  926. end
  927. local
  928. function utf8_char(c)
  929. if c < 128 then
  930. return s_char(c)
  931. elseif c < 2048 then
  932. return s_char(192 + c/64, 128 + c%64)
  933. elseif c < 55296 or 57343 < c and c < 65536 then
  934. return s_char(224 + c/4096, 128 + c/64%64, 128 + c%64)
  935. elseif c < 2097152 then
  936. return s_char(240 + c/262144, 128 + c/4096%64, 128 + c/64%64, 128 + c%64)
  937. elseif c < 67108864 then
  938. return s_char(248 + c/16777216, 128 + c/262144%64, 128 + c/4096%64, 128 + c/64%64, 128 + c%64)
  939. elseif c < 2147483648 then
  940. return s_char( 252 + c/1073741824,
  941. 128 + c/16777216%64, 128 + c/262144%64, 128 + c/4096%64, 128 + c/64%64, 128 + c%64)
  942. end
  943. error("Bad Unicode code point: "..c..".")
  944. end
  945. local
  946. function binary_validate (subject, start, finish)
  947. start = start or 1
  948. finish = finish or #subject
  949. return true, finish
  950. end
  951. local
  952. function binary_next_int (subject, i)
  953. i = i and i+1 or 1
  954. if i >= #subject then return end
  955. return i, i, s_sub(subject, i, i)
  956. end
  957. local
  958. function binary_next_char (subject, i)
  959. i = i and i+1 or 1
  960. if i > #subject then return end
  961. return i, i, s_byte(subject,i)
  962. end
  963. local
  964. function binary_split_int (subject)
  965. local chars = {}
  966. for i = 1, #subject do
  967. t_insert(chars, s_byte(subject,i))
  968. end
  969. return chars
  970. end
  971. local
  972. function binary_split_char (subject)
  973. local chars = {}
  974. for i = 1, #subject do
  975. t_insert(chars, s_sub(subject,i,i))
  976. end
  977. return chars
  978. end
  979. local
  980. function binary_get_int(subject, i)
  981. return s_byte(subject, i), i + 1
  982. end
  983. local
  984. function binary_get_char(subject, i)
  985. return s_sub(subject, i, i), i + 1
  986. end
  987. local charsets = {
  988. binary = {
  989. name = "binary",
  990. binary = true,
  991. validate = binary_validate,
  992. split_char = binary_split_char,
  993. split_int = binary_split_int,
  994. next_char = binary_next_char,
  995. next_int = binary_next_int,
  996. get_char = binary_get_char,
  997. get_int = binary_get_int,
  998. tochar = s_char
  999. },
  1000. ["UTF-8"] = {
  1001. name = "UTF-8",
  1002. validate = utf8_validate,
  1003. split_char = utf8_split_char,
  1004. split_int = utf8_split_int,
  1005. next_char = utf8_next_char,
  1006. next_int = utf8_next_int,
  1007. get_char = utf8_get_char,
  1008. get_int = utf8_get_int
  1009. }
  1010. }
  1011. return function (Builder)
  1012. local cs = Builder.options.charset or "binary"
  1013. if charsets[cs] then
  1014. Builder.charset = copy(charsets[cs])
  1015. Builder.binary_split_int = binary_split_int
  1016. else
  1017. error("NYI: custom charsets")
  1018. end
  1019. end
  1020. end
  1021. end
  1022. --=============================================================================
  1023. do local _ENV = _ENV
  1024. packages['re'] = function (...)
  1025. return function(Builder, LL)
  1026. local tonumber, type, print, error = tonumber, type, print, error
  1027. local setmetatable = setmetatable
  1028. local m = LL
  1029. local mm = m
  1030. local mt = getmetatable(mm.P(0))
  1031. local version = _VERSION
  1032. if version == "Lua 5.2" then _ENV = nil end
  1033. local any = m.P(1)
  1034. local Predef = { nl = m.P"\n" }
  1035. local mem
  1036. local fmem
  1037. local gmem
  1038. local function updatelocale ()
  1039. mm.locale(Predef)
  1040. Predef.a = Predef.alpha
  1041. Predef.c = Predef.cntrl
  1042. Predef.d = Predef.digit
  1043. Predef.g = Predef.graph
  1044. Predef.l = Predef.lower
  1045. Predef.p = Predef.punct
  1046. Predef.s = Predef.space
  1047. Predef.u = Predef.upper
  1048. Predef.w = Predef.alnum
  1049. Predef.x = Predef.xdigit
  1050. Predef.A = any - Predef.a
  1051. Predef.C = any - Predef.c
  1052. Predef.D = any - Predef.d
  1053. Predef.G = any - Predef.g
  1054. Predef.L = any - Predef.l
  1055. Predef.P = any - Predef.p
  1056. Predef.S = any - Predef.s
  1057. Predef.U = any - Predef.u
  1058. Predef.W = any - Predef.w
  1059. Predef.X = any - Predef.x
  1060. mem = {} -- restart memoization
  1061. fmem = {}
  1062. gmem = {}
  1063. local mt = {__mode = "v"}
  1064. setmetatable(mem, mt)
  1065. setmetatable(fmem, mt)
  1066. setmetatable(gmem, mt)
  1067. end
  1068. updatelocale()
  1069. local function getdef (id, defs)
  1070. local c = defs and defs[id]
  1071. if not c then error("undefined name: " .. id) end
  1072. return c
  1073. end
  1074. local function patt_error (s, i)
  1075. local msg = (#s < i + 20) and s:sub(i)
  1076. or s:sub(i,i+20) .. "..."
  1077. msg = ("pattern error near '%s'"):format(msg)
  1078. error(msg, 2)
  1079. end
  1080. local function mult (p, n)
  1081. local np = mm.P(true)
  1082. while n >= 1 do
  1083. if n%2 >= 1 then np = np * p end
  1084. p = p * p
  1085. n = n/2
  1086. end
  1087. return np
  1088. end
  1089. local function equalcap (s, i, c)
  1090. if type(c) ~= "string" then return nil end
  1091. local e = #c + i
  1092. if s:sub(i, e - 1) == c then return e else return nil end
  1093. end
  1094. local S = (Predef.space + "--" * (any - Predef.nl)^0)^0
  1095. local name = m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0
  1096. local arrow = S * "<-"
  1097. local seq_follow = m.P"/" + ")" + "}" + ":}" + "~}" + "|}" + (name * arrow) + -1
  1098. name = m.C(name)
  1099. local Def = name * m.Carg(1)
  1100. local num = m.C(m.R"09"^1) * S / tonumber
  1101. local String = "'" * m.C((any - "'")^0) * "'" +
  1102. '"' * m.C((any - '"')^0) * '"'
  1103. local defined = "%" * Def / function (c,Defs)
  1104. local cat = Defs and Defs[c] or Predef[c]
  1105. if not cat then error ("name '" .. c .. "' undefined") end
  1106. return cat
  1107. end
  1108. local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R
  1109. local item = defined + Range + m.C(any)
  1110. local Class =
  1111. "["
  1112. * (m.C(m.P"^"^-1)) -- optional complement symbol
  1113. * m.Cf(item * (item - "]")^0, mt.__add) /
  1114. function (c, p) return c == "^" and any - p or p end
  1115. * "]"
  1116. local function adddef (t, k, exp)
  1117. if t[k] then
  1118. error("'"..k.."' already defined as a rule")
  1119. else
  1120. t[k] = exp
  1121. end
  1122. return t
  1123. end
  1124. local function firstdef (n, r) return adddef({n}, n, r) end
  1125. local function NT (n, b)
  1126. if not b then
  1127. error("rule '"..n.."' used outside a grammar")
  1128. else return mm.V(n)
  1129. end
  1130. end
  1131. local exp = m.P{ "Exp",
  1132. Exp = S * ( m.V"Grammar"
  1133. + m.Cf(m.V"Seq" * ("/" * S * m.V"Seq")^0, mt.__add) );
  1134. Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul)
  1135. * (m.L(seq_follow) + patt_error);
  1136. Prefix = "&" * S * m.V"Prefix" / mt.__len
  1137. + "!" * S * m.V"Prefix" / mt.__unm
  1138. + m.V"Suffix";
  1139. Suffix = m.Cf(m.V"Primary" * S *
  1140. ( ( m.P"+" * m.Cc(1, mt.__pow)
  1141. + m.P"*" * m.Cc(0, mt.__pow)
  1142. + m.P"?" * m.Cc(-1, mt.__pow)
  1143. + "^" * ( m.Cg(num * m.Cc(mult))
  1144. + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow))
  1145. )
  1146. + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div))
  1147. + m.P"{}" * m.Cc(nil, m.Ct)
  1148. + m.Cg(Def / getdef * m.Cc(mt.__div))
  1149. )
  1150. + "=>" * S * m.Cg(Def / getdef * m.Cc(m.Cmt))
  1151. ) * S
  1152. )^0, function (a,b,f) return f(a,b) end );
  1153. Primary = "(" * m.V"Exp" * ")"
  1154. + String / mm.P
  1155. + Class
  1156. + defined
  1157. + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" /
  1158. function (n, p) return mm.Cg(p, n) end
  1159. + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end
  1160. + m.P"{}" / mm.Cp
  1161. + "{~" * m.V"Exp" * "~}" / mm.Cs
  1162. + "{|" * m.V"Exp" * "|}" / mm.Ct
  1163. + "{" * m.V"Exp" * "}" / mm.C
  1164. + m.P"." * m.Cc(any)
  1165. + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT;
  1166. Definition = name * arrow * m.V"Exp";
  1167. Grammar = m.Cg(m.Cc(true), "G") *
  1168. m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0,
  1169. adddef) / mm.P
  1170. }
  1171. local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error)
  1172. local function compile (p, defs)
  1173. if mm.type(p) == "pattern" then return p end -- already compiled
  1174. local cp = pattern:match(p, 1, defs)
  1175. if not cp then error("incorrect pattern", 3) end
  1176. return cp
  1177. end
  1178. local function match (s, p, i)
  1179. local cp = mem[p]
  1180. if not cp then
  1181. cp = compile(p)
  1182. mem[p] = cp
  1183. end
  1184. return cp:match(s, i or 1)
  1185. end
  1186. local function find (s, p, i)
  1187. local cp = fmem[p]
  1188. if not cp then
  1189. cp = compile(p) / 0
  1190. cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) }
  1191. fmem[p] = cp
  1192. end
  1193. local i, e = cp:match(s, i or 1)
  1194. if i then return i, e - 1
  1195. else return i
  1196. end
  1197. end
  1198. local function gsub (s, p, rep)
  1199. local g = gmem[p] or {} -- ensure gmem[p] is not collected while here
  1200. gmem[p] = g
  1201. local cp = g[rep]
  1202. if not cp then
  1203. cp = compile(p)
  1204. cp = mm.Cs((cp / rep + 1)^0)
  1205. g[rep] = cp
  1206. end
  1207. return cp:match(s)
  1208. end
  1209. local re = {
  1210. compile = compile,
  1211. match = match,
  1212. find = find,
  1213. gsub = gsub,
  1214. updatelocale = updatelocale,
  1215. }
  1216. return re
  1217. end
  1218. end
  1219. end
  1220. --=============================================================================
  1221. do local _ENV = _ENV
  1222. packages['evaluator'] = function (...)
  1223. local select, tonumber, tostring, type
  1224. = select, tonumber, tostring, type
  1225. local s, t, u = require"string", require"table", require"util"
  1226. local s_sub, t_concat
  1227. = s.sub, t.concat
  1228. local t_unpack
  1229. = u.unpack
  1230. local _ENV = u.noglobals() ----------------------------------------------------
  1231. return function(Builder, LL) -- Decorator wrapper
  1232. local eval = {}
  1233. local
  1234. function insert (caps, sbj, vals, ci, vi)
  1235. local openclose, kind = caps.openclose, caps.kind
  1236. while kind[ci] and openclose[ci] >= 0 do
  1237. ci, vi = eval[kind[ci]](caps, sbj, vals, ci, vi)
  1238. end
  1239. return ci, vi
  1240. end
  1241. function eval.C (caps, sbj, vals, ci, vi)
  1242. if caps.openclose[ci] > 0 then
  1243. vals[vi] = s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
  1244. return ci + 1, vi + 1
  1245. end
  1246. vals[vi] = false -- pad it for now
  1247. local cj, vj = insert(caps, sbj, vals, ci + 1, vi + 1)
  1248. vals[vi] = s_sub(sbj, caps.bounds[ci], caps.bounds[cj] - 1)
  1249. return cj + 1, vj
  1250. end
  1251. local
  1252. function lookback (caps, label, ci)
  1253. local aux, openclose, kind= caps.aux, caps.openclose, caps.kind
  1254. repeat
  1255. ci = ci - 1
  1256. local auxv, oc = aux[ci], openclose[ci]
  1257. if oc < 0 then ci = ci + oc end
  1258. if oc ~= 0 and kind[ci] == "Clb" and label == auxv then
  1259. return ci
  1260. end
  1261. until ci == 1
  1262. label = type(label) == "string" and "'"..label.."'" or tostring(label)
  1263. error("back reference "..label.." not found")
  1264. end
  1265. function eval.Cb (caps, sbj, vals, ci, vi)
  1266. local Cb_ci = lookback(caps, caps.aux[ci], ci)
  1267. Cb_ci, vi = eval.Cg(caps, sbj, vals, Cb_ci, vi)
  1268. return ci + 1, vi
  1269. end
  1270. function eval.Cc (caps, sbj, vals, ci, vi)
  1271. local these_values = caps.aux[ci]
  1272. for i = 1, these_values.n do
  1273. vi, vals[vi] = vi + 1, these_values[i]
  1274. end
  1275. return ci + 1, vi
  1276. end
  1277. eval["Cf"] = function() error("NYI: Cf") end
  1278. function eval.Cf (caps, sbj, vals, ci, vi)
  1279. if caps.openclose[ci] > 0 then
  1280. error"No First Value"
  1281. end
  1282. local func, Cf_vals, Cf_vi = caps.aux[ci], {}
  1283. ci = ci + 1
  1284. ci, Cf_vi = eval[caps.kind[ci]](caps, sbj, Cf_vals, ci, 1)
  1285. if Cf_vi == 1 then
  1286. error"No first value"
  1287. end
  1288. local result = Cf_vals[1]
  1289. while caps.kind[ci] and caps.openclose[ci] >= 0 do
  1290. ci, Cf_vi = eval[caps.kind[ci]](caps, sbj, Cf_vals, ci, 1)
  1291. result = func(result, t_unpack(Cf_vals, 1, Cf_vi - 1))
  1292. end
  1293. vals[vi] = result
  1294. return ci +1, vi + 1
  1295. end
  1296. function eval.Cg (caps, sbj, vals, ci, vi)
  1297. if caps.openclose[ci] > 0 then
  1298. vals[vi] = s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
  1299. return ci + 1, vi + 1
  1300. end
  1301. local cj, vj = insert(caps, sbj, vals, ci + 1, vi)
  1302. if vj == vi then
  1303. vals[vj] = s_sub(sbj, caps.bounds[ci], caps.bounds[cj] - 1)
  1304. vj = vj + 1
  1305. end
  1306. return cj + 1, vj
  1307. end
  1308. function eval.Clb (caps, sbj, vals, ci, vi)
  1309. local oc = caps.openclose
  1310. if oc[ci] > 0 then
  1311. return ci + 1, vi
  1312. end
  1313. local depth = 0
  1314. repeat
  1315. if oc[ci] == 0 then depth = depth + 1
  1316. elseif oc[ci] < 0 then depth = depth - 1
  1317. end
  1318. ci = ci + 1
  1319. until depth == 0
  1320. return ci, vi
  1321. end
  1322. function eval.Cp (caps, sbj, vals, ci, vi)
  1323. vals[vi] = caps.bounds[ci]
  1324. return ci + 1, vi + 1
  1325. end
  1326. function eval.Ct (caps, sbj, vals, ci, vi)
  1327. local aux, openclose, kind = caps. aux, caps.openclose, caps.kind
  1328. local tbl_vals = {}
  1329. vals[vi] = tbl_vals
  1330. if openclose[ci] > 0 then
  1331. return ci + 1, vi + 1
  1332. end
  1333. local tbl_vi, Clb_vals = 1, {}
  1334. ci = ci + 1
  1335. while kind[ci] and openclose[ci] >= 0 do
  1336. if kind[ci] == "Clb" then
  1337. local label, Clb_vi = aux[ci], 1
  1338. ci, Clb_vi = eval.Cg(caps, sbj, Clb_vals, ci, 1)
  1339. if Clb_vi ~= 1 then tbl_vals[label] = Clb_vals[1] end
  1340. else
  1341. ci, tbl_vi = eval[kind[ci]](caps, sbj, tbl_vals, ci, tbl_vi)
  1342. end
  1343. end
  1344. return ci + 1, vi + 1
  1345. end
  1346. local inf = 1/0
  1347. function eval.value (caps, sbj, vals, ci, vi)
  1348. local val
  1349. if caps.aux[ci] ~= inf or caps.openclose[ci] ~= inf
  1350. then val = caps.aux[ci]
  1351. end
  1352. vals[vi] = val
  1353. return ci + 1, vi + 1
  1354. end
  1355. function eval.Cs (caps, sbj, vals, ci, vi)
  1356. if caps.openclose[ci] > 0 then
  1357. vals[vi] = s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
  1358. else
  1359. local bounds, kind, openclose = caps.bounds, caps.kind, caps.openclose
  1360. local start, buffer, Cs_vals, bi, Cs_vi = bounds[ci], {}, {}, 1, 1
  1361. local last
  1362. ci = ci + 1
  1363. while openclose[ci] >= 0 do
  1364. last = bounds[ci]
  1365. buffer[bi] = s_sub(sbj, start, last - 1)
  1366. bi = bi + 1
  1367. ci, Cs_vi = eval[kind[ci]](caps, sbj, Cs_vals, ci, 1)
  1368. if Cs_vi > 1 then
  1369. buffer[bi] = Cs_vals[1]
  1370. bi = bi + 1
  1371. start = openclose[ci-1] > 0 and openclose[ci-1] or bounds[ci-1]
  1372. else
  1373. start = last
  1374. end
  1375. end
  1376. buffer[bi] = s_sub(sbj, start, bounds[ci] - 1)
  1377. vals[vi] = t_concat(buffer)
  1378. end
  1379. return ci + 1, vi + 1
  1380. end
  1381. local
  1382. function insert_divfunc_results(acc, val_i, ...)
  1383. local n = select('#', ...)
  1384. for i = 1, n do
  1385. val_i, acc[val_i] = val_i + 1, select(i, ...)
  1386. end
  1387. return val_i
  1388. end
  1389. function eval.div_function (caps, sbj, vals, ci, vi)
  1390. local func = caps.aux[ci]
  1391. local params, divF_vi
  1392. if caps.openclose[ci] > 0 then
  1393. params, divF_vi = {s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)}, 2
  1394. else
  1395. params = {}
  1396. ci, divF_vi = insert(caps, sbj, params, ci + 1, 1)
  1397. end
  1398. ci = ci + 1 -- skip the closed or closing node.
  1399. vi = insert_divfunc_results(vals, vi, func(t_unpack(params, 1, divF_vi - 1)))
  1400. return ci, vi
  1401. end
  1402. function eval.div_number (caps, sbj, vals, ci, vi)
  1403. local this_aux = caps.aux[ci]
  1404. local divN_vals, divN_vi
  1405. if caps.openclose[ci] > 0 then
  1406. divN_vals, divN_vi = {s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)}, 2
  1407. else
  1408. divN_vals = {}
  1409. ci, divN_vi = insert(caps, sbj, divN_vals, ci + 1, 1)
  1410. end
  1411. ci = ci + 1 -- skip the closed or closing node.
  1412. if this_aux >= divN_vi then error("no capture '"..this_aux.."' in /number capture.") end
  1413. vals[vi] = divN_vals[this_aux]
  1414. return ci, vi + 1
  1415. end
  1416. local function div_str_cap_refs (caps, ci)
  1417. local opcl = caps.openclose
  1418. local refs = {open=caps.bounds[ci]}
  1419. if opcl[ci] > 0 then
  1420. refs.close = opcl[ci]
  1421. return ci + 1, refs, 0
  1422. end
  1423. local first_ci = ci
  1424. local depth = 1
  1425. ci = ci + 1
  1426. repeat
  1427. local oc = opcl[ci]
  1428. if depth == 1 and oc >= 0 then refs[#refs+1] = ci end
  1429. if oc == 0 then
  1430. depth = depth + 1
  1431. elseif oc < 0 then
  1432. depth = depth - 1
  1433. end
  1434. ci = ci + 1
  1435. until depth == 0
  1436. refs.close = caps.bounds[ci - 1]
  1437. return ci, refs, #refs
  1438. end
  1439. function eval.div_string (caps, sbj, vals, ci, vi)
  1440. local n, refs
  1441. local cached
  1442. local cached, divS_vals = {}, {}
  1443. local the_string = caps.aux[ci]
  1444. ci, refs, n = div_str_cap_refs(caps, ci)
  1445. vals[vi] = the_string:gsub("%%([%d%%])", function (d)
  1446. if d == "%" then return "%" end
  1447. d = tonumber(d)
  1448. if not cached[d] then
  1449. if d > n then
  1450. error("no capture at index "..d.." in /string capture.")
  1451. end
  1452. if d == 0 then
  1453. cached[d] = s_sub(sbj, refs.open, refs.close - 1)
  1454. else
  1455. local _, vi = eval[caps.kind[refs[d]]](caps, sbj, divS_vals, refs[d], 1)
  1456. if vi == 1 then error("no values in capture at index"..d.." in /string capture.") end
  1457. cached[d] = divS_vals[1]
  1458. end
  1459. end
  1460. return cached[d]
  1461. end)
  1462. return ci, vi + 1
  1463. end
  1464. function eval.div_table (caps, sbj, vals, ci, vi)
  1465. local this_aux = caps.aux[ci]
  1466. local key
  1467. if caps.openclose[ci] > 0 then
  1468. key = s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
  1469. else
  1470. local divT_vals, _ = {}
  1471. ci, _ = insert(caps, sbj, divT_vals, ci + 1, 1)
  1472. key = divT_vals[1]
  1473. end
  1474. ci = ci + 1
  1475. if this_aux[key] then
  1476. vals[vi] = this_aux[key]
  1477. return ci, vi + 1
  1478. else
  1479. return ci, vi
  1480. end
  1481. end
  1482. function LL.evaluate (caps, sbj, ci)
  1483. local vals = {}
  1484. local _, vi = insert(caps, sbj, vals, ci, 1)
  1485. return vals, 1, vi - 1
  1486. end
  1487. end -- Decorator wrapper
  1488. end
  1489. end
  1490. --=============================================================================
  1491. do local _ENV = _ENV
  1492. packages['printers'] = function (...)
  1493. return function(Builder, LL)
  1494. local ipairs, pairs, print, tostring, type
  1495. = ipairs, pairs, print, tostring, type
  1496. local s, t, u = require"string", require"table", require"util"
  1497. local S_tostring = Builder.set.tostring
  1498. local _ENV = u.noglobals() ----------------------------------------------------
  1499. local s_char, s_sub, t_concat
  1500. = s.char, s.sub, t.concat
  1501. local expose, load, map
  1502. = u.expose, u.load, u.map
  1503. local escape_index = {
  1504. ["\f"] = "\\f",
  1505. ["\n"] = "\\n",
  1506. ["\r"] = "\\r",
  1507. ["\t"] = "\\t",
  1508. ["\v"] = "\\v",
  1509. ["\127"] = "\\ESC"
  1510. }
  1511. local function flatten(kind, list)
  1512. if list[2].pkind == kind then
  1513. return list[1], flatten(kind, list[2])
  1514. else
  1515. return list[1], list[2]
  1516. end
  1517. end
  1518. for i = 0, 8 do escape_index[s_char(i)] = "\\"..i end
  1519. for i = 14, 31 do escape_index[s_char(i)] = "\\"..i end
  1520. local
  1521. function escape( str )
  1522. return str:gsub("%c", escape_index)
  1523. end
  1524. local
  1525. function set_repr (set)
  1526. return s_char(load("return "..S_tostring(set))())
  1527. end
  1528. local printers = {}
  1529. local
  1530. function LL_pprint (pt, offset, prefix)
  1531. return printers[pt.pkind](pt, offset, prefix)
  1532. end
  1533. function LL.pprint (pt0)
  1534. local pt = LL.P(pt0)
  1535. print"\nPrint pattern"
  1536. LL_pprint(pt, "", "")
  1537. print"--- /pprint\n"
  1538. return pt0
  1539. end
  1540. for k, v in pairs{
  1541. string = [[ "P( \""..escape(pt.as_is).."\" )" ]],
  1542. char = [[ "P( \""..escape(to_char(pt.aux)).."\" )"]],
  1543. ["true"] = [[ "P( true )" ]],
  1544. ["false"] = [[ "P( false )" ]],
  1545. eos = [[ "~EOS~" ]],
  1546. one = [[ "P( one )" ]],
  1547. any = [[ "P( "..pt.aux.." )" ]],
  1548. set = [[ "S( "..'"'..escape(set_repr(pt.aux))..'"'.." )" ]],
  1549. ["function"] = [[ "P( "..pt.aux.." )" ]],
  1550. ref = [[
  1551. "V( ",
  1552. (type(pt.aux) == "string" and "\""..pt.aux.."\"")
  1553. or tostring(pt.aux)
  1554. , " )"
  1555. ]],
  1556. range = [[
  1557. "R( ",
  1558. escape(t_concat(map(
  1559. pt.as_is,
  1560. function(e) return '"'..e..'"' end)
  1561. , ", "))
  1562. ," )"
  1563. ]]
  1564. } do
  1565. printers[k] = load(([==[
  1566. local k, map, t_concat, to_char, escape, set_repr = ...
  1567. return function (pt, offset, prefix)
  1568. print(t_concat{offset,prefix,XXXX})
  1569. end
  1570. ]==]):gsub("XXXX", v), k.." printer")(k, map, t_concat, s_char, escape, set_repr)
  1571. end
  1572. for k, v in pairs{
  1573. ["behind"] = [[ LL_pprint(pt.pattern, offset, "B ") ]],
  1574. ["at least"] = [[ LL_pprint(pt.pattern, offset, pt.aux.." ^ ") ]],
  1575. ["at most"] = [[ LL_pprint(pt.pattern, offset, pt.aux.." ^ ") ]],
  1576. unm = [[LL_pprint(pt.pattern, offset, "- ")]],
  1577. lookahead = [[LL_pprint(pt.pattern, offset, "# ")]],
  1578. choice = [[
  1579. print(offset..prefix.."+")
  1580. local ch, i = {}, 1
  1581. while pt.pkind == "choice" do
  1582. ch[i], pt, i = pt[1], pt[2], i + 1
  1583. end
  1584. ch[i] = pt
  1585. map(ch, LL_pprint, offset.." :", "")
  1586. ]],
  1587. sequence = [=[
  1588. print(offset..prefix.."*")
  1589. local acc, p2 = {}
  1590. offset = offset .. " |"
  1591. while true do
  1592. if pt.pkind ~= "sequence" then -- last element
  1593. if pt.pkind == "char" then
  1594. acc[#acc + 1] = pt.aux
  1595. print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
  1596. else
  1597. if #acc ~= 0 then
  1598. print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
  1599. end
  1600. LL_pprint(pt, offset, "")
  1601. end
  1602. break
  1603. elseif pt[1].pkind == "char" then
  1604. acc[#acc + 1] = pt[1].aux
  1605. elseif #acc ~= 0 then
  1606. print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
  1607. acc = {}
  1608. LL_pprint(pt[1], offset, "")
  1609. else
  1610. LL_pprint(pt[1], offset, "")
  1611. end
  1612. pt = pt[2]
  1613. end
  1614. ]=],
  1615. grammar = [[
  1616. print(offset..prefix.."Grammar")
  1617. for k, pt in pairs(pt.aux) do
  1618. local prefix = ( type(k)~="string"
  1619. and tostring(k)
  1620. or "\""..k.."\"" )
  1621. LL_pprint(pt, offset.." ", prefix .. " = ")
  1622. end
  1623. ]]
  1624. } do
  1625. printers[k] = load(([[
  1626. local map, LL_pprint, pkind, s, u, flatten = ...
  1627. return function (pt, offset, prefix)
  1628. XXXX
  1629. end
  1630. ]]):gsub("XXXX", v), k.." printer")(map, LL_pprint, type, s, u, flatten)
  1631. end
  1632. for _, cap in pairs{"C", "Cs", "Ct"} do
  1633. printers[cap] = function (pt, offset, prefix)
  1634. print(offset..prefix..cap)
  1635. LL_pprint(pt.pattern, offset.." ", "")
  1636. end
  1637. end
  1638. for _, cap in pairs{"Cg", "Clb", "Cf", "Cmt", "div_number", "/zero", "div_function", "div_table"} do
  1639. printers[cap] = function (pt, offset, prefix)
  1640. print(offset..prefix..cap.." "..tostring(pt.aux or ""))
  1641. LL_pprint(pt.pattern, offset.." ", "")
  1642. end
  1643. end
  1644. printers["div_string"] = function (pt, offset, prefix)
  1645. print(offset..prefix..'/string "'..tostring(pt.aux or "")..'"')
  1646. LL_pprint(pt.pattern, offset.." ", "")
  1647. end
  1648. for _, cap in pairs{"Carg", "Cp"} do
  1649. printers[cap] = function (pt, offset, prefix)
  1650. print(offset..prefix..cap.."( "..tostring(pt.aux).." )")
  1651. end
  1652. end
  1653. printers["Cb"] = function (pt, offset, prefix)
  1654. print(offset..prefix.."Cb( \""..pt.aux.."\" )")
  1655. end
  1656. printers["Cc"] = function (pt, offset, prefix)
  1657. print(offset..prefix.."Cc(" ..t_concat(map(pt.aux, tostring),", ").." )")
  1658. end
  1659. local cprinters = {}
  1660. local padding = " "
  1661. local function padnum(n)
  1662. n = tostring(n)
  1663. n = n .."."..((" "):rep(4 - #n))
  1664. return n
  1665. end
  1666. local function _cprint(caps, ci, indent, sbj, n)
  1667. local openclose, kind = caps.openclose, caps.kind
  1668. indent = indent or 0
  1669. while kind[ci] and openclose[ci] >= 0 do
  1670. if caps.openclose[ci] > 0 then
  1671. print(t_concat({
  1672. padnum(n),
  1673. padding:rep(indent),
  1674. caps.kind[ci],
  1675. ": start = ", tostring(caps.bounds[ci]),
  1676. " finish = ", tostring(caps.openclose[ci]),
  1677. caps.aux[ci] and " aux = " or "",
  1678. caps.aux[ci] and (
  1679. type(caps.aux[ci]) == "string"
  1680. and '"'..tostring(caps.aux[ci])..'"'
  1681. or tostring(caps.aux[ci])
  1682. ) or "",
  1683. " \t", s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
  1684. }))
  1685. if type(caps.aux[ci]) == "table" then expose(caps.aux[ci]) end
  1686. else
  1687. local kind = caps.kind[ci]
  1688. local start = caps.bounds[ci]
  1689. print(t_concat({
  1690. padnum(n),
  1691. padding:rep(indent), kind,
  1692. ": start = ", start,
  1693. caps.aux[ci] and " aux = " or "",
  1694. caps.aux[ci] and (
  1695. type(caps.aux[ci]) == "string"
  1696. and '"'..tostring(caps.aux[ci])..'"'
  1697. or tostring(caps.aux[ci])
  1698. ) or ""
  1699. }))
  1700. ci, n = _cprint(caps, ci + 1, indent + 1, sbj, n + 1)
  1701. print(t_concat({
  1702. padnum(n),
  1703. padding:rep(indent),
  1704. "/", kind,
  1705. " finish = ", tostring(caps.bounds[ci]),
  1706. " \t", s_sub(sbj, start, (caps.bounds[ci] or 1) - 1)
  1707. }))
  1708. end
  1709. n = n + 1
  1710. ci = ci + 1
  1711. end
  1712. return ci, n
  1713. end
  1714. function LL.cprint (caps, ci, sbj)
  1715. ci = ci or 1
  1716. print"\nCapture Printer:\n================"
  1717. _cprint(caps, ci, 0, sbj, 1)
  1718. print"================\n/Cprinter\n"
  1719. end
  1720. return { pprint = LL.pprint,cprint = LL.cprint }
  1721. end -- module wrapper ---------------------------------------------------------
  1722. end
  1723. end
  1724. --=============================================================================
  1725. do local _ENV = _ENV
  1726. packages['compat'] = function (...)
  1727. local _, debug, jit
  1728. _, debug = pcall(require, "debug")
  1729. _, jit = pcall(require, "jit")
  1730. jit = _ and jit
  1731. local compat = {
  1732. debug = debug,
  1733. lua51 = (_VERSION == "Lua 5.1") and not jit,
  1734. lua52 = _VERSION == "Lua 5.2",
  1735. luajit = jit and true or false,
  1736. jit = jit and jit.status(),
  1737. lua52_len = not #setmetatable({},{__len = function()end}),
  1738. proxies = pcall(function()
  1739. local prox = newproxy(true)
  1740. local prox2 = newproxy(prox)
  1741. assert (type(getmetatable(prox)) == "table"
  1742. and (getmetatable(prox)) == (getmetatable(prox2)))
  1743. end),
  1744. _goto = not not(loadstring or load)"::R::"
  1745. }
  1746. return compat
  1747. end
  1748. end
  1749. --=============================================================================
  1750. do local _ENV = _ENV
  1751. packages['factorizer'] = function (...)
  1752. local ipairs, pairs, print, setmetatable
  1753. = ipairs, pairs, print, setmetatable
  1754. local u = require"util"
  1755. local id, nop, setify, weakkey
  1756. = u.id, u.nop, u.setify, u.weakkey
  1757. local _ENV = u.noglobals() ----------------------------------------------------
  1758. local
  1759. function process_booleans(a, b, opts)
  1760. local id, brk = opts.id, opts.brk
  1761. if a == id then return true, b
  1762. elseif b == id then return true, a
  1763. elseif a == brk then return true, brk
  1764. else return false end
  1765. end
  1766. local unary = setify{
  1767. "unm", "lookahead", "C", "Cf",
  1768. "Cg", "Cs", "Ct", "/zero"
  1769. }
  1770. local unary_aux = setify{
  1771. "behind", "at least", "at most", "Clb", "Cmt",
  1772. "div_string", "div_number", "div_table", "div_function"
  1773. }
  1774. local unifiable = setify{"char", "set", "range"}
  1775. local hasCmt; hasCmt = setmetatable({}, {__mode = "k", __index = function(self, pt)
  1776. local kind, res = pt.pkind, false
  1777. if kind == "Cmt"
  1778. or kind == "ref"
  1779. then
  1780. res = true
  1781. elseif unary[kind] or unary_aux[kind] then
  1782. res = hasCmt[pt.pattern]
  1783. elseif kind == "choice" or kind == "sequence" then
  1784. res = hasCmt[pt[1]] or hasCmt[pt[2]]
  1785. end
  1786. hasCmt[pt] = res
  1787. return res
  1788. end})
  1789. return function (Builder, LL) --------------------------------------------------
  1790. if Builder.options.factorize == false then
  1791. return {
  1792. choice = nop,
  1793. sequence = nop,
  1794. lookahead = nop,
  1795. unm = nop
  1796. }
  1797. end
  1798. local constructors, LL_P = Builder.constructors, LL.P
  1799. local truept, falsept
  1800. = constructors.constant.truept
  1801. , constructors.constant.falsept
  1802. local --Range, Set,
  1803. S_union
  1804. = --Builder.Range, Builder.set.new,
  1805. Builder.set.union
  1806. local mergeable = setify{"char", "set"}
  1807. local type2cons = {
  1808. ["/zero"] = "__div",
  1809. ["div_number"] = "__div",
  1810. ["div_string"] = "__div",
  1811. ["div_table"] = "__div",
  1812. ["div_function"] = "__div",
  1813. ["at least"] = "__exp",
  1814. ["at most"] = "__exp",
  1815. ["Clb"] = "Cg",
  1816. }
  1817. local
  1818. function choice (a, b)
  1819. do -- handle the identity/break properties of true and false.
  1820. local hasbool, res = process_booleans(a, b, { id = falsept, brk = truept })
  1821. if hasbool then return res end
  1822. end
  1823. local ka, kb = a.pkind, b.pkind
  1824. if a == b and not hasCmt[a] then
  1825. return a
  1826. elseif ka == "choice" then -- correct associativity without blowing up the stack
  1827. local acc, i = {}, 1
  1828. while a.pkind == "choice" do
  1829. acc[i], a, i = a[1], a[2], i + 1
  1830. end
  1831. acc[i] = a
  1832. for j = i, 1, -1 do
  1833. b = acc[j] + b
  1834. end
  1835. return b
  1836. elseif mergeable[ka] and mergeable[kb] then
  1837. return constructors.aux("set", S_union(a.aux, b.aux))
  1838. elseif mergeable[ka] and kb == "any" and b.aux == 1
  1839. or mergeable[kb] and ka == "any" and a.aux == 1 then
  1840. return ka == "any" and a or b
  1841. elseif ka == kb then
  1842. if (unary[ka] or unary_aux[ka]) and ( a.aux == b.aux ) then
  1843. return LL[type2cons[ka] or ka](a.pattern + b.pattern, a.aux)
  1844. elseif ( ka == kb ) and ka == "sequence" then
  1845. if a[1] == b[1] and not hasCmt[a[1]] then
  1846. return a[1] * (a[2] + b[2])
  1847. end
  1848. end
  1849. end
  1850. return false
  1851. end
  1852. local
  1853. function lookahead (pt)
  1854. return pt
  1855. end
  1856. local
  1857. function sequence(a, b)
  1858. do
  1859. local hasbool, res = process_booleans(a, b, { id = truept, brk = falsept })
  1860. if hasbool then return res end
  1861. end
  1862. local ka, kb = a.pkind, b.pkind
  1863. if ka == "sequence" then -- correct associativity without blowing up the stack
  1864. local acc, i = {}, 1
  1865. while a.pkind == "sequence" do
  1866. acc[i], a, i = a[1], a[2], i + 1
  1867. end
  1868. acc[i] = a
  1869. for j = i, 1, -1 do
  1870. b = acc[j] * b
  1871. end
  1872. return b
  1873. elseif (ka == "one" or ka == "any") and (kb == "one" or kb == "any") then
  1874. return LL_P(a.aux + b.aux)
  1875. end
  1876. return false
  1877. end
  1878. local
  1879. function unm (pt)
  1880. if pt == truept then return falsept
  1881. elseif pt == falsept then return truept
  1882. elseif pt.pkind == "unm" then return #pt.pattern
  1883. elseif pt.pkind == "lookahead" then return -pt.pattern
  1884. end
  1885. end
  1886. return {
  1887. choice = choice,
  1888. lookahead = lookahead,
  1889. sequence = sequence,
  1890. unm = unm
  1891. }
  1892. end
  1893. end
  1894. end
  1895. --=============================================================================
  1896. do local _ENV = _ENV
  1897. packages['match'] = function (...)
  1898. end
  1899. end
  1900. --=============================================================================
  1901. do local _ENV = _ENV
  1902. packages['util'] = function (...)
  1903. local getmetatable, setmetatable, load, loadstring, next
  1904. , pairs, pcall, print, rawget, rawset, select, tostring
  1905. , type, unpack
  1906. = getmetatable, setmetatable, load, loadstring, next
  1907. , pairs, pcall, print, rawget, rawset, select, tostring
  1908. , type, unpack
  1909. local m, s, t = require"math", require"string", require"table"
  1910. local m_max, s_match, s_gsub, t_concat, t_insert
  1911. = m.max, s.match, s.gsub, t.concat, t.insert
  1912. local compat = require"compat"
  1913. local
  1914. function nop () end
  1915. local noglobals, getglobal, setglobal if pcall and not compat.lua52 and not release then
  1916. local function errR (_,i)
  1917. error("illegal global read: " .. tostring(i), 2)
  1918. end
  1919. local function errW (_,i, v)
  1920. error("illegal global write: " .. tostring(i)..": "..tostring(v), 2)
  1921. end
  1922. local env = setmetatable({}, { __index=errR, __newindex=errW })
  1923. noglobals = function()
  1924. pcall(setfenv, 3, env)
  1925. end
  1926. function getglobal(k) rawget(env, k) end
  1927. function setglobal(k, v) rawset(env, k, v) end
  1928. else
  1929. noglobals = nop
  1930. end
  1931. local _ENV = noglobals() ------------------------------------------------------
  1932. local util = {
  1933. nop = nop,
  1934. noglobals = noglobals,
  1935. getglobal = getglobal,
  1936. setglobal = setglobal
  1937. }
  1938. util.unpack = t.unpack or unpack
  1939. util.pack = t.pack or function(...) return { n = select('#', ...), ... } end
  1940. if compat.lua51 then
  1941. local old_load = load
  1942. function util.load (ld, source, mode, env)
  1943. local fun
  1944. if type (ld) == 'string' then
  1945. fun = loadstring (ld)
  1946. else
  1947. fun = old_load (ld, source)
  1948. end
  1949. if env then
  1950. setfenv (fun, env)
  1951. end
  1952. return fun
  1953. end
  1954. else
  1955. util.load = load
  1956. end
  1957. if compat.luajit and compat.jit then
  1958. function util.max (ary)
  1959. local max = 0
  1960. for i = 1, #ary do
  1961. max = m_max(max,ary[i])
  1962. end
  1963. return max
  1964. end
  1965. elseif compat.luajit then
  1966. local t_unpack = util.unpack
  1967. function util.max (ary)
  1968. local len = #ary
  1969. if len <=30 or len > 10240 then
  1970. local max = 0
  1971. for i = 1, #ary do
  1972. local j = ary[i]
  1973. if j > max then max = j end
  1974. end
  1975. return max
  1976. else
  1977. return m_max(t_unpack(ary))
  1978. end
  1979. end
  1980. else
  1981. local t_unpack = util.unpack
  1982. local safe_len = 1000
  1983. function util.max(array)
  1984. local len = #array
  1985. if len == 0 then return -1 end -- FIXME: shouldn't this be `return -1`?
  1986. local off = 1
  1987. local off_end = safe_len
  1988. local max = array[1] -- seed max.
  1989. repeat
  1990. if off_end > len then off_end = len end
  1991. local seg_max = m_max(t_unpack(array, off, off_end))
  1992. if seg_max > max then
  1993. max = seg_max
  1994. end
  1995. off = off + safe_len
  1996. off_end = off_end + safe_len
  1997. until off >= len
  1998. return max
  1999. end
  2000. end
  2001. local
  2002. function setmode(t,mode)
  2003. local mt = getmetatable(t) or {}
  2004. if mt.__mode then
  2005. error("The mode has already been set on table "..tostring(t)..".")
  2006. end
  2007. mt.__mode = mode
  2008. return setmetatable(t, mt)
  2009. end
  2010. util.setmode = setmode
  2011. function util.weakboth (t)
  2012. return setmode(t,"kv")
  2013. end
  2014. function util.weakkey (t)
  2015. return setmode(t,"k")
  2016. end
  2017. function util.weakval (t)
  2018. return setmode(t,"v")
  2019. end
  2020. function util.strip_mt (t)
  2021. return setmetatable(t, nil)
  2022. end
  2023. local getuniqueid
  2024. do
  2025. local N, index = 0, {}
  2026. function getuniqueid(v)
  2027. if not index[v] then
  2028. N = N + 1
  2029. index[v] = N
  2030. end
  2031. return index[v]
  2032. end
  2033. end
  2034. util.getuniqueid = getuniqueid
  2035. do
  2036. local counter = 0
  2037. function util.gensym ()
  2038. counter = counter + 1
  2039. return "___SYM_"..counter
  2040. end
  2041. end
  2042. function util.passprint (...) print(...) return ... end
  2043. local val_to_str_, key_to_str, table_tostring, cdata_to_str, t_cache
  2044. local multiplier = 2
  2045. local
  2046. function val_to_string (v, indent)
  2047. indent = indent or 0
  2048. t_cache = {} -- upvalue.
  2049. local acc = {}
  2050. val_to_str_(v, acc, indent, indent)
  2051. local res = t_concat(acc, "")
  2052. return res
  2053. end
  2054. util.val_to_str = val_to_string
  2055. function val_to_str_ ( v, acc, indent, str_indent )
  2056. str_indent = str_indent or 1
  2057. if "string" == type( v ) then
  2058. v = s_gsub( v, "\n", "\n" .. (" "):rep( indent * multiplier + str_indent ) )
  2059. if s_match( s_gsub( v,"[^'\"]",""), '^"+$' ) then
  2060. acc[#acc+1] = t_concat{ "'", "", v, "'" }
  2061. else
  2062. acc[#acc+1] = t_concat{'"', s_gsub(v,'"', '\\"' ), '"' }
  2063. end
  2064. elseif "cdata" == type( v ) then
  2065. cdata_to_str( v, acc, indent )
  2066. elseif "table" == type(v) then
  2067. if t_cache[v] then
  2068. acc[#acc+1] = t_cache[v]
  2069. else
  2070. t_cache[v] = tostring( v )
  2071. table_tostring( v, acc, indent )
  2072. end
  2073. else
  2074. acc[#acc+1] = tostring( v )
  2075. end
  2076. end
  2077. function key_to_str ( k, acc, indent )
  2078. if "string" == type( k ) and s_match( k, "^[_%a][_%a%d]*$" ) then
  2079. acc[#acc+1] = s_gsub( k, "\n", (" "):rep( indent * multiplier + 1 ) .. "\n" )
  2080. else
  2081. acc[#acc+1] = "[ "
  2082. val_to_str_( k, acc, indent )
  2083. acc[#acc+1] = " ]"
  2084. end
  2085. end
  2086. function cdata_to_str(v, acc, indent)
  2087. acc[#acc+1] = ( " " ):rep( indent * multiplier )
  2088. acc[#acc+1] = "["
  2089. print(#acc)
  2090. for i = 0, #v do
  2091. if i % 16 == 0 and i ~= 0 then
  2092. acc[#acc+1] = "\n"
  2093. acc[#acc+1] = (" "):rep(indent * multiplier + 2)
  2094. end
  2095. acc[#acc+1] = v[i] and 1 or 0
  2096. acc[#acc+1] = i ~= #v and ", " or ""
  2097. end
  2098. print(#acc, acc[1], acc[2])
  2099. acc[#acc+1] = "]"
  2100. end
  2101. function table_tostring ( tbl, acc, indent )
  2102. acc[#acc+1] = t_cache[tbl]
  2103. acc[#acc+1] = "{\n"
  2104. for k, v in pairs( tbl ) do
  2105. local str_indent = 1
  2106. acc[#acc+1] = (" "):rep((indent + 1) * multiplier)
  2107. key_to_str( k, acc, indent + 1)
  2108. if acc[#acc] == " ]"
  2109. and acc[#acc - 2] == "[ "
  2110. then str_indent = 8 + #acc[#acc - 1]
  2111. end
  2112. acc[#acc+1] = " = "
  2113. val_to_str_( v, acc, indent + 1, str_indent)
  2114. acc[#acc+1] = "\n"
  2115. end
  2116. acc[#acc+1] = ( " " ):rep( indent * multiplier )
  2117. acc[#acc+1] = "}"
  2118. end
  2119. function util.expose(v) print(val_to_string(v)) return v end
  2120. function util.map (ary, func, ...)
  2121. if type(ary) == "function" then ary, func = func, ary end
  2122. local res = {}
  2123. for i = 1,#ary do
  2124. res[i] = func(ary[i], ...)
  2125. end
  2126. return res
  2127. end
  2128. function util.selfmap (ary, func, ...)
  2129. if type(ary) == "function" then ary, func = func, ary end
  2130. for i = 1,#ary do
  2131. ary[i] = func(ary[i], ...)
  2132. end
  2133. return ary
  2134. end
  2135. local
  2136. function map_all (tbl, func, ...)
  2137. if type(tbl) == "function" then tbl, func = func, tbl end
  2138. local res = {}
  2139. for k, v in next, tbl do
  2140. res[k]=func(v, ...)
  2141. end
  2142. return res
  2143. end
  2144. util.map_all = map_all
  2145. local
  2146. function fold (ary, func, acc)
  2147. local i0 = 1
  2148. if not acc then
  2149. acc = ary[1]
  2150. i0 = 2
  2151. end
  2152. for i = i0, #ary do
  2153. acc = func(acc,ary[i])
  2154. end
  2155. return acc
  2156. end
  2157. util.fold = fold
  2158. local
  2159. function foldr (ary, func, acc)
  2160. local offset = 0
  2161. if not acc then
  2162. acc = ary[#ary]
  2163. offset = 1
  2164. end
  2165. for i = #ary - offset, 1 , -1 do
  2166. acc = func(ary[i], acc)
  2167. end
  2168. return acc
  2169. end
  2170. util.foldr = foldr
  2171. local
  2172. function map_fold(ary, mfunc, ffunc, acc)
  2173. local i0 = 1
  2174. if not acc then
  2175. acc = mfunc(ary[1])
  2176. i0 = 2
  2177. end
  2178. for i = i0, #ary do
  2179. acc = ffunc(acc,mfunc(ary[i]))
  2180. end
  2181. return acc
  2182. end
  2183. util.map_fold = map_fold
  2184. local
  2185. function map_foldr(ary, mfunc, ffunc, acc)
  2186. local offset = 0
  2187. if not acc then
  2188. acc = mfunc(ary[#acc])
  2189. offset = 1
  2190. end
  2191. for i = #ary - offset, 1 , -1 do
  2192. acc = ffunc(mfunc(ary[i], acc))
  2193. end
  2194. return acc
  2195. end
  2196. util.map_foldr = map_fold
  2197. function util.zip(a1, a2)
  2198. local res, len = {}, m_max(#a1,#a2)
  2199. for i = 1,len do
  2200. res[i] = {a1[i], a2[i]}
  2201. end
  2202. return res
  2203. end
  2204. function util.zip_all(t1, t2)
  2205. local res = {}
  2206. for k,v in pairs(t1) do
  2207. res[k] = {v, t2[k]}
  2208. end
  2209. for k,v in pairs(t2) do
  2210. if res[k] == nil then
  2211. res[k] = {t1[k], v}
  2212. end
  2213. end
  2214. return res
  2215. end
  2216. function util.filter(ary,func)
  2217. local res = {}
  2218. for i = 1,#ary do
  2219. if func(ary[i]) then
  2220. t_insert(res, ary[i])
  2221. end
  2222. end
  2223. end
  2224. local
  2225. function id (...) return ... end
  2226. util.id = id
  2227. local function AND (a,b) return a and b end
  2228. local function OR (a,b) return a or b end
  2229. function util.copy (tbl) return map_all(tbl, id) end
  2230. function util.all (ary, mfunc)
  2231. if mfunc then
  2232. return map_fold(ary, mfunc, AND)
  2233. else
  2234. return fold(ary, AND)
  2235. end
  2236. end
  2237. function util.any (ary, mfunc)
  2238. if mfunc then
  2239. return map_fold(ary, mfunc, OR)
  2240. else
  2241. return fold(ary, OR)
  2242. end
  2243. end
  2244. function util.get(field)
  2245. return function(tbl) return tbl[field] end
  2246. end
  2247. function util.lt(ref)
  2248. return function(val) return val < ref end
  2249. end
  2250. function util.compose(f,g)
  2251. return function(...) return f(g(...)) end
  2252. end
  2253. function util.extend (destination, ...)
  2254. for i = 1, select('#', ...) do
  2255. for k,v in pairs((select(i, ...))) do
  2256. destination[k] = v
  2257. end
  2258. end
  2259. return destination
  2260. end
  2261. function util.setify (t)
  2262. local set = {}
  2263. for i = 1, #t do
  2264. set[t[i]]=true
  2265. end
  2266. return set
  2267. end
  2268. function util.arrayify (...) return {...} end
  2269. local
  2270. function _checkstrhelper(s)
  2271. return s..""
  2272. end
  2273. function util.checkstring(s, func)
  2274. local success, str = pcall(_checkstrhelper, s)
  2275. if not success then
  2276. if func == nil then func = "?" end
  2277. error("bad argument to '"
  2278. ..tostring(func)
  2279. .."' (string expected, got "
  2280. ..type(s)
  2281. ..")",
  2282. 2)
  2283. end
  2284. return str
  2285. end
  2286. return util
  2287. end
  2288. end
  2289. --=============================================================================
  2290. do local _ENV = _ENV
  2291. packages['API'] = function (...)
  2292. local assert, error, ipairs, pairs, pcall, print
  2293. , require, select, tonumber, tostring, type
  2294. = assert, error, ipairs, pairs, pcall, print
  2295. , require, select, tonumber, tostring, type
  2296. local t, u = require"table", require"util"
  2297. local _ENV = u.noglobals() ---------------------------------------------------
  2298. local t_concat = t.concat
  2299. local checkstring, copy, fold, load, map_fold, map_foldr, setify, t_pack, t_unpack
  2300. = u.checkstring, u.copy, u.fold, u.load, u.map_fold, u.map_foldr, u.setify, u.pack, u.unpack
  2301. local
  2302. function charset_error(index, charset)
  2303. error("Character at position ".. index + 1
  2304. .." is not a valid "..charset.." one.",
  2305. 2)
  2306. end
  2307. return function(Builder, LL) -- module wrapper -------------------------------
  2308. local cs = Builder.charset
  2309. local constructors, LL_ispattern
  2310. = Builder.constructors, LL.ispattern
  2311. local truept, falsept, Cppt
  2312. = constructors.constant.truept
  2313. , constructors.constant.falsept
  2314. , constructors.constant.Cppt
  2315. local split_int, validate
  2316. = cs.split_int, cs.validate
  2317. local Range, Set, S_union, S_tostring
  2318. = Builder.Range, Builder.set.new
  2319. , Builder.set.union, Builder.set.tostring
  2320. local factorize_choice, factorize_lookahead, factorize_sequence, factorize_unm
  2321. local
  2322. function makechar(c)
  2323. return constructors.aux("char", c)
  2324. end
  2325. local
  2326. function LL_P (...)
  2327. local v, n = (...), select('#', ...)
  2328. if n == 0 then error"bad argument #1 to 'P' (value expected)" end
  2329. local typ = type(v)
  2330. if LL_ispattern(v) then
  2331. return v
  2332. elseif typ == "function" then
  2333. return
  2334. LL.Cmt("", v)
  2335. elseif typ == "string" then
  2336. local success, index = validate(v)
  2337. if not success then
  2338. charset_error(index, cs.name)
  2339. end
  2340. if v == "" then return truept end
  2341. return
  2342. map_foldr(split_int(v), makechar, Builder.sequence)
  2343. elseif typ == "table" then
  2344. local g = copy(v)
  2345. if g[1] == nil then error("grammar has no initial rule") end
  2346. if not LL_ispattern(g[1]) then g[1] = LL.V(g[1]) end
  2347. return
  2348. constructors.none("grammar", g)
  2349. elseif typ == "boolean" then
  2350. return v and truept or falsept
  2351. elseif typ == "number" then
  2352. if v == 0 then
  2353. return truept
  2354. elseif v > 0 then
  2355. return
  2356. constructors.aux("any", v)
  2357. else
  2358. return
  2359. - constructors.aux("any", -v)
  2360. end
  2361. else
  2362. error("bad argument #1 to 'P' (lpeg-pattern expected, got "..typ..")")
  2363. end
  2364. end
  2365. LL.P = LL_P
  2366. local
  2367. function LL_S (set)
  2368. if set == "" then
  2369. return
  2370. falsept
  2371. else
  2372. local success
  2373. set = checkstring(set, "S")
  2374. return
  2375. constructors.aux("set", Set(split_int(set)), set)
  2376. end
  2377. end
  2378. LL.S = LL_S
  2379. local
  2380. function LL_R (...)
  2381. if select('#', ...) == 0 then
  2382. return LL_P(false)
  2383. else
  2384. local range = Range(1,0)--Set("")
  2385. for _, r in ipairs{...} do
  2386. r = checkstring(r, "R")
  2387. assert(#r == 2, "bad argument #1 to 'R' (range must have two characters)")
  2388. range = S_union ( range, Range(t_unpack(split_int(r))) )
  2389. end
  2390. return
  2391. constructors.aux("set", range)
  2392. end
  2393. end
  2394. LL.R = LL_R
  2395. local
  2396. function LL_V (name)
  2397. assert(name ~= nil)
  2398. return
  2399. constructors.aux("ref", name)
  2400. end
  2401. LL.V = LL_V
  2402. do
  2403. local one = setify{"set", "range", "one", "char"}
  2404. local zero = setify{"true", "false", "lookahead", "unm"}
  2405. local forbidden = setify{
  2406. "Carg", "Cb", "C", "Cf",
  2407. "Cg", "Cs", "Ct", "/zero",
  2408. "Clb", "Cmt", "Cc", "Cp",
  2409. "div_string", "div_number", "div_table", "div_function",
  2410. "at least", "at most", "behind"
  2411. }
  2412. local function fixedlen(pt, gram, cycle)
  2413. local typ = pt.pkind
  2414. if forbidden[typ] then return false
  2415. elseif one[typ] then return 1
  2416. elseif zero[typ] then return 0
  2417. elseif typ == "string" then return #pt.as_is
  2418. elseif typ == "any" then return pt.aux
  2419. elseif typ == "choice" then
  2420. local l1, l2 = fixedlen(pt[1], gram, cycle), fixedlen(pt[2], gram, cycle)
  2421. return (l1 == l2) and l1
  2422. elseif typ == "sequence" then
  2423. local l1, l2 = fixedlen(pt[1], gram, cycle), fixedlen(pt[2], gram, cycle)
  2424. return l1 and l2 and l1 + l2
  2425. elseif typ == "grammar" then
  2426. if pt.aux[1].pkind == "ref" then
  2427. return fixedlen(pt.aux[pt.aux[1].aux], pt.aux, {})
  2428. else
  2429. return fixedlen(pt.aux[1], pt.aux, {})
  2430. end
  2431. elseif typ == "ref" then
  2432. if cycle[pt] then return false end
  2433. cycle[pt] = true
  2434. return fixedlen(gram[pt.aux], gram, cycle)
  2435. else
  2436. print(typ,"is not handled by fixedlen()")
  2437. end
  2438. end
  2439. function LL.B (pt)
  2440. pt = LL_P(pt)
  2441. local len = fixedlen(pt)
  2442. assert(len, "A 'behind' pattern takes a fixed length pattern as argument.")
  2443. if len >= 260 then error("Subpattern too long in 'behind' pattern constructor.") end
  2444. return
  2445. constructors.both("behind", pt, len)
  2446. end
  2447. end
  2448. local function nameify(a, b)
  2449. return tostring(a)..tostring(b)
  2450. end
  2451. local
  2452. function choice (a, b)
  2453. local name = tostring(a)..tostring(b)
  2454. local ch = Builder.ptcache.choice[name]
  2455. if not ch then
  2456. ch = factorize_choice(a, b) or constructors.binary("choice", a, b)
  2457. Builder.ptcache.choice[name] = ch
  2458. end
  2459. return ch
  2460. end
  2461. function LL.__add (a, b)
  2462. return
  2463. choice(LL_P(a), LL_P(b))
  2464. end
  2465. local
  2466. function sequence (a, b)
  2467. local name = tostring(a)..tostring(b)
  2468. local seq = Builder.ptcache.sequence[name]
  2469. if not seq then
  2470. seq = factorize_sequence(a, b) or constructors.binary("sequence", a, b)
  2471. Builder.ptcache.sequence[name] = seq
  2472. end
  2473. return seq
  2474. end
  2475. Builder.sequence = sequence
  2476. function LL.__mul (a, b)
  2477. return
  2478. sequence(LL_P(a), LL_P(b))
  2479. end
  2480. local
  2481. function LL_lookahead (pt)
  2482. if pt == truept
  2483. or pt == falsept
  2484. or pt.pkind == "unm"
  2485. or pt.pkind == "lookahead"
  2486. then
  2487. return pt
  2488. end
  2489. return
  2490. constructors.subpt("lookahead", pt)
  2491. end
  2492. LL.__len = LL_lookahead
  2493. LL.L = LL_lookahead
  2494. local
  2495. function LL_unm(pt)
  2496. return
  2497. factorize_unm(pt)
  2498. or constructors.subpt("unm", pt)
  2499. end
  2500. LL.__unm = LL_unm
  2501. local
  2502. function LL_sub (a, b)
  2503. a, b = LL_P(a), LL_P(b)
  2504. return LL_unm(b) * a
  2505. end
  2506. LL.__sub = LL_sub
  2507. local
  2508. function LL_repeat (pt, n)
  2509. local success
  2510. success, n = pcall(tonumber, n)
  2511. assert(success and type(n) == "number",
  2512. "Invalid type encountered at right side of '^'.")
  2513. return constructors.both(( n < 0 and "at most" or "at least" ), pt, n)
  2514. end
  2515. LL.__pow = LL_repeat
  2516. for _, cap in pairs{"C", "Cs", "Ct"} do
  2517. LL[cap] = function(pt)
  2518. pt = LL_P(pt)
  2519. return
  2520. constructors.subpt(cap, pt)
  2521. end
  2522. end
  2523. LL["Cb"] = function(aux)
  2524. return
  2525. constructors.aux("Cb", aux)
  2526. end
  2527. LL["Carg"] = function(aux)
  2528. assert(type(aux)=="number", "Number expected as parameter to Carg capture.")
  2529. assert( 0 < aux and aux <= 200, "Argument out of bounds in Carg capture.")
  2530. return
  2531. constructors.aux("Carg", aux)
  2532. end
  2533. local
  2534. function LL_Cp ()
  2535. return Cppt
  2536. end
  2537. LL.Cp = LL_Cp
  2538. local
  2539. function LL_Cc (...)
  2540. return
  2541. constructors.none("Cc", t_pack(...))
  2542. end
  2543. LL.Cc = LL_Cc
  2544. for _, cap in pairs{"Cf", "Cmt"} do
  2545. local msg = "Function expected in "..cap.." capture"
  2546. LL[cap] = function(pt, aux)
  2547. assert(type(aux) == "function", msg)
  2548. pt = LL_P(pt)
  2549. return
  2550. constructors.both(cap, pt, aux)
  2551. end
  2552. end
  2553. local
  2554. function LL_Cg (pt, tag)
  2555. pt = LL_P(pt)
  2556. if tag ~= nil then
  2557. return
  2558. constructors.both("Clb", pt, tag)
  2559. else
  2560. return
  2561. constructors.subpt("Cg", pt)
  2562. end
  2563. end
  2564. LL.Cg = LL_Cg
  2565. local valid_slash_type = setify{"string", "number", "table", "function"}
  2566. local
  2567. function LL_slash (pt, aux)
  2568. if LL_ispattern(aux) then
  2569. error"The right side of a '/' capture cannot be a pattern."
  2570. elseif not valid_slash_type[type(aux)] then
  2571. error("The right side of a '/' capture must be of type "
  2572. .."string, number, table or function.")
  2573. end
  2574. local name
  2575. if aux == 0 then
  2576. name = "/zero"
  2577. else
  2578. name = "div_"..type(aux)
  2579. end
  2580. return
  2581. constructors.both(name, pt, aux)
  2582. end
  2583. LL.__div = LL_slash
  2584. if Builder.proxymt then
  2585. for k, v in pairs(LL) do
  2586. if k:match"^__" then
  2587. Builder.proxymt[k] = v
  2588. end
  2589. end
  2590. else
  2591. LL.__index = LL
  2592. end
  2593. local factorizer
  2594. = Builder.factorizer(Builder, LL)
  2595. factorize_choice, factorize_lookahead, factorize_sequence, factorize_unm =
  2596. factorizer.choice, factorizer.lookahead, factorizer.sequence, factorizer.unm
  2597. end -- module wrapper --------------------------------------------------------
  2598. end
  2599. end
  2600. --=============================================================================
  2601. do local _ENV = _ENV
  2602. packages['constructors'] = function (...)
  2603. local getmetatable, ipairs, newproxy, print, setmetatable
  2604. = getmetatable, ipairs, newproxy, print, setmetatable
  2605. local t, u, compat
  2606. = require"table", require"util", require"compat"
  2607. local t_concat = t.concat
  2608. local copy, getuniqueid, id, map
  2609. , weakkey, weakval
  2610. = u.copy, u.getuniqueid, u.id, u.map
  2611. , u.weakkey, u.weakval
  2612. local _ENV = u.noglobals() ----------------------------------------------------
  2613. local patternwith = {
  2614. constant = {
  2615. "Cp", "true", "false"
  2616. },
  2617. aux = {
  2618. "string", "any",
  2619. "char", "range", "set",
  2620. "ref", "sequence", "choice",
  2621. "Carg", "Cb"
  2622. },
  2623. subpt = {
  2624. "unm", "lookahead", "C", "Cf",
  2625. "Cg", "Cs", "Ct", "/zero"
  2626. },
  2627. both = {
  2628. "behind", "at least", "at most", "Clb", "Cmt",
  2629. "div_string", "div_number", "div_table", "div_function"
  2630. },
  2631. none = "grammar", "Cc"
  2632. }
  2633. return function(Builder, LL) --- module wrapper.
  2634. local S_tostring = Builder.set.tostring
  2635. local newpattern, pattmt
  2636. if compat.proxies and not compat.lua52_len then
  2637. local proxycache = weakkey{}
  2638. local __index_LL = {__index = LL}
  2639. local baseproxy = newproxy(true)
  2640. pattmt = getmetatable(baseproxy)
  2641. Builder.proxymt = pattmt
  2642. function pattmt:__index(k)
  2643. return proxycache[self][k]
  2644. end
  2645. function pattmt:__newindex(k, v)
  2646. proxycache[self][k] = v
  2647. end
  2648. function LL.getdirect(p) return proxycache[p] end
  2649. function newpattern(cons)
  2650. local pt = newproxy(baseproxy)
  2651. setmetatable(cons, __index_LL)
  2652. proxycache[pt]=cons
  2653. return pt
  2654. end
  2655. else
  2656. if LL.warnings and not compat.lua52_len then
  2657. print("Warning: The `__len` metatethod won't work with patterns, "
  2658. .."use `LL.L(pattern)` for lookaheads.")
  2659. end
  2660. pattmt = LL
  2661. function LL.getdirect (p) return p end
  2662. function newpattern(pt)
  2663. return setmetatable(pt,LL)
  2664. end
  2665. end
  2666. Builder.newpattern = newpattern
  2667. local
  2668. function LL_ispattern(pt) return getmetatable(pt) == pattmt end
  2669. LL.ispattern = LL_ispattern
  2670. function LL.type(pt)
  2671. if LL_ispattern(pt) then
  2672. return "pattern"
  2673. else
  2674. return nil
  2675. end
  2676. end
  2677. local ptcache, meta
  2678. local
  2679. function resetcache()
  2680. ptcache, meta = {}, weakkey{}
  2681. Builder.ptcache = ptcache
  2682. for _, p in ipairs(patternwith.aux) do
  2683. ptcache[p] = weakval{}
  2684. end
  2685. for _, p in ipairs(patternwith.subpt) do
  2686. ptcache[p] = weakval{}
  2687. end
  2688. for _, p in ipairs(patternwith.both) do
  2689. ptcache[p] = {}
  2690. end
  2691. return ptcache
  2692. end
  2693. LL.resetptcache = resetcache
  2694. resetcache()
  2695. local constructors = {}
  2696. Builder.constructors = constructors
  2697. constructors["constant"] = {
  2698. truept = newpattern{ pkind = "true" },
  2699. falsept = newpattern{ pkind = "false" },
  2700. Cppt = newpattern{ pkind = "Cp" }
  2701. }
  2702. local getauxkey = {
  2703. string = function(aux, as_is) return as_is end,
  2704. table = copy,
  2705. set = function(aux, as_is)
  2706. return S_tostring(aux)
  2707. end,
  2708. range = function(aux, as_is)
  2709. return t_concat(as_is, "|")
  2710. end,
  2711. sequence = function(aux, as_is)
  2712. return t_concat(map(getuniqueid, aux),"|")
  2713. end
  2714. }
  2715. getauxkey.choice = getauxkey.sequence
  2716. constructors["aux"] = function(typ, aux, as_is)
  2717. local cache = ptcache[typ]
  2718. local key = (getauxkey[typ] or id)(aux, as_is)
  2719. if not cache[key] then
  2720. cache[key] = newpattern{
  2721. pkind = typ,
  2722. aux = aux,
  2723. as_is = as_is
  2724. }
  2725. end
  2726. return cache[key]
  2727. end
  2728. constructors["none"] = function(typ, aux)
  2729. return newpattern{
  2730. pkind = typ,
  2731. aux = aux
  2732. }
  2733. end
  2734. constructors["subpt"] = function(typ, pt)
  2735. local cache = ptcache[typ]
  2736. if not cache[pt] then
  2737. cache[pt] = newpattern{
  2738. pkind = typ,
  2739. pattern = pt
  2740. }
  2741. end
  2742. return cache[pt]
  2743. end
  2744. constructors["both"] = function(typ, pt, aux)
  2745. local cache = ptcache[typ][aux]
  2746. if not cache then
  2747. ptcache[typ][aux] = weakval{}
  2748. cache = ptcache[typ][aux]
  2749. end
  2750. if not cache[pt] then
  2751. cache[pt] = newpattern{
  2752. pkind = typ,
  2753. pattern = pt,
  2754. aux = aux,
  2755. cache = cache -- needed to keep the cache as long as the pattern exists.
  2756. }
  2757. end
  2758. return cache[pt]
  2759. end
  2760. constructors["binary"] = function(typ, a, b)
  2761. return newpattern{
  2762. a, b;
  2763. pkind = typ,
  2764. }
  2765. end
  2766. end -- module wrapper
  2767. end
  2768. end
  2769. --=============================================================================
  2770. do local _ENV = _ENV
  2771. packages['init'] = function (...)
  2772. local getmetatable, setmetatable, pcall
  2773. = getmetatable, setmetatable, pcall
  2774. local u = require"util"
  2775. local copy, map, nop, t_unpack
  2776. = u.copy, u.map, u.nop, u.unpack
  2777. local API, charsets, compiler, constructors
  2778. , datastructures, evaluator, factorizer
  2779. , locale, printers, re
  2780. = t_unpack(map(require,
  2781. { "API", "charsets", "compiler", "constructors"
  2782. , "datastructures", "evaluator", "factorizer"
  2783. , "locale", "printers", "re" }))
  2784. local _, package = pcall(require, "package")
  2785. local _ENV = u.noglobals() ----------------------------------------------------
  2786. local VERSION = "0.12"
  2787. local LuVERSION = "0.1.0"
  2788. local function global(self, env) setmetatable(env,{__index = self}) end
  2789. local function register(self, env)
  2790. pcall(function()
  2791. package.loaded.lpeg = self
  2792. package.loaded.re = self.re
  2793. end)
  2794. if env then
  2795. env.lpeg, env.re = self, self.re
  2796. end
  2797. return self
  2798. end
  2799. local
  2800. function LuLPeg(options)
  2801. options = options and copy(options) or {}
  2802. local Builder, LL
  2803. = { options = options, factorizer = factorizer }
  2804. , { new = LuLPeg
  2805. , version = function () return VERSION end
  2806. , luversion = function () return LuVERSION end
  2807. , setmaxstack = nop --Just a stub, for compatibility.
  2808. }
  2809. LL.util = u
  2810. LL.global = global
  2811. LL.register = register
  2812. ;-- Decorate the LuLPeg object.
  2813. charsets(Builder, LL)
  2814. datastructures(Builder, LL)
  2815. printers(Builder, LL)
  2816. constructors(Builder, LL)
  2817. API(Builder, LL)
  2818. evaluator(Builder, LL)
  2819. ;(options.compiler or compiler)(Builder, LL)
  2820. locale(Builder, LL)
  2821. LL.re = re(Builder, LL)
  2822. return LL
  2823. end -- LuLPeg
  2824. local LL = LuLPeg()
  2825. return LL
  2826. end
  2827. end
  2828. --=============================================================================
  2829. do local _ENV = _ENV
  2830. packages['locale'] = function (...)
  2831. local extend = require"util".extend
  2832. local _ENV = require"util".noglobals() ----------------------------------------
  2833. return function(Builder, LL) -- Module wrapper {-------------------------------
  2834. local R, S = LL.R, LL.S
  2835. local locale = {}
  2836. locale["cntrl"] = R"\0\31" + "\127"
  2837. locale["digit"] = R"09"
  2838. locale["lower"] = R"az"
  2839. locale["print"] = R" ~" -- 0x20 to 0xee
  2840. locale["space"] = S" \f\n\r\t\v" -- \f == form feed (for a printer), \v == vtab
  2841. locale["upper"] = R"AZ"
  2842. locale["alpha"] = locale["lower"] + locale["upper"]
  2843. locale["alnum"] = locale["alpha"] + locale["digit"]
  2844. locale["graph"] = locale["print"] - locale["space"]
  2845. locale["punct"] = locale["graph"] - locale["alnum"]
  2846. locale["xdigit"] = locale["digit"] + R"af" + R"AF"
  2847. function LL.locale (t)
  2848. return extend(t or {}, locale)
  2849. end
  2850. end -- Module wrapper --------------------------------------------------------}
  2851. end
  2852. end
  2853. --=============================================================================
  2854. do local _ENV = _ENV
  2855. packages['optimizer'] = function (...)
  2856. -- Nothing for now.
  2857. end
  2858. end
  2859. return require"init"
  2860. -- The Romantic WTF public license.
  2861. -- --------------------------------
  2862. -- a.k.a. version "<3" or simply v3
  2863. --
  2864. --
  2865. -- Dear user,
  2866. --
  2867. -- The LuLPeg library
  2868. --
  2869. -- \
  2870. -- '.,__
  2871. -- \ /
  2872. -- '/,__
  2873. -- /
  2874. -- /
  2875. -- /
  2876. -- has been / released
  2877. -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  2878. -- under the Romantic WTF Public License.
  2879. -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~`,´ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  2880. -- I hereby grant you an irrevocable license to
  2881. -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  2882. -- do what the gentle caress you want to
  2883. -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  2884. -- with this lovely
  2885. -- ~ ~ ~ ~ ~ ~ ~ ~
  2886. -- / library...
  2887. -- / ~ ~ ~ ~
  2888. -- / Love,
  2889. -- # / ','
  2890. -- ####### ·
  2891. -- #####
  2892. -- ###
  2893. -- #
  2894. --
  2895. -- -- Pierre-Yves
  2896. --
  2897. --
  2898. --
  2899. -- P.S.: Even though I poured my heart into this work,
  2900. -- I _cannot_ provide any warranty regarding
  2901. -- its fitness for _any_ purpose. You
  2902. -- acknowledge that I will not be held liable
  2903. -- for any damage its use could incur.
  2904. --
  2905. -- -----------------------------------------------------------------------------
  2906. --
  2907. -- LuLPeg, Copyright (C) 2013 Pierre-Yves Gérardy.
  2908. --
  2909. -- The `re` module and lpeg.*.*.test.lua,
  2910. -- Copyright (C) 2013 Lua.org, PUC-Rio.
  2911. --
  2912. -- Permission is hereby granted, free of charge,
  2913. -- to any person obtaining a copy of this software and
  2914. -- associated documentation files (the "Software"),
  2915. -- to deal in the Software without restriction,
  2916. -- including without limitation the rights to use,
  2917. -- copy, modify, merge, publish, distribute, sublicense,
  2918. -- and/or sell copies of the Software,
  2919. -- and to permit persons to whom the Software is
  2920. -- furnished to do so,
  2921. -- subject to the following conditions:
  2922. --
  2923. -- The above copyright notice and this permission notice
  2924. -- shall be included in all copies or substantial portions of the Software.
  2925. --
  2926. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2927. -- EXPRESS OR IMPLIED,
  2928. -- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2929. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  2930. -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2931. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  2932. -- TORT OR OTHERWISE, ARISING FROM,
  2933. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  2934. -- THE SOFTWARE.