@rohal12/spindle 0.59.22 → 0.59.25
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/README.md +3 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +1873 -1340
- package/dist/pkg/macro-registry.js +575 -0
- package/dist/pkg/story-variables.js +1882 -1223
- package/dist/pkg/tooling.js +26 -16
- package/package.json +2 -1
- package/src/code-check.ts +315 -64
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- package/src/components/macros/SettingsControls.tsx +10 -6
- package/src/components/macros/Type.tsx +1 -1
- package/src/components/macros/detached-body.tsx +62 -3
- package/src/hooks/use-modal-focus.ts +62 -19
- package/src/markup/ast.ts +88 -1
- package/src/markup/line-column.ts +14 -0
- package/src/markup/pair.ts +331 -0
- package/src/markup/parse.ts +166 -36
- package/src/markup/render.tsx +15 -4
- package/src/markup/spindle.d.peggy.ts +3 -3
- package/src/markup/spindle.peggy +81 -180
- package/src/markup/tokens.ts +49 -9
- package/src/markup/validate.ts +167 -79
- package/src/settings.ts +9 -0
- package/src/static-literal.ts +292 -0
- package/src/story-variables.ts +438 -125
- package/src/tooling.ts +178 -9
- package/src/types-drift-check.ts +52 -0
- package/src/widgets/ast-scanner.ts +16 -4
- package/src/widgets/widget-def.ts +93 -22
- package/types/tooling.d.ts +571 -7
package/types/tooling.d.ts
CHANGED
|
@@ -156,11 +156,47 @@ export interface MarkupDiagnostic {
|
|
|
156
156
|
/** 1-based column (UTF-16 code units). */
|
|
157
157
|
column: number;
|
|
158
158
|
message: string;
|
|
159
|
+
/** The kind of error, which tools tell apart without reading `message`. */
|
|
160
|
+
code: MarkupDiagnosticCode;
|
|
161
|
+
/**
|
|
162
|
+
* Where the offending text is in the passage's content: from `start` up to
|
|
163
|
+
* `end` (UTF-16 offsets). `line` and `column` are those of `start`.
|
|
164
|
+
*/
|
|
165
|
+
start: number;
|
|
166
|
+
end: number;
|
|
167
|
+
/** What the diagnostic names, by `code`: the macro, the passage, what to try. */
|
|
168
|
+
data?: MarkupDiagnosticData;
|
|
159
169
|
/** The source file and line, when the passage says where it came from. */
|
|
160
170
|
file?: string;
|
|
161
171
|
fileLine?: number;
|
|
162
172
|
}
|
|
163
173
|
|
|
174
|
+
/**
|
|
175
|
+
* The kinds of diagnostic, which stay the same where the wording of the
|
|
176
|
+
* message changes (they are covered by semver, with the data they carry).
|
|
177
|
+
* The kinds of malformed markup are those of {@link MarkupErrorCode}.
|
|
178
|
+
*
|
|
179
|
+
* | code | `data` |
|
|
180
|
+
* | --- | --- |
|
|
181
|
+
* | `unknown-macro` | `name`, `suggestions` (the closest known macro, if close) |
|
|
182
|
+
* | `unknown-passage` | `name`, `macro` (`link` for `[[…]]`), `suggestions` |
|
|
183
|
+
* | `unquoted-passage-name` | `name`, `macro` |
|
|
184
|
+
* | `argument-error` | `macro` |
|
|
185
|
+
* | `code-syntax` | `macro`, for a macro argument |
|
|
186
|
+
*/
|
|
187
|
+
export type MarkupDiagnosticCode =
|
|
188
|
+
| MarkupErrorCode
|
|
189
|
+
| 'unknown-macro'
|
|
190
|
+
| 'argument-error'
|
|
191
|
+
| 'code-syntax'
|
|
192
|
+
| 'unknown-passage'
|
|
193
|
+
| 'unquoted-passage-name';
|
|
194
|
+
|
|
195
|
+
/** What a diagnostic names, by code (see {@link MarkupDiagnosticCode}). */
|
|
196
|
+
export type MarkupDiagnosticData = Readonly<
|
|
197
|
+
Record<string, string | readonly string[]>
|
|
198
|
+
>;
|
|
199
|
+
|
|
164
200
|
/**
|
|
165
201
|
* Validate the markup of a story's passages as Spindle does when the story
|
|
166
202
|
* starts: malformed markup (unclosed or mismatched macros, tags, links,
|
|
@@ -308,42 +344,90 @@ interface TokenSpan {
|
|
|
308
344
|
end: number;
|
|
309
345
|
}
|
|
310
346
|
|
|
347
|
+
/**
|
|
348
|
+
* Where the selectors of a token are written: from `selectorsStart` up to
|
|
349
|
+
* `selectorsEnd`, without the space that may follow them. Both are absent for
|
|
350
|
+
* a token without selectors.
|
|
351
|
+
*/
|
|
352
|
+
export interface SelectorSpan {
|
|
353
|
+
selectorsStart?: number;
|
|
354
|
+
selectorsEnd?: number;
|
|
355
|
+
}
|
|
356
|
+
|
|
311
357
|
export interface TextToken extends TokenSpan {
|
|
312
358
|
type: 'text';
|
|
313
359
|
value: string;
|
|
360
|
+
/** A closed HTML comment: markdown drops it, and so does raw rendering. */
|
|
361
|
+
comment?: true;
|
|
314
362
|
}
|
|
315
363
|
|
|
316
|
-
export interface LinkToken extends TokenSpan, Selectors {
|
|
364
|
+
export interface LinkToken extends TokenSpan, Selectors, SelectorSpan {
|
|
317
365
|
type: 'link';
|
|
318
366
|
display: string;
|
|
367
|
+
/** Where the label is written, from `displayStart` to `displayEnd`. */
|
|
368
|
+
displayStart: number;
|
|
369
|
+
displayEnd: number;
|
|
319
370
|
target: string;
|
|
320
371
|
/** Where the target is written, from `targetStart` to `targetEnd`. */
|
|
321
372
|
targetStart: number;
|
|
322
373
|
targetEnd: number;
|
|
323
374
|
}
|
|
324
375
|
|
|
325
|
-
export interface MacroToken extends TokenSpan, Selectors {
|
|
376
|
+
export interface MacroToken extends TokenSpan, Selectors, SelectorSpan {
|
|
326
377
|
type: 'macro';
|
|
327
378
|
name: string;
|
|
379
|
+
/** Where the name is written (after `{`, `/` and the selectors). */
|
|
380
|
+
nameStart: number;
|
|
381
|
+
nameEnd: number;
|
|
328
382
|
rawArgs: string;
|
|
383
|
+
/**
|
|
384
|
+
* Where `rawArgs` is written, without the whitespace around it; an empty
|
|
385
|
+
* span at `nameEnd` when there are none.
|
|
386
|
+
*/
|
|
387
|
+
argsStart: number;
|
|
388
|
+
argsEnd: number;
|
|
329
389
|
isClose: boolean;
|
|
330
390
|
}
|
|
331
391
|
|
|
332
|
-
export interface VariableToken extends TokenSpan, Selectors {
|
|
392
|
+
export interface VariableToken extends TokenSpan, Selectors, SelectorSpan {
|
|
333
393
|
type: 'variable';
|
|
334
394
|
name: string;
|
|
395
|
+
/** Where the name is written, without the sigil. */
|
|
396
|
+
nameStart: number;
|
|
397
|
+
nameEnd: number;
|
|
335
398
|
scope: VariableScope;
|
|
336
399
|
}
|
|
337
400
|
|
|
338
|
-
export interface ExpressionToken extends TokenSpan, Selectors {
|
|
401
|
+
export interface ExpressionToken extends TokenSpan, Selectors, SelectorSpan {
|
|
339
402
|
type: 'expression';
|
|
340
403
|
expression: string;
|
|
404
|
+
/** Where `expression` is written, after the `{` and the selectors. */
|
|
405
|
+
expressionStart: number;
|
|
406
|
+
expressionEnd: number;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** An attribute of an HTML tag as it is written, in order. */
|
|
410
|
+
export interface AttributeSpan {
|
|
411
|
+
name: string;
|
|
412
|
+
nameStart: number;
|
|
413
|
+
nameEnd: number;
|
|
414
|
+
/** Where the value is written, without its quotes; absent with no `=`. */
|
|
415
|
+
valueStart?: number;
|
|
416
|
+
valueEnd?: number;
|
|
417
|
+
/** The quote around the value, if it has one. */
|
|
418
|
+
quote?: '"' | "'";
|
|
341
419
|
}
|
|
342
420
|
|
|
343
421
|
export interface HtmlToken extends TokenSpan {
|
|
344
422
|
type: 'html';
|
|
345
423
|
tag: string;
|
|
424
|
+
/** Where the tag name is written. */
|
|
425
|
+
tagNameStart: number;
|
|
426
|
+
tagNameEnd: number;
|
|
427
|
+
/** The attribute values by name; the first of equal names (any case). */
|
|
346
428
|
attributes: Record<string, string>;
|
|
429
|
+
/** Every attribute as written, in order, duplicates included. */
|
|
430
|
+
attributeSpans: AttributeSpan[];
|
|
347
431
|
isClose: boolean;
|
|
348
432
|
isSelfClose: boolean;
|
|
349
433
|
}
|
|
@@ -357,12 +441,49 @@ export type Token =
|
|
|
357
441
|
| ExpressionToken
|
|
358
442
|
| HtmlToken;
|
|
359
443
|
|
|
360
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* The kinds of malformed markup. A tag that is malformed: `unclosed-link`
|
|
446
|
+
* (`[[` with no `]]`), `unclosed-expression` (`{$…` with no `}`),
|
|
447
|
+
* `unclosed-macro` (`{name…` with no `}`), `invalid-closer` (`{/` and no
|
|
448
|
+
* name), `closer-with-selectors`, `closer-with-arguments`, `unclosed-tag`
|
|
449
|
+
* (`<div` with no `>`), `unexpected-character` (in a tag) and
|
|
450
|
+
* `unclosed-attribute` (a value with no closing quote). Tags that pair with
|
|
451
|
+
* nothing: `unclosed-block` (`{if}` with no `{/if}`, `<div>` with no
|
|
452
|
+
* `</div>`), `mismatched-closer`, `stray-closer` and `misplaced-branch`
|
|
453
|
+
* (`{else}` outside `{if}`). `syntax` is any other.
|
|
454
|
+
*/
|
|
455
|
+
export type MarkupErrorCode =
|
|
456
|
+
| PairingErrorCode
|
|
457
|
+
| 'syntax'
|
|
458
|
+
| 'unclosed-link'
|
|
459
|
+
| 'unclosed-expression'
|
|
460
|
+
| 'unclosed-macro'
|
|
461
|
+
| 'invalid-closer'
|
|
462
|
+
| 'closer-with-selectors'
|
|
463
|
+
| 'closer-with-arguments'
|
|
464
|
+
| 'unclosed-tag'
|
|
465
|
+
| 'unexpected-character'
|
|
466
|
+
| 'unclosed-attribute';
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Malformed markup, with where it starts (0-based offset, 1-based line and
|
|
470
|
+
* column).
|
|
471
|
+
*/
|
|
361
472
|
export declare class MarkupError extends Error {
|
|
473
|
+
/** What is wrong, without the position. */
|
|
362
474
|
reason: string;
|
|
363
475
|
offset: number;
|
|
364
476
|
line: number;
|
|
365
477
|
column: number;
|
|
478
|
+
/** The kind of error, which stays the same where the wording changes. */
|
|
479
|
+
code: MarkupErrorCode;
|
|
480
|
+
/** Where the offending text ends (0-based, exclusive). */
|
|
481
|
+
end: number;
|
|
482
|
+
/**
|
|
483
|
+
* The names involved, by `code`: `name` for an unclosed, mismatched or
|
|
484
|
+
* stray tag (and `closer`, `parent`, `inside` where there are several).
|
|
485
|
+
*/
|
|
486
|
+
data: Readonly<Record<string, string>>;
|
|
366
487
|
}
|
|
367
488
|
|
|
368
489
|
export interface ParseMarkupOptions {
|
|
@@ -501,9 +622,452 @@ export interface PassageReference {
|
|
|
501
622
|
* the passage of `{goto}`, `{include}` and `{link}` (and of macros that
|
|
502
623
|
* declare a `passage` argument), the `goto` and `dialog` actions of
|
|
503
624
|
* `{watch}` (and the `string` and `text` arguments of macros that declare
|
|
504
|
-
* they hold a passage name) and the body of `{dialog}
|
|
505
|
-
*
|
|
625
|
+
* they hold a passage name) and the body of `{dialog}`, also in the labels
|
|
626
|
+
* and attribute values that hold markup. Malformed tags are skipped, so
|
|
627
|
+
* half-typed markup reads. It uses the macros registered here; see
|
|
628
|
+
* {@link collectStoryPassageReferences}.
|
|
506
629
|
*/
|
|
507
630
|
export declare function collectPassageReferences(
|
|
508
631
|
source: string,
|
|
509
632
|
): PassageReference[];
|
|
633
|
+
|
|
634
|
+
// ---------------------------------------------------------------------------
|
|
635
|
+
// Stateless checks
|
|
636
|
+
//
|
|
637
|
+
// `validateMarkup` and `collectPassageReferences` read the process-global
|
|
638
|
+
// registry (`defineMacro`). The functions below take the macros to check
|
|
639
|
+
// against and keep no state: use them to analyse several projects in one
|
|
640
|
+
// process.
|
|
641
|
+
// ---------------------------------------------------------------------------
|
|
642
|
+
|
|
643
|
+
/** What tooling knows about a macro (a `MacroMetadata` has all of it). */
|
|
644
|
+
export interface ToolingMacro {
|
|
645
|
+
name: string;
|
|
646
|
+
block: boolean;
|
|
647
|
+
subMacros: string[];
|
|
648
|
+
/**
|
|
649
|
+
* Its declared parameters: their types tell which arguments are code, and
|
|
650
|
+
* what a `string` or `text` argument holds (see `ParameterDef.holds`).
|
|
651
|
+
*/
|
|
652
|
+
parameters?: readonly ParameterDef[];
|
|
653
|
+
/** Whether it resolves markup: its strings hold markup by default. */
|
|
654
|
+
interpolate?: boolean;
|
|
655
|
+
/** Whether its first argument names the story variable it binds. */
|
|
656
|
+
storeVar?: boolean;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* The built-in macros, as data: what `getMacroRegistry()` returns before any
|
|
661
|
+
* `defineMacro`. It is loaded without `fs`, so a bundler inlines it.
|
|
662
|
+
*/
|
|
663
|
+
export declare const builtinMacros: readonly MacroMetadata[];
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* `validateMarkup` against the given `macros` (the built-in ones are in
|
|
667
|
+
* `builtinMacros`; add the user-defined ones): the same checks, with no
|
|
668
|
+
* registry involved.
|
|
669
|
+
*/
|
|
670
|
+
export declare function validateStoryMarkup(
|
|
671
|
+
passages: Iterable<MarkupPassage>,
|
|
672
|
+
macros: Iterable<ToolingMacro>,
|
|
673
|
+
options?: ValidateMarkupOptions,
|
|
674
|
+
): MarkupDiagnostic[];
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* `collectPassageReferences` against the given `macros`, with no registry
|
|
678
|
+
* involved.
|
|
679
|
+
*/
|
|
680
|
+
export declare function collectStoryPassageReferences(
|
|
681
|
+
source: string,
|
|
682
|
+
macros: Iterable<ToolingMacro>,
|
|
683
|
+
): PassageReference[];
|
|
684
|
+
|
|
685
|
+
// ---------------------------------------------------------------------------
|
|
686
|
+
// Variable references
|
|
687
|
+
// ---------------------------------------------------------------------------
|
|
688
|
+
|
|
689
|
+
/** A reference to a story (`$`) or transient (`%`) variable in markup. */
|
|
690
|
+
export interface VariableReference {
|
|
691
|
+
sigil: '$' | '%';
|
|
692
|
+
/** The variable, without the sigil. */
|
|
693
|
+
name: string;
|
|
694
|
+
/** The fields accessed with dots (`$a.b.c`: `['b', 'c']`). */
|
|
695
|
+
path: string[];
|
|
696
|
+
/** Where `$a.b.c` is written: `[start, end)` UTF-16 offsets into the markup. */
|
|
697
|
+
start: number;
|
|
698
|
+
end: number;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* What is declared. A schema is absent for a variable whose initializer is not
|
|
703
|
+
* static (`parseDeclarations`): it is declared, and its fields are not checked.
|
|
704
|
+
*/
|
|
705
|
+
export interface VariableDeclarations {
|
|
706
|
+
variables: ReadonlyMap<string, FieldSchema | undefined>;
|
|
707
|
+
/** The transients (`%`); not checked when absent, as at story start. */
|
|
708
|
+
transients?: ReadonlyMap<string, FieldSchema | undefined>;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
export type VariableDiagnosticCode =
|
|
712
|
+
| 'undeclared-variable'
|
|
713
|
+
| 'undeclared-transient'
|
|
714
|
+
| 'reserved-name'
|
|
715
|
+
| 'primitive-field';
|
|
716
|
+
|
|
717
|
+
/** A variable reference that is not valid against the declarations. */
|
|
718
|
+
export interface VariableDiagnostic {
|
|
719
|
+
passage: string;
|
|
720
|
+
code: VariableDiagnosticCode;
|
|
721
|
+
/** The variable, without the sigil. */
|
|
722
|
+
name: string;
|
|
723
|
+
/** The fields accessed with dots, if any. */
|
|
724
|
+
path?: string[];
|
|
725
|
+
/** The reference, as offsets into the passage content. */
|
|
726
|
+
start: number;
|
|
727
|
+
end: number;
|
|
728
|
+
/** The message the story start shows (after `Passage "name": `). */
|
|
729
|
+
message: string;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* The variable references the markup of a passage evaluates, in source order,
|
|
734
|
+
* with their positions. Malformed tags are skipped. The macros are those the
|
|
735
|
+
* markup may use (`builtinMacros` and the user-defined ones).
|
|
736
|
+
*/
|
|
737
|
+
export declare function variableReferences(
|
|
738
|
+
source: string,
|
|
739
|
+
macros?: Iterable<ToolingMacro>,
|
|
740
|
+
): VariableReference[];
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The variable references of the passages that are not valid against the
|
|
744
|
+
* declarations, as the story start checks them: undeclared variables (and
|
|
745
|
+
* transients, when `transients` is given), and fields accessed on primitives
|
|
746
|
+
* that have no such member. Unknown fields of objects are valid (classes may
|
|
747
|
+
* add members), as are any fields of arrays and `null` defaults. Tolerant of
|
|
748
|
+
* half-typed markup; evaluates no project code.
|
|
749
|
+
*/
|
|
750
|
+
export declare function validateVariableReferences(
|
|
751
|
+
passages: Iterable<MarkupPassage>,
|
|
752
|
+
declarations: VariableDeclarations,
|
|
753
|
+
macros?: Iterable<ToolingMacro>,
|
|
754
|
+
): VariableDiagnostic[];
|
|
755
|
+
|
|
756
|
+
// ---------------------------------------------------------------------------
|
|
757
|
+
// Widget definitions
|
|
758
|
+
// ---------------------------------------------------------------------------
|
|
759
|
+
|
|
760
|
+
/** What a `{widget}` definition's arguments declare. */
|
|
761
|
+
export interface WidgetDef {
|
|
762
|
+
name: string;
|
|
763
|
+
/** The `@` parameters. */
|
|
764
|
+
params: string[];
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* A widget defined by a `{widget}` macro. The offsets are UTF-16 offsets into
|
|
769
|
+
* the content of `passage`.
|
|
770
|
+
*/
|
|
771
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
772
|
+
/** Whether its body renders `{@children}`: it takes a closing tag. */
|
|
773
|
+
block: boolean;
|
|
774
|
+
passage: string;
|
|
775
|
+
/** The opening `{widget …}` tag. */
|
|
776
|
+
start: number;
|
|
777
|
+
end: number;
|
|
778
|
+
/** The name as written, without quotes. */
|
|
779
|
+
nameStart: number;
|
|
780
|
+
nameEnd: number;
|
|
781
|
+
/** The `{/widget}` closer; absent while the definition is not closed. */
|
|
782
|
+
closeStart?: number;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* The name and `@` parameters a `{widget}` macro reads from its arguments
|
|
787
|
+
* (`"Wrap" @a @b`), as the runtime reads them. Throws where the macro
|
|
788
|
+
* reports the arguments.
|
|
789
|
+
*/
|
|
790
|
+
export declare function parseWidgetDef(rawArgs: string): WidgetDef;
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* The widgets the story defines, as the runtime registers them: those of
|
|
794
|
+
* `StoryInit` and of the passages tagged `widget`, in source order. Block-ness
|
|
795
|
+
* is decided on tokens, so a `{@children}` in an HTML comment or a `{do}` body
|
|
796
|
+
* does not count; one in an attribute value or label of the `macros` does.
|
|
797
|
+
* Tolerant: a half-typed definition is reported without `closeStart`, and one
|
|
798
|
+
* whose arguments cannot be read is left out.
|
|
799
|
+
*/
|
|
800
|
+
export declare function widgetDefinitions(
|
|
801
|
+
passages: Iterable<MarkupPassage>,
|
|
802
|
+
macros?: Iterable<ToolingMacro>,
|
|
803
|
+
): WidgetDefinition[];
|
|
804
|
+
|
|
805
|
+
// ---------------------------------------------------------------------------
|
|
806
|
+
// Pieces of a passage
|
|
807
|
+
// ---------------------------------------------------------------------------
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* What every piece says of where it is: it is at `offset` in the markup
|
|
811
|
+
* given (UTF-16 code units). A piece in the markup of a text (a label, an
|
|
812
|
+
* attribute value) is `nested`, with `where` the description of that text.
|
|
813
|
+
*/
|
|
814
|
+
export interface PieceBase {
|
|
815
|
+
offset: number;
|
|
816
|
+
nested?: true;
|
|
817
|
+
where?: string;
|
|
818
|
+
/**
|
|
819
|
+
* For the code or text of a quoted string with escapes (a `\"`), whose
|
|
820
|
+
* characters are not where `offset` plus their index says: the offset of
|
|
821
|
+
* each character in the source, and of the end of the text, so that
|
|
822
|
+
* `sourceOffsets[i]` is where character `i` of `code` or `text` is. Absent
|
|
823
|
+
* when `offset + i` is (see {@link pieceOffset}).
|
|
824
|
+
*/
|
|
825
|
+
sourceOffsets?: readonly number[];
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/** Where character `index` of the code or text of `piece` is in the source. */
|
|
829
|
+
export declare function pieceOffset(piece: PieceBase, index: number): number;
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* A piece of code: a `{$expr}` display, a `{do}` body, a condition, a macro
|
|
833
|
+
* argument of an `expression`, `statements` or `passage` parameter, a string
|
|
834
|
+
* that holds code, or an attribute that holds code (`onclick`).
|
|
835
|
+
*/
|
|
836
|
+
export interface CodePiece extends PieceBase {
|
|
837
|
+
kind: 'code';
|
|
838
|
+
code: string;
|
|
839
|
+
goal: JsGoal;
|
|
840
|
+
/** The markup it is in, for an error: `{print $a +}`. */
|
|
841
|
+
label: string;
|
|
842
|
+
/**
|
|
843
|
+
* Whether it names a passage: a `passage` argument that is an expression
|
|
844
|
+
* (one that is a string literal is a PassagePiece).
|
|
845
|
+
*/
|
|
846
|
+
passage?: boolean;
|
|
847
|
+
/** Whether it is the code in a quoted string, as in `{watch}`. */
|
|
848
|
+
inString?: boolean;
|
|
849
|
+
/** The macro it is the argument of, for a `passage` argument. */
|
|
850
|
+
macro?: string;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** A passage name written out: in a link, or a quoted `passage` argument. */
|
|
854
|
+
export interface PassagePiece extends PieceBase {
|
|
855
|
+
kind: 'passage';
|
|
856
|
+
name: string;
|
|
857
|
+
/** The markup it is in, for an error: `[[Go->Hall]]`. */
|
|
858
|
+
label: string;
|
|
859
|
+
/** How much of the markup is the name, as written (quotes included). */
|
|
860
|
+
length: number;
|
|
861
|
+
/** The macro it is the argument of; `link` for `[[…]]` links. */
|
|
862
|
+
macro: string;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** Macro arguments that don't have their parameters' forms. */
|
|
866
|
+
export interface ArgumentErrorPiece extends PieceBase {
|
|
867
|
+
kind: 'argument-error';
|
|
868
|
+
message: string;
|
|
869
|
+
/** How much of the markup the arguments are. */
|
|
870
|
+
length: number;
|
|
871
|
+
/** The markup it is in, for an error: `{link Go}`. */
|
|
872
|
+
label: string;
|
|
873
|
+
/** The macro whose arguments they are. */
|
|
874
|
+
macro: string;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* A text that may hold markup of its own: a label, an attribute value. The
|
|
879
|
+
* pieces of that markup follow it, `nested`.
|
|
880
|
+
*/
|
|
881
|
+
export interface TextPiece extends PieceBase {
|
|
882
|
+
kind: 'text';
|
|
883
|
+
text: string;
|
|
884
|
+
/** Where it is, for an error: `In the label of {button}: `. */
|
|
885
|
+
where: string;
|
|
886
|
+
/**
|
|
887
|
+
* The tokens of the markup in it, with offsets in the source given (none
|
|
888
|
+
* for a text with no `{`, which has no markup in it).
|
|
889
|
+
*/
|
|
890
|
+
tokens: Token[];
|
|
891
|
+
/**
|
|
892
|
+
* Its malformed markup, in the order a parser reading from left to right
|
|
893
|
+
* meets it, with offsets in the source given.
|
|
894
|
+
*/
|
|
895
|
+
errors: MarkupError[];
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/** What a passage runs and names, and where (see {@link passagePieces}). */
|
|
899
|
+
export type Piece = CodePiece | TextPiece | PassagePiece | ArgumentErrorPiece;
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* What a passage's markup runs and names, and where: the pieces of code
|
|
903
|
+
* (`{$expr}` displays, `{do}` bodies, conditions, macro arguments by their
|
|
904
|
+
* declared parameter types, strings that hold code), the passage names
|
|
905
|
+
* written out (links, `passage` arguments, `{dialog}` bodies), the texts that
|
|
906
|
+
* hold markup of their own (labels, attribute values) and the arguments that
|
|
907
|
+
* don't have their parameters' forms, in source order with offsets (UTF-16)
|
|
908
|
+
* into `source`. The markup in a text follows it, as pieces marked `nested`
|
|
909
|
+
* with offsets into `source` too. Malformed tags are skipped, so it reads
|
|
910
|
+
* half-typed markup. `macros` are those the markup may use (see
|
|
911
|
+
* `builtinMacros`); without them only the code that needs no declaration is
|
|
912
|
+
* found.
|
|
913
|
+
*/
|
|
914
|
+
export declare function passagePieces(
|
|
915
|
+
source: string,
|
|
916
|
+
macros?: Iterable<ToolingMacro>,
|
|
917
|
+
): Piece[];
|
|
918
|
+
|
|
919
|
+
// ---------------------------------------------------------------------------
|
|
920
|
+
// Pairing
|
|
921
|
+
// ---------------------------------------------------------------------------
|
|
922
|
+
|
|
923
|
+
/** What is wrong with the pairing of tags (a subset of {@link MarkupErrorCode}). */
|
|
924
|
+
export type PairingErrorCode =
|
|
925
|
+
| 'unclosed-block'
|
|
926
|
+
| 'mismatched-closer'
|
|
927
|
+
| 'stray-closer'
|
|
928
|
+
| 'misplaced-branch';
|
|
929
|
+
|
|
930
|
+
/** A problem in the pairing of the tokens. */
|
|
931
|
+
export interface PairingError {
|
|
932
|
+
code: PairingErrorCode;
|
|
933
|
+
/** What is wrong, without the position. */
|
|
934
|
+
message: string;
|
|
935
|
+
/** The token it is at: from `start` up to `end` (offsets in the source). */
|
|
936
|
+
start: number;
|
|
937
|
+
end: number;
|
|
938
|
+
/**
|
|
939
|
+
* The offset a parser reading left to right notices it at: where the
|
|
940
|
+
* closer ends, or past the end of the markup for what is left open there.
|
|
941
|
+
*/
|
|
942
|
+
noticedAt: number;
|
|
943
|
+
/** The names involved, by code (see {@link MarkupError.data}). */
|
|
944
|
+
data: Record<string, string>;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/** A branch (`{else}`, `{case}`, …) of a macro, with what follows it. */
|
|
948
|
+
export interface PairedBranch {
|
|
949
|
+
tag: MacroToken;
|
|
950
|
+
children: PairedNode[];
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/** What a macro or element holds, up to its closer. */
|
|
954
|
+
export interface PairedBody {
|
|
955
|
+
children: PairedNode[];
|
|
956
|
+
branches: PairedBranch[];
|
|
957
|
+
/** The closing tag; absent if it is never closed. */
|
|
958
|
+
close?: MacroToken | HtmlToken;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* A node of the tree: a token that stands alone (text, a link, a variable, an
|
|
963
|
+
* expression, a macro without a body, a self-closing element), or the opening
|
|
964
|
+
* tag of a macro or element with its `body`.
|
|
965
|
+
*/
|
|
966
|
+
export interface PairedNode {
|
|
967
|
+
/** The token, or the opening tag of the element. */
|
|
968
|
+
token: Token;
|
|
969
|
+
body?: PairedBody;
|
|
970
|
+
/** From the start of the token to the end of the closer (or of the body). */
|
|
971
|
+
start: number;
|
|
972
|
+
end: number;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
export interface PairMarkupOptions {
|
|
976
|
+
/** Whether a macro takes a body (default: {@link isBlockMacro}). */
|
|
977
|
+
isBlock?(name: string): boolean;
|
|
978
|
+
/**
|
|
979
|
+
* Whether a macro's body is JavaScript, kept verbatim as one text token
|
|
980
|
+
* (default: `{do}`). One that is never closed is a problem at its opening
|
|
981
|
+
* tag, and the markup after it is not its body.
|
|
982
|
+
*/
|
|
983
|
+
isRaw?(name: string): boolean;
|
|
984
|
+
/**
|
|
985
|
+
* The markup the tokens are of: lets a message say at which line and column
|
|
986
|
+
* an element was opened (else it says at which offset).
|
|
987
|
+
*/
|
|
988
|
+
source?: string;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
export interface PairedMarkup {
|
|
992
|
+
nodes: PairedNode[];
|
|
993
|
+
errors: PairingError[];
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Pair the flat `tokens` of markup: which closer closes which opener, which
|
|
998
|
+
* branches belong to which macro, as the runtime reads them (it builds its
|
|
999
|
+
* AST from this). It never throws: a problem is an entry of `errors`, in the
|
|
1000
|
+
* order a parser reading from left to right notices them, and the tree is
|
|
1001
|
+
* recovered around it. A closer closes the innermost open element, or, when
|
|
1002
|
+
* it names one further out, that one, leaving what is open inside it
|
|
1003
|
+
* unclosed.
|
|
1004
|
+
*/
|
|
1005
|
+
export declare function pairMarkup(
|
|
1006
|
+
tokens: readonly Token[],
|
|
1007
|
+
options?: PairMarkupOptions,
|
|
1008
|
+
): PairedMarkup;
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Whether a macro takes a body closed by `{/name}`: the built-in block
|
|
1012
|
+
* macros. Pass the block macros of your story to `pairMarkup` instead when it
|
|
1013
|
+
* defines others (see `ToolingMacro.block`, and `{widget}`).
|
|
1014
|
+
*/
|
|
1015
|
+
export declare function isBlockMacro(name: string): boolean;
|
|
1016
|
+
|
|
1017
|
+
// ---------------------------------------------------------------------------
|
|
1018
|
+
// Declarations
|
|
1019
|
+
// ---------------------------------------------------------------------------
|
|
1020
|
+
|
|
1021
|
+
/** What is wrong with a declaration (see {@link DeclarationError}). */
|
|
1022
|
+
export type DeclarationErrorCode =
|
|
1023
|
+
| 'invalid-declaration'
|
|
1024
|
+
| 'invalid-name'
|
|
1025
|
+
| 'duplicate-declaration'
|
|
1026
|
+
| 'unsupported-value';
|
|
1027
|
+
|
|
1028
|
+
/** A declaration of a `StoryVariables` or `StoryTransients` passage. */
|
|
1029
|
+
export interface Declaration {
|
|
1030
|
+
name: string;
|
|
1031
|
+
/** Where the name is written, without the sigil (UTF-16 offsets). */
|
|
1032
|
+
nameStart: number;
|
|
1033
|
+
nameEnd: number;
|
|
1034
|
+
/** Where the initializer expression is written, without surrounding space. */
|
|
1035
|
+
valueStart: number;
|
|
1036
|
+
valueEnd: number;
|
|
1037
|
+
/**
|
|
1038
|
+
* The shape of the initializer when it is static: a literal, or an array or
|
|
1039
|
+
* object literal of them. Absent when it is not (a call, a reference, an
|
|
1040
|
+
* operator…).
|
|
1041
|
+
*/
|
|
1042
|
+
schema?: FieldSchema;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/** An invalid line of a `StoryVariables` or `StoryTransients` passage. */
|
|
1046
|
+
export interface DeclarationError {
|
|
1047
|
+
code: DeclarationErrorCode;
|
|
1048
|
+
/** From `offset` up to `end` (UTF-16 offsets into the content). */
|
|
1049
|
+
offset: number;
|
|
1050
|
+
end: number;
|
|
1051
|
+
/** What is wrong, as `parseStoryVariables` throws it after the passage name. */
|
|
1052
|
+
message: string;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* Read the declarations of a `StoryVariables` (`$name = value`) or
|
|
1057
|
+
* `StoryTransients` (`%name = value`, pass `sigil: '%'`) passage, without
|
|
1058
|
+
* evaluating them and without throwing: every valid declaration, and every
|
|
1059
|
+
* invalid line as an error. It is the grammar `parseStoryVariables` reads
|
|
1060
|
+
* with.
|
|
1061
|
+
*
|
|
1062
|
+
* What is static: `number`, `string` (no template with `${}`), `boolean` and
|
|
1063
|
+
* `null` literals (also signed numbers), arrays of them, and objects with
|
|
1064
|
+
* identifier, string or number keys and static values. The `schema` of an
|
|
1065
|
+
* array is `{ type: 'array' }`, as `parseStoryVariables` makes it. Anything
|
|
1066
|
+
* else (`Math.PI`, `new Date()`, `1 + 2`, a reference, a spread, a computed
|
|
1067
|
+
* key) has no `schema`, and is no error: only a value that no variable can
|
|
1068
|
+
* hold is, a function (`function`, `=>`, `class`) or `undefined`.
|
|
1069
|
+
*/
|
|
1070
|
+
export declare function parseDeclarations(
|
|
1071
|
+
content: string,
|
|
1072
|
+
sigil?: '$' | '%',
|
|
1073
|
+
): { declarations: Declaration[]; errors: DeclarationError[] };
|