1 ! Copyright (C) 2004 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: lazy-lists kernel sequences sequences-contrib strings math io arrays errors namespaces ;
7 ! Parser combinator protocol
8 GENERIC: (parse) ( input parser -- list )
10 M: promise (parse) ( input parser -- list )
13 LAZY: parse ( input parser -- promise )
16 TUPLE: parse-result parsed unparsed ;
17 TUPLE: token-parser string ;
19 LAZY: token ( string -- parser )
22 M: token-parser (parse) ( input parser -- list )
23 token-parser-string swap over ?head-slice [
29 TUPLE: satisfy-parser quot ;
31 LAZY: satisfy ( quot -- parser )
34 M: satisfy-parser (parse) ( input parser -- list )
35 #! A parser that succeeds if the predicate,
36 #! when passed the first character in the input, returns
41 satisfy-parser-quot >r unclip-slice dup r> call [
42 swap <parse-result> 1list
48 TUPLE: epsilon-parser ;
50 LAZY: epsilon ( -- parser )
53 M: epsilon-parser (parse) ( input parser -- list )
54 #! A parser that parses the empty string. It
55 #! does not consume any input and always returns
56 #! an empty list as the parse tree with the
58 drop "" swap <parse-result> 1list ;
60 TUPLE: succeed-parser result ;
62 LAZY: succeed ( result -- parser )
65 M: succeed-parser (parse) ( input parser -- list )
66 #! A parser that always returns 'result' as a
67 #! successful parse with no input consumed.
68 succeed-parser-result swap <parse-result> 1list ;
72 LAZY: fail ( -- parser )
75 M: fail-parser (parse) ( input parser -- list )
76 #! A parser that always fails and returns
77 #! an empty list of successes.
80 TUPLE: and-parser p1 p2 ;
82 LAZY: <&> ( parser1 parser2 -- parser )
85 M: and-parser (parse) ( input parser -- list )
86 #! Parse 'input' by sequentially combining the
87 #! two parsers. First parser1 is applied to the
88 #! input then parser2 is applied to the rest of
89 #! the input strings from the first parser.
90 [ and-parser-p1 ] keep and-parser-p2 -rot parse [
91 dup parse-result-unparsed rot parse
93 >r parse-result-parsed r>
94 [ parse-result-parsed 2array ] keep
95 parse-result-unparsed <parse-result>
99 TUPLE: or-parser p1 p2 ;
101 LAZY: <|> ( parser1 parser2 -- parser )
104 M: or-parser (parse) ( input parser1 -- list )
105 #! Return the combined list resulting from the parses
106 #! of parser1 and parser2 being applied to the same
107 #! input. This implements the choice parsing operator.
108 [ or-parser-p1 ] keep or-parser-p2 >r dupd parse swap r> parse lappend ;
110 : ltrim-slice ( string -- string )
111 #! Return a new string without any leading whitespace
112 #! from the original string.
113 dup first blank? [ 1 tail-slice ltrim-slice ] when ;
115 TUPLE: sp-parser p1 ;
117 LAZY: sp ( p1 -- parser )
118 #! Return a parser that first skips all whitespace before
119 #! calling the original parser.
122 M: sp-parser (parse) ( input parser -- list )
123 #! Skip all leading whitespace from the input then call
124 #! the parser on the remaining input.
125 >r ltrim-slice r> sp-parser-p1 parse ;
127 TUPLE: just-parser p1 ;
129 LAZY: just ( p1 -- parser )
132 M: just-parser (parse) ( input parser -- result )
133 #! Calls the given parser on the input removes
134 #! from the results anything where the remaining
135 #! input to be parsed is not empty. So ensures a
136 #! fully parsed input string.
137 just-parser-p1 parse [ parse-result-unparsed empty? ] lsubset ;
139 TUPLE: apply-parser p1 quot ;
141 LAZY: <@ ( parser quot -- parser )
144 M: apply-parser (parse) ( input parser -- result )
145 #! Calls the parser on the input. For each successfull
146 #! parse the quot is call with the parse result on the stack.
147 #! The result of that quotation then becomes the new parse result.
148 #! This allows modification of parse tree results (like
149 #! converting strings to integers, etc).
150 [ apply-parser-p1 ] keep apply-parser-quot
152 [ parse-result-parsed swap call ] keep
153 parse-result-unparsed <parse-result>
156 TUPLE: some-parser p1 ;
158 LAZY: some ( p1 -- parser )
161 M: some-parser (parse) ( input parser -- result )
162 #! Calls the parser on the input, guarantees
163 #! the parse is complete (the remaining input is empty),
164 #! picks the first solution and only returns the parse
165 #! tree since the remaining input is empty.
166 some-parser-p1 just parse car parse-result-parsed ;
169 LAZY: <& ( parser1 parser2 -- parser )
170 #! Same as <&> except discard the results of the second parser.
173 LAZY: &> ( parser1 parser2 -- parser )
174 #! Same as <&> except discard the results of the first parser.
177 LAZY: <:&> ( parser1 parser2 -- result )
178 #! Same as <&> except flatten the result.
179 <&> [ dup second swap first [ % , ] { } make ] <@ ;
181 LAZY: <&:> ( parser1 parser2 -- result )
182 #! Same as <&> except flatten the result.
183 <&> [ dup second swap first [ , % ] { } make ] <@ ;
185 LAZY: <*> ( parser -- parser )
186 dup <*> <&:> { } succeed <|> ;
188 LAZY: <+> ( parser -- parser )
189 #! Return a parser that accepts one or more occurences of the original
193 LAZY: <?> ( parser -- parser )
194 #! Return a parser that optionally uses the parser
195 #! if that parser would be successfull.
196 [ 1array ] <@ f succeed <|> ;