@op-ted/efx-syntax-highlighter 0.2.1 → 0.3.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.
- package/README.md +58 -22
- package/dist/completion/CompletionItems.js +51 -0
- package/dist/completion/EfxCompletions.js +86 -0
- package/dist/completion/ExpectedTokens.js +64 -0
- package/dist/completion/Variables.js +60 -0
- package/dist/efx-parser.js +20 -12
- package/dist/efx-parser.terms.js +317 -45
- package/dist/efxHighlighting.js +82 -0
- package/dist/efxLanguage.js +26 -9
- package/dist/efxSymbols.js +12 -0
- package/dist/efxTheme.js +22 -80
- package/dist/generated/completions.js +2318 -0
- package/dist/generated/words.js +215 -0
- package/dist/index.js +5 -3
- package/dist/lexer/ExpressionMode.js +44 -0
- package/dist/lexer/FormatMode.js +31 -0
- package/dist/lexer/IdentifierMode.js +12 -0
- package/dist/lexer/LabelMode.js +27 -0
- package/dist/lexer/LexerContext.js +76 -0
- package/dist/lexer/LexerInput.js +188 -0
- package/dist/lexer/LexerMode.js +24 -0
- package/dist/lexer/LexerModes.js +50 -0
- package/dist/lexer/LineMode.js +85 -0
- package/dist/lexer/ReservedWords.js +75 -0
- package/dist/lexer/StageHeaderMode.js +12 -0
- package/dist/lexer/TemplateMode.js +77 -0
- package/dist/lexer/TemplateStartMode.js +25 -0
- package/package.json +18 -8
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@op-ted/efx-syntax-highlighter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Lezer-based parser and CodeMirror 6 language support for EFX syntax highlighting.",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Lezer-based parser and CodeMirror 6 language support for EFX: syntax highlighting and completions.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./dist/index.js"
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
10
|
"dist",
|
|
11
|
-
"!dist/**/*.map"
|
|
12
|
-
"!dist/demo.js",
|
|
13
|
-
"!dist/highlight.js"
|
|
11
|
+
"!dist/**/*.map"
|
|
14
12
|
],
|
|
15
13
|
"scripts": {
|
|
16
14
|
"build": "node scripts/build.js",
|
|
17
|
-
"test": "node test/
|
|
15
|
+
"test": "node test/package.js && node test/trees.js && node test/conformance.js && node test/styles.js && node test/completions.js",
|
|
16
|
+
"sync": "node scripts/sync.js",
|
|
17
|
+
"demo": "node scripts/demo.js",
|
|
18
|
+
"site": "node scripts/site.js"
|
|
18
19
|
},
|
|
19
20
|
"keywords": [
|
|
20
21
|
"efx",
|
|
@@ -42,12 +43,21 @@
|
|
|
42
43
|
"provenance": true
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
46
|
+
"@codemirror/autocomplete": "^6.20.4",
|
|
45
47
|
"@codemirror/language": "^6.6.0",
|
|
46
48
|
"@codemirror/state": "^6.2.0",
|
|
47
49
|
"@codemirror/view": "^6.8.0",
|
|
48
|
-
"@lezer/common": "^1.0.0"
|
|
50
|
+
"@lezer/common": "^1.0.0",
|
|
51
|
+
"@lezer/highlight": "^1.2.1",
|
|
52
|
+
"@lezer/lr": "^1.4.12"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
51
|
-
"@lezer/generator": "^1.3.0"
|
|
55
|
+
"@lezer/generator": "^1.3.0",
|
|
56
|
+
"antlr-ng": "1.0.10",
|
|
57
|
+
"antlr4ng": "3.0.16"
|
|
58
|
+
},
|
|
59
|
+
"config": {
|
|
60
|
+
"sdk": "OP-TED/eForms-SDK@b43466c004b0865a979c9ffd2cc84781bf081fe0",
|
|
61
|
+
"toolkit": "OP-TED/efx-toolkit-java@1fa3745e5fe2d04acd7fcb337702f328f432a6fb"
|
|
52
62
|
}
|
|
53
63
|
}
|