squiffy-compiler 7.0.0-beta.3 → 7.0.0-beta.4
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/analyse.d.ts +2 -0
- package/dist/analyse.js +30 -1
- package/dist/compiler.d.ts +3 -0
- package/dist/compiler.js +1 -0
- package/dist/emit.d.ts +1 -0
- package/dist/emit.js +6 -0
- package/dist/language-service.js +19 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +3 -2
- package/dist/parse.js +4 -2
- package/dist/setters.d.ts +34 -0
- package/dist/setters.js +371 -0
- package/dist/syntax.d.ts +1 -1
- package/dist/text.d.ts +2 -1
- package/dist/text.js +34 -15
- package/package.json +2 -2
- package/syntaxes/squiffy.tmLanguage.json +33 -7
package/dist/analyse.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface AnalyseSettings {
|
|
|
12
12
|
parseCache?: ParseCache;
|
|
13
13
|
recover?: boolean;
|
|
14
14
|
storyFiles?: string[];
|
|
15
|
+
expressions?: boolean;
|
|
15
16
|
}
|
|
16
17
|
export declare class ParseCache {
|
|
17
18
|
private files;
|
|
@@ -33,6 +34,7 @@ export interface Analysis {
|
|
|
33
34
|
diagnostics: Diagnostic[];
|
|
34
35
|
sections: SectionSymbol[];
|
|
35
36
|
start: string;
|
|
37
|
+
expressions: boolean;
|
|
36
38
|
}
|
|
37
39
|
export declare class Story {
|
|
38
40
|
sections: Record<string, Section>;
|
package/dist/analyse.js
CHANGED
|
@@ -2,6 +2,7 @@ import { parse } from "./parse.js";
|
|
|
2
2
|
import { positionMapper, transformText } from "./text.js";
|
|
3
3
|
import { PROJECT_FILENAME } from "./project.js";
|
|
4
4
|
import { findV5Syntax } from "./v5.js";
|
|
5
|
+
import { attributesSetIn, findAtAttributes, findUnquotedText } from "./setters.js";
|
|
5
6
|
// Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
|
|
6
7
|
export class ParseCache {
|
|
7
8
|
constructor() {
|
|
@@ -191,6 +192,8 @@ export async function analyse(settings) {
|
|
|
191
192
|
const story = new Story();
|
|
192
193
|
const cache = settings.parseCache ?? new ParseCache();
|
|
193
194
|
const files = [];
|
|
195
|
+
// Each file's text, in the same order as files
|
|
196
|
+
const texts = [];
|
|
194
197
|
const diagnostics = [];
|
|
195
198
|
const legacyErrors = [];
|
|
196
199
|
const legacyWarnings = [];
|
|
@@ -212,9 +215,16 @@ export async function analyse(settings) {
|
|
|
212
215
|
async function processFile(text, filename, isFirst) {
|
|
213
216
|
const file = cache.parse(text, filename);
|
|
214
217
|
files.push(file);
|
|
218
|
+
texts.push(text);
|
|
215
219
|
processed.add(filename);
|
|
216
220
|
const lines = text.replace(/\r/g, "").split("\n");
|
|
217
221
|
warnAboutV5Syntax(text, lines, filename);
|
|
222
|
+
if (settings.expressions) {
|
|
223
|
+
for (const found of findAtAttributes(file)) {
|
|
224
|
+
const name = found.text.substring(1);
|
|
225
|
+
report("warning", "squiffy/at-attribute", filename, found.range, `Use ${name} rather than ${found.text}: in a story with a ${PROJECT_FILENAME}, setters name attributes without "@".`, found.range.start.line + 1);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
218
228
|
let section = null;
|
|
219
229
|
let passage = null;
|
|
220
230
|
let inUiBlock = false;
|
|
@@ -460,6 +470,21 @@ export async function analyse(settings) {
|
|
|
460
470
|
function warnExternalFilesUnavailable(line, node, filename) {
|
|
461
471
|
report("warning", "squiffy/external-files-unavailable", filename, lineRange(node), `"${line}" was ignored, as other files can't be loaded here`, node.range.start.line + 1);
|
|
462
472
|
}
|
|
473
|
+
// Text in a setter without quotes, as in Squiffy 6 (#373). A single word is only reported if no
|
|
474
|
+
// attribute of that name is set anywhere in the story, so this needs every file first.
|
|
475
|
+
function warnAboutUnquotedText() {
|
|
476
|
+
const set = new Set();
|
|
477
|
+
files.forEach((file, i) => attributesSetIn(file, texts[i]).forEach(name => set.add(name)));
|
|
478
|
+
for (const file of files) {
|
|
479
|
+
for (const found of findUnquotedText(file, name => set.has(name))) {
|
|
480
|
+
const quoted = found.quoted ?? `"${found.text}"`;
|
|
481
|
+
const message = found.kind == "word"
|
|
482
|
+
? `${found.text} isn't set anywhere in the story, so this sets ${found.attribute} to nothing. For the text ${quoted}, use quotes.`
|
|
483
|
+
: `Text in a setter goes in quotes in a story with a ${PROJECT_FILENAME}: ${quoted}.`;
|
|
484
|
+
report("warning", "squiffy/unquoted-text", file.filename, found.range, message, found.range.start.line + 1);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
463
488
|
function warnAboutV5Syntax(text, lines, filename) {
|
|
464
489
|
const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
|
|
465
490
|
for (const match of findV5Syntax(text)) {
|
|
@@ -487,7 +512,7 @@ export async function analyse(settings) {
|
|
|
487
512
|
const getNextSectionName = () => {
|
|
488
513
|
return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
|
|
489
514
|
};
|
|
490
|
-
const result = transformText(container.text.join("\n"), getNextSectionName);
|
|
515
|
+
const result = transformText(container.text.join("\n"), getNextSectionName, settings.expressions);
|
|
491
516
|
container.output = result.output;
|
|
492
517
|
const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
|
|
493
518
|
const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
|
|
@@ -553,6 +578,9 @@ export async function analyse(settings) {
|
|
|
553
578
|
}
|
|
554
579
|
}
|
|
555
580
|
}
|
|
581
|
+
if (settings.expressions) {
|
|
582
|
+
warnAboutUnquotedText();
|
|
583
|
+
}
|
|
556
584
|
const analysis = {
|
|
557
585
|
success,
|
|
558
586
|
files,
|
|
@@ -564,6 +592,7 @@ export async function analyse(settings) {
|
|
|
564
592
|
passages: Object.values(section.passages).map(passage => ({ name: passage.name, node: passage.node })),
|
|
565
593
|
})),
|
|
566
594
|
start: story.start,
|
|
595
|
+
expressions: settings.expressions ?? false,
|
|
567
596
|
};
|
|
568
597
|
analysisInternals.set(analysis, { story, legacyErrors, legacyWarnings });
|
|
569
598
|
return analysis;
|
package/dist/compiler.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { CompileSuccess } from "./emit.js";
|
|
|
3
3
|
import type { Diagnostic } from "./syntax.js";
|
|
4
4
|
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
5
5
|
export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
|
|
6
|
+
export { convertSetters, convertSetter } from "./setters.js";
|
|
7
|
+
export type { SetterConversion, SetterConversionProblem, SetterConversionResult } from "./setters.js";
|
|
6
8
|
export { parse } from "./parse.js";
|
|
7
9
|
export { analyse, ParseCache } from "./analyse.js";
|
|
8
10
|
export type { AnalyseSettings, Analysis, ExternalFiles, SectionSymbol, PassageSymbol } from "./analyse.js";
|
|
@@ -20,6 +22,7 @@ interface CompilerSettings {
|
|
|
20
22
|
globalJs?: boolean;
|
|
21
23
|
parseCache?: ParseCache;
|
|
22
24
|
storyFiles?: string[];
|
|
25
|
+
expressions?: boolean;
|
|
23
26
|
}
|
|
24
27
|
export interface CompileError {
|
|
25
28
|
success: false;
|
package/dist/compiler.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { analyse, analysisInternals } from "./analyse.js";
|
|
2
2
|
import { emit } from "./emit.js";
|
|
3
3
|
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
4
|
+
export { convertSetters, convertSetter } from "./setters.js";
|
|
4
5
|
// The compiler works in three stages, which can also be used separately:
|
|
5
6
|
// - parse() turns one file into a syntax tree, with a source range on every node
|
|
6
7
|
// - analyse() resolves @imports, builds the story's sections and passages, and reports diagnostics
|
package/dist/emit.d.ts
CHANGED
package/dist/emit.js
CHANGED
|
@@ -19,6 +19,9 @@ export function emit(analysis, settings = {}) {
|
|
|
19
19
|
},
|
|
20
20
|
js: [],
|
|
21
21
|
};
|
|
22
|
+
if (analysis.expressions) {
|
|
23
|
+
output.story.expressions = true;
|
|
24
|
+
}
|
|
22
25
|
if (story.uiJs.length > 0) {
|
|
23
26
|
output.js.push(story.uiJs);
|
|
24
27
|
output.story.uiJsIndex = output.js.length - 1;
|
|
@@ -83,6 +86,9 @@ export function emit(analysis, settings = {}) {
|
|
|
83
86
|
if (output.story.uiJsIndex !== undefined) {
|
|
84
87
|
outputJs.push(`story.uiJsIndex = ${output.story.uiJsIndex};`);
|
|
85
88
|
}
|
|
89
|
+
if (output.story.expressions) {
|
|
90
|
+
outputJs.push("story.expressions = true;");
|
|
91
|
+
}
|
|
86
92
|
outputJs.push(`story.start = ${JSON.stringify(output.story.start, null, 4)};`);
|
|
87
93
|
outputJs.push(`story.sections = ${JSON.stringify(output.story.sections, null, 4)};`);
|
|
88
94
|
outputJs.push("story.js = [");
|
package/dist/language-service.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { analyse, ParseCache } from "./analyse.js";
|
|
9
9
|
import { getStoryFiles, globToRegExp, parseProjectFile, PROJECT_FILENAME } from "./project.js";
|
|
10
10
|
import { convertV5Syntax, findV5Syntax } from "./v5.js";
|
|
11
|
+
import { findUnquotedText } from "./setters.js";
|
|
11
12
|
import { buildStoryGraph, storyGraphDiagnostics } from "./story-graph.js";
|
|
12
13
|
const sameSymbol = (a, b) => a.kind == b.kind && a.name == b.name && (a.kind == "section" || a.section == b.section);
|
|
13
14
|
// Directives, for completion, with what they do
|
|
@@ -175,6 +176,7 @@ export function createLanguageService(options) {
|
|
|
175
176
|
parseCache,
|
|
176
177
|
recover: true,
|
|
177
178
|
...(project.projectFile ? { storyFiles: project.storyFiles } : {}),
|
|
179
|
+
expressions: !!project.projectFile,
|
|
178
180
|
});
|
|
179
181
|
if (project.error) {
|
|
180
182
|
// A broken squiffy.json stops the story building, as it does in the CLI
|
|
@@ -676,6 +678,23 @@ export function createLanguageService(options) {
|
|
|
676
678
|
actions.push({ title: `Create passage '${name}'`, kind: "quickfix", diagnostic, edit });
|
|
677
679
|
}
|
|
678
680
|
}
|
|
681
|
+
else if (diagnostic.code == "squiffy/at-attribute") {
|
|
682
|
+
const start = diagnostic.range.start;
|
|
683
|
+
const name = lines[start.line].substring(start.character + 1, diagnostic.range.end.character);
|
|
684
|
+
const edit = {};
|
|
685
|
+
addEdit(edit, path, { range: { start, end: { line: start.line, character: start.character + 1 } }, newText: "" });
|
|
686
|
+
actions.push({ title: `Change to ${name}`, kind: "quickfix", diagnostic, edit });
|
|
687
|
+
}
|
|
688
|
+
else if (diagnostic.code == "squiffy/unquoted-text") {
|
|
689
|
+
const start = diagnostic.range.start;
|
|
690
|
+
const found = findUnquotedText(tree.file, () => false)
|
|
691
|
+
.find(u => comparePositions(u.range.start, start) == 0);
|
|
692
|
+
if (!found?.quoted)
|
|
693
|
+
continue;
|
|
694
|
+
const edit = {};
|
|
695
|
+
addEdit(edit, path, { range: found.range, newText: found.quoted });
|
|
696
|
+
actions.push({ title: `Change to ${found.quoted}`, kind: "quickfix", diagnostic, edit });
|
|
697
|
+
}
|
|
679
698
|
else if (diagnostic.code == "squiffy/v5-syntax") {
|
|
680
699
|
const line = diagnostic.range.start.line;
|
|
681
700
|
const match = v5.find(m => m.line == line + 1
|
package/dist/node.d.ts
CHANGED
package/dist/node.js
CHANGED
|
@@ -70,7 +70,7 @@ async function folderStory(projectFile, options) {
|
|
|
70
70
|
throw new StoryNotFoundError(`The main file in ${projectFile}, ${project.main}, doesn't exist.`);
|
|
71
71
|
}
|
|
72
72
|
const story = getStoryFiles(project, await listSquiffyFiles(root), options.skip);
|
|
73
|
-
return { mainFile, storyFiles: story.included.map(file => path.resolve(root, file)), projectFile };
|
|
73
|
+
return { mainFile, storyFiles: story.included.map(file => path.resolve(root, file)), projectFile, expressions: true };
|
|
74
74
|
}
|
|
75
75
|
// Finds the story to build from what the author gave: a folder with a squiffy.json in it, or a
|
|
76
76
|
// .squiffy file.
|
|
@@ -93,7 +93,7 @@ export async function findStory(input, options = {}) {
|
|
|
93
93
|
}
|
|
94
94
|
const projectFile = await findProjectFile(path.dirname(resolved));
|
|
95
95
|
if (!projectFile)
|
|
96
|
-
return { mainFile: resolved };
|
|
96
|
+
return { mainFile: resolved, expressions: false };
|
|
97
97
|
const project = await readProject(projectFile);
|
|
98
98
|
if (path.resolve(path.dirname(projectFile), project.main) == resolved) {
|
|
99
99
|
return folderStory(projectFile, options);
|
|
@@ -101,5 +101,6 @@ export async function findStory(input, options = {}) {
|
|
|
101
101
|
return {
|
|
102
102
|
mainFile: resolved,
|
|
103
103
|
note: `Building just ${path.basename(resolved)}, as it isn't the main file in ${projectFile} (${project.main}).`,
|
|
104
|
+
expressions: true,
|
|
104
105
|
};
|
|
105
106
|
}
|
package/dist/parse.js
CHANGED
|
@@ -243,8 +243,10 @@ function findLinks(node, lines) {
|
|
|
243
243
|
const toPosition = positionMapper(sources, textNodes.map(n => n.text.length));
|
|
244
244
|
const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
|
|
245
245
|
// Which section comes next isn't known until the story is analysed, but all that matters
|
|
246
|
-
// here is that there is one, so that [[text>]] is treated as a link.
|
|
247
|
-
|
|
246
|
+
// here is that there is one, so that [[text>]] is treated as a link. Link
|
|
247
|
+
// targets can have brackets in them, as in a story with a squiffy.json, whether or not this
|
|
248
|
+
// one has one: the two only differ when a target has a "(" in it.
|
|
249
|
+
const result = transformText(textNodes.map(n => n.text).join("\n"), () => "", true);
|
|
248
250
|
node.links = result.links.map((link) => ({
|
|
249
251
|
type: "link",
|
|
250
252
|
linkType: link.linkType,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Range, SquiffyFile } from "./syntax.js";
|
|
2
|
+
export interface SetterConversionProblem {
|
|
3
|
+
line: number;
|
|
4
|
+
text: string;
|
|
5
|
+
reason: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SetterConversionResult {
|
|
8
|
+
script: string;
|
|
9
|
+
converted: number;
|
|
10
|
+
problems: SetterConversionProblem[];
|
|
11
|
+
}
|
|
12
|
+
export type SetterConversion = {
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
problem: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function convertSetter(setter: string, options?: {
|
|
18
|
+
preferSingleQuotes?: boolean;
|
|
19
|
+
}): SetterConversion;
|
|
20
|
+
export declare function convertSetters(script: string): SetterConversionResult;
|
|
21
|
+
export interface AtAttribute {
|
|
22
|
+
text: string;
|
|
23
|
+
range: Range;
|
|
24
|
+
}
|
|
25
|
+
export declare function findAtAttributes(file: SquiffyFile): AtAttribute[];
|
|
26
|
+
export declare function attributesSetIn(file: SquiffyFile, text: string): Set<string>;
|
|
27
|
+
export interface UnquotedText {
|
|
28
|
+
text: string;
|
|
29
|
+
range: Range;
|
|
30
|
+
attribute: string;
|
|
31
|
+
kind: "word" | "words";
|
|
32
|
+
quoted: string | null;
|
|
33
|
+
}
|
|
34
|
+
export declare function findUnquotedText(file: SquiffyFile, isSet: (name: string) => boolean): UnquotedText[];
|
package/dist/setters.js
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
// Converts setters in @set, links and the set= option of {{section}} and {{passage}} from the
|
|
2
|
+
// Squiffy 6 rules to the rules of a story with a squiffy.json (#373), where the right-hand side is
|
|
3
|
+
// an expression, as in {{#if}}:
|
|
4
|
+
//
|
|
5
|
+
// Squiffy 6 With a squiffy.json
|
|
6
|
+
// @set weapon = sword @set weapon = "sword"
|
|
7
|
+
// @set best = @score @set best = score
|
|
8
|
+
// @set gold += @price @set gold += price
|
|
9
|
+
// [[Go]](next, name = Bob) [[Go]](next, name = "Bob")
|
|
10
|
+
//
|
|
11
|
+
// Numbers, true and false, flags (has_key, not has_key), := and @inc/@dec mean the same under
|
|
12
|
+
// both rules, so they're left alone. The Squiffy 6 rules are simple enough that the conversion is
|
|
13
|
+
// exact: a converted story runs the same as before, once it has a squiffy.json. Anything that
|
|
14
|
+
// can't be converted exactly is left as it is and listed as a problem.
|
|
15
|
+
//
|
|
16
|
+
// Conversion isn't idempotent: a converted @set best = score would be converted again to
|
|
17
|
+
// @set best = "score". So it's only for stories without a squiffy.json.
|
|
18
|
+
import { parse } from "./parse.js";
|
|
19
|
+
// The same as the tokenizer's numbers and literals in runtime/src/infixExpressions.ts
|
|
20
|
+
const numberPattern = /^-?\d+(\.\d+)?$/;
|
|
21
|
+
const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
|
|
22
|
+
// Text as a quoted string. preferSingle is for setters inside a set="..." string, where double
|
|
23
|
+
// quotes would need escaping.
|
|
24
|
+
function quote(text, preferSingle) {
|
|
25
|
+
// The tokenizer treats a backslash as escaping the next character, but Handlebars doesn't
|
|
26
|
+
// remove it (except before a quote), so text with a backslash can't be written exactly
|
|
27
|
+
if (text.includes("\\"))
|
|
28
|
+
return null;
|
|
29
|
+
const hasDouble = text.includes("\"");
|
|
30
|
+
const hasSingle = text.includes("'");
|
|
31
|
+
if (hasDouble && hasSingle)
|
|
32
|
+
return null;
|
|
33
|
+
if (hasDouble || (preferSingle && !hasSingle))
|
|
34
|
+
return `'${text}'`;
|
|
35
|
+
return `"${text}"`;
|
|
36
|
+
}
|
|
37
|
+
// @name as an attribute in an expression: the name, or [name] if it isn't a plain word
|
|
38
|
+
function attribute(name) {
|
|
39
|
+
if (/^\w+$/.test(name) && !reservedWords.has(name) && !/^\d/.test(name))
|
|
40
|
+
return name;
|
|
41
|
+
if (name === "" || name.includes("]"))
|
|
42
|
+
return null;
|
|
43
|
+
return `[${name}]`;
|
|
44
|
+
}
|
|
45
|
+
// The value side of name = value under the Squiffy 6 rules, as an expression
|
|
46
|
+
function convertValue(rhs, preferSingle) {
|
|
47
|
+
if (rhs === "true" || rhs === "false")
|
|
48
|
+
return { text: rhs };
|
|
49
|
+
if (rhs === "")
|
|
50
|
+
return { problem: "it has no value" };
|
|
51
|
+
// Squiffy 6 treated anything that isn't NaN as a number, using parseFloat
|
|
52
|
+
if (!isNaN(rhs)) {
|
|
53
|
+
if (numberPattern.test(rhs))
|
|
54
|
+
return { text: rhs };
|
|
55
|
+
const n = String(parseFloat(rhs));
|
|
56
|
+
return numberPattern.test(n) ? { text: n } : { problem: `"${rhs}" isn't a plain number` };
|
|
57
|
+
}
|
|
58
|
+
if (rhs.startsWith("@")) {
|
|
59
|
+
const name = attribute(rhs.substring(1));
|
|
60
|
+
return name === null ? { problem: `"${rhs}" isn't an attribute name that can be converted` } : { text: name };
|
|
61
|
+
}
|
|
62
|
+
const quoted = quote(rhs, preferSingle);
|
|
63
|
+
return quoted === null ? { problem: "the text has both kinds of quote, or a backslash, in it" } : { text: quoted };
|
|
64
|
+
}
|
|
65
|
+
// Converts one setter, e.g. "weapon = sword" to "weapon = \"sword\"". Whitespace around it is
|
|
66
|
+
// kept. The result is the same text if nothing needs changing.
|
|
67
|
+
export function convertSetter(setter, options = {}) {
|
|
68
|
+
const preferSingle = options.preferSingleQuotes ?? false;
|
|
69
|
+
const leading = setter.match(/^\s*/)[0];
|
|
70
|
+
const trailing = setter.substring(leading.length).match(/\s*$/)[0];
|
|
71
|
+
const expr = setter.substring(leading.length, setter.length - trailing.length);
|
|
72
|
+
const wrap = (c) => "text" in c ? { text: leading + c.text + trailing } : c;
|
|
73
|
+
// name := text processes {{...}} in the text, under both rules
|
|
74
|
+
if (/^\w*\s*:=/.test(expr))
|
|
75
|
+
return { text: setter };
|
|
76
|
+
const set = /^(\w*)(\s*=\s*)(.*)$/.exec(expr);
|
|
77
|
+
if (set) {
|
|
78
|
+
const [, lhs, eq, rhs] = set;
|
|
79
|
+
if (!lhs)
|
|
80
|
+
return { problem: "it has no attribute name before \"=\"" };
|
|
81
|
+
const value = convertValue(rhs, preferSingle);
|
|
82
|
+
return "text" in value ? wrap({ text: lhs + eq + value.text }) : value;
|
|
83
|
+
}
|
|
84
|
+
const operate = /^(\w*)(\s*[+\-*/]=\s*)(.*)$/.exec(expr);
|
|
85
|
+
if (operate) {
|
|
86
|
+
const [, lhs, op, rhs] = operate;
|
|
87
|
+
if (!lhs)
|
|
88
|
+
return { problem: "it has no attribute name" };
|
|
89
|
+
const value = convertValue(rhs.trim(), preferSingle);
|
|
90
|
+
return "text" in value ? wrap({ text: lhs + op + value.text }) : value;
|
|
91
|
+
}
|
|
92
|
+
// A flag: name or not name
|
|
93
|
+
if (/^(not )?\w+$/.test(expr))
|
|
94
|
+
return { text: setter };
|
|
95
|
+
return { problem: "it isn't an attribute name, or name = value" };
|
|
96
|
+
}
|
|
97
|
+
// Converts a comma-separated list of setters. Squiffy 6 split them on every comma.
|
|
98
|
+
function convertSetterList(setters, preferSingle) {
|
|
99
|
+
let converted = 0;
|
|
100
|
+
const problems = [];
|
|
101
|
+
const parts = setters.split(",").map(part => {
|
|
102
|
+
if (part.trim() === "")
|
|
103
|
+
return part;
|
|
104
|
+
const result = convertSetter(part, { preferSingleQuotes: preferSingle });
|
|
105
|
+
if ("problem" in result) {
|
|
106
|
+
problems.push(`${part.trim()}: ${result.problem}`);
|
|
107
|
+
return part;
|
|
108
|
+
}
|
|
109
|
+
if (result.text !== part)
|
|
110
|
+
converted++;
|
|
111
|
+
return result.text;
|
|
112
|
+
});
|
|
113
|
+
return { text: parts.join(","), converted, problems };
|
|
114
|
+
}
|
|
115
|
+
// {{section ...}} or {{passage ...}} with a set="..." or set='...' string
|
|
116
|
+
const helperSetPattern = /\{\{~?\s*(?:section|passage)\b[^}]*?\bset=("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')/g;
|
|
117
|
+
// Converts every setter in a .squiffy file. See the top of this file.
|
|
118
|
+
export function convertSetters(script) {
|
|
119
|
+
const crlf = script.includes("\r\n");
|
|
120
|
+
const lines = script.replace(/\r/g, "").split("\n");
|
|
121
|
+
const file = parse(script);
|
|
122
|
+
const edits = [];
|
|
123
|
+
const problems = [];
|
|
124
|
+
let converted = 0;
|
|
125
|
+
const problem = (line, text, reason) => {
|
|
126
|
+
problems.push({ line: line + 1, text, reason });
|
|
127
|
+
};
|
|
128
|
+
const replace = (range, text) => {
|
|
129
|
+
edits.push({ line: range.start.line, start: range.start.character, end: range.end.character, text });
|
|
130
|
+
};
|
|
131
|
+
const containers = [];
|
|
132
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
133
|
+
if (section.type !== "section")
|
|
134
|
+
continue;
|
|
135
|
+
containers.push(section, ...section.passages);
|
|
136
|
+
}
|
|
137
|
+
for (const container of containers) {
|
|
138
|
+
for (const node of container.body) {
|
|
139
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
140
|
+
const result = convertSetter(node.value);
|
|
141
|
+
if ("problem" in result) {
|
|
142
|
+
problem(node.range.start.line, node.value, result.problem);
|
|
143
|
+
}
|
|
144
|
+
else if (result.text !== node.value) {
|
|
145
|
+
replace(node.valueRange, result.text);
|
|
146
|
+
converted++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (node.type === "text") {
|
|
150
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
151
|
+
const literal = match[1];
|
|
152
|
+
const delimiter = literal[0];
|
|
153
|
+
// The setters as Handlebars reads the string
|
|
154
|
+
const inner = literal.slice(1, -1).replace(new RegExp(`\\\\${delimiter}`, "g"), delimiter);
|
|
155
|
+
const result = convertSetterList(inner, delimiter === "\"");
|
|
156
|
+
for (const reason of result.problems)
|
|
157
|
+
problem(node.range.start.line, literal, reason);
|
|
158
|
+
if (result.converted) {
|
|
159
|
+
const escaped = result.text.replace(new RegExp(delimiter, "g"), `\\${delimiter}`);
|
|
160
|
+
const start = node.range.start.character + match.index + match[0].length - literal.length;
|
|
161
|
+
replace({ start: { line: node.range.start.line, character: start }, end: { line: node.range.start.line, character: start + literal.length } }, delimiter + escaped + delimiter);
|
|
162
|
+
converted += result.converted;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const link of container.links) {
|
|
168
|
+
converted += convertLink(link, lines, replace, problem);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Apply the edits from the end of each line, so earlier positions stay right
|
|
172
|
+
edits.sort((a, b) => b.line - a.line || b.start - a.start);
|
|
173
|
+
for (const edit of edits) {
|
|
174
|
+
const line = lines[edit.line];
|
|
175
|
+
lines[edit.line] = line.substring(0, edit.start) + edit.text + line.substring(edit.end);
|
|
176
|
+
}
|
|
177
|
+
const output = lines.join("\n");
|
|
178
|
+
return { script: crlf ? output.replace(/\n/g, "\r\n") : output, converted, problems };
|
|
179
|
+
}
|
|
180
|
+
// A link's setters: everything after the first comma of its target. For a next link, the target
|
|
181
|
+
// is just the setters part, e.g. ", gold=1".
|
|
182
|
+
function convertLink(link, lines, replace, problem) {
|
|
183
|
+
if (link.form === "unnamed")
|
|
184
|
+
return 0;
|
|
185
|
+
const comma = link.target.indexOf(",");
|
|
186
|
+
if (comma === -1)
|
|
187
|
+
return 0;
|
|
188
|
+
const { targetRange } = link;
|
|
189
|
+
if (targetRange.start.line !== targetRange.end.line
|
|
190
|
+
|| lines[targetRange.start.line].substring(targetRange.start.character, targetRange.end.character) !== link.target) {
|
|
191
|
+
problem(link.range.start.line, link.target, "the link's setters couldn't be found");
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
const result = convertSetterList(link.target.substring(comma + 1), false);
|
|
195
|
+
for (const reason of result.problems)
|
|
196
|
+
problem(link.range.start.line, link.target, reason);
|
|
197
|
+
if (result.converted) {
|
|
198
|
+
replace(targetRange, link.target.substring(0, comma + 1) + result.text);
|
|
199
|
+
}
|
|
200
|
+
return result.converted;
|
|
201
|
+
}
|
|
202
|
+
// Finds "@name" on the right-hand side of setters, outside quotes. Under the Squiffy 6 rules that's
|
|
203
|
+
// how a setter used another attribute, but with expressions it's just name: Handlebars reads
|
|
204
|
+
// @name as one of its own data variables, which is never set.
|
|
205
|
+
export function findAtAttributes(file) {
|
|
206
|
+
const result = [];
|
|
207
|
+
// Scans text that starts at (line, character) in the file
|
|
208
|
+
const scan = (text, line, character) => {
|
|
209
|
+
let quote = null;
|
|
210
|
+
for (let i = 0; i < text.length; i++) {
|
|
211
|
+
const c = text[i];
|
|
212
|
+
if (quote) {
|
|
213
|
+
if (c === "\\")
|
|
214
|
+
i++;
|
|
215
|
+
else if (c === quote)
|
|
216
|
+
quote = null;
|
|
217
|
+
}
|
|
218
|
+
else if (c === "\"" || c === "'")
|
|
219
|
+
quote = c;
|
|
220
|
+
else if (c === "@" && !/\w/.test(text[i - 1] ?? "") && /\w/.test(text[i + 1] ?? "")) {
|
|
221
|
+
const name = /^@\w+/.exec(text.substring(i))[0];
|
|
222
|
+
result.push({ text: name, range: { start: { line, character: character + i }, end: { line, character: character + i + name.length } } });
|
|
223
|
+
i += name.length - 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
228
|
+
if (section.type !== "section")
|
|
229
|
+
continue;
|
|
230
|
+
for (const container of [section, ...section.passages]) {
|
|
231
|
+
for (const node of container.body) {
|
|
232
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
233
|
+
scan(node.value, node.valueRange.start.line, node.valueRange.start.character);
|
|
234
|
+
}
|
|
235
|
+
else if (node.type === "text") {
|
|
236
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
237
|
+
const start = match.index + match[0].length - match[1].length + 1;
|
|
238
|
+
scan(match[1].slice(1, -1), node.range.start.line, node.range.start.character + start);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const link of container.links) {
|
|
243
|
+
const comma = link.target.indexOf(",");
|
|
244
|
+
const { targetRange } = link;
|
|
245
|
+
if (link.form === "unnamed" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
246
|
+
continue;
|
|
247
|
+
scan(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
// Splits a list of setters with the rules of a story that uses expressions (as splitSetters in
|
|
254
|
+
// runtime/src/infixExpressions.ts does): on commas outside quotes and brackets. Keeps offsets.
|
|
255
|
+
function splitWithOffsets(setters) {
|
|
256
|
+
const result = [];
|
|
257
|
+
let depth = 0;
|
|
258
|
+
let quote = null;
|
|
259
|
+
let start = 0;
|
|
260
|
+
for (let i = 0; i <= setters.length; i++) {
|
|
261
|
+
const c = setters[i];
|
|
262
|
+
if (i < setters.length && quote) {
|
|
263
|
+
if (c === "\\")
|
|
264
|
+
i++;
|
|
265
|
+
else if (c === quote)
|
|
266
|
+
quote = null;
|
|
267
|
+
}
|
|
268
|
+
else if (c === "\"" || c === "'")
|
|
269
|
+
quote = c;
|
|
270
|
+
else if (c === "(")
|
|
271
|
+
depth++;
|
|
272
|
+
else if (c === ")")
|
|
273
|
+
depth = Math.max(0, depth - 1);
|
|
274
|
+
else if (i === setters.length || (c === "," && depth === 0)) {
|
|
275
|
+
result.push({ text: setters.slice(start, i), offset: start });
|
|
276
|
+
start = i + 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
// Every setter in a file: @set, link setters and set="..." on {{section}} and {{passage}}
|
|
282
|
+
function setterSites(file) {
|
|
283
|
+
const sites = [];
|
|
284
|
+
const addList = (text, line, character, inHelperString) => {
|
|
285
|
+
for (const part of splitWithOffsets(text)) {
|
|
286
|
+
sites.push({ text: part.text, line, character: character + part.offset, inHelperString });
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
290
|
+
if (section.type !== "section")
|
|
291
|
+
continue;
|
|
292
|
+
for (const container of [section, ...section.passages]) {
|
|
293
|
+
for (const node of container.body) {
|
|
294
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
295
|
+
sites.push({ text: node.value, line: node.valueRange.start.line, character: node.valueRange.start.character, inHelperString: false });
|
|
296
|
+
}
|
|
297
|
+
else if (node.type === "text") {
|
|
298
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
299
|
+
const start = match.index + match[0].length - match[1].length + 1;
|
|
300
|
+
addList(match[1].slice(1, -1), node.range.start.line, node.range.start.character + start, match[1][0] === "\"");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
for (const link of container.links) {
|
|
305
|
+
const comma = link.target.indexOf(",");
|
|
306
|
+
const { targetRange } = link;
|
|
307
|
+
if (link.form === "unnamed" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
308
|
+
continue;
|
|
309
|
+
addList(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1, false);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return sites;
|
|
314
|
+
}
|
|
315
|
+
// The names of the attributes a file sets, or might: those on the left of a setter, those of
|
|
316
|
+
// @inc, @dec and @unset, and anything in quotes, which covers {{set "x" ...}}, data-attribute="x",
|
|
317
|
+
// id="x" on an input, squiffy.set("x", ...) in JavaScript, and so on.
|
|
318
|
+
export function attributesSetIn(file, text) {
|
|
319
|
+
const names = new Set();
|
|
320
|
+
for (const site of setterSites(file)) {
|
|
321
|
+
const name = /^\s*(?:not\s+)?(\w+)/.exec(site.text);
|
|
322
|
+
if (name)
|
|
323
|
+
names.add(name[1]);
|
|
324
|
+
}
|
|
325
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
326
|
+
if (section.type !== "section")
|
|
327
|
+
continue;
|
|
328
|
+
for (const container of [section, ...section.passages]) {
|
|
329
|
+
for (const node of container.body) {
|
|
330
|
+
if (node.type === "attribute" && node.directive !== "set")
|
|
331
|
+
names.add(node.value.trim());
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
for (const match of text.matchAll(/"([^"\\\n]*)"|'([^'\\\n]*)'/g)) {
|
|
336
|
+
names.add((match[1] ?? match[2]).trim());
|
|
337
|
+
}
|
|
338
|
+
return names;
|
|
339
|
+
}
|
|
340
|
+
// Finds setters whose value looks like text without quotes, written with the Squiffy 6 rules, in a
|
|
341
|
+
// story that uses expressions: name = sword, where sword isn't an attribute the story sets
|
|
342
|
+
// (isSet), or name = Hello there.
|
|
343
|
+
export function findUnquotedText(file, isSet) {
|
|
344
|
+
const result = [];
|
|
345
|
+
for (const site of setterSites(file)) {
|
|
346
|
+
// Only plain "=": text with +=, -=, *= or /= is an error whatever it's written as
|
|
347
|
+
const match = /^(\s*)(\w+)(\s*=(?!=)\s*)(.*?)\s*$/.exec(site.text);
|
|
348
|
+
if (!match)
|
|
349
|
+
continue;
|
|
350
|
+
const [, leading, attribute, eq, value] = match;
|
|
351
|
+
let kind = null;
|
|
352
|
+
if (/^[A-Za-z_]\w*$/.test(value)) {
|
|
353
|
+
if (!reservedWords.has(value) && !value.startsWith("_") && !isSet(value))
|
|
354
|
+
kind = "word";
|
|
355
|
+
}
|
|
356
|
+
else if (/\s/.test(value) && /^[^"'()+*%<>=!&|{}[\]@\\/]+$/.test(value) && !/(^|\s)-|\b(and|or|not)\b/.test(value)) {
|
|
357
|
+
kind = "words";
|
|
358
|
+
}
|
|
359
|
+
if (!kind)
|
|
360
|
+
continue;
|
|
361
|
+
const character = site.character + leading.length + attribute.length + eq.length;
|
|
362
|
+
result.push({
|
|
363
|
+
text: value,
|
|
364
|
+
range: { start: { line: site.line, character }, end: { line: site.line, character: character + value.length } },
|
|
365
|
+
attribute,
|
|
366
|
+
kind,
|
|
367
|
+
quoted: quote(value, site.inHelperString),
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return result;
|
|
371
|
+
}
|
package/dist/syntax.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface Range {
|
|
|
7
7
|
end: Position;
|
|
8
8
|
}
|
|
9
9
|
export type DiagnosticSeverity = "error" | "warning" | "hint";
|
|
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" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
|
|
10
|
+
export type DiagnosticCode = "squiffy/passage-without-section" | "squiffy/text-in-ui-block" | "squiffy/content-before-section" | "squiffy/v5-syntax" | "squiffy/at-attribute" | "squiffy/unquoted-text" | "squiffy/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
|
|
11
11
|
export interface Diagnostic {
|
|
12
12
|
file: string;
|
|
13
13
|
range: Range;
|
package/dist/text.d.ts
CHANGED
|
@@ -26,7 +26,8 @@ export interface TransformResult {
|
|
|
26
26
|
embeds: TextEmbed[];
|
|
27
27
|
noNextSection: NoNextSection[];
|
|
28
28
|
}
|
|
29
|
-
export declare
|
|
29
|
+
export declare const balancedTarget: string;
|
|
30
|
+
export declare function transformText(text: string, getNextSectionName: () => string | null, expressions?: boolean): TransformResult;
|
|
30
31
|
export interface SourceLine {
|
|
31
32
|
line: number;
|
|
32
33
|
length: number;
|
package/dist/text.js
CHANGED
|
@@ -115,30 +115,45 @@ class MappedString {
|
|
|
115
115
|
return this.segments[low];
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
// The target is everything before the first comma, and the setters everything after it. With
|
|
119
|
+
// expressions, they're kept as written, since a setter can contain commas in quotes, and the
|
|
120
|
+
// runtime splits them. Without, a space is added after each comma, as Squiffy 6 did.
|
|
121
|
+
function extractLinkFunctions(link, expressions) {
|
|
122
|
+
if (!expressions) {
|
|
123
|
+
const fragments = link.split(",");
|
|
124
|
+
return {
|
|
125
|
+
target: fragments[0].trim(),
|
|
126
|
+
setters: fragments.slice(1).join(", ")
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const comma = link.indexOf(",");
|
|
120
130
|
return {
|
|
121
|
-
target:
|
|
122
|
-
setters:
|
|
131
|
+
target: (comma == -1 ? link : link.substring(0, comma)).trim(),
|
|
132
|
+
setters: comma == -1 ? "" : link.substring(comma + 1),
|
|
123
133
|
};
|
|
124
134
|
}
|
|
125
|
-
function getAdditionalLinkParameters(link) {
|
|
126
|
-
const functions = extractLinkFunctions(link);
|
|
135
|
+
function getAdditionalLinkParameters(link, expressions) {
|
|
136
|
+
const functions = extractLinkFunctions(link, expressions);
|
|
127
137
|
let additionalParameters = "";
|
|
128
|
-
if (functions.setters.length > 0) {
|
|
129
|
-
additionalParameters += ` set="${functions.setters}"`;
|
|
138
|
+
if (functions.setters.trim().length > 0) {
|
|
139
|
+
additionalParameters += ` set="${escapeForHandlebarsString(functions.setters)}"`;
|
|
130
140
|
}
|
|
131
141
|
return {
|
|
132
142
|
target: functions.target,
|
|
133
143
|
additionalParameters
|
|
134
144
|
};
|
|
135
145
|
}
|
|
146
|
+
// Text with balanced brackets, nested up to three deep, as few characters as possible
|
|
147
|
+
const noBrackets = "[^()]";
|
|
148
|
+
const nested = (inner) => `\\((?:${noBrackets}|${inner})*\\)`;
|
|
149
|
+
export const balancedTarget = `(?:${noBrackets}|${nested(nested(nested(noBrackets)))})*?`;
|
|
136
150
|
function escapeForHandlebarsString(s) {
|
|
137
151
|
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
138
152
|
}
|
|
139
153
|
// getNextSectionName returns the section after this one, for [[text>]] links, or null if
|
|
140
154
|
// there isn't one.
|
|
141
|
-
|
|
155
|
+
// expressions: whether link setters take expressions (see AnalyseSettings.expressions)
|
|
156
|
+
export function transformText(text, getNextSectionName, expressions = false) {
|
|
142
157
|
const input = new MappedString(text);
|
|
143
158
|
const links = [];
|
|
144
159
|
const embeds = [];
|
|
@@ -162,6 +177,10 @@ export function transformText(text, getNextSectionName) {
|
|
|
162
177
|
return `${mustaches.length - 1}`;
|
|
163
178
|
});
|
|
164
179
|
const restore = (s) => s.replace(/(\d+)/g, (_match, i) => mustaches[Number(i)]);
|
|
180
|
+
// A link's target, inside its (...). With expressions, a setter can have brackets in it, e.g.
|
|
181
|
+
// [[Buy]](shop, gold -= (price - 1) * 2), so the target can contain balanced brackets, up to
|
|
182
|
+
// three deep. Without, it ends at the first ")", as in Squiffy 6.
|
|
183
|
+
const target = expressions ? balancedTarget : "(?:.*?)";
|
|
165
184
|
// nextSectionLinkRegex matches [[text>]] - a link to the next section
|
|
166
185
|
const nextSectionLinkRegex = /\[\[([^\]]*?)>\]\]/g;
|
|
167
186
|
input.replace(nextSectionLinkRegex, ([match, rawText], offset) => {
|
|
@@ -176,7 +195,7 @@ export function transformText(text, getNextSectionName) {
|
|
|
176
195
|
});
|
|
177
196
|
// namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
|
|
178
197
|
// - a named link where target starts with >
|
|
179
|
-
const namedNextSectionLinkRegex =
|
|
198
|
+
const namedNextSectionLinkRegex = new RegExp(`\\[\\[([^\\]]*?)\\]\\]\\(>(${target})\\)`, "g");
|
|
180
199
|
input.replace(namedNextSectionLinkRegex, ([match, rawText, rawRest], offset) => {
|
|
181
200
|
const text = restore(rawText);
|
|
182
201
|
const rest = restore(rawRest);
|
|
@@ -187,7 +206,7 @@ export function transformText(text, getNextSectionName) {
|
|
|
187
206
|
}
|
|
188
207
|
addLink({ linkType: "section", form: "next", text, target: rest }, offset, match.length, offset + 2 + rawText.length + 4, rawRest.length);
|
|
189
208
|
// rest could be empty or ", setter=value"
|
|
190
|
-
const parsedName = getAdditionalLinkParameters(nextSectionName + rest);
|
|
209
|
+
const parsedName = getAdditionalLinkParameters(nextSectionName + rest, expressions);
|
|
191
210
|
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
192
211
|
});
|
|
193
212
|
// namedSectionLinkRegex matches:
|
|
@@ -197,12 +216,12 @@ export function transformText(text, getNextSectionName) {
|
|
|
197
216
|
// open bracket
|
|
198
217
|
// any text - the name of the section
|
|
199
218
|
// closing bracket
|
|
200
|
-
const namedSectionLinkRegex =
|
|
219
|
+
const namedSectionLinkRegex = new RegExp(`\\[\\[([^\\]]*?)\\]\\]\\((${target})\\)`, "g");
|
|
201
220
|
input.replace(namedSectionLinkRegex, ([match, rawText, rawName], offset) => {
|
|
202
221
|
const text = restore(rawText);
|
|
203
222
|
const name = restore(rawName);
|
|
204
223
|
addLink({ linkType: "section", form: "named", text, target: name }, offset, match.length, offset + 2 + rawText.length + 3, rawName.length);
|
|
205
|
-
const parsedName = getAdditionalLinkParameters(name);
|
|
224
|
+
const parsedName = getAdditionalLinkParameters(name, expressions);
|
|
206
225
|
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
207
226
|
});
|
|
208
227
|
// namedPassageLinkRegex matches:
|
|
@@ -213,12 +232,12 @@ export function transformText(text, getNextSectionName) {
|
|
|
213
232
|
// open bracket, but not http(s):// after it
|
|
214
233
|
// any text - the name of the passage
|
|
215
234
|
// closing bracket
|
|
216
|
-
const namedPassageLinkRegex =
|
|
235
|
+
const namedPassageLinkRegex = new RegExp(`(?<!!)\\[([^\\]]*?)\\]\\(((?!https?:\\/\\/)${target})\\)`, "g");
|
|
217
236
|
input.replace(namedPassageLinkRegex, ([match, rawText, rawName], offset) => {
|
|
218
237
|
const text = restore(rawText);
|
|
219
238
|
const name = restore(rawName);
|
|
220
239
|
addLink({ linkType: "passage", form: "named", text, target: name }, offset, match.length, offset + 1 + rawText.length + 2, rawName.length);
|
|
221
|
-
const parsedName = getAdditionalLinkParameters(name);
|
|
240
|
+
const parsedName = getAdditionalLinkParameters(name, expressions);
|
|
222
241
|
return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
223
242
|
});
|
|
224
243
|
// unnamedSectionLinkRegex matches:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-compiler",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.4",
|
|
4
4
|
"description": "A tool for creating multiple-choice interactive stories",
|
|
5
5
|
"author": "Alex Warren",
|
|
6
6
|
"contributors": [
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"syntaxes/squiffy.tmLanguage.json"
|
|
51
51
|
],
|
|
52
52
|
"type": "module",
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "751f5f19aebb41942dd45f0de61f60a3ed5f9b8e"
|
|
54
54
|
}
|
|
@@ -377,7 +377,7 @@
|
|
|
377
377
|
},
|
|
378
378
|
{
|
|
379
379
|
"name": "meta.link.section.squiffy",
|
|
380
|
-
"match": "(\\[\\[)([^\\]]*?)(\\]\\])(\\()(
|
|
380
|
+
"match": "(\\[\\[)([^\\]]*?)(\\]\\])(\\()((?:[^()]|\\((?:[^()]|\\((?:[^()]|\\([^()]*\\))*\\))*\\))*?)(\\))",
|
|
381
381
|
"captures": {
|
|
382
382
|
"1": {
|
|
383
383
|
"name": "punctuation.definition.link.begin.squiffy"
|
|
@@ -461,7 +461,7 @@
|
|
|
461
461
|
},
|
|
462
462
|
{
|
|
463
463
|
"name": "meta.link.passage.squiffy",
|
|
464
|
-
"match": "(\\[)([^\\]]*?)(\\])(\\()(
|
|
464
|
+
"match": "(\\[)([^\\]]*?)(\\])(\\()((?:[^()]|\\((?:[^()]|\\((?:[^()]|\\([^()]*\\))*\\))*\\))*?)(\\))",
|
|
465
465
|
"captures": {
|
|
466
466
|
"1": {
|
|
467
467
|
"name": "punctuation.definition.link.begin.squiffy"
|
|
@@ -560,10 +560,10 @@
|
|
|
560
560
|
}
|
|
561
561
|
},
|
|
562
562
|
"assignments": {
|
|
563
|
-
"comment": "Comma-separated setters after a link's target: [[text]](section, score+=1, not visited)",
|
|
563
|
+
"comment": "Comma-separated setters after a link's target: [[text]](section, score+=1, not visited, msg = \"Hi, there\"). A comma inside quotes or brackets doesn't separate them.",
|
|
564
564
|
"patterns": [
|
|
565
565
|
{
|
|
566
|
-
"match": "([
|
|
566
|
+
"match": "((?:\"[^\"]*\"?|'[^']*'?|\\([^)]*\\)?|[^,\"'(])+)|(,)",
|
|
567
567
|
"captures": {
|
|
568
568
|
"1": {
|
|
569
569
|
"patterns": [
|
|
@@ -580,7 +580,7 @@
|
|
|
580
580
|
]
|
|
581
581
|
},
|
|
582
582
|
"assignment": {
|
|
583
|
-
"comment": "One setter
|
|
583
|
+
"comment": "One setter (setAttribute in runtime/src/squiffy.runtime.ts): a = 1, a += 1 (or -=, *=, /=), a := text with {{...}}, not a, a. In a story with a squiffy.json the value is an expression, as in {{#if}}: a = \"text\", a = b + 1; without one, a = text and a = @b. Patterns inside a capture can't anchor to its start (^ and \\G never match there), but $ is its end, and the earliest match wins.",
|
|
584
584
|
"patterns": [
|
|
585
585
|
{
|
|
586
586
|
"match": "\\s*(\\w*)\\s*(:=)(.*)$",
|
|
@@ -633,7 +633,21 @@
|
|
|
633
633
|
]
|
|
634
634
|
},
|
|
635
635
|
"value": {
|
|
636
|
+
"comment": "A setter's value. One with quotes, brackets, operators or and/or/not is an expression, as in {{#if}}. A single word is an attribute in a story with a squiffy.json, and anything else unquoted is text, as in a story without one. Of two matches at the start, the first listed wins.",
|
|
636
637
|
"patterns": [
|
|
638
|
+
{
|
|
639
|
+
"match": "(?=.*(?:[\"'()+*%<>!]|\\s[-/]|\\b(?:and|or|not)\\b)).+$",
|
|
640
|
+
"name": "meta.expression.squiffy",
|
|
641
|
+
"captures": {
|
|
642
|
+
"0": {
|
|
643
|
+
"patterns": [
|
|
644
|
+
{
|
|
645
|
+
"include": "#handlebars-expression"
|
|
646
|
+
}
|
|
647
|
+
]
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
},
|
|
637
651
|
{
|
|
638
652
|
"match": "-?\\d+(?:\\.\\d+)?$",
|
|
639
653
|
"name": "constant.numeric.squiffy"
|
|
@@ -646,6 +660,10 @@
|
|
|
646
660
|
"match": "@\\w+$",
|
|
647
661
|
"name": "variable.parameter.attribute.squiffy"
|
|
648
662
|
},
|
|
663
|
+
{
|
|
664
|
+
"match": "(?:[\\w-]+|\\[[^\\]]*\\])$",
|
|
665
|
+
"name": "variable.parameter.attribute.squiffy"
|
|
666
|
+
},
|
|
649
667
|
{
|
|
650
668
|
"match": ".+$",
|
|
651
669
|
"name": "string.unquoted.squiffy"
|
|
@@ -720,8 +738,8 @@
|
|
|
720
738
|
]
|
|
721
739
|
},
|
|
722
740
|
{
|
|
723
|
-
"comment": "{{helper args}},
|
|
724
|
-
"begin": "(\\{\\{\\{?~?)\\s*(?:([\\w.-]+)(?=\\s)|([\\w.-]+)(?=\\s*~?\\}))?",
|
|
741
|
+
"comment": "{{helper args}}, {{attribute}} on its own, or {{= expression}}",
|
|
742
|
+
"begin": "(\\{\\{\\{?~?)\\s*(?:([\\w.-]+)(?=\\s)|([\\w.-]+)(?=\\s*~?\\})|(=))?",
|
|
725
743
|
"beginCaptures": {
|
|
726
744
|
"1": {
|
|
727
745
|
"name": "support.constant.handlebars.squiffy punctuation.section.embedded.begin.handlebars.squiffy"
|
|
@@ -731,6 +749,9 @@
|
|
|
731
749
|
},
|
|
732
750
|
"3": {
|
|
733
751
|
"name": "variable.parameter.attribute.handlebars.squiffy"
|
|
752
|
+
},
|
|
753
|
+
"4": {
|
|
754
|
+
"name": "keyword.operator.display.handlebars.squiffy"
|
|
734
755
|
}
|
|
735
756
|
},
|
|
736
757
|
"end": "~?\\}\\}\\}?",
|
|
@@ -798,6 +819,11 @@
|
|
|
798
819
|
"match": "!=|>=|<=|=(?!=)|>|<",
|
|
799
820
|
"name": "keyword.operator.comparison.handlebars.squiffy"
|
|
800
821
|
},
|
|
822
|
+
{
|
|
823
|
+
"comment": "- and / are operators only at the start of a word: a-b is an attribute name and a/b a path",
|
|
824
|
+
"match": "[+*%]|(?<![\\w.\\]-])[-/]",
|
|
825
|
+
"name": "keyword.operator.arithmetic.handlebars.squiffy"
|
|
826
|
+
},
|
|
801
827
|
{
|
|
802
828
|
"match": "([\\w-]+)(=)",
|
|
803
829
|
"captures": {
|