myna-parser icon indicating copy to clipboard operation
myna-parser copied to clipboard

Sample Usage of Myna in TypeScript

Open rmtuckerphx opened this issue 5 years ago • 7 comments

How would you create grammar_markdown.js in TypeScript?

rmtuckerphx avatar Feb 19 '19 00:02 rmtuckerphx

All JavaScript is also valid TypeScript, so you can start by just renaming the file from .js to .ts. For more information perhaps see: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html. It depends on how strict your TypeScript is, I suppose. I've never really tried. If you give it a shot, let me know how it works, and I can help you out with specific issues along the way.

cdiggins avatar Mar 25 '19 17:03 cdiggins

Tried to setup a TypeScript class for my grammar, but property chaining doesn't work: image

rmtuckerphx avatar Mar 25 '19 17:03 rmtuckerphx

Not sure that property chaining is the problem there, you are missing a closing parentheses. If you fix that, what happens next?

cdiggins avatar Mar 26 '19 22:03 cdiggins

error TS2339: Property 'opt' does not exist on type 'Rule'. public number: Rule = Myna.seq(Myna.integer, this.fraction.opt.ast);

rmtuckerphx avatar Mar 29 '19 19:03 rmtuckerphx

This seems like a configuration problem: the opt property does exists on Rule: https://github.com/cdiggins/myna-parser/blob/master/myna.ts#L343. I'll be honest, this kind of silliness, is why I stopped coding in TypeScript, and went back to C#.

cdiggins avatar Mar 31 '19 20:03 cdiggins

I think the language and tooling for TypeScript is mediocre.

cdiggins avatar Mar 31 '19 20:03 cdiggins

Following code works without error in my env. (myna-parser: 2.5.1 & typescript: 3.5.1)

import { Myna } from "myna-parser";

export class MyGrammar {
  public fraction: Myna.Rule = Myna.seq(".", Myna.digit.zeroOrMore).ast;
  public number: Myna.Rule = Myna.seq(Myna.integer, this.fraction.opt).ast;
  public plainText: Myna.Rule = Myna.notChar("\n\r").oneOrMore;
  public document: Myna.Rule = Myna.choice(this.plainText, this.number)
    .zeroOrMore;
}

const myGrammar = new MyGrammar();

const input = "11111";

const ast = Myna.parse(myGrammar.document, input);
console.log(ast);

ninoseki avatar Jun 11 '19 12:06 ninoseki