myna-parser
myna-parser copied to clipboard
Sample Usage of Myna in TypeScript
How would you create grammar_markdown.js in TypeScript?
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.
Tried to setup a TypeScript class for my grammar, but property chaining doesn't work:
Not sure that property chaining is the problem there, you are missing a closing parentheses. If you fix that, what happens next?
error TS2339: Property 'opt' does not exist on type 'Rule'. public number: Rule = Myna.seq(Myna.integer, this.fraction.opt.ast);
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#.
I think the language and tooling for TypeScript is mediocre.
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);