|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/Arith4.lua.html
-- http://angg.twu.net/LUA/Arith4.lua
-- (find-angg "LUA/Arith4.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun a () (interactive) (find-angg "LUA/Arith4.lua"))
-- (find-es "lpeg" "lpegrex")
-- (find-lpegrexpage)
-- (find-lpegrextext)
-- (find-lpegrexfile "parsers/lua.lua")
Path.prepend("cpath", "~/usrc/lpeglabel/?.so")
Path.prepend("path", "~/usrc/lpegrex/?.lua")
lpegrex = require "lpegrex"
Grammar = [==[
top <-- plusexpr
plusexpr <-| timesexpr ({"+"} timesexpr)*
timesexpr <-| norp ({"*"} norp)*
norp <-| num / ("(" plusexpr ")")
num <-- {%d}
]==]
Grammar = [==[
top <-- expr
SKIP <- %s+
Id <== NAME
NAME <-- {NAME_PREFIX NAME_SUFFIX?} SKIP
NAME_PREFIX <-- [_a-zA-Z]
NAME_SUFFIX <-- [_a-zA-Z0-9]+
Nil <== `nil`
opor :BinaryOp <== `or`->'or' @exprand
opand :BinaryOp <== `and`->'and' @exprcmp
opcmp :BinaryOp <== (`==`->'eq' / `~=`->'ne' / `<=`->'le' / `>=`->'ge' / `<`->'lt' / `>`->'gt') @exprbor
opbor :BinaryOp <== `|`->'bor' @exprbxor
opbxor :BinaryOp <== `~`->'bxor' @exprband
opband :BinaryOp <== `&`->'band' @exprbshift
opbshift :BinaryOp <== (`<<`->'shl' / `>>`->'shr') @exprconcat
opconcat :BinaryOp <== `..`->'concat' @exprconcat
oparit :BinaryOp <== (`+`->'add' / `-`->'sub') @exprfact
opfact :BinaryOp <== (`*`->'mul' / `//`->'idiv' / `/`->'div' / `%`->'mod') @exprunary
oppow :BinaryOp <== `^`->'pow' @exprunary
opunary :UnaryOp <== (`not`->'not' / `#`->'len' / `-`->'unm' / `~`->'bnot') @exprunary
Paren <== `(` @expr @`)`
expr <-- expror
expror <-- (exprand opor*)~>foldleft
exprand <-- (exprcmp opand*)~>foldleft
exprcmp <-- (exprbor opcmp*)~>foldleft
exprbor <-- (exprbxor opbor*)~>foldleft
exprbxor <-- (exprband opbxor*)~>foldleft
exprband <-- (exprbshift opband*)~>foldleft
exprbshift <-- (exprconcat opbshift*)~>foldleft
exprconcat <-- (exprarit opconcat*)~>foldleft
exprarit <-- (exprfact oparit*)~>foldleft
exprfact <-- (exprunary opfact*)~>foldleft
exprunary <-- opunary / exprpow
exprpow <-- (exprsimple oppow*)~>foldleft
exprsimple <-- [0-9]+
exprprimary <-- Id / Paren
]==]
patt = lpegrex.compile(Grammar)
bigstr = "(1+2)*3"
ast, errlabel, errpos = patt:match(bigstr)
PPPV(ast, errlabel, errpos)
--[[
* (eepitch-lua52)
* (eepitch-kill)
* (eepitch-lua52)
dofile "Arith4.lua"
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: