Skip to content

Commit ab20d80

Browse files
authored
Merge pull request #568 from hildjj/typescript-5.6
Update to TypeScript 5.6. Begin generating .d.ts for parser.js.
2 parents a17f8ce + f493b4c commit ab20d80

File tree

7 files changed

+394
-180
lines changed

7 files changed

+394
-180
lines changed

.ncurc.cjs

-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ module.exports = {
66
"chai", // Moved to es6
77
"@types/chai", // Should match chai,
88
"rimraf", // Requires node 20
9-
"typescript", // Waiting on typescript-eslint
109
],
1110
};

eslint.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default [
1616
"test/cli/fixtures/useFrags/fs.js", // Generated
1717
"test/cli/fixtures/useFrags/identifier.js", // Generated
1818
"lib/parser.js", // Generated
19+
"lib/parser.d.ts", // Generated
1920
"bin/generated_template.d.ts", // Generated
2021
],
2122
},

lib/parser.d.ts

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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[];

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@
5757
},
5858
"devDependencies": {
5959
"@peggyjs/eslint-config": "^5.0.0",
60-
"@rollup/plugin-commonjs": "^28.0.0",
60+
"@rollup/plugin-commonjs": "^28.0.1",
6161
"@rollup/plugin-json": "^6.1.0",
6262
"@rollup/plugin-multi-entry": "^6.0.1",
6363
"@rollup/plugin-node-resolve": "^15.3.0",
64-
"@rollup/plugin-typescript": "^12.1.0",
64+
"@rollup/plugin-typescript": "^12.1.1",
6565
"@types/chai": "^4.3.11",
6666
"@types/jest": "^29.5.13",
67-
"@types/node": "^22.7.5",
67+
"@types/node": "^22.7.6",
6868
"chai": "^4.3.11",
6969
"chai-like": "^1.1.3",
7070
"copyfiles": "^2.4.1",
@@ -79,11 +79,11 @@
7979
"rollup": "^4.24.0",
8080
"rollup-plugin-ignore": "1.0.10",
8181
"source-map": "^0.8.0-beta.0",
82-
"terser": "^5.34.1",
82+
"terser": "^5.36.0",
8383
"ts-jest": "^29.2.5",
84-
"tslib": "^2.7.0",
85-
"typescript": "5.5.4",
86-
"typescript-eslint": "8.8.1"
84+
"tslib": "^2.8.0",
85+
"typescript": "5.6.3",
86+
"typescript-eslint": "8.10.0"
8787
},
8888
"dependencies": {
8989
"@peggyjs/from-mem": "1.3.5",
@@ -98,7 +98,7 @@
9898
"browserslist": [
9999
"defaults, maintained node versions, not op_mini all"
100100
],
101-
"packageManager": "[email protected].1",
101+
"packageManager": "[email protected].2",
102102
"engines": {
103103
"node": ">=18"
104104
}

0 commit comments

Comments
 (0)