Comments (7)
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
from myna-parser.
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.
from myna-parser.
I closed it because you solved my "issue". :)
from myna-parser.
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!
from myna-parser.
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.
from myna-parser.
@elidoran I updated the main help page (https://cdiggins.github.io/myna-parser/) to try and integerate your suggestions. Thanks a lot!
from myna-parser.
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.
from myna-parser.
Related Issues (20)
- explainjs listed as dependency
- Possible performance degradation under Chrome Canary 61 HOT 2
- Feature request: Karma and QUnit HOT 7
- Possible performance improvements HOT 2
- Add return types to standalone functions
- 2.4.0 no longer works in the browser. HOT 8
- Case insensitive keywords? HOT 5
- Myna module improperly exported
- Error recovery and continuation
- 2 small mistakes in the sample code in readme.md
- typescript declaration file absent
- Sample Usage of Myna in TypeScript HOT 7
- ParserError messages don't seem to be propogated HOT 3
- Question: how to check in sequence if there is a whitespace + symbol
- Example in the README file does not work out of the box.
- Question on requirements for the programing language you are working on. HOT 5
- delay() rule ID number changes each time HOT 4
- Create railroad diagrams HOT 4
- Add Myna to JavaScript performance benchmarks HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from myna-parser.