Access name of production rule in postprocessing function?
See original GitHub issueI’m using Nearley to generate my own abstract syntax trees. I’d love to be able to use the same postprocessing function across production rules, without having to repeat the name of the production rule twice in my code. For example, instead of this:
foo1 -> "bar1" {% function(data){
return { data: data, type: 'foo' }
} %}
foo2 -> "bar2" {% function(data){
return { data: data, type: 'foo2' }
} %}
# etc.
I’d love for the library to be able to do this:
@{%
function makeNode(data, reference, fail, name){
return { data: data, type: name }
}
%}
foo1 -> "bar1" {% makeNode %}
foo2 -> "bar2" {% makeNode %}
Or better yet, set this
to the Rule
object:
@{%
function makeNode(data){
return { data: data, type: this.name }
}
%}
Can this be made possible?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How to Perform CIF Post Processing. - SAP Blogs
Give a suitable file name and click on generate. P12.png. You will get a message on the status bar stating so and so...
Read more >Preprocessing and Postprocessing - Amazon SageMaker
To do this, configure Model Monitor's first-party, pre-built container to ignore a percentage of the records according to your specified ...
Read more >How to modify Post Processing Profiles in Script? - Unity Forum
If you get the reference after the render camera is created. You will get the correct reference and can adjust all the settings...
Read more >Postprocessing - an overview | ScienceDirect Topics
The objective of postprocessing is to improve the quality of the images produced in the decoder of a lossy image compression system. Such...
Read more >Post Processing Content Examples | Unreal Engine 4.27 ...
Overview of the samples provided in the Post Processing Content Examples. ... find additional resources for each example by clicking on the example's...
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
Ah, got it! Thank you.
So a more straightforward solution is:
While this requires you to duplicate the rule name, it separates CST from grammar details: you’re free to rename rule
foo
or usemakeNode
in a macro where the name isn’t preserved in runtime.There really shouldn’t be a way to do this. As it stands, we make no guarantees on the internal structure of the
Rule
object. Its fields might change if we decide to implement some kind of optimization or something. Your day-to-day nearley grammars should not depend on postprocessing based on theRule
object. And @darrylyeo’s example is simple enough that I don’t think anyone minds the redundancy of writing something like the following.If we introduce this as an officially-supported “feature”, the potential for doing icky things is just way too high. And, historically, we’ve seen that people not only use nearley features, but in fact often use them in surprisingly scary ways. Nobody wants to deal with the weird edge-cases, backwards-compatibility issues, and painful bug reports that would result if this becomes mainstream.
Now, if this is the case, then why do I want it documented? Well, if you actually do know what you’re doing and need a patch for some reason, then binding
this
toRule
is a nice side-effect of the way nearley works internally, providing you with a hook for getting started with your wizardry. TheRule
object (currently!) is quite simple to understand, and provides enough reflection for advanced developers to do some neat things.For example, you might want to dynamically generate a parser based on various conditions (imagine an interactive fiction game where your vocabulary increases as you pick up objects or whatever). You can do that with nearley.
But — crucially — this is one of those with-great-power-comes-great-responsibility moments. If we are giving people the power to do cool/awful things, then we should make sure they know that they are wandering off the trail and should keep an eye out for bears. That is why I think it should be labeled with some kind of warning.