-
-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parameterizing semantic operations #38
Comments
Thanks, Stephan. I'd love to enable Ohm's operations to take arguments, and have been thinking about how to do this for a while. This is a good time to make it a higher-priority item, so I'm glad you've opened this issue. Accessing the operation's arguments via I'll think about this a little more over the next week, and let you know if I come up with anything. Cheers, |
Alex, thank you for looking into this :) |
I would think that the arguments should behave like regular function arguments -- so it should be possible to write a semantic action like this: name: function(lastName, comma, firstName) {
return lastName.stringValue('upper') + firstName.stringValue('lower');
} ...where function makeActionDict(now) {
return {
Date_later: function() {
return new Date(now + 3 * 3600);
},
...
};
}
semantics.addOperation('extractDate', makeActionDict(1440000000000)); |
Yes, that's true. My concern was for parsing incrementally (as input is gathered) where this might be creating lots of function objects. I'm not sure about the overhead involved though, parsing might actually be more expensive :) Oh, and actually in my case it's probably fine to create an action dict at the start of the input, since the context won't have to change or exist for that long. Thanks :) |
You can reuse the same semantics instance with different MatchResult instances, so the overhead shouldn't be a concern. |
Fixed by 8e3ad7f. It used to be that you could only add parameter-less operations to a semantics, e.g.,
This is still allowed, but it's now shorthand for:
The first argument to
These arguments must be supplied every time the operation is invoked, and are accessed by name via the node's
(Stephan: as you can see, I changed my mind on the Please let us know if you have any questions, comments, etc. Cheers, |
Alex, this looks great! I'll change my current usage of my semantics and report back if I lean anything new. Thank you for adding a major feature so quickly! |
Glad you like it, Stephan! Looking forward to hearing whatever thoughts you have about it after you get a chance to use it. |
I'll start with an example grammar:
And a semantic operation:
Now, when writing tests for this semantic operation I'd like to set a fixed date instead of relying on the current time. I realize this probably wasn't possible short of wrapping everything in another scope or (gulp) using dynamic scope.
So I imagined something like this:
What if semantic operations could be parameterized?
And the parameters could be accessed like this?
This could be accessed via
this.parameters
, orthis.context
, orthis.contextParameters
, and would be the same value in all nodes.The text was updated successfully, but these errors were encountered: