UNPKG

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