chiark / gitweb /
src/parser/: Allow parsers to commit to a parse while peeking.
There is a new parser macro `commit' which may be used lexically within
the body of `peek' to commit to the current parse: if a failure is
encountered once the parse is committed, then the entire `peek' fails
having consumed input, rather than rewinding to the start. This allows
parsers to limit the amount of lookahead they use once the ambiguity is
resolved.
Technically, the changes are as follows.
* The `peek' parser macro introduces a new lexically-scoped macro into
its body which releases its captured place and sets the
corresponding variable to `nil'.
* The `with-parser-place' and `with-scanner-place' macros don't try to
release their captured places on unwind if they've been set to
`nil'. (It's not necessary to have changed `with-scanner-place'
here, but it improves symmetry.)
* There's a new `commit' parser macro which just invokes the magic
macro introduced by `peek' and succeeds.
* There's a top-level function with the same name as the local macro,
to report errors in a slightly less confusing way.