Skip to content

Instantly share code, notes, and snippets.

@premek
Created June 19, 2017 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save premek/9a8ebcc8275d16c7c3c5ac2fcf1893fb to your computer and use it in GitHub Desktop.
Save premek/9a8ebcc8275d16c7c3c5ac2fcf1893fb to your computer and use it in GitHub Desktop.
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 eof = -1
local sp = S" \t" ^0 + eof
local wh = S" \t\r\n" ^0 + eof
local nl = S"\r\n" ^1 + eof
local symbols = S":{}, \t\r\n"
local iid = C((P(1)-symbols)^1)
local id = Ct(Cc"id" * iid) *wh
local num = Ct(Cc"num" * C(lpeg.digit^1)) *wh
local params = Ct(Cc"params" * iid * (P"," * wh * iid)^0) *wh
local g = P({
"prog",
block = Ct(P"{"/"block" * ((V'stmt'+V'block')^0) * "}"),
assign = Ct(Cc"assign" * id * params^0 * ":" * wh * V'stmt'),
stmt = (V'block' + V'assign' + id + num)*wh,
prog = Ct(V'stmt'^1)
})
require "pl.pretty".dump(g:match("fun a, b,c : {A:2 34}"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment