Ramarren / cl-parser-combinators

An experimental implementation of parser combinators in Common Lisp
80 stars 5 forks source link

FIND? behavior #1

Closed thomp closed 13 years ago

thomp commented 13 years ago

Thanks for making your parser combinator code available. Fun stuff. I noted that FIND?, while advertised as looking for the first match, does not seem to leave the context at the corresponding point (i.e., directly succeeding the first match):

PARSER-COMBINATORS>  (tree-of (current-result 
                   (parse-string 
                (mdo (<- x (find? "aa"))
                     (<- c (context?))
                 (result (cons x (context-peek c))))
            "aabbaacc")))
("aa" . #\c)

Given the advertised behavior of FIND?, would it be preferable to leave the context at the point directly after the first match?

PARSER-COMBINATORS>  (tree-of (current-result 
               (parse-string 
            (mdo (<- x (find?2 "aa"))
                 (<- c (context?))
                 (result (cons x (context-peek c))))
            "aabbaacc")))
("aa" . #\b)

where FIND?2 is something like

(defun find?2 (q)
  (with-parsers (q)
    (mdo (<- x (breadth? q nil nil))
     (result (first x)))))

Thanks again,

Alan

Ramarren commented 13 years ago

The actual problem was in breadth? parser, which didn't emit an empty match even when argument MIN was nil/0, which means that FIND? only matched when there was at least one other token before the match. I fixed that (by removing a condition actually, not sure what I was thinking) and added a test case.

FIND? doesn't technically always find the first match, since backtracking might force a match to another one. FIND* actually forces the first match. I clarified the docstrings.

thomp commented 13 years ago

Looks good to me.