|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://anggtwu.net/LUA/Gram3.lua.html
-- http://anggtwu.net/LUA/Gram3.lua
-- (find-angg "LUA/Gram3.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Gram3.lua"))
-- «.Block» (to "Block")
-- «.Block-test» (to "Block-test")
-- «.Call» (to "Call")
-- «.var» (to "var")
-- «.var-tests» (to "var-tests")
-- «.callargs» (to "callargs")
-- «.callargs-tests» (to "callargs-tests")
-- «.opor» (to "opor")
-- «.opor-tests» (to "opor-tests")
-- «.expr» (to "expr")
-- «.expr-tests» (to "expr-tests")
require "Gram2" -- (find-angg "LUA/Gram2.lua")
gr,V,VA,VE = Gram.new()
wordpat = Cs(R("__", "AZ", "az") *
R("__", "AZ", "az", "09")^0)
keywords,K,KE,KW,NKW = Keywords.from(wordpat)
V.S = (S" \t\n")^0
s = V.S
_ = V.S
V[","] = P","
V["]"] = P"]"
V["}"] = P"}"
V[")"] = P")"
-- (find-es "lpeg" "lpegrex")
-- (find-es "lpeg" "lpegrex" "lua.lua")
-- «Block» (to ".Block")
-- (find-lpegrexfile "parsers/lua.lua" "\nBlock")
V.Block = ((V.Label + V.Return + V.Break + V.Goto + V.Do
+ V.While + V.Repeat + V.If + V.ForNum + V.ForIn
+ V.FuncDef + V.FuncDecl + V.VarDecl + V.Assign + V.call + P";")*_)^0
VA.Label = P"::" * VE.NAME * P"::"
VA.Return = K"return" *_* V.exprlist^0
VA.Break = K"break"
VA.Goto = K"goto" *_* VE.NAME
VA.Do = K"do" *_* V.Block *_* KE"end"
VA.While = K"while" *_* VE.expr *_* KE"do" *_* V.Block *_* KE"end"
VA.Repeat = K"repeat" *_* V.Block *_* KE"until" *_* V.expr
VA.If = K"if" *_* VE.expr *_* KE"then" *_* V.Block
* (K"elseif" *_* V.expr *_* KE"then" *_* V.Block)^0
* (K"else" *_* V.Block)^-1
* KE"end"
VA.ForNum = K"for" *_* V.Id *_
* P"=" *_* VE.expr *_* VE[","] *_* VE.expr *_* (P"," *_* VE.expr *_)^-1
* KE"do" *_* V.Block *_* KE"end"
VA.ForIn = K"for" *_* V.idlist *_* K"in" *_* V.exprlist *_
* KE"do" *_* V.Block *_* KE"end"
VA.FuncDef = K"function" *_* VE.funcname *_* VE.funcbody
VA.FuncDecl = K"local" *_* K"function" *_* VE.Id *_* VE.funcbody
VA.VarDecl = K"local" *_* V.iddecllist * (_* P"=" *_* V.exprlist)^-1
VA.Assign = V.varlist *_* P"=" *_* V.exprlist
-- «Block-test» (to ".Block-test")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
= keywords
= gr:cm("Block", "")
= gr:cm("Block", "a b")
= gr:cm("Block", "a=b")
= gr:cm("Block", "return")
= gr:cm("Block", "return a,b,c")
= gr:cm("Block", "break")
= gr:cm("Block", "goto foo")
= gr:cm("Block", "do a=b end")
= gr:cm("Block", "do a=b c(d) end")
= gr:cm("Block", "do a=b c(d) e end")
= gr:cm("Block", "while a do end")
= gr:cm("Block", "while a do b=c end")
= gr:cm("Block", "repeat a=b until c")
= gr:cm("Block", "repeat a until c")
= gr:cm("Block", "if a then b=c end")
= gr:cm("Block", "if a then b=c else d=e end")
= gr:cm("Block", "if a then b=c else d=e else f=g end")
= gr:cm("Block", "if a then b end")
= gr:cm("Block", "if a then b=c elseif d then e=f end")
= gr:cm("Block", "for a=b do end")
= gr:cm("Block", "for a=b,c do end")
= gr:cm("Block", "for a=b,c,d do end")
= gr:cm("Block", "for a=b,c,d,e do end")
= gr:cm("Block", "for a,b in c,d do end")
= gr:cm("Block", "function a (c) end")
= gr:cm("Block", "function a.b(c) end")
= gr:cm("Block", "function a end")
= gr:cm("Block", "function (c) end")
= gr:cm("Block", "local function a (c) end")
= gr:cm("Block", "local function a.b(c) end")
+ V.FuncDef + V.FuncDecl + V.VarDecl + V.Assign + V.call + P";")*_)^0
grd:dbg("Block Assign Id call Call")
= grd
= grd:cm("Block", "e")
= grd:cm("call", "e")
--]]
-- (find-lpegrexfile "parsers/lua.lua" "\nNumber")
V.Number = Cttag("N", Cs(R("09")^1))
V.String = Cttag("Str", Cs(V.STRING))
V.Boolean = Cs(P"false" + P"true")
V.Nil = Cs(P"nil")
V.Varargs = Cs(P"...")
VA.Id = Cs(NKW)
VA.IdDecl = Cs(NKW)
V.fieldsep = S",;"
V.Function = P"notyet"
V.Table = Cttag("{}", P"{" *_* zeroormorecc(V.field, V.fieldsep) *_* P"}")
V.Paren = P"(" *_* V.expr *_* P")"
V.Pair = Cttag("Pair", V.PairL *_*P"="*_* V.expr)
V.PairL = P"["*_* V.expr *_*P"]" + V.NAME
-- «Call» (to ".Call")
-- (find-lpegrexfile "parsers/lua.lua" "\nCall")
V.Call = V.callargs
V.CallMethod = P":"*_* V.NAME *_* V.callargs
V.DotIndex = P"."*_* V.NAME
V.ColonIndex = P":"*_* V.NAME
V.KeyIndex = P"["*_* V.expr *_*P"]"
-- (find-lpegrexfile "parsers/lua.lua" "\nindexsuffix")
V.indexsuffix = V.DotIndex + V.KeyIndex
V.callsuffix = V.Call + V.CallMethod
-- «var» (to ".var")
-- (find-lpegrexfile "parsers/lua.lua" "\nvar ")
V.var = assocpost (V.exprprimary, endingwith(V.callsuffix, V.indexsuffix)) + V.Id
V.call = assocpostp(V.exprprimary, endingwith(V.indexsuffix, V.callsuffix))
V.exprsuffixed = assocpost (V.exprprimary, V.indexsuffix + V.callsuffix)
V.funcname = Ct(V.Id * (_*V.DotIndex)^0 * (_*V.ColonIndex)^-1) / foldpost
-- «var-tests» (to ".var-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
= gr:cm("var", "a")
= gr:cm("call", "a")
= gr:cm("var", "a.b")
= gr:cm("var", "a.b(c)")
= gr:cm("call", "a.b(c)")
= gr:cm("var", "a.b(c).d")
= gr:cm("var", "a:b(c).d")
= gr:cm("var", "a[234].d")
= gr:cm("var", "a[234]")
= gr:cm("funcname", "a.b.c:d")
--]]
-- (find-lpegrexfile "parsers/lua.lua" "\nfuncbody")
V.funcbody = P"(" *_* V.funcargs *_* P")" *_* V.Block *_* KE"end"
V.field = V.Pair + V.expr
V.fieldsep = P"," + P";"
-- «callargs» (to ".callargs")
-- (find-lpegrexfile "parsers/lua.lua" "\ncallargs")
V.callargs = Cast("()", P"("*_* zeroormorec(V.expr, P",") *_*P")") + V.Table + V.String
V.idlist = oneormorec (V.Id, P",")
V.iddecllist = oneormorec (V.IdDecl, P",")
V.funcargs = oneormorec (V.Id, P",") * (_*P","*_* V.Varargs)^-1 + V.Varargs^-1
V.exprlist = oneormorec (V.expr, P",")
V.varlist = oneormorec (V.var, P",")
-- «callargs-tests» (to ".callargs-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
= gr:cm("callargs", "(a, bc, def)")
= gr:cm("callargs", "(a, bc, def, ...)")
= gr:cm("callargs", "(...)")
= gr:cm("callargs", "()")
= gr:cm("callargs", "'abc'")
= gr:cm("callargs", "{abc}")
--]]
-- «opor» (to ".opor")
-- (find-lpegrexfile "parsers/lua.lua" "opor")
V.opor = anyof "or"
V.opand = anyof "and"
V.opcmp = anyof "== ~= <= >= < >"
V.opbor = anyof "|"
V.opbxor = anyof "~"
V.opband = anyof "&"
V.opbshift = anyof "<< >>"
V.opconcat = anyof ".."
V.oparit = anyof "+ -"
V.opfact = anyof "* // / %"
V.oppow = anyof "^"
V.opunary = anyof "not # - ~"
-- «opor-tests» (to ".opor-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
V.pat = assocpre(V.opunary, V.Id)
= gr:cm("pat", "not # ~ a")
--]]
-- «expr» (to ".expr")
-- (find-lpegrexfile "parsers/lua.lua" "\nexpr")
V.expr = V.expror
V.expror = assocl(V.exprand, V.opor )
V.exprand = assocl(V.exprcmp, V.opand )
V.exprcmp = assocl(V.exprbor, V.opcmp )
V.exprbor = assocl(V.exprbxor, V.opbor )
V.exprbxor = assocl(V.exprband, V.opbxor )
V.exprband = assocl(V.exprbshift, V.opband )
V.exprbshift = assocl(V.exprconcat, V.opbshift)
V.exprconcat = assocl(V.exprarit, V.opconcat)
V.exprarit = assocl(V.exprfact, V.oparit )
V.exprfact = assocl(V.exprunary, V.opfact )
V.exprunary = assocpre(V.opunary, V.exprpow )
V.exprpow = assocr(V.exprsimple, V.oppow )
V.exprsimple = V.Nil + V.Boolean + V.Number + V.String + V.Varargs
+ V.Function + V.Table + V.exprsuffixed
V.exprprimary = V.Id + V.Paren
-- «expr-tests» (to ".expr-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
= gr:cm("expr", "2 + 3 * 'a'")
= gr:cm("expr", "2^3^4 - 5*6*7 - (8 / 9)")
= gr:cm("expr", "{a=3, ['b']=4, 5}")
= gr:cm("expr", "a")
= gr:cm("expr", "a.b")
--]]
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Gram3.lua"
= gr:cm("NAME", "foo_9")
= gr:cm("Id", "foo_9")
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: