Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample Usage of Myna in TypeScript #23

Open
rmtuckerphx opened this issue Feb 19, 2019 · 7 comments
Open

Sample Usage of Myna in TypeScript #23

rmtuckerphx opened this issue Feb 19, 2019 · 7 comments

Comments

@rmtuckerphx
Copy link

How would you create grammar_markdown.js in TypeScript?

@cdiggins
Copy link
Owner

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 cdiggins reopened this Mar 25, 2019
@rmtuckerphx
Copy link
Author

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

@cdiggins
Copy link
Owner

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

@cdiggins cdiggins reopened this Mar 26, 2019
@rmtuckerphx
Copy link
Author

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

@cdiggins
Copy link
Owner

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
Copy link
Owner

I think the language and tooling for TypeScript is mediocre.

@ninoseki
Copy link
Contributor

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants