squiffy-compiler 7.0.0-beta.7 → 7.0.0-beta.8

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 CHANGED
@@ -1,4 +1,4 @@
1
- import type { Diagnostic, PassageNode, SectionNode, SquiffyFile } from "./syntax.js";
1
+ import type { Diagnostic, PassageNode, Range, SectionNode, SquiffyFile } from "./syntax.js";
2
2
  import { SourceLine } from "./text.js";
3
3
  export interface ExternalFiles {
4
4
  getMatchingFilenames(pattern: string): Promise<string[]>;
@@ -58,6 +58,14 @@ declare class TextContainer {
58
58
  music?: string | null;
59
59
  sounds: string[];
60
60
  classes: string[];
61
+ transition?: string;
62
+ transitionTime?: number;
63
+ leave?: string;
64
+ leaveTime?: number;
65
+ leaveNode?: {
66
+ filename: string | undefined;
67
+ range: Range;
68
+ };
61
69
  output: string;
62
70
  addText(text: string, source: SourceLine): void;
63
71
  }
package/dist/analyse.js CHANGED
@@ -2,6 +2,8 @@ import { parse } from "./parse.js";
2
2
  import { positionMapper, transformText } from "./text.js";
3
3
  import { isScriptPath, isSquiffyPath, isStylesheetPath, PROJECT_FILENAME } from "./project.js";
4
4
  import { findV5Syntax } from "./v5.js";
5
+ import { findTemplateError } from "./templates.js";
6
+ import { sectionLabels } from "./section-labels.js";
5
7
  import { attributesSetIn, BUILT_IN_ATTRIBUTES, findAtAttributes, findBuiltInAttributeSets, findUnquotedText } from "./setters.js";
6
8
  // Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
7
9
  export class ParseCache {
@@ -130,6 +132,39 @@ class TextContainer {
130
132
  writable: true,
131
133
  value: []
132
134
  });
135
+ // @transition (#489): how it appears, and how long that takes in milliseconds
136
+ Object.defineProperty(this, "transition", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: void 0
141
+ });
142
+ Object.defineProperty(this, "transitionTime", {
143
+ enumerable: true,
144
+ configurable: true,
145
+ writable: true,
146
+ value: void 0
147
+ });
148
+ // @leave: how the old text goes when this clears the screen, and how long that takes
149
+ Object.defineProperty(this, "leave", {
150
+ enumerable: true,
151
+ configurable: true,
152
+ writable: true,
153
+ value: void 0
154
+ });
155
+ Object.defineProperty(this, "leaveTime", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: void 0
160
+ });
161
+ // Where @leave is, to say if there's nothing for it to do
162
+ Object.defineProperty(this, "leaveNode", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: void 0
167
+ });
133
168
  // The text after links have been converted
134
169
  Object.defineProperty(this, "output", {
135
170
  enumerable: true,
@@ -215,6 +250,13 @@ export class Passage extends TextContainer {
215
250
  }
216
251
  export const analysisInternals = new WeakMap();
217
252
  const lineRange = (node) => node.range;
253
+ // A @transition time (#489), such as "1s", "0.5s" or "400ms", in milliseconds
254
+ function parseTransitionTime(time) {
255
+ const match = /^(\d+(?:\.\d+)?|\.\d+)(s|ms)$/.exec(time);
256
+ if (!match)
257
+ return undefined;
258
+ return Math.round(parseFloat(match[1]) * (match[2] == "s" ? 1000 : 1));
259
+ }
218
260
  // Resolves @imports, builds the story's sections and passages, and reports diagnostics.
219
261
  export async function analyse(settings) {
220
262
  const story = new Story();
@@ -459,6 +501,32 @@ export async function analyse(settings) {
459
501
  }
460
502
  return true;
461
503
  }
504
+ case "transition": {
505
+ const target = passage ?? (section = ensureSectionExists(lineCount));
506
+ if (!target)
507
+ return sectionRequired(node);
508
+ if (!/^[A-Za-z][\w-]*$/.test(node.value)) {
509
+ report("warning", "squiffy/transition", filename, node.valueRange, `"${node.value}" can't be the name of a transition, so this was ignored. A name is letters, numbers, - and _, starting with a letter.`, lineCount);
510
+ return true;
511
+ }
512
+ let time;
513
+ if (node.time !== undefined) {
514
+ time = parseTransitionTime(node.time);
515
+ if (time === undefined) {
516
+ report("warning", "squiffy/transition", filename, lineRange(node), `"${node.time}" isn't a time, such as 1s or 400ms, so the transition takes its usual time.`, lineCount);
517
+ }
518
+ }
519
+ if (node.directive == "leave") {
520
+ target.leave = node.value;
521
+ target.leaveTime = time;
522
+ target.leaveNode = { filename, range: lineRange(node) };
523
+ }
524
+ else {
525
+ target.transition = node.value;
526
+ target.transitionTime = time;
527
+ }
528
+ return true;
529
+ }
462
530
  case "attribute": {
463
531
  section = ensureSectionExists(lineCount);
464
532
  let attribute;
@@ -584,6 +652,8 @@ export async function analyse(settings) {
584
652
  // Set once the sections are all known, before checking links
585
653
  let sectionNames = [];
586
654
  let sectionIndexes = new Map();
655
+ // What warnings call sections without names of their own, e.g. 'the "+++Continue..." after "cave"'
656
+ let labels = new Map();
587
657
  function checkText(container, section, passage) {
588
658
  const getNextSectionName = () => {
589
659
  return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
@@ -592,32 +662,48 @@ export async function analyse(settings) {
592
662
  container.output = result.output;
593
663
  const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
594
664
  const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
595
- const where = passage
596
- ? `In section '${section.name}', passage '${passage.name}'`
597
- : `In section '${section.name}'`;
665
+ const label = labels.get(section.name);
666
+ const inSection = label && section.node?.kind != "named" ? `In ${label}` : `In section '${section.name}'`;
667
+ const where = passage ? `${inSection}, passage '${passage.name}'` : inSection;
598
668
  const legacyLine = passage ? passage.line : section.line;
599
669
  const warn = (code, start, end, message, line = legacyLine) => {
600
670
  report("warning", code, section.filename, toRange(start, end), message, line);
601
671
  };
672
+ // Handlebars syntax errors, which would stop the text being shown at all
673
+ const templateError = findTemplateError(result.output);
674
+ if (templateError) {
675
+ const range = toRange(result.mapStart(templateError.start), result.mapEnd(templateError.end));
676
+ report("error", "squiffy/template-syntax", section.filename, range, `${where}, ${templateError.message}`, range.start.line + 1);
677
+ }
602
678
  for (const missing of result.noNextSection) {
603
- warn("squiffy/no-next-section", missing.start, missing.end, `In section '${section.name}', there is a ${missing.display} link but no following section exists`, section.line);
679
+ warn("squiffy/no-next-section", missing.start, missing.end, `${inSection}, there is a ${missing.display} link but no following section exists`, section.line);
604
680
  }
681
+ // Like the runtime, a passage link can go to one of the master section's passages
682
+ const masterPassages = story.sections[""]?.passages ?? {};
605
683
  for (const link of result.links) {
606
684
  if (link.form == "next")
607
685
  continue;
608
- if (link.linkType == "section") {
686
+ if (link.form == "input") {
687
+ const exists = link.linkType == "section"
688
+ ? hasOwn(story.sections, link.target)
689
+ : hasOwn(section.passages, link.target) || hasOwn(masterPassages, link.target);
690
+ if (!exists) {
691
+ warn(link.linkType == "section" ? "squiffy/unknown-section-link" : "squiffy/unknown-passage-link", link.start, link.end, `${where} there is a text box that goes to a ${link.linkType} called "${link.target}" when Enter is pressed, which doesn't exist`);
692
+ }
693
+ }
694
+ else if (link.linkType == "section") {
609
695
  if (!linkDestinationExists(link.target, story.sections)) {
610
696
  warn("squiffy/unknown-section-link", link.start, link.end, `${where} there is a link to a section called [[${link.target}]], which doesn't exist`);
611
697
  }
612
698
  }
613
- else if (!linkDestinationExists(link.target, section.passages)) {
699
+ else if (!linkDestinationExists(link.target, section.passages) && !linkDestinationExists(link.target, masterPassages)) {
614
700
  warn("squiffy/unknown-passage-link", link.start, link.end, `${where} there is a link to a passage called [${link.target}], which doesn't exist`);
615
701
  }
616
702
  }
617
703
  // Like the runtime's embed helper, a name can refer to a passage in the
618
- // current section or to any section.
704
+ // current section or the master section, or to any section.
619
705
  for (const embed of result.embeds) {
620
- if (!(embed.name in section.passages) && !(embed.name in story.sections)) {
706
+ if (!(embed.name in section.passages) && !(embed.name in story.sections) && !hasOwn(masterPassages, embed.name)) {
621
707
  warn("squiffy/unknown-embed", embed.start, embed.end, `${where} there is an embed of {{embed "${embed.name}"}}, but no section or passage with that name exists`);
622
708
  }
623
709
  }
@@ -629,7 +715,6 @@ export async function analyse(settings) {
629
715
  }
630
716
  }
631
717
  // {{deactivate}} disables links to a passage in this section, or in the master section
632
- const masterPassages = story.sections[""]?.passages ?? {};
633
718
  for (const deactivate of result.deactivates) {
634
719
  if (!(deactivate.name in section.passages) && !(deactivate.name in masterPassages)) {
635
720
  warn("squiffy/unknown-deactivate", deactivate.start, deactivate.end, `${where} there is a {{deactivate "${deactivate.name}"}}, but no passage with that name exists in this section`);
@@ -666,12 +751,15 @@ export async function analyse(settings) {
666
751
  }
667
752
  sectionNames = Object.keys(story.sections);
668
753
  sectionIndexes = new Map(sectionNames.map((name, i) => [name, i]));
754
+ const nodeNames = new Map(Object.values(story.sections).flatMap(section => section.node ? [[section.node, section.name]] : []));
755
+ labels = sectionLabels(files, node => nodeNames.get(node));
669
756
  for (const section of Object.values(story.sections)) {
670
757
  checkText(section, section, null);
671
758
  for (const passage of Object.values(section.passages)) {
672
759
  checkText(passage, section, passage);
673
760
  }
674
761
  }
762
+ success && (success = !legacyErrors.length);
675
763
  }
676
764
  if (settings.expressions) {
677
765
  warnAboutUnquotedText();
@@ -682,6 +770,23 @@ export async function analyse(settings) {
682
770
  report("warning", "squiffy/built-in-attribute", file.filename, found.range, `Squiffy sets ${found.name} itself: it's ${BUILT_IN_ATTRIBUTES.get(found.name)}. Use a different name for your own attribute.`, found.range.start.line + 1);
683
771
  }
684
772
  });
773
+ // @leave only does something when the screen is cleared: by the section or passage it's in, or by
774
+ // one that it's for, as the master section's @leave is for every section
775
+ const master = story.sections[""];
776
+ const clears = (s) => s.clear || Object.values(s.passages).some(p => p.clear);
777
+ const anyClear = Object.values(story.sections).some(clears);
778
+ const checkLeave = (target, used) => {
779
+ if (!target.leaveNode || target.leave == "none" || used)
780
+ return;
781
+ report("warning", "squiffy/transition", target.leaveNode.filename, target.leaveNode.range, "@leave is for when the screen is cleared, but nothing it's for has @clear, so it does nothing.", target.leaveNode.range.start.line + 1);
782
+ };
783
+ for (const section of Object.values(story.sections)) {
784
+ const masterPassageClears = !!(section.passages[""]?.clear || master?.passages[""]?.clear);
785
+ checkLeave(section, section === master ? anyClear : clears(section) || !!master?.clear || (master ? Object.values(master.passages).some(p => p.clear) : false));
786
+ for (const passage of Object.values(section.passages)) {
787
+ checkLeave(passage, passage.clear || masterPassageClears || (passage.name == "" && clears(section)));
788
+ }
789
+ }
685
790
  const analysis = {
686
791
  success,
687
792
  files,
@@ -1,7 +1,7 @@
1
1
  import { ExternalFiles, ParseCache } from "./analyse.js";
2
2
  import { CompileSuccess } from "./emit.js";
3
3
  import type { Diagnostic } from "./syntax.js";
4
- import type { Panel } from "./project.js";
4
+ import type { Panel, PlayerSetting } from "./project.js";
5
5
  export { findV5Syntax, convertV5Syntax } from "./v5.js";
6
6
  export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
7
7
  export { convertSetters, convertSetter } from "./setters.js";
@@ -20,8 +20,8 @@ export { emit } from "./emit.js";
20
20
  export type { EmitSettings, Output, UiInfo, CompileSuccess } from "./emit.js";
21
21
  export { formatDiagnostic } from "./syntax.js";
22
22
  export { findCssUrls, isRelativeUrl, resolveCssUrl, rewriteCssUrls } from "./css-urls.js";
23
- export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, storyFileList, BUILD_FOLDER, } from "./project.js";
24
- export type { MapPositions, PageStringName, PageStrings, Panel, PanelIcon, ProjectFile, ParsedProjectFile, StoryFiles } from "./project.js";
23
+ export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, PLAYER_SETTING_TYPES, storyFileList, BUILD_FOLDER, } from "./project.js";
24
+ export type { MapPositions, PageStringName, PageStrings, Panel, PanelIcon, PlayerSetting, PlayerSettingOption, ProjectFile, ParsedProjectFile, StoryFiles } from "./project.js";
25
25
  export type * from "./syntax.js";
26
26
  interface CompilerSettings {
27
27
  scriptBaseFilename?: string;
@@ -34,6 +34,7 @@ interface CompilerSettings {
34
34
  expressions?: boolean;
35
35
  projectId?: string;
36
36
  panels?: Panel[];
37
+ settings?: PlayerSetting[];
37
38
  }
38
39
  export interface CompileError {
39
40
  success: false;
package/dist/compiler.js CHANGED
@@ -15,7 +15,7 @@ export { analyse, ParseCache } from "./analyse.js";
15
15
  export { emit } from "./emit.js";
16
16
  export { formatDiagnostic } from "./syntax.js";
17
17
  export { findCssUrls, isRelativeUrl, resolveCssUrl, rewriteCssUrls } from "./css-urls.js";
18
- export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, storyFileList, BUILD_FOLDER, } from "./project.js";
18
+ export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, PLAYER_SETTING_TYPES, storyFileList, BUILD_FOLDER, } from "./project.js";
19
19
  export async function compile(settings) {
20
20
  const analysis = await analyse(settings);
21
21
  const { legacyErrors, legacyWarnings } = analysisInternals.get(analysis);
@@ -31,5 +31,5 @@ export async function compile(settings) {
31
31
  diagnostics: analysis.diagnostics,
32
32
  };
33
33
  }
34
- return emit(analysis, { globalJs: settings.globalJs, projectId: settings.projectId, panels: settings.panels });
34
+ return emit(analysis, { globalJs: settings.globalJs, projectId: settings.projectId, panels: settings.panels, settings: settings.settings });
35
35
  }
package/dist/emit.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Analysis } from "./analyse.js";
2
2
  import type { Diagnostic } from "./syntax.js";
3
- import type { Panel } from "./project.js";
3
+ import type { Panel, PlayerSetting } from "./project.js";
4
4
  export interface Output {
5
5
  story: OutputStory;
6
6
  js: string[][];
@@ -12,6 +12,7 @@ interface OutputStory {
12
12
  uiJsIndex?: number;
13
13
  expressions?: boolean;
14
14
  panels?: Panel[];
15
+ settings?: PlayerSetting[];
15
16
  sections: Record<string, OutputSection>;
16
17
  audio?: string[];
17
18
  }
@@ -21,6 +22,10 @@ interface OutputSection {
21
22
  music?: string | null;
22
23
  sounds?: string[];
23
24
  classes?: string[];
25
+ transition?: string;
26
+ transitionTime?: number;
27
+ leave?: string;
28
+ leaveTime?: number;
24
29
  attributes?: string[];
25
30
  jsIndex?: number;
26
31
  passages?: Record<string, OutputPassage>;
@@ -32,6 +37,10 @@ interface OutputPassage {
32
37
  music?: string | null;
33
38
  sounds?: string[];
34
39
  classes?: string[];
40
+ transition?: string;
41
+ transitionTime?: number;
42
+ leave?: string;
43
+ leaveTime?: number;
35
44
  repeat?: boolean;
36
45
  attributes?: string[];
37
46
  jsIndex?: number;
@@ -45,6 +54,7 @@ export interface EmitSettings {
45
54
  globalJs?: boolean;
46
55
  projectId?: string;
47
56
  panels?: Panel[];
57
+ settings?: PlayerSetting[];
48
58
  }
49
59
  export interface CompileSuccess {
50
60
  success: true;
package/dist/emit.js CHANGED
@@ -16,6 +16,16 @@ function addSounds(output, from, audio) {
16
16
  for (const match of from.output.matchAll(SOUND_HELPER))
17
17
  audio.add(match[2]);
18
18
  }
19
+ function addTransition(output, from) {
20
+ if (from.transition !== undefined)
21
+ output.transition = from.transition;
22
+ if (from.transitionTime !== undefined)
23
+ output.transitionTime = from.transitionTime;
24
+ if (from.leave !== undefined)
25
+ output.leave = from.leave;
26
+ if (from.leaveTime !== undefined)
27
+ output.leaveTime = from.leaveTime;
28
+ }
19
29
  // Produces the story data and JavaScript for an analysis that succeeded.
20
30
  export function emit(analysis, settings = {}) {
21
31
  const internals = analysisInternals.get(analysis);
@@ -43,6 +53,9 @@ export function emit(analysis, settings = {}) {
43
53
  if (settings.panels?.length) {
44
54
  output.story.panels = settings.panels;
45
55
  }
56
+ if (settings.settings?.length) {
57
+ output.story.settings = settings.settings;
58
+ }
46
59
  if (story.uiJs.length > 0) {
47
60
  output.js.push(story.uiJs);
48
61
  output.story.uiJsIndex = output.js.length - 1;
@@ -59,6 +72,7 @@ export function emit(analysis, settings = {}) {
59
72
  addSounds(outputSection, section, audio);
60
73
  if (section.classes.length)
61
74
  outputSection.classes = section.classes;
75
+ addTransition(outputSection, section);
62
76
  if (section.attributes.length > 0) {
63
77
  outputSection.attributes = section.attributes;
64
78
  }
@@ -93,6 +107,7 @@ export function emit(analysis, settings = {}) {
93
107
  addSounds(outputPassage, passage, audio);
94
108
  if (passage.classes.length)
95
109
  outputPassage.classes = passage.classes;
110
+ addTransition(outputPassage, passage);
96
111
  if (passage.attributes.length > 0) {
97
112
  outputPassage.attributes = passage.attributes;
98
113
  }
@@ -130,6 +145,9 @@ export function emit(analysis, settings = {}) {
130
145
  if (output.story.panels) {
131
146
  outputJs.push(`story.panels = ${JSON.stringify(output.story.panels, null, 4)};`);
132
147
  }
148
+ if (output.story.settings) {
149
+ outputJs.push(`story.settings = ${JSON.stringify(output.story.settings, null, 4)};`);
150
+ }
133
151
  if (output.story.audio) {
134
152
  outputJs.push(`story.audio = ${JSON.stringify(output.story.audio, null, 4)};`);
135
153
  }
@@ -0,0 +1,19 @@
1
+ export declare function rewriteInfixCondition(condition: string): string | null;
2
+ export declare function rewriteInfixArguments(args: string): string | null;
3
+ export declare function rewriteInfixEmbed(args: string, label?: string): string | null;
4
+ export declare function rewriteDisplayExpression(expression: string): string;
5
+ export declare function rewriteValueExpression(expression: string): string;
6
+ export declare function splitSetters(setters: string): string[];
7
+ export declare function findTagEnd(text: string, from: number): number;
8
+ export declare class InfixRewriteError extends Error {
9
+ readonly start: number;
10
+ readonly end: number;
11
+ constructor(message: string, start: number, end: number);
12
+ }
13
+ export interface InfixEdit {
14
+ start: number;
15
+ end: number;
16
+ length: number;
17
+ }
18
+ export declare function rewriteInfixExpressions(text: string, edits?: InfixEdit[]): string;
19
+ export declare function mapRewrittenPosition(edits: InfixEdit[], pos: number): number;