regexper-static/src/js/parser/javascript/grammar.peg

57 lines
2.3 KiB
Plaintext

grammar JavascriptRegexp
root <- ( ( "/" regexp "/" flags:[yigmu]* ) / regexp flags:""? ) <Root>
regexp <- match:match alternates:( "|" match )* <Regexp>
match <- (!repeat) parts:match_fragment* <Match>
anchor <- ( "^" / "$" ) <Anchor>
match_fragment <- content:( anchor / subexp / charset / terminal ) repeat:repeat? <MatchFragment>
repeat <- spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) greedy:"?"? <Repeat>
repeat_any <- "*" <RepeatAny>
repeat_required <- "+" <RepeatRequired>
repeat_optional <- "?" <RepeatOptional>
repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}"
/ "{" min:[0-9]+ ",}"
/ "{" exact:[0-9]+ "}" ) <RepeatSpec>
subexp <- "(" capture:( "?:" / "?=" / "?!" )? regexp ")" <Subexp>
charset <- "[" invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset>
charset_range <- first:charset_range_terminal "-" last:charset_range_terminal <CharsetRange>
charset_terminal <- charset_escape <CharsetEscape>
/ charset_literal <Literal>
charset_range_terminal <- charset_range_escape <CharsetEscape>
/ charset_literal <Literal>
charset_escape <- "\\" esc:(
code:[bdDfnrsStvwW] arg:""?
/ control_escape
/ octal_escape
/ hex_escape
/ unicode_escape
/ null_escape )
charset_range_escape <- "\\" esc:(
code:[bfnrtv] arg:""?
/ control_escape
/ octal_escape
/ hex_escape
/ unicode_escape
/ null_escape )
charset_literal <- ( ""? literal:[^\\\]] )
/ ( literal:"\\" &"c" )
/ ( "\\" literal:[^bdDfnrsStvwW] )
terminal <- "." <AnyCharacter>
/ escape <Escape>
/ literal <Literal>
escape <- "\\" esc:(
code:[bBdDfnrsStvwW1-9] arg:""?
/ control_escape
/ octal_escape
/ hex_escape
/ unicode_escape
/ null_escape )
literal <- ( ""? literal:[^|\\/.\[\(\)?+*$^] )
/ ( literal:"\\" &"c" )
/ ( "\\" literal:. )
control_escape <- code:"c" arg:[a-zA-Z]
octal_escape <- code:"0" arg:[0-7]+
hex_escape <- code:"x" arg:( [0-9a-fA-F] [0-9a-fA-F] )
unicode_escape <- code:"u" arg:( [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] )
null_escape <- code:"0" arg:""?