|
| 1 | +/** Provides information pointing to a location within a source. */ |
| 2 | +export interface Location { |
| 3 | + /** Line in the parsed source (1-based). */ |
| 4 | + readonly line: number; |
| 5 | + /** Column in the parsed source (1-based). */ |
| 6 | + readonly column: number; |
| 7 | + /** Offset in the parsed source (0-based). */ |
| 8 | + readonly offset: number; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Anything that can successfully be converted to a string with `String()` |
| 13 | + * so that it can be used in error messages. |
| 14 | + * |
| 15 | + * The GrammarLocation class in Peggy is a good example. |
| 16 | + */ |
| 17 | +export interface GrammarSourceObject { |
| 18 | + readonly toString: () => string; |
| 19 | + |
| 20 | + /** |
| 21 | + * If specified, allows the grammar source to be embedded in a larger file |
| 22 | + * at some offset. |
| 23 | + */ |
| 24 | + readonly offset?: undefined | ((loc: Location) => Location); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Most often, you just use a string with the file name. |
| 29 | + */ |
| 30 | +export type GrammarSource = string | GrammarSourceObject; |
| 31 | + |
| 32 | +/** The `start` and `end` position's of an object within the source. */ |
| 33 | +export interface LocationRange { |
| 34 | + /** |
| 35 | + * A string or object that was supplied to the `parse()` call as the |
| 36 | + * `grammarSource` option. |
| 37 | + */ |
| 38 | + readonly source: GrammarSource; |
| 39 | + /** Position at the beginning of the expression. */ |
| 40 | + readonly start: Location; |
| 41 | + /** Position after the end of the expression. */ |
| 42 | + readonly end: Location; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Expected a literal string, like `"foo"i`. |
| 47 | + */ |
| 48 | +export interface LiteralExpectation { |
| 49 | + readonly type: "literal"; |
| 50 | + readonly text: string; |
| 51 | + readonly ignoreCase: boolean; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Range of characters, like `a-z` |
| 56 | + */ |
| 57 | +export type ClassRange = [ |
| 58 | + start: string, |
| 59 | + end: string, |
| 60 | +] |
| 61 | + |
| 62 | +export interface ClassParts extends Array<string | ClassRange> { |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Expected a class, such as `[^acd-gz]i` |
| 67 | + */ |
| 68 | +export interface ClassExpectation { |
| 69 | + readonly type: "class"; |
| 70 | + readonly parts: ClassParts; |
| 71 | + readonly inverted: boolean; |
| 72 | + readonly ignoreCase: boolean; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Expected any character, with `.` |
| 77 | + */ |
| 78 | +export interface AnyExpectation { |
| 79 | + readonly type: "any"; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Expected the end of input. |
| 84 | + */ |
| 85 | +export interface EndExpectation { |
| 86 | + readonly type: "end"; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Expected some other input. These are specified with a rule's |
| 91 | + * "human-readable name", or with the `expected(message, location)` |
| 92 | + * function. |
| 93 | + */ |
| 94 | +export interface OtherExpectation { |
| 95 | + readonly type: "other"; |
| 96 | + readonly description: string; |
| 97 | +} |
| 98 | + |
| 99 | +export type Expectation = |
| 100 | + | AnyExpectation |
| 101 | + | ClassExpectation |
| 102 | + | EndExpectation |
| 103 | + | LiteralExpectation |
| 104 | + | OtherExpectation; |
| 105 | + |
| 106 | +/** |
| 107 | + * Pass an array of these into `SyntaxError.prototype.format()` |
| 108 | + */ |
| 109 | +export interface SourceText { |
| 110 | + /** |
| 111 | + * Identifier of an input that was used as a grammarSource in parse(). |
| 112 | + */ |
| 113 | + readonly source: GrammarSource; |
| 114 | + /** Source text of the input. */ |
| 115 | + readonly text: string; |
| 116 | +} |
| 117 | + |
| 118 | +export declare class SyntaxError extends Error { |
| 119 | + /** |
| 120 | + * Constructs the human-readable message from the machine representation. |
| 121 | + * |
| 122 | + * @param expected Array of expected items, generated by the parser |
| 123 | + * @param found Any text that will appear as found in the input instead of |
| 124 | + * expected |
| 125 | + */ |
| 126 | + static buildMessage(expected: Expectation[], found?: string | null | undefined): string; |
| 127 | + readonly message: string; |
| 128 | + readonly expected: Expectation[]; |
| 129 | + readonly found: string | null | undefined; |
| 130 | + readonly location: LocationRange; |
| 131 | + readonly name: string; |
| 132 | + constructor( |
| 133 | + message: string, |
| 134 | + expected: Expectation[], |
| 135 | + found: string | null, |
| 136 | + location: LocationRange, |
| 137 | + ); |
| 138 | + |
| 139 | + /** |
| 140 | + * With good sources, generates a feature-rich error message pointing to the |
| 141 | + * error in the input. |
| 142 | + * @param sources List of {source, text} objects that map to the input. |
| 143 | + */ |
| 144 | + format(sources: SourceText[]): string; |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Trace execution of the parser. |
| 149 | + */ |
| 150 | +export interface ParserTracer { |
| 151 | + trace: (event: ParserTracerEvent) => void; |
| 152 | +} |
| 153 | + |
| 154 | +export type ParserTracerEvent |
| 155 | + = { |
| 156 | + readonly type: "rule.enter"; |
| 157 | + readonly rule: string; |
| 158 | + readonly location: LocationRange |
| 159 | + } |
| 160 | + | { |
| 161 | + readonly type: "rule.fail"; |
| 162 | + readonly rule: string; |
| 163 | + readonly location: LocationRange |
| 164 | + } |
| 165 | + | { |
| 166 | + readonly type: "rule.match"; |
| 167 | + readonly rule: string; |
| 168 | + readonly location: LocationRange |
| 169 | + /** Return value from the rule. */ |
| 170 | + readonly result: unknown; |
| 171 | + }; |
| 172 | + |
| 173 | +export type StartRuleNames = "Grammar" | "ImportsAndSource"; |
| 174 | +export interface ParseOptions<T extends StartRuleNames = "Grammar"> { |
| 175 | + /** |
| 176 | + * String or object that will be attached to the each `LocationRange` object |
| 177 | + * created by the parser. For example, this can be path to the parsed file |
| 178 | + * or even the File object. |
| 179 | + */ |
| 180 | + readonly grammarSource?: GrammarSource; |
| 181 | + readonly startRule?: T; |
| 182 | + readonly tracer?: ParserTracer; |
| 183 | + |
| 184 | + // Internal use only: |
| 185 | + readonly peg$library?: boolean; |
| 186 | + // Internal use only: |
| 187 | + peg$currPos?: number; |
| 188 | + // Internal use only: |
| 189 | + peg$silentFails?: number; |
| 190 | + // Internal use only: |
| 191 | + peg$maxFailExpected?: Expectation[]; |
| 192 | + // Extra application-specific properties |
| 193 | + [key: string]: unknown; |
| 194 | +} |
| 195 | + |
| 196 | +export declare const StartRules: StartRuleNames[]; |
| 197 | +export declare const parse: typeof ParseFunction; |
| 198 | + |
| 199 | +// Overload of ParseFunction for each allowedStartRule |
| 200 | + |
| 201 | +declare function ParseFunction<Options extends ParseOptions<"Grammar">>( |
| 202 | + input: string, |
| 203 | + options?: Options, |
| 204 | +): import('./peg.d.ts').ast.Grammar; |
| 205 | + |
| 206 | +declare function ParseFunction<Options extends ParseOptions<"ImportsAndSource">>( |
| 207 | + input: string, |
| 208 | + options?: Options, |
| 209 | +): import('./peg.d.ts').ast.TopLevelInitializer[]; |
| 210 | + |
| 211 | +declare function ParseFunction<Options extends ParseOptions<StartRuleNames>>( |
| 212 | + input: string, |
| 213 | + options?: Options, |
| 214 | +): import('./peg.d.ts').ast.Grammar | import('./peg.d.ts').ast.TopLevelInitializer[]; |
0 commit comments