As title say, I know lua has a offical extended BNF in The Complete Syntax of Lua. I want to write a PEG to pass to lpeg.re.compile to parse lua itself. Maybe the Lua PEG is something like it's BNF. I have read the BNF and try to translate it to a PEG, but I found Numeral and LiteralString it hard to write. Is there someone had do something like this?
local lpeg = require "lpeg"
local re = lpeg.re
local p = re.compile([[
chunk <- block
block <- stat * retstat ?
stat <- ';' /
varlist '=' explist /
functioncall /
label /
'break' /
'goto' Name /
'do' block 'end' /
'while' exp 'do' block 'end' /
'repeat' block 'until' exp /
'if' exp 'then' block ('elseif' exp 'then' block) * ('else' block) ? 'end' /
'for' Name '=' exp ',' exp (',' exp) ? 'do' block 'end' /
'for' namelist 'in' explist 'do' block 'end' /
'function' funcname funcbody /
'local function' Name funcbody /
'local' attnamelist ('=' explist) ?
attnamelist <- Name attrib (',' Name attrib) *
attrib <- ('<' Name '>') ?
retstat <- 'return' explist ? ';' ?
label <- '::' Name '::'
funcname <- Name ('.' Name) * (':' Name) ?
varlist <- var (',' var) *
var <- Name / prefixexp '[' exp ']' / prefixexp '.' Name
namelist <- Name (',' Name) *
explist <- exp (',' exp) *
exp <- 'nil' / 'false' / 'true' / Numeral / LiteralString / "..." / functiondef /
prefixexp / tableconstructor / exp binop exp / unop exp
prefixexp <- var / functioncall / '(' exp ')'
functioncall <- prefixexp args / prefixexp ":" Name args
args <- '(' explist ? ')' / tableconstructor / LiteralString
functiondef <- 'function' funcbody
funcbody <- '(' parlist ? ')' block 'end'
parlist <- namelist (',' '...') ? / '...'
tableconstructor <- '{' fieldlist ? '}'
fieldlist <- field (fieldsep field) * fieldsep ?
field <- '[' exp ']' '=' exp / Name '=' exp / exp
fieldsep <- ',' / ';'
binop <- '+' / '-' / ‘*’ / '/' / '//' / '^' / '%' /
'&' / '~' / '|' / '>>' / '<<' / '..' /
'<' / '<=' / '>' / '>=' / '==' / '~=' /
'and' / 'or'
unop <- '-' / 'not' / '#' / '~'
saveword <- "and" / "break" / "do" / "else" / "elseif" / "end" /
"false" / "for" / "function" / "goto" / "if" / "in" /
"local" / "nil" / "no"t / "or" / "repeat" / "return" /
"then" / "true" / "until" / "while"
Name <- ! saveword / name
Numeral <-
LiteralString <-
]])