Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lpegrex grammar #4

Open
mingodad opened this issue Dec 10, 2022 · 0 comments
Open

lpegrex grammar #4

mingodad opened this issue Dec 10, 2022 · 0 comments

Comments

@mingodad
Copy link

mingodad commented Dec 10, 2022

Would be nice to have a lpegrex grammar parser as example, I've got this one grammar so far:

-- lpegrex in lpeg
pattern         <- exp !.
exp             <- S (grammar / alternative)

alternative     <- seq ('/' S seq)*
seq             <- prefix*
prefix          <- '&' S prefix / '!' S prefix / suffix
suffix          <- primary S (([+*?] S
                            / '^' (
				[-+]? num  -- (exactly n / at (least / most) n repetitions
				/ '^' name -- Syntax of relabel module. The pattern p^l is equivalent to p + lpeglabel.T(l).
				)
                            / '->' S (string / pcap / name / num)  -- capture
                            / '=>' S name) S)* -- match time capture

primary         <- '(' exp  ')' S / string / keyword / class / defined
                 / '{:' (name ':')? exp ':}' S  -- (named or anonymous) group capture
                 / '=' name           -- back reference
                 / '@' exp   -- for throwing labels errors on failure of expected matches
                 / pcap
                 / '{~' exp '~}' S  -- substitution capture
                 / '{|' exp '|}' S  -- table capture
                 / '{' exp '}' S    -- simple capture
		 / '~?' S             -- optional capture
		 / '~>' S ( 'foldleft' / 'foldright' / 'rfoldleft' / 'rfoldright' / name ) S -- fold capture
		 / '$' (string / name / num / pcap)
                 / '.' S
                 / name S !(asttag / arrow )
                 / '<' S name '>' S    -- old-style non terminals
		 / '%{' S name '}' S   -- Syntax of relabel module. Equivalent to lpeglabel.T(l)

grammar         <- definition+
definition      <- name S asttag? arrow exp

class           <- '[' '^'? item (!']' item)* ']' S
item            <- defined / range / .
range           <- . '-' [^]]

S               <- (%s / '--' [^%nl]*)*   -- spaces and comments
name            <- [A-Za-z_]([A-Za-z0-9_] / '-' !'>' )* S
arrow           <- (  '<--'    -- rule
                    / '<=='    -- for rules that capture AST Nodes
		    / '<-|'    -- for rules that capture tables
		    / '<-'
		    ) S
num             <- [0-9]+ S
string          <- ('"' [^"]* '"' / "'" [^']* "'") S
defined         <- '%' name  -- pattern defs[name] or a pre-defined pattern
keyword         <- '`' [^`]+ '`' S  -- tokens/keywords with automatic skipping
asttag          <- ':' S name  -- Capture tagged node rule
pcap            <- '{}' S -- position capture

But it doesn't do any capture to genereate an AST.

Can someone help finish it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant