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

44 lines
1.9 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]+ "}"
/ "{," _max:[0-9]+ "}"
/ "{" _min:[0-9]+ ",}"
/ "{" _exact:[0-9]+ "}" ) <RepeatSpec>
2014-11-26 00:37:54 +00:00
repeat_greedy <- "?"
subexp <- "(" ( 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 <- "?!"
charset <- "[" "^"? ( charset_range / charset_terminal )* "]" <Charset>
2014-11-26 00:37:54 +00:00
charset_range <- charset_terminal "-" charset_terminal
charset_terminal <- charset_escape / charset_literal
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] ) )
charset_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:. )