Comments (7)
You're right, this kind of thing should be easy to do. And I think it is in this case: have you tried the message
and shortMessage
properties of the MatchFailure
object that you get from a failed call to yourGrammar.match(...)
? Let me know if those properties don't do what you want and I'll figure something out :)
Alternatively, you could compose your own error message using a combination of other methods from MatchFailure
. It's more work, but gives you more control:
getPos()
method will tell you where the match failed. (We should probably also support agetLineAndCol
method, so that clients don't have to compute that information themselves.)getExpectedText()
gives you a string, e.g.,"+, -, *, or /"
from ohm.
Sorry - this is for when the parse is succeeding but I want to report errors detected later in the semantics.
from ohm.
Oh, I see! Thanks for clarifying.
I think your suggestion makes perfect sense. I can imagine exposing this kind of functionality in a couple of different ways. One option (as you suggested) would be to add it as a method on Interval
s. Another would be to make it a method of CST nodes (e.g., someNode.createErrorMessage('a string that explains what is wrong with this node')
) -- then you wouldn't even have to extract the node's interval. IMO the latter would be nicer to use, assuming it's expressive enough. Can you think of any cases where you'd want to create a custom interval?
Anyway, I'll leave this one for Pat to look at on Monday. He's been working on improving the way error messages are displayed, so it's probably best if he adds this feature.
Thanks again, Jonathan!
from ohm.
Great, thanks. I'd vote for a method on Interval, because it only depends on info from the Interval. I've been stashing Intervals in the DOM because that's all i need so far.
Another reason I haven't been using Nodes is because semantic actions get the current node as a dynamic this, which is subtle and not expressible in the Typescript type system and not available to ES6 lambda expressions.
from ohm.
As of d376cf3, it's exposed via ohm.util.getLineAndColumnMessage
. I will probably also make a convenience method on Interval, but I thought it would be better to have a more flexible version for cases for when you might not have an interval in hand.
from ohm.
@JonathanMEdwards, would you mind opening a new issue with some more details about the problem with dynamic this
in Typescript? We'll eventually want to figure out a solution that works well in other languages.
from ohm.
Upon reflection I think the current design is the best. It is certainly
idiomatic javascript. Dynamic this and first-class methods are outliers in
the landscape of OO languages but it actually works nicely in this case.
Typescript doesn't allow dynamic this to be typed but still allows it to be
used.
On Mon, Jun 1, 2015 at 6:36 AM, Patrick Dubroy [email protected]
wrote:
@JonathanMEdwards https://github.com/JonathanMEdwards, would you mind
opening a new issue with some more details about the problem with dynamic
this in Typescript? We'll eventually want to figure out a solution that
works well in other languages.—
Reply to this email directly or view it on GitHub
#15 (comment).
from ohm.
Related Issues (20)
- Transform a match into another grammar HOT 1
- asIteration is missing from the TypeScript types HOT 2
- Would it be conceivable to pass an argument to _terminal? HOT 1
- Visiting without changing the input actually outputs a different value HOT 4
- getLineAndColumn is missing from the TypeScript types HOT 2
- Fix typing tests on main HOT 1
- Cherry-pick 5643c1e onto main
- Crash when calling `toString` on a NodeWrapper
- Release: ohm-js v17
- chore: Eliminate circular dependency between ohm-js and @ohmjs/cli packages
- grammar name of test example does not track grammar name changes
- Ohm-JS fails to complain about (some) missing semantic actions
- Type definitions for parameterized rules are inconsistent with behavior HOT 1
- Semantic action '[...]' has the wrong arity: expected 3, got 0 HOT 5
- Semantic actions for comments defined under the space rule HOT 1
- Add ability to type operation parameters & return values HOT 11
- CLI: Line endings in generated files vary across systems and platforms HOT 1
- cli.js is reading incorrect package.json file for version HOT 2
- Typescript example causes error HOT 1
- space with applySyntactic causes failure
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ohm.