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