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

Is it possible to use nearley-unparse with Moo? #341

Open
RobRoseKnows opened this issue Feb 6, 2018 · 5 comments
Open

Is it possible to use nearley-unparse with Moo? #341

RobRoseKnows opened this issue Feb 6, 2018 · 5 comments
Labels

Comments

@RobRoseKnows
Copy link
Contributor

I've been trying to use Nearley to parse a list of problems but I'm having trouble getting nearley-unparse to correctly generate text matching Moo tokens. I posted a StackOverflow question to no avail so I thought it might be better suited for here. I'm using Nearley v2.11.0, Node v7.9.0 and NPM v4.2.0.

Everything is in the same Gist, but my grammar looks like this and the problems I'm trying to parse look like this.

After compiling the grammar however and using nearley-unparse however, I just end up with text that looks like this, with the Moo tokens missing.

Section  (Due )- #*, , 
Section  (Due )- #, *, 
Section  (Due )- #*, , , *, 
Section  (Due )- #*, *
Section (Due )- #*, *, *, *

Is this a limitation of Nearley or Moo? Or am I doing something wrong with my grammar?

@tjvr
Copy link
Collaborator

tjvr commented Feb 6, 2018

Hello there! Thanks for trying to post a StackOverflow question first, I’m sorry there wasn’t anyone able to help there :-)

This is a limitation of the unparser: it doesn’t know how to generate random strings satisfying a regexp, nor are we planning to do so (that would be a project in itself!).

Your grammar looks fine to me, at a brief glance; if you test it with nearley-test, hopefully you’ll find you get the parse trees you expect.

Sent with GitHawk

@tjvr tjvr added the question label Feb 6, 2018
@RobRoseKnows
Copy link
Contributor Author

That's disappointing though not unexpected. Have you considered using a preexisting library to do that? On a cursory search it looks like this library can generate random strings to match regular expressions and it looks to be still actively maintained. It also looks to not have that many dependencies. I don't know whether or not this would be a change to Moo instead though.

Do you want to answer the StackOverflow question, or should I just self-answer?

@danielo515
Copy link

Glad I found this issue, I was going crazy.
In my case the un parse is not generating anything, just empty strings.
Since you are promoting the usage of tokenizers on the main page I think you should also put a note explaining that unparse will not be able to generate strings for those lexers using regular expressions.
That said, how important is to use a lexer? I find nearly itself good enough.

@bumbaaz
Copy link

bumbaaz commented Jan 5, 2019

Hi, i just want to point out that it may be the case that nearley-unparse may not work even when the rules defined in moo do not involve regexes.

The following grammar, when compiled, only returns empty strings when unparsed

    @{%
        const moo = require('moo')
        let lexer = moo.compile({
    	word: ["hellothere"]
        });
    %}
    @lexer lexer
    result -> %word

While this works:

    result -> "hellothere"

@tjvr
Copy link
Collaborator

tjvr commented Jan 5, 2019

I haven't tested the unparser with Moo myself. I suppose it's plausible that it needs to be updated!

Sent with GitHawk

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

4 participants