]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/parser/parser.factor
Some reorganizing in Unicode; regexp class changes
[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 ;
6 IN: regexp.parser
7
8 : allowed-char? ( ch -- ? )
9     ".()|[*+?$^" member? not ;
10
11 ERROR: bad-number ;
12
13 : ensure-number ( n -- n )
14     [ bad-number ] unless* ;
15
16 :: at-error ( key assoc quot: ( key -- replacement ) -- value )
17     key assoc at* [ drop key quot call ] unless ; inline
18
19 ERROR: bad-class name ;
20
21 : parse-unicode-class ( name -- class )
22     ! Implement this!
23     drop f ;
24
25 : unicode-class ( name -- class )
26     parse-unicode-class [ bad-class ] unless* ;
27
28 : name>class ( name -- class )
29     >string >case-fold {
30         { "lower" letter-class }
31         { "upper" LETTER-class }
32         { "alpha" Letter-class }
33         { "ascii" ascii-class }
34         { "digit" digit-class }
35         { "alnum" alpha-class }
36         { "punct" punctuation-class }
37         { "graph" java-printable-class }
38         { "blank" non-newline-blank-class }
39         { "cntrl" control-character-class }
40         { "xdigit" hex-digit-class }
41         { "space" java-blank-class }
42     } [ unicode-class ] at-error ;
43
44 : lookup-escape ( char -- ast )
45     {
46         { CHAR: t [ CHAR: \t ] }
47         { CHAR: n [ CHAR: \n ] }
48         { CHAR: r [ CHAR: \r ] }
49         { CHAR: f [ HEX: c ] }
50         { CHAR: a [ HEX: 7 ] }
51         { CHAR: e [ HEX: 1b ] }
52         { CHAR: \\ [ CHAR: \\ ] }
53
54         { CHAR: w [ c-identifier-class <primitive-class> ] }
55         { CHAR: W [ c-identifier-class <primitive-class> <not-class> ] }
56         { CHAR: s [ java-blank-class <primitive-class> ] }
57         { CHAR: S [ java-blank-class <primitive-class> <not-class> ] }
58         { CHAR: d [ digit-class <primitive-class> ] }
59         { CHAR: D [ digit-class <primitive-class> <not-class> ] }
60
61         { CHAR: z [ end-of-input <tagged-epsilon> ] }
62         { CHAR: Z [ end-of-file <tagged-epsilon> ] }
63         { CHAR: A [ beginning-of-input <tagged-epsilon> ] }
64         { CHAR: b [ word-break <tagged-epsilon> ] }
65         { CHAR: B [ word-break <not-class> <tagged-epsilon> ] }
66         [ ]
67     } case ;
68
69 : options-assoc ( -- assoc )
70     H{
71         { CHAR: i case-insensitive }
72         { CHAR: d unix-lines }
73         { CHAR: m multiline }
74         { CHAR: r reversed-regexp }
75         { CHAR: s dotall }
76     } ;
77
78 : ch>option ( ch -- singleton )
79     options-assoc at ;
80
81 : option>ch ( option -- string )
82     options-assoc value-at ;
83
84 : parse-options ( on off -- options )
85     [ [ ch>option ] { } map-as ] bi@ <options> ;
86
87 : string>options ( string -- options )
88     "-" split1 parse-options ;
89  
90 : options>string ( options -- string )
91     [ on>> ] [ off>> ] bi
92     [ [ option>ch ] map ] bi@
93     [ "-" glue ] unless-empty
94     "" like ;
95
96 ! TODO: add syntax for various parenthized things,
97 !       add greedy and nongreedy forms of matching
98 ! (once it's all implemented)
99
100 EBNF: parse-regexp
101
102 CharacterInBracket = !("}") Character
103
104 QuotedCharacter = !("\\E") .
105
106 Escape = "p{" CharacterInBracket*:s "}" => [[ s name>class <primitive-class> ]]
107        | "P{" CharacterInBracket*:s "}" => [[ s name>class <primitive-class> <negation> ]]
108        | "Q" QuotedCharacter*:s "\\E" => [[ s <concatenation> ]]
109        | "u" Character:a Character:b Character:c Character:d
110             => [[ { a b c d } hex> ensure-number ]]
111        | "x" Character:a Character:b
112             => [[ { a b } hex> ensure-number ]]
113        | "0" Character:a Character:b Character:c
114             => [[ { a b c } oct> ensure-number ]]
115        | . => [[ lookup-escape ]]
116
117 EscapeSequence = "\\" Escape:e => [[ e ]]
118
119 Character = EscapeSequence
120           | "$" => [[ $ <tagged-epsilon> ]]
121           | "^" => [[ ^ <tagged-epsilon> ]]
122           | . ?[ allowed-char? ]?
123
124 AnyRangeCharacter = EscapeSequence | .
125
126 RangeCharacter = !("]") AnyRangeCharacter
127
128 Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
129       | RangeCharacter
130
131 StartRange = AnyRangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
132            | AnyRangeCharacter
133
134 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
135
136 CharClass = "^"?:n Ranges:e => [[ e n char-class ]]
137
138 Options = [idmsux]*
139
140 Parenthized = "?:" Alternation:a => [[ a ]]
141             | "?" Options:on "-"? Options:off ":" Alternation:a
142                 => [[ a on off parse-options <with-options> ]]
143             | "?#" [^)]* => [[ f ]]
144             | "?~" Alternation:a => [[ a <negation> ]]
145             | "?=" Alternation:a => [[ a <lookahead> <tagged-epsilon> ]]
146             | "?!" Alternation:a => [[ a <lookahead> <not-class> <tagged-epsilon> ]]
147             | "?<=" Alternation:a => [[ a <lookbehind> <tagged-epsilon> ]]
148             | "?<!" Alternation:a => [[ a <lookbehind> <not-class> <tagged-epsilon> ]]
149             | Alternation
150
151 Element = "(" Parenthized:p ")" => [[ p ]]
152         | "[" CharClass:r "]" => [[ r ]]
153         | ".":d => [[ dot ]]
154         | Character
155
156 Number = (!(","|"}").)* => [[ string>number ensure-number ]]
157
158 Times = "," Number:n "}" => [[ 0 n <from-to> ]]
159       | Number:n ",}" => [[ n <at-least> ]]
160       | Number:n "}" => [[ n n <from-to> ]]
161       | "}" => [[ bad-number ]]
162       | Number:n "," Number:m "}" => [[ n m <from-to> ]]
163
164 Repeated = Element:e "{" Times:t => [[ e t <times> ]]
165          | Element:e "??" => [[ e <maybe> ]]
166          | Element:e "*?" => [[ e <star> ]]
167          | Element:e "+?" => [[ e <plus> ]]
168          | Element:e "?" => [[ e <maybe> ]]
169          | Element:e "*" => [[ e <star> ]]
170          | Element:e "+" => [[ e <plus> ]]
171          | Element
172
173 Concatenation = Repeated*:r => [[ r sift <concatenation> ]]
174
175 Alternation = Concatenation:c ("|" Concatenation)*:a
176                 => [[ a empty? [ c ] [ a values c prefix <alternation> ] if ]]
177
178 End = !(.)
179
180 Main = Alternation End
181 ;EBNF