From: chris.double Date: Fri, 6 Oct 2006 01:28:01 +0000 (+0000) Subject: parser-combinators: refactor 'sp' and 'just' X-Git-Tag: 0.85~84 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=0ea3720a339b2652dffb43622d4804f186255aed parser-combinators: refactor 'sp' and 'just' --- diff --git a/contrib/parser-combinators/parser-combinators.factor b/contrib/parser-combinators/parser-combinators.factor index b16a301d9e..a304b8628c 100644 --- a/contrib/parser-combinators/parser-combinators.factor +++ b/contrib/parser-combinators/parser-combinators.factor @@ -114,28 +114,31 @@ M: or-parser (parse) ( input parser1 -- list ) : string-ltrim ( string -- string ) #! Return a new string without any leading whitespace #! from the original string. - dup first blank? [ 1 tail string-ltrim ] when ; + dup first blank? [ 1 tail-slice string-ltrim ] when ; -: sp-parser ( input parser -- result ) - #! Skip all leading whitespace from the input then call - #! the parser on the remaining input. - >r string-ltrim r> call ; +TUPLE: sp-parser p1 ; -: sp ( parser -- parser ) +: sp ( p1 -- parser ) #! Return a parser that first skips all whitespace before #! calling the original parser. - [ sp-parser ] curry ; + ; + +M: sp-parser (parse) ( input parser -- list ) + #! Skip all leading whitespace from the input then call + #! the parser on the remaining input. + >r string-ltrim r> sp-parser-p1 parse ; + +TUPLE: just-parser p1 ; -: just-parser ( input parser -- result ) +: just ( p1 -- parser ) + ; + +M: just-parser (parse) ( input parser -- result ) #! Calls the given parser on the input removes #! from the results anything where the remaining #! input to be parsed is not empty. So ensures a #! fully parsed input string. - call [ parse-result-unparsed empty? ] lsubset ; - -: just ( parser -- parser ) - #! Return an instance of the just-parser. - [ just-parser ] curry ; + just-parser-p1 parse [ parse-result-unparsed empty? ] lsubset ; : <@-parser ( input parser quot -- result ) #! Calls the parser on the input. For each successfull