Giter VIP home page Giter VIP logo

Comments (7)

cdiggins avatar cdiggins commented on May 28, 2024

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.

elidoran avatar elidoran commented on May 28, 2024

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.

elidoran avatar elidoran commented on May 28, 2024

I closed it because you solved my "issue". :)

from myna-parser.

cdiggins avatar cdiggins commented on May 28, 2024

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.

elidoran avatar elidoran commented on May 28, 2024

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.

cdiggins avatar cdiggins commented on May 28, 2024

@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.

elidoran avatar elidoran commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.