Skip to content
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

Allow for rules that don't create separate nodes #36

Closed
victorandree opened this issue Oct 22, 2013 · 3 comments
Closed

Allow for rules that don't create separate nodes #36

victorandree opened this issue Oct 22, 2013 · 3 comments

Comments

@victorandree
Copy link

It's often useful to define some basic rules that can be composited to form larger expressions:

digit = ~r"[0-9]"
number = digit+

However, there is not always any reason why each separate digit needs its own node, even if it's useful as a "building block" in the grammar. As such, it would be nice to be able to mark a rule as "non-node creating". The library simpleparse supports this by creating 'unreported productions' that would translate into something like this for parsimonious:

<digit> = ~r"[0-9]"
number = digit+

Then, any number node could simply be the number, and we don't get a bunch of (unneeded) nodes for each component – because the angle brackets signify that this rule should not be "reported" separately.

I suppose this could either be implemented while parsing the grammar – replacing any reference to such a rule with simply its contents (at a preprocessing stage), or as part of the parsing of the text...

@erikrose
Copy link
Owner

But then wouldn't you have to repeat the visitation code (e.g. int(...)) in the visit_* method for each rule that calls digit? Maybe there's a more compelling example I'm not forseeing; I could use some help.

If we do this, here's a spelling idea:

>thing< = some nodeless rule  # or think of it as a dumb, C-style "macro"

That's consistent with, or at least reminiscent, of the term-silencing syntax proposed in #29 (comment).

@erikrose
Copy link
Owner

I suppose suppressing node formation might not be as disastrous if you aren't using NodeVisitor. But I can't help thinking that, whatever you write to process the tree, you'll end up repeating yourself if you don't let digit be a node.

@keleshev
Copy link
Contributor

If you take number from JSON grammar:

number = int frac? exp?
int = "-"? ((digit1to9 digits) / digit)
frac = "." digits
exp = e digits
digits = digit+
e = "e+" / "e-" / "e" / "E+" / "E-" / "E"

Here, you don't really care about int or frac or exp, you just want to call float(number), you don't need the rest of nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants