Skip to content

Commit

Permalink
added TRY keyword to camlp4 parsing post
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed Sep 3, 2010
1 parent 7daffd9 commit d67f2bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions _posts/2010-05-19-camlp4-parsing.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,28 @@ anonymous entry (but can have only one level). For example, the rule
{% endhighlight %}
parses both `1 + 2` and `1 plus 2`.

Addendum: A new special symbol appeared in the 3.12.0 release, `TRY`
*elem*, which provides non-local backtracking: a `Stream.Error`
occurring in *elem* is converted to a `Stream.Failure`. (It works by
running *elem* on an on-demand copy of the token stream; tokens are
not consumed from the real token stream until *elem* succeeds.) `TRY`
replaces most (all?) cases where you'd need to drop down to a stream
parser for lookahead. So another way to fix the local backtracking
example above is:

{% highlight ocaml %}
EXTEND Gram
GLOBAL: expr;

g: [[ "plugh" ]];
f1: [[ g; "quux" ]];
f2: [[ g; "xyzzy" ]];

expr:
[[ TRY f1 -> "f1" | f2 -> "f2" ]];
END
{% endhighlight %}

***

Almost the whole point of Camlp4 grammars is that they are
Expand Down

0 comments on commit d67f2bf

Please sign in to comment.