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

null indicates failure - workaround for a grammar that should produce nulls? #17

Closed
patrickdlogan opened this issue Feb 24, 2011 · 7 comments
Assignees
Labels
Milestone

Comments

@patrickdlogan
Copy link

I have a grammar for a data structure that ideally should include nulls. If the top-level result is null, no problem: the grammar can produce a special object. In that case the function calling the parser can detect the special object and return a null.

However for nulls deeper in the data structure, I'd hate to have to traverse that structure to replace all special objects with nulls. Any ideas?

One thought is to tell the parser what value to use as the failure indicator. Then I can tell the parser to fail when some unique object is returned.

Another thought is to indicate failure by throwing an exception. Or maybe there is an existing workaround I don't know of?

Thanks! PEG.js has saved me a ton of time.

@dmajda
Copy link
Contributor

dmajda commented Mar 28, 2011

I recommend using a special object to represent null when returning from an action and converting it to regular null in the first action up in the grammar where it is possible. This way you would not have to walk the whole tree.

Example:

{
  null_ = new Object();
}

start = items:(t / f / n)* {
  return items.map(function(item) { return item === null_ ? null : item});
}

t = "t" { return true;  }
f = "f" { return false; }
n = "n" { return null_; }

Telling the parser what value to use to represent the failure would work, but I don't think the additional complexity is worth it.

Exceptions would work too, but they don't have the right semantics here (they are for exceptional circumstances, which failing to parse something inside a backtrcking parser is not). Moreover, I think they would hinder peformance (but this is just a guess, it would have to be implemented and benchamrked to be sure).

I'll keep this one open for a while since it bothers me too a bit :-)

@ghost ghost assigned dmajda Aug 13, 2011
@michaelficarra
Copy link

Why not just define a global fail object, which can be returned to fail a rule? That's probably what should have been done in the first place.

@curvedmark
Copy link

Encounter this one today as well.

Also since null represents absence in js, maybe the value of var in var:ident? should be null instead of undefined when ident is unmatched?

Should I open a new issue for it? Feel like somewhat related.

@dignifiedquire
Copy link

+1 for fail object

Not sure about null for var in var:ident. I think undefined is fine here.

@curvedmark
Copy link

Another small reason for using null is that if you want to represent the ast using json, which doesn't allow undefined, you either have to litter grammar actions with conversions or traverse the whole ast to convert undefined to null

@jceme
Copy link

jceme commented Oct 7, 2012

I encountered the same problem allowing null and undefined for the top-level result as well as in-between results.
It can be quite a mess to handle a sentinel object in all the appropriate places.

Couldn't you throw something in the action block to signalize failures or is the performance impact too much?

@dmajda
Copy link
Contributor

dmajda commented Aug 14, 2013

This will be resolved as part of #198, closing.

@dmajda dmajda closed this as completed Aug 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants