]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/parser/parser.factor
Merge branch 'master' into regexp
[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 ;
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 : name>class ( name -- class )
22     {
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 ;
38
39 : lookup-escape ( char -- ast )
40     {
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: \\ ] }
48
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> ] }
55
56         { CHAR: z [ end-of-input <tagged-epsilon> ] }
57         { CHAR: A [ beginning-of-input <tagged-epsilon> ] }
58         [ ]
59     } case ;
60
61 : options-assoc ( -- assoc )
62     H{
63         { CHAR: i case-insensitive }
64         { CHAR: d unix-lines }
65         { CHAR: m multiline }
66         { CHAR: n multiline }
67         { CHAR: r reversed-regexp }
68         { CHAR: s dotall }
69         { CHAR: u unicode-case }
70         { CHAR: x comments }
71     } ;
72
73 : ch>option ( ch -- singleton )
74     options-assoc at ;
75
76 : option>ch ( option -- string )
77     options-assoc value-at ;
78
79 : parse-options ( on off -- options )
80     [ [ ch>option ] { } map-as ] bi@ <options> ;
81
82 : string>options ( string -- options )
83     "-" split1 parse-options ;
84  
85 : options>string ( options -- string )
86     [ on>> ] [ off>> ] bi
87     [ [ option>ch ] map ] bi@
88     [ "-" glue ] unless-empty
89     "" like ;
90
91 ! TODO: add syntax for various parenthized things,
92 !       add greedy and nongreedy forms of matching
93 ! (once it's all implemented)
94
95 EBNF: parse-regexp
96
97 CharacterInBracket = !("}") Character
98
99 QuotedCharacter = !("\\E") .
100
101 Escape = "p{" CharacterInBracket*:s "}" => [[ s >string name>class <primitive-class> ]]
102        | "P{" CharacterInBracket*:s "}" => [[ s >string name>class <primitive-class> <negation> ]]
103        | "Q" QuotedCharacter*:s "\\E" => [[ s <concatenation> ]]
104        | "u" Character:a Character:b Character:c Character:d
105             => [[ { a b c d } hex> ensure-number ]]
106        | "x" Character:a Character:b
107             => [[ { a b } hex> ensure-number ]]
108        | "0" Character:a Character:b Character:c
109             => [[ { a b c } oct> ensure-number ]]
110        | . => [[ lookup-escape ]]
111
112 EscapeSequence = "\\" Escape:e => [[ e ]]
113
114 Character = EscapeSequence
115           | "$" => [[ $ <tagged-epsilon> ]]
116           | "^" => [[ ^ <tagged-epsilon> ]]
117           | . ?[ allowed-char? ]?
118
119 AnyRangeCharacter = EscapeSequence | .
120
121 RangeCharacter = !("]") AnyRangeCharacter
122
123 Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
124       | RangeCharacter
125
126 StartRange = AnyRangeCharacter:a "-" RangeCharacter:b => [[ a b <range> ]]
127            | AnyRangeCharacter
128
129 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
130
131 CharClass = "^"?:n Ranges:e => [[ e n char-class ]]
132
133 Options = [idmsux]*
134
135 Parenthized = "?:" Alternation:a => [[ a ]]
136             | "?" Options:on "-"? Options:off ":" Alternation:a
137                 => [[ a on off parse-options <with-options> ]]
138             | "?#" [^)]* => [[ f ]]
139             | "?~" Alternation:a => [[ a <negation> ]]
140             | "?=" Alternation:a => [[ a <lookahead> ]]
141             | "?!" Alternation:a => [[ a <negation> <lookahead> ]]
142             | "?<=" Alternation:a => [[ a <lookbehind> ]]
143             | "?<!" Alternation:a => [[ a <negation> <lookbehind> ]]
144             | Alternation
145
146 Element = "(" Parenthized:p ")" => [[ p ]]
147         | "[" CharClass:r "]" => [[ r ]]
148         | ".":d => [[ any-char <primitive-class> ]]
149         | Character
150
151 Number = (!(","|"}").)* => [[ string>number ensure-number ]]
152
153 Times = "," Number:n "}" => [[ 0 n <from-to> ]]
154       | Number:n ",}" => [[ n <at-least> ]]
155       | Number:n "}" => [[ n n <from-to> ]]
156       | "}" => [[ bad-number ]]
157       | Number:n "," Number:m "}" => [[ n m <from-to> ]]
158
159 Repeated = Element:e "{" Times:t => [[ e t <times> ]]
160          | Element:e "*+" => [[ e <possessive-star> ]]
161          | Element:e "++" => [[ e <possessive-plus> ]]
162          | Element:e "?" => [[ e <maybe> ]]
163          | Element:e "*" => [[ e <star> ]]
164          | Element:e "+" => [[ e <plus> ]]
165          | Element
166
167 Concatenation = Repeated*:r => [[ r sift <concatenation> ]]
168
169 Alternation = Concatenation:c ("|" Concatenation)*:a
170                 => [[ a empty? [ c ] [ a values c prefix <alternation> ] if ]]
171
172 End = !(.)
173
174 Main = Alternation End
175 ;EBNF