Files
DefinitelyTyped/pegjs/pegjs.d.ts
CJ Bell a7d9f4a4f5 Updating pegs to 0.10.0. (#12151)
Also added comments and more detailed typing restrictions on parser-generator
options.
2016-10-25 22:30:55 +09:00

138 lines
6.5 KiB
TypeScript

// Type definitions for PEG.js v0.10.0
// Project: http://pegjs.org/
// Definitions by: vvakame <https://github.com/vvakame>, Tobias Kahlert <https://github.com/SrTobi>, C.J. Bell <https://github.com/siegebell>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace PEG {
function parse(input:string):any;
interface Location {
line: number;
column: number;
offset: number;
}
interface LocationRange {
start: Location,
end: Location
}
class SyntaxError {
line: number;
column: number;
offset: number;
location: LocationRange;
expected:any[];
found:any;
name:string;
message:string;
}
}
declare module "pegjs" {
type Location = PEG.Location;
type LocationRange = PEG.LocationRange;
interface ExpectedItem {
type: string;
value?: string;
description: string;
}
interface PegjsError extends Error {
name: string;
message: string;
location: LocationRange;
found?: any;
expected?: ExpectedItem[];
stack?: any;
}
type GrammarError = PegjsError;
var GrammarError: any;
interface ParserOptions {
startRule: string;
tracer: any;
}
interface Parser {
parse(input: string, options?:ParserOptions): any;
SyntaxError: any;
}
interface BuildOptionsBase {
/** rules the parser will be allowed to start parsing from (default: the first rule in the grammar) */
allowedStartRules?: string[];
/** if `true`, makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower (default: `false`) */
cache?: boolean;
/** selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) (default: `"speed"`) */
optimize?: "speed" | "size";
/** plugins to use */
plugins?: any[];
/** makes the parser trace its progress (default: `false`) */
trace?: boolean
}
interface ParserBuildOptions extends BuildOptionsBase {
/** if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return parser source code as a string (default: `"parser"`) */
output?: "parser"
}
interface OutputFormatAmdCommonjs extends BuildOptionsBase {
/** if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return parser source code as a string (default: `"parser"`) */
output: "source";
/** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
format: "amd" | "commonjs";
/** parser dependencies, the value is an object which maps variables used to access the dependencies in the parser to module IDs used to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, or `"umd"` (default: `{}`) */
dependencies?: any
}
interface OutputFormatUmd extends BuildOptionsBase {
/** if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return parser source code as a string (default: `"parser"`) */
output: "source";
/** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
format: "umd";
/** parser dependencies, the value is an object which maps variables used to access the dependencies in the parser to module IDs used to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, or `"umd"` (default: `{}`) */
dependencies?: any
/** name of a global variable into which the parser object is assigned to when no module loader is detected; valid only when `format` is set to `"globals"` or `"umd"` (default: `null`) */
exportVar?: any
}
interface OutputFormatGlobals extends BuildOptionsBase {
/** if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return parser source code as a string (default: `"parser"`) */
output: "source";
/** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
format: "globals";
/** name of a global variable into which the parser object is assigned to when no module loader is detected; valid only when `format` is set to `"globals"` or `"umd"` (default: `null`) */
exportVar?: any
}
interface OutputFormatBare extends BuildOptionsBase {
/** if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return parser source code as a string (default: `"parser"`) */
output: "source";
/** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
format?: "bare"
}
/** Returns a generated parser object. It will throw an exception if the grammar is invalid. The exception will contain `message` property with more details about the error. */
function generate(grammar: string, options?: ParserBuildOptions): Parser;
/** Returns the generated source code as a `string`. It will throw an exception if the grammar is invalid. The exception will contain `message` property with more details about the error. */
function generate(grammar: string, options: OutputFormatAmdCommonjs): string;
/** Returns the generated source code as a `string`. It will throw an exception if the grammar is invalid. The exception will contain `message` property with more details about the error. */
function generate(grammar: string, options: OutputFormatUmd): string;
/** Returns the generated source code as a `string`. It will throw an exception if the grammar is invalid. The exception will contain `message` property with more details about the error. */
function generate(grammar: string, options: OutputFormatGlobals): string;
/** Returns the generated source code as a `string`. It will throw an exception if the grammar is invalid. The exception will contain `message` property with more details about the error. */
function generate(grammar: string, options: OutputFormatBare): string;
namespace parser {
type SyntaxError = PegjsError;
var SyntaxError: any;
}
}