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.

How to parse with the API: docs outdated?

See original GitHub issue

I already have managed to port one of my parsing tasks to nearley (see https://github.com/loveencounterflow/mojikura-idl/blob/nearley/src/idlx.nearley etc), but this time around i seem to be doing something wrong.

For easier reference, I’ve uploaded a simple demo project to https://github.com/loveencounterflow/nearley-examples. To wit:

The grammar I use here is the JSON grammar from the nearley/examples folder; that I compiled with nearleyc grammars/json.ne -o grammars/json.js. Then, I copy-pasted (and adapted) this code from the README:

// from-the-docs.js
var grammar = require("./grammars/json.js");
var nearley = require("nearley");
var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
p.feed("[1,2,3]");
console.log( require( 'util' ).inspect( p.results ) );

But this fails:

$ ./run-example-from-the-docs.sh                 
xxx/nearley-examples/node_modules/nearley/lib/nearley.js:323
            throw err;
            ^

Error: invalid syntax at line 1 col 2:

  [1,2,3]
   ^
Unexpected "1"

    at Parser.feed (xxx/nearley-examples/node_modules/nearley/lib/nearley.js:320:23)
    at Object.<anonymous> (xxx/nearley-examples/from-the-docs.js:8:3)
...

OTOH, I can run nearleyc grammars/json.ne -o grammars/json.js && nearley-test grammars/json.js -i "[1,2,3]":

$ ./run-example-with-nearley-test.sh
...
1: {json$subexpression$1 → array ● }, from: 0
2: {json → _ json$subexpression$1 ● _}, from: 0
3: {_ →  ● }, from: 7
4: {_ →  ● %space}, from: 7
5: {json → _ json$subexpression$1 _ ● }, from: 0


Parse results: 
[ [ 1, 2, 3 ] ]

Did I miss something here?

BTW when studying the JSON example, I realized it uses the moo parser; I guess the docs would definitely profit from a link to that software, as it fits so nicely into the nearley setup.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
deltaideacommented, Jun 22, 2017

Yeah, I’ve seen this problem before. Solution:

- var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
+ var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart, { lexer: grammar.Lexer });

Without this, nearley uses its default parser instead of moo because it doesn’t know about your custom one. Look at the compiled json.js, it exports { Lexer, ParserRules, ParserStart }.

You might ask, “Then what’s the point of @lexer and inlining lexer into .ne”? And the answer is that you’re right, there’s no point. You can have your lexer = moo.compile() in a separate file and pass it into new nearley.Parser() just like now.

This is an issue with docs. 😢 I’ll see if I can fix that when I have some time.

@Hardmath123 Alternatively, it might be a good idea to allow passing the whole grammar object as a single argument to new Parser(). Just duck-type that the first argument has ParserRules and ParserStart. This would make @lexer actually useful.

0reactions
loveencounterflowcommented, Jun 23, 2017

OK var p = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)) looks better than what I’m doing now… should definitely get mention in the README, didn’t know about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Parse A Document (Old API) · jbeder/yaml-cpp Wiki
The following describes the old API. For the new API, see the Tutorial. Contents. Basic Parsing. The parser accepts streams, not file names, ......
Read more >
REST API Guide | Parse
Storing data through the Parse REST API is built around a JSON encoding of the object's data. This data is schemaless, which means...
Read more >
API documentation with comments parsing
parse -comments extracts comment blocks from Javascript-type source code and analyzes Javadoc and JSDoc-like tags to produce JSON objects. It ...
Read more >
apiDoc - Inline Documentation for RESTful web APIs
Settings for apidoc.json ; version, Version of your project. If no apidoc.json with the field exists, then apiDoc try to determine the the...
Read more >
Indexing API Errors - Google Developers
Check the value of the q request parameter. keyExpired, The API key provided in the request expired, which means the API server is...
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