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

44 lines
2.0 KiB
Plaintext
Raw Normal View History

2014-11-26 00:37:54 +00:00
grammar JavascriptRegexp
root <- ( ( "/" regexp "/" _flags:[igm]* ) / regexp ""? ) <Root>
regexp <- _match:match _alternates:( "|" match )* <Regexp>
match <- _anchor_start:anchor_start?
(!repeat) _parts:match_fragment*
_anchor_end:anchor_end? <Match>
match_fragment <- _content:( subexp / charset / terminal ) _repeat:repeat? <MatchFragment>
2014-11-26 00:37:54 +00:00
anchor_start <- "^"
anchor_end <- "$"
repeat <- _spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) _greedy:repeat_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>
2014-11-26 00:37:54 +00:00
repeat_greedy <- "?"
2014-12-08 02:16:23 +00:00
subexp <- "(" _capture:( subexp_no_capture / subexp_positive_lookahead / subexp_negative_lookahead )? regexp ")" <Subexp>
2014-11-26 00:37:54 +00:00
subexp_no_capture <- "?:"
subexp_positive_lookahead <- "?="
subexp_negative_lookahead <- "?!"
2014-12-07 22:38:24 +00:00
charset <- "[" _invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset>
charset_range <- first:charset_terminal "-" last:charset_terminal <CharsetRange>
charset_terminal <- charset_escape <CharsetEscape>
/ charset_literal <CharsetLiteral>
charset_escape <- "\\" esc:(
code:[bdDfnrsStvwW] arg:""?
/ code:"0" arg:[0-7]+
/ code:"x" arg:( [0-9a-fA-F] [0-9a-fA-F] )
/ code:"u" arg:( [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) )
2014-12-07 22:38:24 +00:00
charset_literal <- ( ""? literal:[^\\\]] ) / ( "\\" literal:. )
terminal <- any_character <AnyCharacter>
/ escape <Escape>
/ literal <Literal>
2014-11-26 00:37:54 +00:00
any_character <- "."
escape <- "\\" esc:(
code:[bBdDfnrsStvwW1-9] arg:""?
/ code:"c" arg:.
/ code:"0" arg:[0-7]+
/ code:"x" arg:( [0-9a-fA-F] [0-9a-fA-F] )
/ code:"u" arg:( [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] )
/ code:"0" arg:""? )
literal <- ( ""? literal:[^|\\/.\[\(\)?+*$^] ) / ( "\\" literal:. )