@op-ted/efx-syntax-highlighter 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +15 -10
  2. package/dist/efx1/efx1Highlighting.js +45 -0
  3. package/dist/efx1/generated/lexer.js +1772 -0
  4. package/dist/efx1/lexer.js +26 -0
  5. package/dist/efx1/parser.js +23 -0
  6. package/dist/efx1/parser.terms.js +118 -0
  7. package/dist/{completion → efx2/completion}/CompletionItems.js +1 -1
  8. package/dist/{completion → efx2/completion}/EfxCompletions.js +2 -2
  9. package/dist/{completion → efx2/completion}/ExpectedTokens.js +14 -14
  10. package/dist/{efxHighlighting.js → efx2/efx2Highlighting.js} +5 -5
  11. package/dist/{generated → efx2/generated}/completions.js +1 -1
  12. package/dist/efx2/generated/lexer.js +3880 -0
  13. package/dist/efx2/generated/words.js +213 -0
  14. package/dist/efx2/lexer.js +49 -0
  15. package/dist/efx2/parser.js +23 -0
  16. package/dist/efx2/parser.terms.js +318 -0
  17. package/dist/efxLanguage.js +66 -17
  18. package/dist/lexer/AntlrLexer.js +427 -0
  19. package/dist/lexer/EditorLexer.js +134 -0
  20. package/dist/lexer/EditorParser.js +69 -0
  21. package/dist/lexer/Lexing.js +238 -0
  22. package/dist/lexer/ParseRanges.js +22 -0
  23. package/package.json +5 -4
  24. package/dist/efx-parser.js +0 -26
  25. package/dist/efx-parser.terms.js +0 -319
  26. package/dist/generated/words.js +0 -215
  27. package/dist/lexer/ExpressionMode.js +0 -44
  28. package/dist/lexer/FormatMode.js +0 -31
  29. package/dist/lexer/IdentifierMode.js +0 -12
  30. package/dist/lexer/LabelMode.js +0 -27
  31. package/dist/lexer/LexerContext.js +0 -76
  32. package/dist/lexer/LexerInput.js +0 -188
  33. package/dist/lexer/LexerMode.js +0 -24
  34. package/dist/lexer/LexerModes.js +0 -50
  35. package/dist/lexer/LineMode.js +0 -85
  36. package/dist/lexer/ReservedWords.js +0 -75
  37. package/dist/lexer/StageHeaderMode.js +0 -12
  38. package/dist/lexer/TemplateMode.js +0 -77
  39. package/dist/lexer/TemplateStartMode.js +0 -25
  40. /package/dist/{completion → efx2/completion}/Variables.js +0 -0
@@ -1,76 +0,0 @@
1
- // What the parse state cannot tell about the lexer's mode, kept as the parser goes: where the last
2
- // token leaves the lexer, in the places where the tokens the parser can take do not say it.
3
- // - LINE_START: after a `;`, a line end, a header or an include, the lexer is at the start of a line
4
- // (DEFAULT or SKIP_WHITESPACE). After a rule of a rules file the parser can take the same tokens,
5
- // but the lexer is still in EXPRESSION mode, until a `;`.
6
- // - TEMPLATE_END: after a template is closed, the lexer is still in TEMPLATE mode, and reads a line
7
- // end as the CRLF that ends an EFX-1 line (TemplateMode.js).
8
- // - TEMPLATE_SEMICOLON: after the `;` that ends a template, the lexer is at the start of a line, as
9
- // at LINE_START, but the `;` takes the blanks and the comment after it on its line (EndTemplate),
10
- // so that comment needs no line end (LineMode.js).
11
- // - BEFORE_FIRST_WORD: after the indentation or the outline number of a line, before its first word.
12
- // - INVOKE_LINE: in a line that starts with INVOKE. At the start of a line the lexer reads INVOKE in
13
- // DEFAULT mode, which pushes EXPRESSION mode only (Invoke), so the line's `;` takes the lexer back
14
- // to DEFAULT mode, where any other `;` takes it to SKIP_WHITESPACE.
15
- // - INVOKE_SEMICOLON: after that `;`, the lexer is at the start of a line in DEFAULT mode
16
- // (LineMode.js).
17
- // The skip tokens leave it as it is, and the tokens of a line that starts with INVOKE keep
18
- // INVOKE_LINE until its `;` or line end; any other token sets none of these values.
19
-
20
- import { ContextTracker } from '@lezer/lr';
21
- import * as terms from '../efx-parser.terms.js';
22
-
23
- export const LINE_START = 1;
24
- export const TEMPLATE_END = 2;
25
- export const TEMPLATE_SEMICOLON = 3;
26
- export const BEFORE_FIRST_WORD = 4;
27
- export const INVOKE_LINE = 5;
28
- export const INVOKE_SEMICOLON = 6;
29
-
30
- // The tokens after which the lexer is at the start of a line.
31
- const LINE_ENDS = new Set([terms.Semicolon, terms.CRLF, terms.IncludePath, terms.StageHeaderEnd,
32
- terms.SummarySectionHeader, terms.NavigationSectionHeader]);
33
-
34
- // The rules that end with a template. The template's own nodes are its fragments, which the
35
- // grammar defines inside templateFragment, so they have no term of their own to look for.
36
- const ENDS_WITH_TEMPLATE = new Set([terms.DisplayTemplate, terms.OtherwiseDisplayTemplate]);
37
-
38
- // The tokens that can come before the first word of a line: its indentation and outline number.
39
- const LINE_PREFIXES = new Set([terms.Tabs, terms.Spaces, terms.MixedIndent, terms.OutlineNumber]);
40
-
41
- const SKIPPED = new Set([terms.space, terms.Comment]);
42
-
43
- /** Whether the lexer is at the start of a line. */
44
- export function isLineStart(context) {
45
- return context === LINE_START || context === TEMPLATE_SEMICOLON || context === INVOKE_SEMICOLON;
46
- }
47
-
48
- function shift(context, term) {
49
- if (SKIPPED.has(term)) {
50
- return context;
51
- }
52
- const lineStart = isLineStart(context) || context === BEFORE_FIRST_WORD;
53
- if (lineStart && LINE_PREFIXES.has(term)) {
54
- return BEFORE_FIRST_WORD;
55
- }
56
- if (lineStart && term === terms.Invoke) {
57
- return INVOKE_LINE;
58
- }
59
- if (term === terms.Semicolon && context === TEMPLATE_END) {
60
- return TEMPLATE_SEMICOLON;
61
- }
62
- if (term === terms.Semicolon && context === INVOKE_LINE) {
63
- return INVOKE_SEMICOLON;
64
- }
65
- if (LINE_ENDS.has(term)) {
66
- return LINE_START;
67
- }
68
- return context === INVOKE_LINE ? INVOKE_LINE : 0;
69
- }
70
-
71
- export const lexerContext = new ContextTracker({
72
- start: LINE_START,
73
- shift,
74
- reduce: (context, term) => (ENDS_WITH_TEMPLATE.has(term) ? TEMPLATE_END : context),
75
- hash: (context) => context,
76
- });
@@ -1,188 +0,0 @@
1
- // The text at the tokenizer's position, read as EfxLexer.g4 reads it: blanks, line ends, comments
2
- // and the shapes of its tokens. Offsets count from the position; a negative one looks back.
3
-
4
- const TAB = 9;
5
- const LF = 10;
6
- const FF = 12;
7
- const CR = 13;
8
- const SPACE = 32;
9
- const HYPHEN = 45;
10
- const DOT = 46;
11
- const SLASH = 47;
12
- const SEMICOLON = 59;
13
- const AT = 64;
14
- const BACKSLASH = 92;
15
-
16
- // The text that starts an IncludeDirective.
17
- const INCLUDE = '#include';
18
-
19
- // The characters FreeText cannot hold: ~[\r\n\f\t #$@&;\\]
20
- const NOT_FREE_TEXT = new Set(Array.from('\r\n\f\t #$@&;\\', (c) => c.charCodeAt(0)));
21
-
22
- export class LexerInput {
23
- #input;
24
-
25
- constructor(input) {
26
- this.#input = input;
27
- }
28
-
29
- /** The text of a length at an offset. */
30
- text(offset, length) {
31
- const from = this.#input.pos + offset;
32
- return this.#input.read(from, from + length);
33
- }
34
-
35
- /** Ends the token: the given one, of a length from the position. */
36
- accept(token, length) {
37
- this.#input.acceptToken(token, length);
38
- }
39
-
40
- /** Ends the token if its length is not 0; whether it did. */
41
- take(token, length) {
42
- if (length > 0) {
43
- this.accept(token, length);
44
- }
45
- return length > 0;
46
- }
47
-
48
- /** TAB | SPACE */
49
- isBlank(offset) {
50
- const c = this.#input.peek(offset);
51
- return c === TAB || c === SPACE;
52
- }
53
-
54
- /** Whether the text ends at an offset (EOF). */
55
- isEnd(offset) {
56
- return this.#input.peek(offset) < 0;
57
- }
58
-
59
- /** EOL: '\r'? '\n' | '\r' | '\f' */
60
- isLineEnd(offset) {
61
- const c = this.#input.peek(offset);
62
- return c === LF || c === CR || c === FF;
63
- }
64
-
65
- isSemicolon(offset) {
66
- return this.#input.peek(offset) === SEMICOLON;
67
- }
68
-
69
- isSlash(offset) {
70
- return this.#input.peek(offset) === SLASH;
71
- }
72
-
73
- isAt(offset) {
74
- return this.#input.peek(offset) === AT;
75
- }
76
-
77
- isBackslash(offset) {
78
- return this.#input.peek(offset) === BACKSLASH;
79
- }
80
-
81
- /** COMMENT: '//' ~[\r\n\f]* */
82
- isComment(offset) {
83
- return this.#input.peek(offset) === SLASH && this.#input.peek(offset + 1) === SLASH;
84
- }
85
-
86
- /** Whether a `;` comes before an offset, with only blanks between them. */
87
- followsSemicolon(offset) {
88
- let before = offset - 1;
89
- while (this.isBlank(before)) {
90
- before -= 1;
91
- }
92
- return this.isSemicolon(before);
93
- }
94
-
95
- /** The number of blanks at an offset. */
96
- blanks(offset) {
97
- return this.#count(offset, (o) => this.isBlank(o));
98
- }
99
-
100
- /** The number of line-end characters at an offset. */
101
- lineEnds(offset) {
102
- return this.#count(offset, (o) => this.isLineEnd(o));
103
- }
104
-
105
- /** The number of blanks and line-end characters at an offset. */
106
- whitespace(offset) {
107
- return this.#count(offset, (o) => this.isBlank(o) || this.isLineEnd(o));
108
- }
109
-
110
- /** The length of the whitespace and comments at an offset, which EXPRESSION mode hides (WS, ExpressionComment). */
111
- hiddenLength(offset) {
112
- const length = this.whitespace(offset) || this.commentLength(offset);
113
- return length === 0 ? 0 : length + this.hiddenLength(offset + length);
114
- }
115
-
116
- /** The length of the comment at an offset, up to the line end. */
117
- commentLength(offset) {
118
- return this.isComment(offset) ? this.#count(offset, (o) => this.#input.peek(o) >= 0 && !this.isLineEnd(o)) : 0;
119
- }
120
-
121
- /** The length of the Identifier at an offset: LETTER ('-'? (LETTER | DIGIT)+)* */
122
- identifierLength(offset) {
123
- if (!LexerInput.#isLetter(this.#input.peek(offset))) {
124
- return 0;
125
- }
126
- let length = 1;
127
- for (;;) {
128
- const hyphen = this.#input.peek(offset + length) === HYPHEN ? 1 : 0;
129
- const part = this.#count(offset + length + hyphen, (o) => LexerInput.#isLetterOrDigit(this.#input.peek(o)));
130
- if (part === 0) {
131
- return length;
132
- }
133
- length += hyphen + part;
134
- }
135
- }
136
-
137
- /** The length of the OtherAssetId at the position: [a-z]+ ([-.] [a-z0-9]+)* */
138
- assetIdLength() {
139
- const isPart = (o) => LexerInput.#isLower(this.#input.peek(o)) || LexerInput.#isDigit(this.#input.peek(o));
140
- let length = this.#count(0, (o) => LexerInput.#isLower(this.#input.peek(o)));
141
- while (length > 0) {
142
- const separator = this.#input.peek(length);
143
- const part = separator === HYPHEN || separator === DOT ? this.#count(length + 1, isPart) : 0;
144
- if (part === 0) {
145
- return length;
146
- }
147
- length += 1 + part;
148
- }
149
- return length;
150
- }
151
-
152
- /** The length of the IncludeDirective at the position: '#include' (TAB | SPACE)+ */
153
- includeDirectiveLength() {
154
- const blanks = this.text(0, INCLUDE.length) === INCLUDE ? this.blanks(INCLUDE.length) : 0;
155
- return blanks > 0 ? INCLUDE.length + blanks : 0;
156
- }
157
-
158
- /** The length of the FreeText at the position. */
159
- freeTextLength() {
160
- return this.#count(0, (o) => this.#input.peek(o) >= 0 && !NOT_FREE_TEXT.has(this.#input.peek(o)));
161
- }
162
-
163
- #count(offset, test) {
164
- let end = offset;
165
- while (test(end)) {
166
- end += 1;
167
- }
168
- return end - offset;
169
- }
170
-
171
- /** LETTER: [a-zA-Z_] */
172
- static #isLetter(c) {
173
- return (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 95;
174
- }
175
-
176
- static #isLower(c) {
177
- return c >= 97 && c <= 122;
178
- }
179
-
180
- /** DIGIT: [0-9] */
181
- static #isDigit(c) {
182
- return c >= 48 && c <= 57;
183
- }
184
-
185
- static #isLetterOrDigit(c) {
186
- return LexerInput.#isLetter(c) || LexerInput.#isDigit(c);
187
- }
188
- }
@@ -1,24 +0,0 @@
1
- // A mode of EfxLexer.g4 as the editor finds it. The tokens the parser can take tell whether the
2
- // lexer would be in the mode there; the mode then hides what the lexer hides in it, as the grammar's
3
- // skip tokens `space` and `Comment`, and reads the tokens whose reading depends on it. The tokens
4
- // that read the same in every mode are the grammar's own. Each subclass is named after its mode.
5
-
6
- export class LexerMode {
7
- /** Whether the lexer would be in this mode where the parser stands. */
8
- isActive(stack) {
9
- throw new Error(`${this.constructor.name} does not say where the lexer is in it`);
10
- }
11
-
12
- /** Hides the text the mode hides at the position, if any; whether there was some. */
13
- hide(input, stack) {
14
- return false;
15
- }
16
-
17
- /** Reads the token the mode reads at the position, if it reads one there. */
18
- read(input, stack) {}
19
-
20
- /** The words the mode reads as tokens of the grammar, each with its token: [word, token] pairs. */
21
- words() {
22
- return [];
23
- }
24
- }
@@ -1,50 +0,0 @@
1
- // The tokens whose reading depends on the mode of EfxLexer.g4. The mode is found from the parse
2
- // state, and the input is read as the lexer reads it in that mode: first the text the mode hides,
3
- // then the token the mode reads there, if any; where it reads none, the grammar's own tokens are
4
- // read.
5
- //
6
- // The modes are tried in this order, and the first the parser is in is the one:
7
- // - where a template can go on, the lexer is in TEMPLATE mode whatever else the parser can take
8
- // (the tokens that end a template are read in that mode too), so template text comes first, then
9
- // the start of a template, which takes text but nothing that ends it;
10
- // - label blocks, the name after a prefix and format options are each found by tokens that no
11
- // other mode takes;
12
- // - the name and end of a stage header are tokens that no other mode takes; the mode hides
13
- // nothing, so the blanks before the end stay with it;
14
- // - the start of a line takes words that an expression also has, so DEFAULT comes before
15
- // EXPRESSION, which is the rest. Where the parser has just read a `;`, a line end, a header or an
16
- // include, the lexer is at the start of a line even when the parser could take more of a rule
17
- // (LexerContext.js).
18
- //
19
- // The completion source asks the same questions where the cursor is: the mode there, and the words
20
- // it reads (src/completion/ExpectedTokens.js).
21
-
22
- import { ExternalTokenizer } from '@lezer/lr';
23
- import { ExpressionMode } from './ExpressionMode.js';
24
- import { FormatMode } from './FormatMode.js';
25
- import { IdentifierMode } from './IdentifierMode.js';
26
- import { LabelMode } from './LabelMode.js';
27
- import { LexerInput } from './LexerInput.js';
28
- import { LineMode } from './LineMode.js';
29
- import { StageHeaderMode } from './StageHeaderMode.js';
30
- import { TemplateMode } from './TemplateMode.js';
31
- import { TemplateStartMode } from './TemplateStartMode.js';
32
-
33
- const MODES = [new TemplateMode(), new TemplateStartMode(), new LabelMode(), new IdentifierMode(), new FormatMode(),
34
- new StageHeaderMode(), new LineMode(), new ExpressionMode()];
35
-
36
- /** The mode the lexer is in where the parser stands. */
37
- export function lexerModeAt(stack) {
38
- return MODES.find((mode) => mode.isActive(stack));
39
- }
40
-
41
- /** Reads the text at the input's position as the lexer reads it in the mode it is in there. */
42
- export function readToken(input, stack) {
43
- const text = new LexerInput(input);
44
- const mode = lexerModeAt(stack);
45
- if (!mode.hide(text, stack)) {
46
- mode.read(text, stack);
47
- }
48
- }
49
-
50
- export const lexerModes = new ExternalTokenizer(readToken, { contextual: true });
@@ -1,85 +0,0 @@
1
- // DEFAULT mode, between lines and at their start. Blanks indent a line (the grammar's own Tabs,
2
- // Spaces and MixedIndent) unless a comment, a line end or the end of the text follows them
3
- // (EmptyOrCommentLine). A `;` takes the lexer to SKIP_WHITESPACE, where it skips one run of
4
- // whitespace: the blanks after the `;` are hidden, and so are line ends. The `;` of a line that
5
- // starts with INVOKE takes it back to DEFAULT mode instead (LexerContext.js), so the blanks after
6
- // it indent the next line. A comment is hidden before a line end, or before the end of the text in
7
- // DEFAULT mode (EmptyOrCommentLine) but not in SKIP_WHITESPACE (SkippedComment), and on the line of
8
- // a template's `;`, which takes it (EndTemplate). A word is one of the line words; DISPLAY takes
9
- // the whitespace after it, which must be there. `#include` and the blanks after it are an
10
- // IncludeDirective, which only this mode reads. Where the lexer is still in SKIP_WHITESPACE,
11
- // directly after a `;` or after a template's `;` and the blanks it takes, it reads only the words
12
- // of that mode, and no IncludeDirective.
13
-
14
- import * as terms from '../efx-parser.terms.js';
15
- import { lineTokens, skipWhitespaceTokens } from '../generated/words.js';
16
- import { INVOKE_SEMICOLON, isLineStart, TEMPLATE_SEMICOLON } from './LexerContext.js';
17
- import { LexerMode } from './LexerMode.js';
18
- import { ReservedWords } from './ReservedWords.js';
19
-
20
- export class LineMode extends LexerMode {
21
- static #words = new ReservedWords(lineTokens);
22
- static #skipWhitespaceWords = new ReservedWords(skipWhitespaceTokens);
23
-
24
- /** At the start of a line (LexerContext.js), or where the parser can take a line's indentation. */
25
- isActive(stack) {
26
- return isLineStart(stack.context) || stack.canShift(terms.Tabs);
27
- }
28
-
29
- hide(input, stack) {
30
- if (input.isComment(0)) {
31
- const length = input.commentLength(0);
32
- const lastLine = input.isEnd(length) && !LineMode.#skipsWhitespace(input, stack);
33
- const afterTemplate = stack.context === TEMPLATE_SEMICOLON && input.followsSemicolon(0);
34
- return (input.isLineEnd(length) || lastLine || afterTemplate) && input.take(terms.Comment, length);
35
- }
36
- const blanks = input.blanks(0);
37
- const hidden = LineMode.#skipsWhitespace(input, stack) || input.isComment(blanks) || input.isLineEnd(blanks)
38
- || input.isEnd(blanks);
39
- if (blanks > 0 && hidden) {
40
- return input.take(terms.space, blanks);
41
- }
42
- return input.take(terms.space, input.lineEnds(0));
43
- }
44
-
45
- read(input, stack) {
46
- if (LineMode.#skipsWhitespace(input, stack)) {
47
- const word = LineMode.#skipWhitespaceWords.wordAt(input, 0);
48
- if (word !== undefined) {
49
- input.accept(word.token, word.length);
50
- }
51
- return;
52
- }
53
- if (input.take(terms.IncludeDirective, input.includeDirectiveLength())) {
54
- return;
55
- }
56
- const word = LineMode.#words.wordAt(input, 0);
57
- if (word === undefined) {
58
- return;
59
- }
60
- if (word.token !== terms.Display) {
61
- input.accept(word.token, word.length);
62
- return;
63
- }
64
- const whitespace = input.whitespace(word.length);
65
- if (whitespace > 0) {
66
- input.accept(word.token, word.length + whitespace);
67
- }
68
- }
69
-
70
- words() {
71
- return LineMode.#words.entries();
72
- }
73
-
74
- /**
75
- * Whether the lexer is still in SKIP_WHITESPACE at the position: directly after a `;`, or after a
76
- * template's `;` and the blanks it takes (EndTemplate), but not after the `;` of a line that starts
77
- * with INVOKE.
78
- */
79
- static #skipsWhitespace(input, stack) {
80
- if (stack.context === TEMPLATE_SEMICOLON) {
81
- return input.followsSemicolon(0);
82
- }
83
- return stack.context !== INVOKE_SEMICOLON && input.isSemicolon(-1);
84
- }
85
- }
@@ -1,75 +0,0 @@
1
- // The words the lexer reserves in one of its modes, each with the token it makes of it: a table of
2
- // src/generated/words.js, its tokens given as the grammar's terms. A word whose token no rule of the
3
- // grammar takes has no term: the lexer's `value` in template text. The table also lists the phrases
4
- // the lexer reads as one token, a word, whitespace and a second word (OTHERWISE DISPLAY), written
5
- // with one blank between the two words.
6
-
7
- import * as terms from '../efx-parser.terms.js';
8
-
9
- export class ReservedWords {
10
- #words = new Map();
11
- // For each word that starts a phrase, the second words of its phrases with their tokens.
12
- #phrases = new Map();
13
-
14
- constructor(table) {
15
- for (const [text, token] of table) {
16
- const [first, second] = text.split(' ');
17
- if (second === undefined) {
18
- this.#words.set(first, terms[token]);
19
- } else {
20
- const phrases = this.#phrases.get(first) ?? [];
21
- this.#phrases.set(first, [...phrases, { second, token: terms[token] }]);
22
- }
23
- }
24
- }
25
-
26
- /** Each word and phrase whose token has a term, with that term: [text, token] pairs. */
27
- entries() {
28
- const phrases = [...this.#phrases].flatMap(([first, seconds]) =>
29
- seconds.map(({ second, token }) => [`${first} ${second}`, token]));
30
- return [...this.#words, ...phrases].filter(([, token]) => token !== undefined);
31
- }
32
-
33
- /**
34
- * The token of the reserved word that is the whole Identifier-shaped word at an offset, or of the
35
- * phrase it starts there, and its length; undefined when that word is not reserved or its token
36
- * has no term.
37
- */
38
- wordAt(input, offset) {
39
- const word = input.text(offset, input.identifierLength(offset));
40
- const read = this.#read(input, offset, word);
41
- return read.token === undefined ? undefined : read;
42
- }
43
-
44
- /**
45
- * The longest reserved word the text at an offset starts with, whole word or not, as template text
46
- * reads it, or the phrase it starts there, with its token (undefined when it has no term) and its
47
- * length; undefined when there is none.
48
- */
49
- prefixAt(input, offset) {
50
- const starts = [...this.#words.keys()].filter((candidate) => input.text(offset, candidate.length) === candidate);
51
- const word = ReservedWords.#longest(starts);
52
- return word === undefined ? undefined : this.#read(input, offset, word);
53
- }
54
-
55
- /**
56
- * A reserved word at an offset, and the phrase it starts if one follows: whitespace, then a second
57
- * word, which the lexer reads as the start of the text there, whole word or not.
58
- */
59
- #read(input, offset, word) {
60
- const gap = input.whitespace(offset + word.length);
61
- const start = offset + word.length + gap;
62
- const phrases = gap === 0 ? [] : this.#phrases.get(word) ?? [];
63
- const seconds = phrases.filter(({ second }) => input.text(start, second.length) === second);
64
- const phrase = ReservedWords.#longest(seconds, ({ second }) => second);
65
- return phrase === undefined
66
- ? { token: this.#words.get(word), length: word.length }
67
- : { token: phrase.token, length: word.length + gap + phrase.second.length };
68
- }
69
-
70
- /** The candidate with the longest text, the first of them if several; undefined when there is none. */
71
- static #longest(candidates, textOf = (candidate) => candidate) {
72
- const longer = (longest, candidate) => (textOf(candidate).length > textOf(longest).length ? candidate : longest);
73
- return candidates.length === 0 ? undefined : candidates.reduce(longer);
74
- }
75
- }
@@ -1,12 +0,0 @@
1
- // STAGE_HEADER mode, in a rules file's stage header: the stage's name and the end of the header
2
- // are the grammar's own StageIdentifier and StageHeaderEnd. Nothing is hidden.
3
-
4
- import * as terms from '../efx-parser.terms.js';
5
- import { LexerMode } from './LexerMode.js';
6
-
7
- export class StageHeaderMode extends LexerMode {
8
- /** Where the parser can take a stage's name or the end of its header. */
9
- isActive(stack) {
10
- return stack.canShift(terms.StageIdentifier) || stack.canShift(terms.StageHeaderEnd);
11
- }
12
- }
@@ -1,77 +0,0 @@
1
- // TEMPLATE mode: template text, where whitespace is text. Hidden: a comment with the blanks before
2
- // it (EndOfLineComment), and a backslash with the line end after it, which joins two lines
3
- // (LineJoining). A template ends with `;`, which takes the blanks before it (EndTemplate), with a
4
- // line end (CRLF, one of the grammar's own tokens), or with WHEN, OTHERWISE or INVOKE, which take
5
- // the whitespace and line ends before them.
6
-
7
- import * as terms from '../efx-parser.terms.js';
8
- import { templateTokens } from '../generated/words.js';
9
- import { TEMPLATE_END } from './LexerContext.js';
10
- import { LexerMode } from './LexerMode.js';
11
- import { ReservedWords } from './ReservedWords.js';
12
-
13
- // The tokens that can end a template.
14
- const TEMPLATE_ENDS = [terms.Semicolon, terms.CRLF, terms.When, terms.Otherwise];
15
-
16
- export class TemplateMode extends LexerMode {
17
- static #words = new ReservedWords(templateTokens);
18
-
19
- /**
20
- * Where the parser can take text and a token that ends a template, or the line end of an EFX-1
21
- * line, or has just closed a template, after which the lexer is still in TEMPLATE mode
22
- * (LexerContext.js).
23
- */
24
- isActive(stack) {
25
- return stack.context === TEMPLATE_END || stack.canShift(terms.CRLF)
26
- || (stack.canShift(terms.FreeText) && TEMPLATE_ENDS.some((token) => stack.canShift(token)));
27
- }
28
-
29
- hide(input) {
30
- if (input.isComment(0)) {
31
- return input.take(terms.Comment, input.commentLength(0));
32
- }
33
- const blanks = input.blanks(0);
34
- if (blanks > 0 && input.isComment(blanks)) {
35
- return input.take(terms.space, blanks);
36
- }
37
- return input.isBackslash(0) && input.take(terms.space, TemplateMode.#lineJoiningLength(input));
38
- }
39
-
40
- read(input) {
41
- const blanks = input.blanks(0);
42
- if (input.isSemicolon(blanks)) {
43
- input.accept(terms.Semicolon, blanks + 1);
44
- return;
45
- }
46
- const whitespace = input.whitespace(0);
47
- const word = TemplateMode.#words.prefixAt(input, whitespace);
48
- if (whitespace > 0) {
49
- // Only the words that end a template take the whitespace before them; any other whitespace
50
- // is the grammar's own Whitespace, NewLine or the start of a link.
51
- if (word?.token !== undefined) {
52
- input.accept(word.token, whitespace + word.length);
53
- }
54
- return;
55
- }
56
- const text = input.freeTextLength();
57
- if (word === undefined || word.length < text) {
58
- input.take(terms.FreeText, text);
59
- } else if (word.token !== undefined) {
60
- // A reserved word as long as the text around it: the lexer lists its rule before FreeText.
61
- input.accept(word.token, word.length);
62
- }
63
- // Otherwise the lexer's `value`, which no rule takes: nothing is read, and the parser reports
64
- // the error ANTLR reports.
65
- }
66
-
67
- words() {
68
- return TemplateMode.#words.entries();
69
- }
70
-
71
- /** LineJoining: '\\' (TAB | SPACE)* EOL+ (TAB | SPACE)*, or 0 when no line end follows. */
72
- static #lineJoiningLength(input) {
73
- const beforeEnd = 1 + input.blanks(1);
74
- const lineEnds = input.lineEnds(beforeEnd);
75
- return lineEnds === 0 ? 0 : beforeEnd + lineEnds + input.blanks(beforeEnd + lineEnds);
76
- }
77
- }
@@ -1,25 +0,0 @@
1
- // SKIP_WHITESPACE before a template: after DISPLAY, OTHERWISE or the context of an EFX-1 line, the
2
- // lexer skips one run of whitespace, blanks or a comment with the line ends after them
3
- // (SkippedWhitespace, SkippedComment), then reads the template in TEMPLATE mode.
4
-
5
- import * as terms from '../efx-parser.terms.js';
6
- import { TemplateMode } from './TemplateMode.js';
7
-
8
- export class TemplateStartMode extends TemplateMode {
9
- /** Where the parser can start a template's text. TemplateMode is tried first: the text can also end it there. */
10
- isActive(stack) {
11
- return stack.canShift(terms.FreeText);
12
- }
13
-
14
- hide(input) {
15
- if (input.isBlank(-1) || input.isLineEnd(-1)) {
16
- // The run is behind: what follows is template text.
17
- return super.hide(input);
18
- }
19
- if (input.isComment(0)) {
20
- return input.take(terms.Comment, input.commentLength(0));
21
- }
22
- const blanks = input.blanks(0);
23
- return input.take(terms.space, blanks + input.lineEnds(blanks));
24
- }
25
- }