]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/parser/parser.factor
d9ab28ebe19615562fc614ec91b7d5e2b609901e
[factor.git] / basis / regexp / parser / parser.factor
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
7 namespaces ;
8 IN: regexp.parser
9
10 : allowed-char? ( ch -- ? )
11     ".()|[*+?$^" member? not ;
12
13 ERROR: bad-number ;
14
15 : ensure-number ( n -- n )
16     [ bad-number ] unless* ;
17
18 :: at-error ( key assoc quot: ( key -- replacement ) -- value )
19     key assoc at* [ drop key quot call ] unless ; inline
20
21 ERROR: bad-class name ;
22
23 : simple ( str -- simple )
24     ! Alternatively, first collation key level?
25     >case-fold [ " \t_" member? not ] filter ;
26
27 : simple-table ( seq -- table )
28     [ [ simple ] keep ] H{ } map>assoc ;
29
30 MEMO: simple-script-table ( -- table )
31     script-table interval-values members simple-table ;
32
33 MEMO: simple-category-table ( -- table )
34     categories simple-table ;
35
36 : parse-unicode-class ( name -- class )
37     {
38         { [ dup { [ length 1 = ] [ first "clmnpsz" member? ] } 1&& ] [
39             >upper first
40             <category-range-class>
41         ] }
42         { [ dup >title categories member? ] [
43             simple-category-table at <category-class>
44         ] }
45         { [ "script=" ?head ] [
46             dup simple-script-table at
47             [ <script-class> ]
48             [ "script=" prepend bad-class ] ?if
49         ] }
50         [ bad-class ]
51     } cond ;
52
53 : unicode-class ( name -- class )
54     dup parse-unicode-class [ ] [ bad-class ] ?if ;
55
56 : name>class ( name -- class )
57     >string simple {
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 ;
71
72 : lookup-escape ( char -- ast )
73     {
74         { CHAR: t [ CHAR: \t ] }
75         { CHAR: n [ CHAR: \n ] }
76         { CHAR: r [ CHAR: \r ] }
77         { CHAR: f [ 0xc ] }
78         { CHAR: a [ 0x7 ] }
79         { CHAR: e [ 0x1b ] }
80         { CHAR: \\ [ CHAR: \\ ] }
81
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> ] }
88
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> ] }
94         [ ]
95     } case ;
96
97 : options-assoc ( -- assoc )
98     H{
99         { CHAR: i case-insensitive }
100         { CHAR: d unix-lines }
101         { CHAR: m multiline }
102         { CHAR: r reversed-regexp }
103         { CHAR: s dotall }
104     } ;
105
106 ERROR: nonexistent-option name ;
107
108 : ch>option ( ch -- singleton )
109     dup options-assoc at [ ] [ nonexistent-option ] ?if ;
110
111 : option>ch ( option -- string )
112     options-assoc value-at ;
113
114 : parse-options ( on off -- options )
115     [ [ ch>option ] { } map-as ] bi@ <options> ;
116
117 : string>options ( string -- options )
118     "-" split1 parse-options ;
119  
120 : options>string ( options -- string )
121     [ on>> ] [ off>> ] bi
122     [ [ option>ch ] map ] bi@
123     [ "-" glue ] unless-empty
124     "" like ;
125
126 ! TODO: add syntax for various parenthized things,
127 !       add greedy and nongreedy forms of matching
128 ! (once it's all implemented)
129
130 EBNF: parse-regexp
131
132 CharacterInBracket = !("}") Character
133
134 QuotedCharacter = !("\\E") .
135
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 ]]
146
147 EscapeSequence = "\\" Escape:e => [[ e ]]
148
149 Character = EscapeSequence
150           | "$" => [[ $ <tagged-epsilon> ]]
151           | "^" => [[ ^ <tagged-epsilon> ]]
152           | . ?[ allowed-char? ]?
153
154 AnyRangeCharacter = !("&&"|"||"|"--"|"~~") (EscapeSequence | .)
155
156 RangeCharacter = !("]") AnyRangeCharacter
157
158 Range = RangeCharacter:a "-" !("-") RangeCharacter:b => [[ a b <range-class> ]]
159       | RangeCharacter
160
161 StartRange = AnyRangeCharacter:a "-" !("-") RangeCharacter:b => [[ a b <range-class> ]]
162            | AnyRangeCharacter
163
164 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
165
166 BasicCharClass =  "^"?:n Ranges:e => [[ e n char-class ]]
167
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> ]]
176           | BasicCharClass
177
178 Options = [idmsux]*
179
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> ]]
189             | Alternation
190
191 Element = "(" Parenthized:p ")" => [[ p ]]
192         | "[" CharClass:r "]" => [[ r ]]
193         | ".":d => [[ dot ]]
194         | Character
195
196 Number = (!(","|"}").)* => [[ string>number ensure-number ]]
197
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> ]]
203
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> ]]
211          | Element
212
213 Concatenation = Repeated*:r => [[ r sift <concatenation> ]]
214
215 Alternation = Concatenation:c ("|" Concatenation)*:a
216                 => [[ a empty? [ c ] [ a values c prefix <alternation> ] if ]]
217
218 End = !(.)
219
220 Main = Alternation End
221 ;EBNF