@k-l-lambda/portolan 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,9 @@
1
+ export interface FormatResult {
2
+ source: string;
3
+ /** IDs generated for nodes that had none, with their 1-based lines. */
4
+ assigned: {
5
+ line: number;
6
+ id: string;
7
+ }[];
8
+ }
9
+ export declare function format(source: string): FormatResult;
package/dist/format.js ADDED
@@ -0,0 +1,35 @@
1
+ // `rhumb fmt`, 0.1 subset: assign missing IDs and normalize status aliases.
2
+ //
3
+ // Not yet implemented from spec section 7: re-indentation to 2 spaces, `-` markers,
4
+ // attribute normalization, edge `^` stripping and dedup, blank-line collapsing.
5
+ // Those need a full CST printer; this pass only rewrites node lines in place.
6
+ import { generateId } from "./edit.js";
7
+ import { parse } from "./parse.js";
8
+ export function format(source) {
9
+ const doc = parse(source);
10
+ const eol = source.includes("\r\n") ? "\r\n" : "\n";
11
+ const lines = source.replace(/\r\n?/g, "\n").split("\n");
12
+ const taken = new Set();
13
+ const all = [];
14
+ const walk = (nodes) => nodes.forEach((n) => (all.push(n), walk(n.children)));
15
+ walk(doc.nodes);
16
+ for (const n of all)
17
+ if (n.id)
18
+ taken.add(n.id);
19
+ // Lines whose trailing token is an invalid ID (W009): appending another ID would
20
+ // silently turn that token into title text, so leave them for the user.
21
+ const skip = new Set(doc.diagnostics.filter((d) => d.code === "W009" || d.code === "E011").map((d) => d.line));
22
+ const assigned = [];
23
+ for (const node of all) {
24
+ const i = node.line - 1;
25
+ lines[i] = lines[i].replace(/^(\s*[-*]\s+\[)([X~])(\])/, (_m, a, s, b) => a + (s === "X" ? "x" : "/") + b);
26
+ if (node.id === null && !skip.has(node.line)) {
27
+ const id = generateId(taken);
28
+ taken.add(id);
29
+ lines[i] = `${lines[i].trimEnd()} ^${id}`;
30
+ assigned.push({ line: node.line, id });
31
+ }
32
+ }
33
+ return { source: lines.join(eol), assigned };
34
+ }
35
+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,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAE/G,MAAM,QAAQ,GAA6B,EAAE,CAAC;IAC9C,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;QACvB,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACxB,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC,OAAO,CAAC,2BAA2B,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAC5G,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7C,MAAM,EAAE,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACd,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC;YAC3C,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,213 @@
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 = "Line" | "Inline";
173
+ export interface ParseOptions<T extends StartRuleNames = "Line"> {
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<"Line">>(
201
+ input: string,
202
+ options?: Options,
203
+ ): any;
204
+
205
+ declare function ParseFunction<Options extends ParseOptions<"Inline">>(
206
+ input: string,
207
+ options?: Options,
208
+ ): any;
209
+
210
+ declare function ParseFunction<Options extends ParseOptions<StartRuleNames>>(
211
+ input: string,
212
+ options?: Options,
213
+ ): any;