How to *use* ?
elidoran opened this issue · 7 comments
Preamble: I like this library's API. I use both Chevrotain and Parsimmon for parsing. This is an interesting new library.
I'm having the same trouble I had with Chevrotain. Both projects don't say how to use them to parse in their README. I've looked thru the grammer/tools and I still can't quite figure out what it is you expect us to do.
What I'm looking for is how to do this:
// get the reusable parser created with a grammar
var parser = require('./getMyAwesomeMynaParser')
// export a parse function which uses it
module.exports = function parse(input) {
return parser.parse(input)
// or, use some top-level rule
return parser.topRule(input)
}
Also, when I figure that out, I'm thinking it'll provide an AST. Or, is it possible to have the parser generate something as it parses similar to Parsimmon's map()
and others to generate a value?
Hi Eli,
Thanks for the feedback! Sorry for the delay in getting back to you.
Perhaps the best self-contained example of how to create parser would be:
https://github.com/cdiggins/myna-parser/blob/master/tools/myna_mustache_expander.js
Please let me know if this works well for you. I will take the time to update my docs to take into account your suggestions.
Thanks a lot!
Christopher
I didn't mind. Short wait. Thank you for replying.
I looked at that one more closely this time (less scanning, more reading). :) I see what you mean.
When I started looking at your library I was a bit overwhelmed. The whole "pass myna to them" and register with myna and grammars/ to tools/ and back and forth. I was getting a bit of "what is going on here"-itis.
I like an example which has little going on in the grammar and AST processing shows the key points in the whole flow.
Here's what I came up with after looking at the one you referenced:
// get the package with a short name for fluent API.
var M = require('myna-parser');
// let's make a grammar with a short name for reuse.
var G = {};
// set some simple grammar rules for:
// 'hello' or 'hello john' or 'hello, john'
G.greeting = M.choice('hello', 'hi').ast;
G.name = M.delimited(M.letters.oneOrMore, ' ').ast;
G.extra = M.seq(M.text(',').opt, ' ', G.name);
G.salutation = M.seq(G.greeting, G.extra.opt, M.end);
// *must* give the grammar to myna for processing.
M.registerGrammar('salutation', G);
// generate a result from the ast
function generate(ast) {
var result;
var node;
if (ast) {
result = {};
node = ast.child('greeting');
if (node) result.greeting = node.allText;
node = ast.child('name')
if (node) result.name = node.allText;
}
else {
result = { error: 'invalid input' };
}
return result;
}
// alias the top rule we want to target for parsing.
var topRule = G.salutation;
// export a parse function
module.exports = function parse(input) {
// use myna to parse input starting at topRule
var ast = M.parse(topRule, input);
var result = generate(ast);
return result;
}
It's all in one file so no bouncing around looking back-and-forth. And, it's short enough to see on the screen at once.
I probably should have used an even more simple grammar so the generate()
function is tiny. Like a comma separated list of words and then map the AST's children array to an array of the words. Anyway, that's what I ended up with.
Also, an API document would help. Even making the above I hunted around in the examples and the source to find what is available.
Thank you.
I closed it because you solved my "issue". :)
Thanks a lot Eli for your help and suggestions. You make some great points! I will not be able to make an update for another week. I'm currently working on a couple of other things, but will ping you as soon as I have an update. All the best!
Thank you for letting me know. I look forward to your reply.
Also, I've found it's a lot easier to look at your TypeScript file and I found places defining the methods I was hunting thru the examples for.
Rule methods
Groups of myna methods: 1 and 2.
There's more mixed in there in between (which I'm sure you know well). These are the main ones helping me so I thought I'd mark them here in case someone else reads this.
Have a great week.
@elidoran I updated the main help page (https://cdiggins.github.io/myna-parser/) to try and integerate your suggestions. Thanks a lot!
Looks great. Faster in version two and better docs. Great progress man, good job.
I noticed the "Writing a Grammar" example has a delimiter
var which wasn't defined anywhere.
And, I'm confused about the grammar object creation. Why you do var g = new function() { this.ruleName = /* ... */ }
like it's a constructor function requiring new/this instead of creating the object directly (and without the new/this stuff): var g = { ruleName: /* ... */ }
.
Or, does myna need the grammar to be a function which builds a new grammar object every time? If so, the grammar could be defined as a normal function returning the object instead of a constructor style function with new/this stuff:
function g() {
return {
ruleName: /* ... */
}
}
The grammar I play around with creates the grammar by creating an object and setting the properties on it. It didn't error so I assumed it was fine. Perhaps there's something else going on.