kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.59k stars 232 forks source link

Typing issue #302

Open glmdgrielson opened 6 years ago

glmdgrielson commented 6 years ago

As a fun little thing I tried porting this to Typescript and I found a big hole in the Typescript generation, namely StreamLexer doesn't match the Lexer interface you provided. Oops...

Class 'StreamLexer' incorrectly implements interface 'Lexer'.
  Types of property 'formatError' are incompatible.
    Type '(token: any, message: any) => any' is not assignable to type '(token: Token) => string'.
tjvr commented 6 years ago

We’ve had some problems recently with the typescript bindings; they’re not written or maintained by the core maintainers (@hardmath123 and I).

Is this a bug in the generator, or in the typescript bindings? Thanks.

Sent with GitHawk

glmdgrielson commented 6 years ago

I think it's in the bindings:

export interface Lexer {reset:(chunk:string, info:any) => void; next:() => Token | undefined; save:() => any; formatError:(token:Token) => string; has:(tokenType:string) => boolean};

The error I got came when I made StreamLexer implement Lexer. Also, about the TypeScript stuff: If you need help with that, I'd be willing to lend a hand! (p.s. I'm not entirely sure what you mean by generator/typescript bindings but it's in the generate.typescript function)

tjvr commented 6 years ago

Sorry. By the typescript bindings, I meant the @types/nearley package: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/nearley

If you’ve found a bug in generate.js, feel free to send a PR :-)

Sent with GitHawk

glmdgrielson commented 6 years ago

Okay, yeah it's definitely a bug in generate.js. I'll try to send a PR soon, because both the default lexer AND the recommended one - yours, moo - fail to match the type specified by the Lexer definition. What you have is:

formatError(token: Token, message?: string): string;

What you require is:

formatError(token: Token): string;
glmdgrielson commented 6 years ago

The pull request is up. :tada: