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

allows old import style + allows nearely-test to run for typescript g… #544

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,7 +13,8 @@
"files": [
"bin/",
"lib/",
"builtin/"
"builtin/",
"types.d.ts"
],
"bin": {
"nearleyc": "bin/nearleyc.js",
Expand Down Expand Up @@ -56,5 +57,6 @@
"microtime": "^2.1.2",
"mocha": "^2.5.3",
"typescript": "^2.6.1"
}
},
"types": "types.d.ts"
}
2 changes: 1 addition & 1 deletion test/nearleyc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '>' ] ]);
});

Expand Down
114 changes: 114 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Type definitions for nearley 2.11
// Project: https://github.com/Hardmath123/nearley#readme
// Definitions by: Nikita Litvin <https://github.com/deltaidea>
// BendingBender <https://github.com/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;
}
}