1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-05-13 06:28:24 +03:00
ink-instead/pink/parser.lua
2017-08-08 16:28:55 +02:00

65 lines
1.7 KiB
Lua

local lpeg = require "lpeg"
lpeg.locale(lpeg)
local S,C,Ct,Cc,Cg,Cb,Cf,Cmt,P,V =
lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cb, lpeg.Cf, lpeg.Cmt,
lpeg.P, lpeg.V
local parserLogger = print
local eof = -1
local sp = S" \t" ^0 + eof
local wh = S" \t\r\n" ^0 + eof
local nl = S"\r\n" ^1 + eof
local id = (lpeg.alpha + '_') * (lpeg.alnum + '_')^0
local addr = C(id) * ('.' * C(id))^-1
local todo = sp * 'TODO:' * sp * (1-nl)^0 / parserLogger * wh -- TODO log location
local commOL = sp * '//' * sp * (1-nl)^0 * wh
local commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh
local comm = commOL + commML + todo
local glue = Ct(P'<>'/'glue') *wh -- FIXME do not consume spaces after glue
local divertSym = '->' *wh
local divertEndSym = Ct(C('END')) *wh
local divertEnd = divertSym * divertEndSym
local divertJump = Ct(divertSym/'divert' * addr * wh)
local divert = divertEnd + divertJump
local knot = Ct(P('=')^2/'knot' * wh * C(id) * wh * P('=')^0) * wh
local stitch = Ct(P('=')^1/'stitch' * wh * C(id) * wh * P('=')^0) * wh
local optDiv = '[' * C((P(1) - ']')^0) * ']'
local optStars = wh * C(P'*') * (sp * C'*')^0
local tag = Ct(wh * P('#')/'tag' * wh * V'text' * wh)
local ink = P({
"lines",
stmt = glue + divert + knot + stitch + V'option' + optDiv + comm + V'include' + tag,
text = C((1-nl-V'stmt')^1) *wh,
textE = C((1-nl-V'stmt')^0) *wh,
optAnsWithDiv = V'textE' * optDiv * V'textE' * wh,
optAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh?
optAns = V'optAnsWithDiv' + V'optAnsWithoutDiv',
option = Ct(Cc'option' * optStars * sp * V'optAns'),
include = Ct(P('INCLUDE')/'include' * wh * V'text' * wh),
para = Ct(Cc'para' * V'text'),
line = V'stmt' + V'para',
lines = Ct(V'line'^0)
})
return ink;