1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: peg.ebnf kernel math.parser sequences assocs arrays fry math
4 combinators regexp.classes strings splitting peg locals accessors
8 : allowed-char? ( ch -- ? )
9 ".()|[*+?$^" member? not ;
13 : ensure-number ( n -- n )
14 [ bad-number ] unless* ;
16 :: at-error ( key assoc quot: ( key -- replacement ) -- value )
17 key assoc at* [ drop key quot call ] unless ; inline
19 ERROR: bad-class name ;
21 : name>class ( name -- class )
23 { "Lower" letter-class }
24 { "Upper" LETTER-class }
25 { "Alpha" Letter-class }
26 { "ASCII" ascii-class }
27 { "Digit" digit-class }
28 { "Alnum" alpha-class }
29 { "Punct" punctuation-class }
30 { "Graph" java-printable-class }
31 { "Print" java-printable-class }
32 { "Blank" non-newline-blank-class }
33 { "Cntrl" control-character-class }
34 { "XDigit" hex-digit-class }
35 { "Space" java-blank-class }
36 ! TODO: unicode-character-class
37 } [ bad-class ] at-error ;
39 : lookup-escape ( char -- ast )
41 { CHAR: t [ CHAR: \t ] }
42 { CHAR: n [ CHAR: \n ] }
43 { CHAR: r [ CHAR: \r ] }
44 { CHAR: f [ HEX: c ] }
45 { CHAR: a [ HEX: 7 ] }
46 { CHAR: e [ HEX: 1b ] }
47 { CHAR: \\ [ CHAR: \\ ] }
49 { CHAR: w [ c-identifier-class <primitive-class> ] }
50 { CHAR: W [ c-identifier-class <primitive-class> <not-class> ] }
51 { CHAR: s [ java-blank-class <primitive-class> ] }
52 { CHAR: S [ java-blank-class <primitive-class> <not-class> ] }
53 { CHAR: d [ digit-class <primitive-class> ] }
54 { CHAR: D [ digit-class <primitive-class> <not-class> ] }
56 { CHAR: z [ end-of-input <tagged-epsilon> ] }
57 { CHAR: Z [ end-of-file <tagged-epsilon> ] }
58 { CHAR: A [ beginning-of-input <tagged-epsilon> ] }
62 : options-assoc ( -- assoc )
64 { CHAR: i case-insensitive }
65 { CHAR: d unix-lines }
68 { CHAR: r reversed-regexp }
70 { CHAR: u unicode-case }
74 : ch>option ( ch -- singleton )
77 : option>ch ( option -- string )
78 options-assoc value-at ;
80 : parse-options ( on off -- options )
81 [ [ ch>option ] { } map-as ] bi@ <options> ;
83 : string>options ( string -- options )
84 "-" split1 parse-options ;
86 : options>string ( options -- string )
88 [ [ option>ch ] map ] bi@
89 [ "-" glue ] unless-empty
92 ! TODO: add syntax for various parenthized things,
93 ! add greedy and nongreedy forms of matching
94 ! (once it's all implemented)
98 CharacterInBracket = !("}") Character
100 QuotedCharacter = !("\\E") .
102 Escape = "p{" CharacterInBracket*:s "}" => [[ s >string name>class <primitive-class> ]]
103 | "P{" CharacterInBracket*:s "}" => [[ s >string name>class <primitive-class> <negation> ]]
104 | "Q" QuotedCharacter*:s "\\E" => [[ s <concatenation> ]]
105 | "u" Character:a Character:b Character:c Character:d
106 => [[ { a b c d } hex> ensure-number ]]
107 | "x" Character:a Character:b
108 => [[ { a b } hex> ensure-number ]]
109 | "0" Character:a Character:b Character:c
110 => [[ { a b c } oct> ensure-number ]]
111 | . => [[ lookup-escape ]]
113 EscapeSequence = "\\" Escape:e => [[ e ]]
115 Character = EscapeSequence
116 | "$" => [[ $ <tagged-epsilon> ]]
117 | "^" => [[ ^ <tagged-epsilon> ]]
118 | . ?[ allowed-char? ]?
120 AnyRangeCharacter = EscapeSequence | .
122 RangeCharacter = !("]") AnyRangeCharacter
124 Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
127 StartRange = AnyRangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
130 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
132 CharClass = "^"?:n Ranges:e => [[ e n char-class ]]
136 Parenthized = "?:" Alternation:a => [[ a ]]
137 | "?" Options:on "-"? Options:off ":" Alternation:a
138 => [[ a on off parse-options <with-options> ]]
139 | "?#" [^)]* => [[ f ]]
140 | "?~" Alternation:a => [[ a <negation> ]]
141 | "?=" Alternation:a => [[ a t <lookahead> <tagged-epsilon> ]]
142 | "?!" Alternation:a => [[ a f <lookahead> <tagged-epsilon> ]]
143 | "?<=" Alternation:a => [[ a t <lookbehind> <tagged-epsilon> ]]
144 | "?<!" Alternation:a => [[ a f <lookbehind> <tagged-epsilon> ]]
147 Element = "(" Parenthized:p ")" => [[ p ]]
148 | "[" CharClass:r "]" => [[ r ]]
149 | ".":d => [[ any-char <primitive-class> ]]
152 Number = (!(","|"}").)* => [[ string>number ensure-number ]]
154 Times = "," Number:n "}" => [[ 0 n <from-to> ]]
155 | Number:n ",}" => [[ n <at-least> ]]
156 | Number:n "}" => [[ n n <from-to> ]]
157 | "}" => [[ bad-number ]]
158 | Number:n "," Number:m "}" => [[ n m <from-to> ]]
160 Repeated = Element:e "{" Times:t => [[ e t <times> ]]
161 | Element:e "??" => [[ e <maybe> ]]
162 | Element:e "*?" => [[ e <star> ]]
163 | Element:e "+?" => [[ e <plus> ]]
164 | Element:e "?" => [[ e <maybe> ]]
165 | Element:e "*" => [[ e <star> ]]
166 | Element:e "+" => [[ e <plus> ]]
169 Concatenation = Repeated*:r => [[ r sift <concatenation> ]]
171 Alternation = Concatenation:c ("|" Concatenation)*:a
172 => [[ a empty? [ c ] [ a values c prefix <alternation> ] if ]]
176 Main = Alternation End