squiffy-compiler 6.0.1 → 7.0.0-beta.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/dist/parse.js ADDED
@@ -0,0 +1,258 @@
1
+ import { positionMapper, transformText } from "./text.js";
2
+ export const lineRegexes = {
3
+ section: /^\[\[(.*)\]\]:$/,
4
+ snippet: /^\[snippet:\s*(.+)\]:$/,
5
+ passage: /^\[(.*)\]:$/,
6
+ title: /^@title (.*)$/,
7
+ import: /^@import (.*)$/,
8
+ start: /^@start (.*)$/,
9
+ attributes: /^@set (.*)$/,
10
+ unset: /^@unset (.*)$/,
11
+ inc: /^@inc (\S+)(?: (\d+))?$/,
12
+ dec: /^@dec (\S+)(?: (\d+))?$/,
13
+ js: /^(\t| {4})(.*)$/,
14
+ continue: /^\+\+\+(.*)$/,
15
+ ui: /^@ui (.*)$/,
16
+ };
17
+ // Parses one .squiffy file into a syntax tree. This never fails: problems such as a passage
18
+ // with no section are reported by analyse(), which knows how the file fits into the story.
19
+ //
20
+ // Each line is classified exactly as the compiler always has (see the if/else chain below,
21
+ // whose order matters). The tree is grouped as if this is the story's main file: in an imported
22
+ // file, content before the first section is ignored, and analyse() deals with that.
23
+ export function parse(text, filename) {
24
+ const lines = text.replace(/\r/g, "").split("\n");
25
+ const lineRange = (line) => ({
26
+ start: { line, character: 0 },
27
+ end: { line, character: lines[line].length },
28
+ });
29
+ // The range of a substring of a line, found by searching from the end, since every
30
+ // value this is used for runs to the end of the (trimmed) line.
31
+ const valueRange = (line, value) => {
32
+ const trimmedEnd = lines[line].trimEnd().length;
33
+ const start = Math.max(0, trimmedEnd - value.length);
34
+ return { start: { line, character: start }, end: { line, character: start + value.length } };
35
+ };
36
+ const nameRange = (line, name, prefix) => {
37
+ const lineText = lines[line];
38
+ const start = lineText.indexOf(name, lineText.indexOf(prefix) + prefix.length);
39
+ return { start: { line, character: start }, end: { line, character: start + name.length } };
40
+ };
41
+ const preamble = newSection("preamble", { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } });
42
+ const file = {
43
+ type: "file",
44
+ filename,
45
+ range: { start: { line: 0, character: 0 }, end: { line: lines.length - 1, character: lines[lines.length - 1].length } },
46
+ preamble,
47
+ sections: [],
48
+ };
49
+ let section = preamble;
50
+ let passage = null;
51
+ let snippet = null;
52
+ const snippetLines = [];
53
+ // Whether the compiler would have a current section at this point. In the main file, content
54
+ // before any section header creates the "_default" section.
55
+ let haveSection = false;
56
+ let textStarted = false;
57
+ let inUiBlock = false;
58
+ const endSnippet = () => {
59
+ if (snippet) {
60
+ snippet.content = snippetLines.join("\n");
61
+ snippetLines.length = 0;
62
+ snippet = null;
63
+ }
64
+ };
65
+ const startSection = (node) => {
66
+ endSnippet();
67
+ section = node;
68
+ passage = null;
69
+ file.sections.push(node);
70
+ };
71
+ const add = (node) => {
72
+ (passage ?? section).body.push(node);
73
+ };
74
+ for (let i = 0; i < lines.length; i++) {
75
+ const line = lines[i];
76
+ const stripLine = line.trim();
77
+ const range = lineRange(i);
78
+ const match = {};
79
+ for (const key of Object.keys(lineRegexes)) {
80
+ const result = lineRegexes[key].exec(key == "js" ? line : stripLine);
81
+ if (result) {
82
+ match[key] = result;
83
+ }
84
+ }
85
+ if (match.snippet) {
86
+ endSnippet();
87
+ const name = match.snippet[1];
88
+ snippet = {
89
+ type: "snippet",
90
+ name,
91
+ nameRange: nameRange(i, name, "snippet:"),
92
+ header: { type: "snippet-header", name, range },
93
+ range,
94
+ content: "",
95
+ };
96
+ file.sections.push(snippet);
97
+ passage = null;
98
+ haveSection = false;
99
+ textStarted = false;
100
+ }
101
+ else if (match.section) {
102
+ const name = match.section[1];
103
+ const node = newSection("named", range);
104
+ node.name = name;
105
+ node.nameRange = nameRange(i, name, "[[");
106
+ node.header = { type: "section-header", name, range };
107
+ startSection(node);
108
+ haveSection = true;
109
+ textStarted = false;
110
+ }
111
+ else if (snippet) {
112
+ snippetLines.push(line);
113
+ }
114
+ else if (match.passage) {
115
+ const name = match.passage[1];
116
+ const header = { type: "passage-header", name, range };
117
+ passage = {
118
+ type: "passage",
119
+ name,
120
+ nameRange: nameRange(i, name, "["),
121
+ header,
122
+ range,
123
+ body: [],
124
+ links: [],
125
+ embeds: [],
126
+ };
127
+ section.passages.push(passage);
128
+ textStarted = false;
129
+ }
130
+ else if (match.continue) {
131
+ const node = newSection("continue", range);
132
+ node.header = { type: "continue", text: match.continue[1], range };
133
+ startSection(node);
134
+ haveSection = true;
135
+ textStarted = false;
136
+ }
137
+ else if (stripLine == "---") {
138
+ inUiBlock = false;
139
+ if (!haveSection) {
140
+ add({ type: "separator", range });
141
+ }
142
+ else {
143
+ const node = newSection("separator", range);
144
+ node.header = { type: "separator", range };
145
+ startSection(node);
146
+ textStarted = false;
147
+ }
148
+ }
149
+ else if (stripLine == "@ui") {
150
+ inUiBlock = true;
151
+ add({ type: "ui-block-start", range });
152
+ }
153
+ else if (match.ui) {
154
+ add({ type: "ui-file", path: match.ui[1], pathRange: valueRange(i, match.ui[1]), range });
155
+ }
156
+ else if (stripLine == "@clear") {
157
+ if (!passage)
158
+ haveSection = true;
159
+ add({ type: "clear", range });
160
+ }
161
+ else if (match.title) {
162
+ add({ type: "title", value: match.title[1], valueRange: valueRange(i, match.title[1]), range });
163
+ }
164
+ else if (match.start) {
165
+ add({ type: "start", value: match.start[1], valueRange: valueRange(i, match.start[1]), range });
166
+ }
167
+ else if (match.import) {
168
+ add({ type: "import", pattern: match.import[1], patternRange: valueRange(i, match.import[1]), range });
169
+ }
170
+ else if (match.attributes || match.unset) {
171
+ haveSection = true;
172
+ const value = (match.attributes ?? match.unset)[1];
173
+ add({ type: "attribute", directive: match.attributes ? "set" : "unset", value, valueRange: valueRange(i, value), range });
174
+ }
175
+ else if (match.inc || match.dec) {
176
+ haveSection = true;
177
+ const m = (match.inc ?? match.dec);
178
+ const value = m[1];
179
+ const start = line.indexOf(value, line.indexOf("@") + 4);
180
+ add({
181
+ type: "attribute",
182
+ directive: match.inc ? "inc" : "dec",
183
+ value,
184
+ valueRange: { start: { line: i, character: start }, end: { line: i, character: start + value.length } },
185
+ ...(m[2] === undefined ? {} : { amount: m[2] }),
186
+ range,
187
+ });
188
+ }
189
+ else if (!textStarted && match.js) {
190
+ if (!inUiBlock && !passage)
191
+ haveSection = true;
192
+ const indent = match.js[1].length;
193
+ add({
194
+ type: "js",
195
+ code: match.js[2],
196
+ codeRange: { start: { line: i, character: indent }, end: { line: i, character: line.length } },
197
+ range,
198
+ });
199
+ }
200
+ else if (textStarted || stripLine.length > 0) {
201
+ add({ type: "text", text: line, range });
202
+ if (!inUiBlock) {
203
+ if (!passage)
204
+ haveSection = true;
205
+ textStarted = true;
206
+ }
207
+ }
208
+ // Otherwise it's a blank line before any text, which is ignored.
209
+ const end = range.end;
210
+ if (snippet)
211
+ snippet.range = { start: snippet.range.start, end };
212
+ else {
213
+ section.range = { start: section.range.start, end };
214
+ if (passage)
215
+ passage.range = { start: passage.range.start, end };
216
+ }
217
+ }
218
+ endSnippet();
219
+ for (const s of [preamble, ...file.sections]) {
220
+ if (s.type != "section")
221
+ continue;
222
+ findLinks(s, lines);
223
+ for (const p of s.passages) {
224
+ findLinks(p, lines);
225
+ }
226
+ }
227
+ return file;
228
+ }
229
+ function newSection(kind, range) {
230
+ return { type: "section", kind, range, body: [], passages: [], links: [], embeds: [] };
231
+ }
232
+ // Fills in the links and embeds in a section's or passage's text.
233
+ function findLinks(node, lines) {
234
+ const textNodes = node.body.filter((n) => n.type == "text");
235
+ if (!textNodes.length)
236
+ return;
237
+ const sources = textNodes.map(n => ({ line: n.range.start.line, length: lines[n.range.start.line].length }));
238
+ const toPosition = positionMapper(sources, textNodes.map(n => n.text.length));
239
+ const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
240
+ // Which section comes next isn't known until the story is analysed, but all that matters
241
+ // here is that there is one, so that [[text>]] is treated as a link.
242
+ const result = transformText(textNodes.map(n => n.text).join("\n"), () => "");
243
+ node.links = result.links.map((link) => ({
244
+ type: "link",
245
+ linkType: link.linkType,
246
+ form: link.form,
247
+ ...(link.text === undefined ? {} : { text: link.text }),
248
+ target: link.target,
249
+ range: toRange(link.start, link.end),
250
+ targetRange: toRange(link.targetStart, link.targetEnd),
251
+ }));
252
+ node.embeds = result.embeds.map((embed) => ({
253
+ type: "embed",
254
+ name: embed.name,
255
+ range: toRange(embed.start, embed.end),
256
+ nameRange: toRange(embed.nameStart, embed.nameEnd),
257
+ }));
258
+ }
@@ -0,0 +1,146 @@
1
+ export interface Position {
2
+ line: number;
3
+ character: number;
4
+ }
5
+ export interface Range {
6
+ start: Position;
7
+ end: Position;
8
+ }
9
+ export type DiagnosticSeverity = "error" | "warning";
10
+ export type DiagnosticCode = "squiffy/passage-without-section" | "squiffy/text-in-ui-block" | "squiffy/content-before-section" | "squiffy/v5-syntax" | "squiffy/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link";
11
+ export interface Diagnostic {
12
+ file: string;
13
+ range: Range;
14
+ severity: DiagnosticSeverity;
15
+ code: DiagnosticCode;
16
+ message: string;
17
+ }
18
+ export declare function formatDiagnostic(diagnostic: Diagnostic): string;
19
+ export interface SquiffyFile {
20
+ type: "file";
21
+ filename: string | undefined;
22
+ range: Range;
23
+ preamble: SectionNode;
24
+ sections: (SectionNode | SnippetNode)[];
25
+ }
26
+ export interface SectionNode {
27
+ type: "section";
28
+ kind: "named" | "continue" | "separator" | "preamble";
29
+ name?: string;
30
+ nameRange?: Range;
31
+ header?: SectionHeaderNode | ContinueNode | SeparatorNode;
32
+ range: Range;
33
+ body: StatementNode[];
34
+ passages: PassageNode[];
35
+ links: LinkNode[];
36
+ embeds: EmbedNode[];
37
+ }
38
+ export interface PassageNode {
39
+ type: "passage";
40
+ name: string;
41
+ nameRange: Range;
42
+ header: PassageHeaderNode;
43
+ range: Range;
44
+ body: StatementNode[];
45
+ links: LinkNode[];
46
+ embeds: EmbedNode[];
47
+ }
48
+ export interface SnippetNode {
49
+ type: "snippet";
50
+ name: string;
51
+ nameRange: Range;
52
+ header: SnippetHeaderNode;
53
+ range: Range;
54
+ content: string;
55
+ }
56
+ export interface SectionHeaderNode {
57
+ type: "section-header";
58
+ name: string;
59
+ range: Range;
60
+ }
61
+ export interface PassageHeaderNode {
62
+ type: "passage-header";
63
+ name: string;
64
+ range: Range;
65
+ }
66
+ export interface SnippetHeaderNode {
67
+ type: "snippet-header";
68
+ name: string;
69
+ range: Range;
70
+ }
71
+ export interface ContinueNode {
72
+ type: "continue";
73
+ text: string;
74
+ range: Range;
75
+ }
76
+ export interface SeparatorNode {
77
+ type: "separator";
78
+ range: Range;
79
+ }
80
+ export interface TitleNode {
81
+ type: "title";
82
+ value: string;
83
+ valueRange: Range;
84
+ range: Range;
85
+ }
86
+ export interface StartNode {
87
+ type: "start";
88
+ value: string;
89
+ valueRange: Range;
90
+ range: Range;
91
+ }
92
+ export interface ImportNode {
93
+ type: "import";
94
+ pattern: string;
95
+ patternRange: Range;
96
+ range: Range;
97
+ }
98
+ export interface UiBlockStartNode {
99
+ type: "ui-block-start";
100
+ range: Range;
101
+ }
102
+ export interface UiFileNode {
103
+ type: "ui-file";
104
+ path: string;
105
+ pathRange: Range;
106
+ range: Range;
107
+ }
108
+ export interface ClearNode {
109
+ type: "clear";
110
+ range: Range;
111
+ }
112
+ export interface AttributeNode {
113
+ type: "attribute";
114
+ directive: "set" | "unset" | "inc" | "dec";
115
+ value: string;
116
+ valueRange: Range;
117
+ amount?: string;
118
+ range: Range;
119
+ }
120
+ export interface JsNode {
121
+ type: "js";
122
+ code: string;
123
+ codeRange: Range;
124
+ range: Range;
125
+ }
126
+ export interface TextNode {
127
+ type: "text";
128
+ text: string;
129
+ range: Range;
130
+ }
131
+ export type StatementNode = SeparatorNode | TitleNode | StartNode | ImportNode | UiBlockStartNode | UiFileNode | ClearNode | AttributeNode | JsNode | TextNode;
132
+ export interface LinkNode {
133
+ type: "link";
134
+ linkType: "section" | "passage";
135
+ form: "named" | "unnamed" | "next";
136
+ text?: string;
137
+ target: string;
138
+ range: Range;
139
+ targetRange: Range;
140
+ }
141
+ export interface EmbedNode {
142
+ type: "embed";
143
+ name: string;
144
+ range: Range;
145
+ nameRange: Range;
146
+ }
package/dist/syntax.js ADDED
@@ -0,0 +1,8 @@
1
+ // The syntax tree that parse() returns for one .squiffy file, and the structured diagnostics
2
+ // that analyse() reports. Positions are zero-based and ranges are end-exclusive, following
3
+ // LSP conventions, so a language server can pass them straight through.
4
+ // Formats a diagnostic the way the compiler has always reported problems, e.g.
5
+ // `WARNING: story.squiffy line 12: In section 'start' there is a link to ...`
6
+ export function formatDiagnostic(diagnostic) {
7
+ return `${diagnostic.severity.toUpperCase()}: ${diagnostic.file} line ${diagnostic.range.start.line + 1}: ${diagnostic.message}`;
8
+ }
package/dist/text.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ export interface TextLink {
2
+ linkType: "section" | "passage";
3
+ form: "named" | "unnamed" | "next";
4
+ text?: string;
5
+ target: string;
6
+ start: number;
7
+ end: number;
8
+ targetStart: number;
9
+ targetEnd: number;
10
+ }
11
+ export interface TextEmbed {
12
+ name: string;
13
+ start: number;
14
+ end: number;
15
+ nameStart: number;
16
+ nameEnd: number;
17
+ }
18
+ export interface NoNextSection {
19
+ display: string;
20
+ start: number;
21
+ end: number;
22
+ }
23
+ export interface TransformResult {
24
+ output: string;
25
+ links: TextLink[];
26
+ embeds: TextEmbed[];
27
+ noNextSection: NoNextSection[];
28
+ }
29
+ export declare function transformText(text: string, getNextSectionName: () => string | null): TransformResult;
30
+ export interface SourceLine {
31
+ line: number;
32
+ length: number;
33
+ }
34
+ export declare function positionMapper(lines: SourceLine[], lineLengths: number[]): (offset: number) => {
35
+ line: number;
36
+ character: number;
37
+ };