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
5 regexp.ast unicode.case unicode.script.private unicode.categories
6 memoize interval-maps sets unicode.data combinators.short-circuit
10 : allowed-char? ( ch -- ? )
11 ".()|[*+?$^" member? not ;
15 : ensure-number ( n -- n )
16 [ bad-number ] unless* ;
18 :: at-error ( key assoc quot: ( key -- replacement ) -- value )
19 key assoc at* [ drop key quot call ] unless ; inline
21 ERROR: bad-class name ;
23 : simple ( str -- simple )
24 ! Alternatively, first collation key level?
25 >case-fold [ " \t_" member? not ] filter ;
27 : simple-table ( seq -- table )
28 [ [ simple ] keep ] H{ } map>assoc ;
30 MEMO: simple-script-table ( -- table )
31 script-table interval-values members simple-table ;
33 MEMO: simple-category-table ( -- table )
34 categories simple-table ;
36 : parse-unicode-class ( name -- class )
38 { [ dup { [ length 1 = ] [ first "clmnpsz" member? ] } 1&& ] [
40 <category-range-class>
42 { [ dup >title categories member? ] [
43 simple-category-table at <category-class>
45 { [ "script=" ?head ] [
46 dup simple-script-table at
48 [ "script=" prepend bad-class ] ?if
53 : unicode-class ( name -- class )
54 dup parse-unicode-class [ ] [ bad-class ] ?if ;
56 : name>class ( name -- class )
58 { "lower" letter-class }
59 { "upper" LETTER-class }
60 { "alpha" Letter-class }
61 { "ascii" ascii-class }
62 { "digit" digit-class }
63 { "alnum" alpha-class }
64 { "punct" punctuation-class }
65 { "graph" java-printable-class }
66 { "blank" non-newline-blank-class }
67 { "cntrl" control-character-class }
68 { "xdigit" hex-digit-class }
69 { "space" java-blank-class }
70 } [ unicode-class ] at-error ;
72 : lookup-escape ( char -- ast )
74 { CHAR: t [ CHAR: \t ] }
75 { CHAR: n [ CHAR: \n ] }
76 { CHAR: r [ CHAR: \r ] }
80 { CHAR: \\ [ CHAR: \\ ] }
82 { CHAR: w [ c-identifier-class <primitive-class> ] }
83 { CHAR: W [ c-identifier-class <primitive-class> <not-class> ] }
84 { CHAR: s [ java-blank-class <primitive-class> ] }
85 { CHAR: S [ java-blank-class <primitive-class> <not-class> ] }
86 { CHAR: d [ digit-class <primitive-class> ] }
87 { CHAR: D [ digit-class <primitive-class> <not-class> ] }
89 { CHAR: z [ end-of-input <tagged-epsilon> ] }
90 { CHAR: Z [ end-of-file <tagged-epsilon> ] }
91 { CHAR: A [ beginning-of-input <tagged-epsilon> ] }
92 { CHAR: b [ word-break <tagged-epsilon> ] }
93 { CHAR: B [ word-break <not-class> <tagged-epsilon> ] }
97 : options-assoc ( -- assoc )
99 { CHAR: i case-insensitive }
100 { CHAR: d unix-lines }
101 { CHAR: m multiline }
102 { CHAR: r reversed-regexp }
106 ERROR: nonexistent-option name ;
108 : ch>option ( ch -- singleton )
109 dup options-assoc at [ ] [ nonexistent-option ] ?if ;
111 : option>ch ( option -- string )
112 options-assoc value-at ;
114 : parse-options ( on off -- options )
115 [ [ ch>option ] { } map-as ] bi@ <options> ;
117 : string>options ( string -- options )
118 "-" split1 parse-options ;
120 : options>string ( options -- string )
121 [ on>> ] [ off>> ] bi
122 [ [ option>ch ] map ] bi@
123 [ "-" glue ] unless-empty
126 ! TODO: add syntax for various parenthized things,
127 ! add greedy and nongreedy forms of matching
128 ! (once it's all implemented)
132 CharacterInBracket = !("}") Character
134 QuotedCharacter = !("\\E") .
136 Escape = "p{" CharacterInBracket*:s "}" => [[ s name>class <primitive-class> ]]
137 | "P{" CharacterInBracket*:s "}" => [[ s name>class <primitive-class> <not-class> ]]
138 | "Q" QuotedCharacter*:s "\\E" => [[ s <concatenation> ]]
139 | "u" Character:a Character:b Character:c Character:d
140 => [[ { a b c d } hex> ensure-number ]]
141 | "x" Character:a Character:b
142 => [[ { a b } hex> ensure-number ]]
143 | "0" Character:a Character:b Character:c
144 => [[ { a b c } oct> ensure-number ]]
145 | . => [[ lookup-escape ]]
147 EscapeSequence = "\\" Escape:e => [[ e ]]
149 Character = EscapeSequence
150 | "$" => [[ $ <tagged-epsilon> ]]
151 | "^" => [[ ^ <tagged-epsilon> ]]
152 | . ?[ allowed-char? ]?
154 AnyRangeCharacter = !("&&"|"||"|"--"|"~~") (EscapeSequence | .)
156 RangeCharacter = !("]") AnyRangeCharacter
158 Range = RangeCharacter:a "-" !("-") RangeCharacter:b => [[ a b <range-class> ]]
161 StartRange = AnyRangeCharacter:a "-" !("-") RangeCharacter:b => [[ a b <range-class> ]]
164 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
166 BasicCharClass = "^"?:n Ranges:e => [[ e n char-class ]]
168 CharClass = BasicCharClass:b "&&" CharClass:c
169 => [[ b c 2array <and-class> ]]
170 | BasicCharClass:b "||" CharClass:c
171 => [[ b c 2array <or-class> ]]
172 | BasicCharClass:b "~~" CharClass:c
173 => [[ b c <sym-diff-class> ]]
174 | BasicCharClass:b "--" CharClass:c
175 => [[ b c <minus-class> ]]
180 Parenthized = "?:" Alternation:a => [[ a ]]
181 | "?" Options:on "-"? Options:off ":" Alternation:a
182 => [[ a on off parse-options <with-options> ]]
183 | "?#" [^)]* => [[ f ]]
184 | "?~" Alternation:a => [[ a <negation> ]]
185 | "?=" Alternation:a => [[ a <lookahead> <tagged-epsilon> ]]
186 | "?!" Alternation:a => [[ a <lookahead> <not-class> <tagged-epsilon> ]]
187 | "?<=" Alternation:a => [[ a <lookbehind> <tagged-epsilon> ]]
188 | "?<!" Alternation:a => [[ a <lookbehind> <not-class> <tagged-epsilon> ]]
191 Element = "(" Parenthized:p ")" => [[ p ]]
192 | "[" CharClass:r "]" => [[ r ]]
196 Number = (!(","|"}").)* => [[ string>number ensure-number ]]
198 Times = "," Number:n "}" => [[ 0 n <from-to> ]]
199 | Number:n ",}" => [[ n <at-least> ]]
200 | Number:n "}" => [[ n n <from-to> ]]
201 | "}" => [[ bad-number ]]
202 | Number:n "," Number:m "}" => [[ n m <from-to> ]]
204 Repeated = Element:e "{" Times:t => [[ e t <times> ]]
205 | Element:e "??" => [[ e <maybe> ]]
206 | Element:e "*?" => [[ e <star> ]]
207 | Element:e "+?" => [[ e <plus> ]]
208 | Element:e "?" => [[ e <maybe> ]]
209 | Element:e "*" => [[ e <star> ]]
210 | Element:e "+" => [[ e <plus> ]]
213 Concatenation = Repeated*:r => [[ r sift <concatenation> ]]
215 Alternation = Concatenation:c ("|" Concatenation)*:a
216 => [[ a empty? [ c ] [ a values c prefix <alternation> ] if ]]
220 Main = Alternation End