1 ! Copyright (C) 2004 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: lazy-lists kernel sequences strings math io arrays errors namespaces ;
7 ! Parser combinator protocol
8 GENERIC: (parse) ( input parser -- list )
10 : parse ( input parser -- promise )
11 [ (parse) ] curry curry <promise> ;
13 TUPLE: parse-result parsed unparsed ;
15 : ?head-slice ( seq begin -- newseq ? )
16 2dup head? [ length tail-slice t ] [ drop f ] if ;
18 : unclip-slice ( seq -- rest first )
19 dup 1 tail-slice swap first ;
21 : h:t ( object -- head tail )
22 #! Return the head and tail of the object.
23 dup empty? [ dup first swap 1 tail ] unless ;
25 TUPLE: token-parser string ;
27 : token ( string -- parser )
30 M: token-parser (parse) ( input parser -- list )
31 token-parser-string swap over ?head-slice [
37 TUPLE: satisfy-parser quot ;
39 : satisfy ( quot -- parser )
42 M: satisfy-parser (parse) ( input parser -- list )
43 #! A parser that succeeds if the predicate,
44 #! when passed the first character in the input, returns
46 satisfy-parser-quot >r unclip-slice dup r> call [
47 swap <parse-result> 1list
52 : satisfy2-parser ( inp pred quot -- llist )
53 #! A parser that succeeds if the predicate,
54 #! when passed the first character in the input, returns
55 #! true. On success the quotation is called with the
56 #! successfully parsed character on the stack. The result
57 #! of that call is returned as the result portion of the
58 #! successfull parse lazy list.
59 -rot over first swap call [
60 h:t >r swap call r> <parse-result> 1list
65 : satisfy2 ( pred quot -- parser )
66 #! Return a satisfy2-parser.
67 [ satisfy2-parser ] curry curry ;
69 : epsilon-parser ( input -- llist )
70 #! A parser that parses the empty string. It
71 #! does not consume any input and always returns
72 #! an empty list as the parse tree with the
74 "" swap <parse-result> 1list ;
76 : epsilon ( -- parser )
77 #! Return an epsilon parser
80 : succeed-parser ( input result -- llist )
81 #! A parser that always returns 'result' as a
82 #! successful parse with no input consumed.
83 swap <parse-result> 1list ;
85 : succeed ( result -- parser )
86 #! Return a succeed parser.
87 [ succeed-parser ] curry ;
89 : fail-parser ( input -- llist )
90 #! A parser that always fails and returns
91 #! an empty list of successes.
95 #! Return a fail-parser.
98 : <&>-parser ( input parser1 parser2 -- parser )
99 #! Parse 'input' by sequentially combining the
100 #! two parsers. First parser1 is applied to the
101 #! input then parser2 is applied to the rest of
102 #! the input strings from the first parser.
104 dup parse-result-unparsed rot call
106 >r parse-result-parsed r>
107 [ parse-result-parsed 2array ] keep
108 parse-result-unparsed <parse-result>
110 ] lmap-with lconcat ;
112 : <&> ( parser1 parser2 -- parser )
113 #! Sequentially combine two parsers, returning a parser
114 #! that first calls p1, then p2 all remaining results from
116 [ <&>-parser ] curry curry ;
118 : <|>-parser ( input parser1 parser2 -- result )
119 #! Return the combined list resulting from the parses
120 #! of parser1 and parser2 being applied to the same
121 #! input. This implements the choice parsing operator.
122 >r dupd call swap r> call lappend ;
124 : <|> ( p1 p2 -- parser )
125 #! Choice operator for parsers. Return a parser that does
126 #! p1 or p2 depending on which will succeed.
127 [ <|>-parser ] curry curry ;
129 : string-ltrim ( string -- string )
130 #! Return a new string without any leading whitespace
131 #! from the original string.
132 dup first blank? [ 1 tail string-ltrim ] when ;
134 : sp-parser ( input parser -- result )
135 #! Skip all leading whitespace from the input then call
136 #! the parser on the remaining input.
137 >r string-ltrim r> call ;
139 : sp ( parser -- parser )
140 #! Return a parser that first skips all whitespace before
141 #! calling the original parser.
142 [ sp-parser ] curry ;
144 : just-parser ( input parser -- result )
145 #! Calls the given parser on the input removes
146 #! from the results anything where the remaining
147 #! input to be parsed is not empty. So ensures a
148 #! fully parsed input string.
149 call [ parse-result-unparsed empty? ] lsubset ;
151 : just ( parser -- parser )
152 #! Return an instance of the just-parser.
153 [ just-parser ] curry ;
155 : <@-parser ( input parser quot -- result )
156 #! Calls the parser on the input. For each successfull
157 #! parse the quot is call with the parse result on the stack.
158 #! The result of that quotation then becomes the new parse result.
159 #! This allows modification of parse tree results (like
160 #! converting strings to integers, etc).
162 [ parse-result-parsed swap call ] keep
163 parse-result-unparsed <parse-result>
166 : <@ ( parser quot -- parser )
167 #! Return an <@-parser.
168 [ <@-parser ] curry curry ;
170 : some-parser ( input parser -- result )
171 #! Calls the parser on the input, guarantees
172 #! the parse is complete (the remaining input is empty),
173 #! picks the first solution and only returns the parse
174 #! tree since the remaining input is empty.
175 just call car parse-result-parsed ;
177 : some ( parser -- deterministic-parser )
178 #! Creates a 'some-parser'.
179 [ some-parser ] curry ;
181 : <& ( parser1 parser2 -- parser )
182 #! Same as <&> except discard the results of the second parser.
185 : &> ( parser1 parser2 -- parser )
186 #! Same as <&> except discard the results of the first parser.
189 : <:&>-parser ( input parser1 parser2 -- result )
190 #! Same as <&> except flatten the result.
191 <&> [ dup second swap first [ % , ] { } make ] <@ call ;
193 : <:&> ( parser1 parser2 -- parser )
194 #! Same as <&> except flatten the result.
195 [ <:&>-parser ] curry curry ;
197 : <&:>-parser ( input parser1 parser2 -- result )
198 #! Same as <&> except flatten the result.
199 <&> [ dup second swap first [ , % ] { } make ] <@ call ;
201 : <&:> ( parser1 parser2 -- parser )
202 #! Same as <&> except flatten the result.
203 [ <&:>-parser ] curry curry ;
207 : (<*>) ( parser -- parser )
208 #! Non-delayed implementation of <*>
209 dup <*> <&:> [ ] succeed <|> ;
211 : <*> ( parser -- parser )
212 #! Return a parser that accepts zero or more occurences of the original
214 [ (<*>) call ] curry ;
216 : (<+>) ( parser -- parser )
217 #! Non-delayed implementation of <+>
220 : <+> ( parser -- parser )
221 #! Return a parser that accepts one or more occurences of the original
223 [ (<+>) call ] curry ;
225 : (<?>) ( parser -- parser )
226 #! Non-delayed implementation of <?>
227 [ unit ] <@ f succeed <|> ;
229 : <?> ( parser -- parser )
230 #! Return a parser that optionally uses the parser
231 #! if that parser would be successfull.
232 [ (<?>) call ] curry ;