UNPKG

6.64 kBTypeScriptView Raw
1// Type definitions for PEG.js v0.10.0
2// Project: http://pegjs.org/
3// Definitions by: vvakame <https://github.com/vvakame>, Tobias Kahlert <https://github.com/SrTobi>, C.J. Bell <https://github.com/siegebell>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6declare namespace PEG {
7 function parse(input:string):any;
8
9 interface Location {
10 line: number;
11 column: number;
12 offset: number;
13 }
14
15 interface LocationRange {
16 start: Location,
17 end: Location
18 }
19
20 class SyntaxError {
21 line: number;
22 column: number;
23 offset: number;
24 location: LocationRange;
25 expected:any[];
26 found:any;
27 name:string;
28 message:string;
29 }
30}
31
32export type Location = PEG.Location;
33export type LocationRange = PEG.LocationRange;
34
35export interface ExpectedItem {
36 type: string;
37 value?: string | undefined;
38 description: string;
39}
40
41export interface PegjsError extends Error {
42 name: string;
43 message: string;
44 location: LocationRange;
45 found?: any;
46 expected?: ExpectedItem[] | undefined;
47 stack?: any;
48}
49
50export type GrammarError = PegjsError;
51export var GrammarError: any;
52
53export interface ParserOptions {
54 startRule?: string | undefined;
55 tracer?: any;
56 [key: string]: any;
57}
58
59export interface Parser {
60 parse(input: string, options?:ParserOptions): any;
61
62 SyntaxError: any;
63}
64
65export interface BuildOptionsBase {
66 /** rules the parser will be allowed to start parsing from (default: the first rule in the grammar) */
67 allowedStartRules?: string[] | undefined;
68 /** if `true`, makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower (default: `false`) */
69 cache?: boolean | undefined;
70 /** selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) (default: `"speed"`) */
71 optimize?: "speed" | "size" | undefined;
72 /** plugins to use */
73 plugins?: any[] | undefined;
74 /** makes the parser trace its progress (default: `false`) */
75 trace?: boolean | undefined
76}
77
78export interface ParserBuildOptions extends BuildOptionsBase {
79 /** 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"`) */
80 output?: "parser" | undefined
81}
82
83export interface OutputFormatAmdCommonjs extends BuildOptionsBase {
84 /** 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"`) */
85 output: "source";
86 /** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
87 format: "amd" | "commonjs";
88 /** 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: `{}`) */
89 dependencies?: any
90}
91
92export interface OutputFormatUmd extends BuildOptionsBase {
93 /** 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"`) */
94 output: "source";
95 /** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
96 format: "umd";
97 /** 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: `{}`) */
98 dependencies?: any
99 /** 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`) */
100 exportVar?: any
101}
102
103export interface OutputFormatGlobals extends BuildOptionsBase {
104 /** 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"`) */
105 output: "source";
106 /** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
107 format: "globals";
108 /** 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`) */
109 exportVar?: any
110}
111
112export interface OutputFormatBare extends BuildOptionsBase {
113 /** 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"`) */
114 output: "source";
115 /** format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`, `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` (default: `"bare"`) */
116 format?: "bare" | undefined
117}
118
119/** 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. */
120export function generate(grammar: string, options?: ParserBuildOptions): Parser;
121/** 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. */
122export function generate(grammar: string, options: OutputFormatAmdCommonjs): string;
123/** 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. */
124export function generate(grammar: string, options: OutputFormatUmd): string;
125/** 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. */
126export function generate(grammar: string, options: OutputFormatGlobals): string;
127/** 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. */
128export function generate(grammar: string, options: OutputFormatBare): string;
129
130export namespace parser {
131 type SyntaxError = PegjsError;
132 var SyntaxError: any;
133}
134export as namespace PEG;
135