question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access name of production rule in postprocessing function?

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deltaideacommented, Aug 4, 2017

Ah, got it! Thank you.

So a more straightforward solution is:

@{%
    const makeNode = name => data => ({ data: data, type: name });
%}

foo -> bar {% makeNode("foo") %}

While this requires you to duplicate the rule name, it separates CST from grammar details: you’re free to rename rule foo or use makeNode in a macro where the name isn’t preserved in runtime.

1reaction
kachcommented, Aug 4, 2017

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 the Rule object. And @darrylyeo’s example is simple enough that I don’t think anyone minds the redundancy of writing something like the following.

foo1 -> xxx {% makePP.bind({name: "foo1"}) %}
foo2 -> yyy (% makePP.bind({name: "foo2"}) %}

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 to Rule is a nice side-effect of the way nearley works internally, providing you with a hook for getting started with your wizardry. The Rule 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found