diff --git a/lib/generate.js b/lib/generate.js index e9e88664..08d52748 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -226,7 +226,7 @@ output += " ParserStart: " + JSON.stringify(parser.start) + ",\n"; output += "};\n"; output += "\n"; - output += "export default grammar;\n"; + output += "export = grammar;\n"; return output; }; diff --git a/package.json b/package.json index e13686fc..7c8d6f1c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "nearley", - "version": "2.19.7", - "description": "Simple, fast, powerful parser toolkit for JavaScript.", + "name": "@meditech/nearley", + "version": "2.19.9", + "description": "Simple, fast, powerful parser toolkit for JavaScript. Added fix for typescript per https://github.com/kach/nearley/pull/544", "main": "lib/nearley.js", "dependencies": { "commander": "^2.19.0", @@ -13,7 +13,8 @@ "files": [ "bin/", "lib/", - "builtin/" + "builtin/", + "types.d.ts" ], "bin": { "nearleyc": "bin/nearleyc.js", @@ -56,5 +57,6 @@ "microtime": "^2.1.2", "mocha": "^2.5.3", "typescript": "^2.6.1" - } + }, + "types": "types.d.ts" } diff --git a/test/nearleyc.test.js b/test/nearleyc.test.js index 5be62a8b..56e70e86 100644 --- a/test/nearleyc.test.js +++ b/test/nearleyc.test.js @@ -65,7 +65,7 @@ describe("bin/nearleyc", function() { expect(stderr).toBe(""); expect(stdout).toBe(""); sh(`tsc ${outPath}.ts`); - const grammar = nearley.Grammar.fromCompiled(require(`./${outPath}.js`).default); + const grammar = nearley.Grammar.fromCompiled(require(`./${outPath}.js`)); expect(parse(grammar, "<123>")).toEqual([ [ '<', '123', '>' ] ]); }); diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 00000000..a5e15237 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,114 @@ +// Type definitions for nearley 2.11 +// Project: https://github.com/Hardmath123/nearley#readme +// Definitions by: Nikita Litvin +// BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module '@meditech/nearley' { + export class Parser { + /** + * Reserved token for indicating a parse fail. + */ + static fail: unknown; + + grammar: Grammar; + options: ParserOptions; + lexer: Lexer; + lexerState?: LexerState; + current: number; + /** + * An array of possible parsings. Each element is the thing returned by your grammar. + * + * Note that this is undefined before the first feed() call. + * It isn't typed as `any[] | undefined` to spare you the null checks when it's definitely an array. + */ + results: any[]; + + constructor(grammar: Grammar, options?: ParserOptions); + + /** + * The Parser object can be fed data in parts with .feed(data). + * You can then find an array of parsings with the .results property. + * If results is empty, then there are no parsings. + * If results contains multiple values, then that combination is ambiguous. + * + * @throws If there are no possible parsings, nearley will throw an error + * whose offset property is the index of the offending token. + */ + feed(chunk: string): this; + finish(): any[]; + restore(column: { [key: string]: any, lexerState: LexerState }): void; + save(): { [key: string]: any, lexerState: LexerState }; + } + + export interface ParserOptions { + keepHistory?: boolean; + lexer?: Lexer; + } + + export class Rule { + static highestId: number; + + id: number; + name: string; + symbols: any[]; + postprocess?: Postprocessor; + + constructor(name: string, symbols: any[], postprocess?: Postprocessor); + + toString(withCursorAt?: number): string; + } + + export class Grammar { + static fromCompiled(rules: CompiledRules): Grammar; + + rules: Rule[]; + start: string; + byName: { [ruleName: string]: Rule[] }; + lexer?: Lexer; + + constructor(rules: Rule[]); + } + + export interface CompiledRules { + Lexer?: Lexer; + ParserStart: string; + ParserRules: ParserRule[]; + } + + export interface ParserRule { + name: string; + symbols: any[]; + postprocess?: Postprocessor; + } + + export type Postprocessor = (data: any[], reference?: number, wantedBy?: unknown) => void; + + export interface Lexer { + /** + * Sets the internal buffer to data, and restores line/col/state info taken from save(). + */ + reset(data: string, state?: LexerState): void; + /** + * Returns e.g. {type, value, line, col, …}. Only the value attribute is required. + */ + next(): Token | undefined; + /** + * Returns an object describing the current line/col etc. This allows us + * to preserve this information between feed() calls, and also to support Parser#rewind(). + * The exact structure is lexer-specific; nearley doesn't care what's in it. + */ + save(): LexerState; + /** + * Returns a string with an error message describing the line/col of the offending token. + * You might like to include a preview of the line in question. + */ + formatError(token: Token, message: string): string; + } + + export type Token = string | { value: string; }; + + export interface LexerState { + [x: string]: any; + } +}