Access to rule name from nodes?
See original GitHub issueE.g. how do I know what type of an element was matched if the parse tree does not contain the matched rule name?
Let’s take a hypothetical programming language, which I’m trying to parse:
class Dog
property dog_name
def bark()
puts "Woof!"
end
def sit()
end
end
And when parsing the class definition, class can contain 0…N (propertyDefinition OR methodDefinition).
When I get the parse tree and am handling the class definition node, it’s sub-elements are (ideally):
- property definition
- method definition
- method definition
Sure, I could detect the type of node by seeing if the object has key “property_name” (=> it’s a property definition) or if it has key “method_name” (=> it’s a method definition), but that’s just ugly.
I noticed there is _types field in the parser and in parse(…, options), but it seems to be unimplemented.
Any advice on this?
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Access Rules and Access Control | Sentinet - Nevatech
Provide an Access Rule name and optional description. Use the Access Rule ... At runtime, the Sentinet Node executes Custom Access Rule Expressions....
Read more >ACL Rules | Consul - HashiCorp Developer
Rules define read, write, and deny access controls for datacenter resources. ... Agent rules are keyed by the node name they apply to....
Read more >More about Access of Role to Object rules - Pega
First create access role names using Access Role form. Start each access role name with an initial portion (such as a RuleSet name)...
Read more >Creating Access Rules
From the Manage this service menu, select Access Rules. On the Access Rules, page, click Create Rule. Source: Select <custom .
Read more >How do I give read/write access to all nodes with Firebase ...
I think this is the answer based on the info provided: The two sets of rules you posted are different. The first set...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve been looking at this again, and I really need it for building better error messages. My thought is something like augmenting the builders so that they set a local variable with the rule name (which you already have when you create the function (something like this):
Then, when you were actually in each function, the rule name would be pushed onto a stack of rules in the Grammar instance so that the current rule failure would have a rule name.
Now, we probably don’t want to give a raw rule name to a user, but it’d be better than this:
However, once you had the rule names, then your error handler could have a strings object of messages with “human” names for each of the rule name keys.
As far as I can see, this is a slight modification to the builder to get it to generate the correct code to set the variables and manage the stack and then modifications to the generated Parser instance so that it includes the top of the rule stack when it creates the SyntaxError instance thrown to the caller.
A few things that I’m not sure about are:
I’m trying to follow the directions in the CONTRIBUTING file, but when I try and run the tests, I get a lot of errors, and, since I’m still getting the hang of the npm environment, I’m not quite sure how to get it to generate a local package that I can play with interactively.
As far as your comment to @joonas-fi about not worrying about the rule names, I think that’s fair in the normal processing sequence now that I understand how you’re supposed to construct your own object graph as the rules fire. However, I don’t think it’s accurate when you’re talking about error messages—especially when you’re trying to figure out a grammar where a) you’re still new to canopy, b) you’re a little new to PEG and c) you’re not 100% convinced exactly how to express the rules you want. 😃
I’m trying to replace a hand-written parser for two existing languages with Canopy, and, after taking a few months getting sidetracked, I’m now making good progress as we’re coming to an understanding. However, somehow, I need better error messages than the above, because the way they are, they’re only slightly better than IBM BASIC (c. 1980) “Syntax error.” messages.
Having this facility would not only make development of grammars with Canopy much faster, but it’s also important to the end product you’re building the parser for in the first place.
This is what I get when I try and build, and, for now, if I can just get it working the way I described with Javascript, I’ll be happy. I don’t need/want the other languages right now.
Happy to help implement this, but I think I need a little bit of guidance on how things work to be efficient.
Cheers
One further thing I’ll note here is that I don’t want to commit to a way of identifying rule names that user programs might come to depend on. Something I’m still considering is if/how Canopy should support composing multiple grammars together, and that it likely to affect how rules are identified since it creates a namespacing problem.
For now I’m happy to include this in error messages but less keen to put them in data that might break its format in later releases.