Add a non-where version if with()
The with() rule uses where() internally, which means the output of the given strategy is not returned. I found this to be surprising in in some cases rather troublesome as I just wanted to mark some rule invokations in “should not fail” without otherwise affecting their usage.
So, I made a wrapper like this:
/** * Run s; if it fails, report a fatal error with m as the message * * @param m Information to include in the fatal error message * @param s Strategy to execute */ req(s|message): input -> output with with(output := <s> | message)
One thing missing is be a facility to capture the name/identity of a passed rule automatically. I’m often writing something like:
foo: Foo(bar) -> <req(foo|"foo failed unexpectedly")> Foo(bar,<new>)
Ideally some kind of macro or introspection facility would be able to generate the message (including the strategy name / source code) without it being passed explicitly.
Submitted by Dobes Vandermeer on 19 November 2012 at 19:28
Issue Log
This is by design, for symmetry with
where()
.
I see - but there’s no “non-where-like” version in the library, perhaps the one I provided here could be put in the standard library somewhere for people who want their “with” without the “where”.
Probably people will sometimes want with to behave like where, and sometimes not.
Okay, I reformulated your issue title into a feature request :)
BTW, you can get close to what the compiler could do if you implement one yourself based on https://svn.strategoxt.org/repos/StrategoXT/strategoxt/trunk/stratego-libraries/lib/spec/lang/with.str.
Given your example
foo: Foo(bar) -> <req(foo|"foo failed unexpectedly")> Foo(bar,<new>)
You can write this simply as:
foo: Foo(bar) -> result with result := <foo> Foo(bar, <new>)
When the
with
clause fails, you get a stack trace forfoo
.
Log in to post comments