> I'm missing something: the first iteration forces a base case, in my
> example no characters with null as the semantic value. With Bryan's
> condition, that's it; no more recursion. But my point is that I need
> to try the recursive case now.
>
> Robert

Actually, my bad again, the return should be "return prev", but given
that, the logic is as follows:

On the first entry into the loop body, cur = -1

Now:
    memory[key] = cur;  // Memorized version is now -1, no match
    prev = cur;  // Prev is also -1
    cur = do_pase(rule, position);

The do_parse calls the actual matcher, none of the recursive calls
work (since memorized version is currently -1), but the null match
does work, resulting in a return of 0 (since null match has length 0).
 Now, cur = 0, prev = -1, thus cur != -1 && cur > prev thus we go
around the loop again.

Now:
    memory[key] = cur;  // Memorized version is null match
    prev = cur;  // Prev is now 0
    cur = do_pase(rule, position);

This time, the recursive calls work (the memorized version is the
empty match), and since all of your cases do actually match real
values the new match that results is cur > 0, thus once again cur !=
-1 && cur > prev, and so on.  When at some point there is no
additional left recursive matches, so we fall though to the lowest
rule, the null rule, which of course matches (since it always does).
At this point, we return cur = 0 again, which will fail the loop
condition, exiting, and the return to the original caller will the the
*previous* match, which was the longest recursive match.

I checked the 'only null' case and the 'non-recursive' case and they
seem to work fine as well.

I think that's all correct now.

-Jeremy

_______________________________________________
PEG mailing list
PEG@lists.csail.mit.edu
https://lists.csail.mit.edu/mailman/listinfo/peg

Reply via email to