@notatki/parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Aliaksandr Radzivanovich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./nodes.js";
2
+ export * from "./parser.js";
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./nodes.js";
2
+ export * from "./parser.js";
package/nodes.d.ts ADDED
@@ -0,0 +1,90 @@
1
+ import type { LocationRange } from "./parser.js";
2
+
3
+ export type Node = {
4
+ loc: LocationRange;
5
+ };
6
+
7
+ export type NoteNode = {
8
+ properties: PropertyNode[];
9
+ fields: FieldNode[];
10
+ end: Token;
11
+ } & Node;
12
+
13
+ export type PropertyNode = {
14
+ name: Token;
15
+ value: Token;
16
+ } & Node;
17
+
18
+ export type FieldNode = {
19
+ name: Token;
20
+ value: Token;
21
+ } & Node;
22
+
23
+ export type ModelNode = {
24
+ name: Token;
25
+ id: ModelIdNode;
26
+ cloze: Token;
27
+ fields: ModelFieldNode[];
28
+ cards: ModelCardNode[];
29
+ styling: CardStylingNode | null;
30
+ } & Node;
31
+
32
+ export type ModelIdNode = {
33
+ id: Token;
34
+ value: number;
35
+ } & Node;
36
+
37
+ export type ModelFieldNode = {
38
+ name: Token;
39
+ required: boolean;
40
+ } & Node;
41
+
42
+ export type ModelCardNode = {
43
+ name: Token;
44
+ front: CardFrontNode;
45
+ back: CardBackNode;
46
+ } & Node;
47
+
48
+ export type CardFrontNode = {
49
+ text: string;
50
+ } & Node;
51
+
52
+ export type CardBackNode = {
53
+ text: string;
54
+ } & Node;
55
+
56
+ export type CardStylingNode = {
57
+ text: string;
58
+ } & Node;
59
+
60
+ export type TemplateItemNode = TemplateTextNode | TemplateFieldNode | TemplateBranchNode;
61
+
62
+ export type TemplateTextNode = {
63
+ type: "text";
64
+ text: string;
65
+ } & Node;
66
+
67
+ export type TemplateFieldNode = {
68
+ type: "field";
69
+ field: Token;
70
+ } & Node;
71
+
72
+ export type TemplateBranchNode = {
73
+ type: "branch";
74
+ cond: IfFieldNode;
75
+ items: TemplateItemNode[];
76
+ end: EndIfFieldNode;
77
+ } & Node;
78
+
79
+ export type IfFieldNode = {
80
+ field: Token;
81
+ not: boolean;
82
+ } & Node;
83
+
84
+ export type EndIfFieldNode = {
85
+ field: Token;
86
+ } & Node;
87
+
88
+ export type Token = {
89
+ text: string;
90
+ } & Node;
package/nodes.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@notatki/parser",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "index.js",
6
+ "devDependencies": {
7
+ "peggy": "^5.0.6"
8
+ },
9
+ "scripts": {
10
+ "compile": "peggy --format=es --dts --output=parser$.js --allowed-start-rules=Start,NoteList,ModelList,Template ./parser.peggy",
11
+ "pretest": "npm run compile",
12
+ "test": "node --test '**/*.test.js'"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "gitHead": "545474a85a5cc649ff9ce50f36e87ccf5c57073a"
18
+ }
package/parser$.d.ts ADDED
@@ -0,0 +1,223 @@
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 = "Start" | "NoteList" | "ModelList" | "Template";
173
+ export interface ParseOptions<T extends StartRuleNames = "Start"> {
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<"Start">>(
201
+ input: string,
202
+ options?: Options,
203
+ ): any;
204
+
205
+ declare function ParseFunction<Options extends ParseOptions<"NoteList">>(
206
+ input: string,
207
+ options?: Options,
208
+ ): any;
209
+
210
+ declare function ParseFunction<Options extends ParseOptions<"ModelList">>(
211
+ input: string,
212
+ options?: Options,
213
+ ): any;
214
+
215
+ declare function ParseFunction<Options extends ParseOptions<"Template">>(
216
+ input: string,
217
+ options?: Options,
218
+ ): any;
219
+
220
+ declare function ParseFunction<Options extends ParseOptions<StartRuleNames>>(
221
+ input: string,
222
+ options?: Options,
223
+ ): any;