Skip to content

Commit 0f6bc35

Browse files
committed
feat: add v1 grammar
1 parent d7ba3bc commit 0f6bc35

7 files changed

Lines changed: 2323 additions & 194 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ build/
55
tmp/
66
a.out
77
bin/
8-
examples/*.bin
8+
examples/*.bin
9+
node_modules/

grammar/package-lock.json

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

grammar/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "grammar",
3+
"version": "0.0.0",
4+
"description": "",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1",
7+
"build": "peggy --allowed-start-rules Program --dts --format es v1.pegjs"
8+
},
9+
"keywords": [],
10+
"author": "Darlan Alves <me@darlanalv.es>",
11+
"license": "MIT",
12+
"type": "module",
13+
"devDependencies": {
14+
"peggy": "^5.0.6"
15+
}
16+
}

grammar/v1.d.ts

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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 globalThis.SyntaxError {
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 expected: Expectation[];
128+
readonly found: string | null | undefined;
129+
readonly location: LocationRange;
130+
readonly name: string;
131+
constructor(
132+
message: string,
133+
expected: Expectation[],
134+
found: string | null,
135+
location: LocationRange,
136+
);
137+
138+
/**
139+
* With good sources, generates a feature-rich error message pointing to the
140+
* error in the input.
141+
* @param sources List of {source, text} objects that map to the input.
142+
*/
143+
format(sources: SourceText[]): string;
144+
}
145+
146+
/**
147+
* Trace execution of the parser.
148+
*/
149+
export interface ParserTracer {
150+
trace: (event: ParserTracerEvent) => void;
151+
}
152+
153+
export type ParserTracerEvent
154+
= {
155+
readonly type: "rule.enter";
156+
readonly rule: string;
157+
readonly location: LocationRange
158+
}
159+
| {
160+
readonly type: "rule.fail";
161+
readonly rule: string;
162+
readonly location: LocationRange
163+
}
164+
| {
165+
readonly type: "rule.match";
166+
readonly rule: string;
167+
readonly location: LocationRange
168+
/** Return value from the rule. */
169+
readonly result: unknown;
170+
};
171+
172+
export type StartRuleNames = "Program";
173+
export interface ParseOptions<T extends StartRuleNames = "Program"> {
174+
/**
175+
* String or object that will be attached to the each `LocationRange` object
176+
* created by the parser. For example, this can be path to the parsed file
177+
* or even the File object.
178+
*/
179+
readonly grammarSource?: GrammarSource;
180+
readonly startRule?: T;
181+
readonly tracer?: ParserTracer;
182+
183+
// Internal use only:
184+
readonly peg$library?: boolean;
185+
// Internal use only:
186+
peg$currPos?: number;
187+
// Internal use only:
188+
peg$silentFails?: number;
189+
// Internal use only:
190+
peg$maxFailExpected?: Expectation[];
191+
// Extra application-specific properties
192+
[key: string]: unknown;
193+
}
194+
195+
export declare const StartRules: StartRuleNames[];
196+
export declare const parse: typeof ParseFunction;
197+
198+
// Overload of ParseFunction for each allowedStartRule
199+
200+
declare function ParseFunction<Options extends ParseOptions<"Program">>(
201+
input: string,
202+
options?: Options,
203+
): any;
204+
205+
declare function ParseFunction<Options extends ParseOptions<StartRuleNames>>(
206+
input: string,
207+
options?: Options,
208+
): any;

0 commit comments

Comments
 (0)