]> gitweb.factorcode.org Git - factor.git/commitdiff
parser-combinators: added <!+>, <!*> and only-first parsers
authorchris.double <chris.double@double.co.nz>
Fri, 15 Dec 2006 12:22:09 +0000 (12:22 +0000)
committerchris.double <chris.double@double.co.nz>
Fri, 15 Dec 2006 12:22:09 +0000 (12:22 +0000)
libs/parser-combinators/parser-combinators.factor

index 4804f3b193445b502883a06b0bd4560b8797d9d0..39ba92807cd8c33180bb63a4e1c6334497773ff0 100644 (file)
@@ -195,4 +195,29 @@ LAZY: <+> ( parser -- parser )
 LAZY: <?> ( parser -- parser )
   #! Return a parser that optionally uses the parser
   #! if that parser would be successfull.
-  [ 1array ] <@ f succeed <|> ;
\ No newline at end of file
+  [ 1array ] <@ f succeed <|> ;
+
+TUPLE: only-first-parser p1 ;
+LAZY: only-first ( parser -- parser )
+  <only-first-parser> ;
+
+M: only-first-parser (parse) ( input parser -- list )
+  #! Transform a parser into a parser that only yields
+  #! the first possibility.
+  only-first-parser-p1 parse 1 swap ltake ;
+
+LAZY: <!*> ( parser -- parser )
+  #! Like <*> but only return one possible result
+  #! containing all matching parses. Does not return
+  #! partial matches. Useful for efficiency since that's
+  #! usually the effect you want and cuts down on backtracking
+  #! required.
+  <*> only-first ;
+
+LAZY: <!+> ( parser -- parser )
+  #! Like <+> but only return one possible result
+  #! containing all matching parses. Does not return
+  #! partial matches. Useful for efficiency since that's
+  #! usually the effect you want and cuts down on backtracking
+  #! required.
+  <+> only-first ;