Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/LpegRex3.lua.html -- http://anggtwu.net/LUA/LpegRex3.lua -- (find-angg "LUA/LpegRex3.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- This is similar to: -- (find-angg "LUA/Re2.lua") -- but for lpegrex.lua. -- -- (defun e1 () (interactive) (find-angg "LUA/ELpeg1.lua")) -- (defun l3 () (interactive) (find-angg "LUA/LpegRex3.lua")) -- (find-es "lpeg" "lpegrex") -- «.LpegRex» (to "LpegRex") -- «.LpegRex-tests» (to "LpegRex-tests") -- «.Wikipedia» (to "Wikipedia") -- «.Wikipedia-tests» (to "Wikipedia-tests") -- _ ____ -- | | _ __ ___ __ _| _ \ _____ __ -- | | | '_ \ / _ \/ _` | |_) / _ \ \/ / -- | |___| |_) | __/ (_| | _ < __/> < -- |_____| .__/ \___|\__, |_| \_\___/_/\_\ -- |_| |___/ -- -- «LpegRex» (to ".LpegRex") LpegRex = Class { type = "LpegRex", from = function (str) return LpegRex {str=str} end, tagasast = function (name,node) node[0] = name; return AST(node) end, __index = { defs = {__options={}}, compile = function (lr) return lpegrex.compile(lr.str, lr.defs) end, match = function (lr,subj,init) return lr:compile():match(subj,init) end, pm = function (lr,subj) return PP(lr:compile():match(subj)) end, pmt = function (lr,subj) print(trees(lr:compile():match(subj))) end, }, } lre = LpegRex.from -- «LpegRex-tests» (to ".LpegRex-tests") --[==[ * (eepitch-lua52) * (eepitch-kill) * (eepitch-lua52) dofile "LpegRex3.lua" loadlpegrex() -- (find-angg "LUA/lua50init.lua" "loadlpegrex") require "Re2" -- (find-angg "LUA/Re2.lua" "Re-tests") rre "{ '<' {[io]} {[0-9]+} '>' } {.*}" :pm "<i42> 2+3;" -- re.lua lre "{ '<' {[io]} {[0-9]+} '>' } {.*}" :pm "<i42> 2+3;" -- lpegrex -- (find-es "lpeg" "lpegrex-tag") -- (find-lpegrexpage 2 "AST Nodes" "NodeName <== patt") -- (find-lpegrextext 2 "AST Nodes" "NodeName <== patt") -- (find-lpegrexpage 5 "pos = 'init'") -- (find-lpegrextext 5 "pos = 'init'") -- (find-lpegrexpage 6 "tag = function(tag, node)") -- (find-lpegrextext 6 "tag = function(tag, node)") require "ELpeg1" lre " foo <== {.} {.} " :pm "ab" lre " foo <== {.} {.} " :pmt "ab" LpegRex.__index.defs.__options.pos = "b" LpegRex.__index.defs.__options.endpos = "e" LpegRex.__index.defs.__options.tag = function (name, node) node[0] = name return AST(node) end -- Or: LpegRex.__index.defs.__options = {pos="b", endpos="e", tag=LpegRex.tagasast} -- Constant capture -- A VA with a different name lre " top <- foo foo <- {.} {.} " :pmt "ab" lre " top <- foo foo <-- {.} {.} " :pmt "ab" lre " top <- foo foo <-- $'bar' {.} {.} " :pmt "ab" lre " top <- foo foo <-| {.} {.} " :pm "ab" lre " top <- foo foo <== {.} {.} " :pm "ab" lre " top <- foo foo <== {.} {.} " :pmt "ab" lre " top <- foo foo : bar <== {.} {.} " :pmt "ab" --]==] -- __ ___ _ _ _ _ -- \ \ / (_) | _(_)_ __ ___ __| (_) __ _ -- \ \ /\ / /| | |/ / | '_ \ / _ \/ _` | |/ _` | -- \ V V / | | <| | |_) | __/ (_| | | (_| | -- \_/\_/ |_|_|\_\_| .__/ \___|\__,_|_|\__,_| -- |_| -- -- A figure from the Wikipedia: -- 2hT6: (c2m231introp 5 "gramatica-fig") -- (c2m231introa "gramatica-fig") -- http://anggtwu.net/LATEX/2023-2-C2-Tudo.pdf#page=6 -- https://en.wikipedia.org/wiki/Context-free_grammar -- -- «Wikipedia» (to ".Wikipedia") wikipedia_lang_example = "if (x>9) { x=0; y=y+1;}" wikipedia_lang_lpegrex = [=[ StmtList <== Stmt+ Stmt <== Id `=` Expr `;` / `{` StmtList `}` / `if` `(` Expr `)` Stmt Expr <== Expr0 (Optr Expr0)* Expr0 <== Id / Num Id <== {[xy]} Num <== {[019]} Optr <== {[>+]} NAME_PREFIX <-- [_a-zA-Z] NAME_SUFFIX <-- [_a-zA-Z0-9]+ SKIP <-- ' '* ]=] -- «Wikipedia-tests» (to ".Wikipedia-tests") --[==[ * (eepitch-lua52) * (eepitch-kill) * (eepitch-lua52) dofile "LpegRex3.lua" require "ELpeg1" loadlpegrex() -- (find-angg "LUA/lua50init.lua" "loadlpegrex") LpegRex.__index.defs.__options = {pos="b", endpos="e", tag=LpegRex.tagasast} lre(wikipedia_lang_lpegrex) :pmt(wikipedia_lang_example) p = lre(wikipedia_lang_lpegrex) = p = p:match(wikipedia_lang_example) = p:match(wikipedia_lang_example, nil, "Num") -- (find-lpegrexfile "parsers/lua.lua") --]==] -- Local Variables: -- coding: utf-8-unix -- End: