seqexp icon indicating copy to clipboard operation
seqexp copied to clipboard

support for failure object return when no match can be found

Open carocad opened this issue 8 years ago • 1 comments

Hey Christophe, first of all thanks for the great library. My attempt to follow Russ Cox implementation sadly isn't as elegant as good as yours. I was wondering if you are planning or thinking of implementing a failure object as a return value instead of 'nil'.

This goes in the lines of 'instaparse' where if the input doesn't match the parser description then a failure object is returned indicating at which point did the match process fail and what was expected.

Something like:

parse failed with input 'hello-word'
                         ^^^^
Expected any of:
- chao
- return

I would also be willing to work on this in case you are quite busy. I have some basic understanding of how the algorithm works but I have yet to understand your full implementation, some guidance tips would be very helpfull.

So far I guess that the change should be done in these lines:

            (reduce (fn [threads pc]
                      (let [[op arg] (nth insts pc)
                            registers (ctxs pc)]
                        (case op
                          :pred (if (arg x)
                                  (add-thread threads (inc pc) (cons idx xs) registers insts)
                                  threads))))

But I'm confused about your use of the 'run' function in the loop for the longest match :/

carocad avatar May 04 '16 08:05 carocad

I can't tell for sure I would make such behavior the default but as an option it could be ok.

run consumes several items until either an accept or failed state is reached. longest-match calls run until either the input is consumed or a failed state is attained. The last accept state is returned (which by definition is the accept state of the longest match).

Currently I'm working on lookaheads (negative lookahead is done and works but I'm not happy with the impl – I want to try a stack of vm instead).

One other thing I'd like to explore is turning instructions into a fn (with combinators not with macros).

cgrand avatar May 17 '16 09:05 cgrand