codemirror-lang-glimmer 0.0.0-beta.1

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/src/index.ts ADDED
@@ -0,0 +1,154 @@
1
+ import { html, htmlCompletionSource } from '@codemirror/lang-html';
2
+ import { foldNodeProp, indentNodeProp, LanguageSupport, LRLanguage } from '@codemirror/language';
3
+ import { parseMixed } from '@lezer/common';
4
+ import { parser as htmlParser } from '@lezer/html';
5
+
6
+ import { parser } from './syntax.grammar';
7
+
8
+ import type { SyntaxNode } from '@lezer/common';
9
+
10
+ export const glimmerParser = parser;
11
+
12
+ export const glimmerLanguage = LRLanguage.define({
13
+ parser: parser.configure({
14
+ wrap: parseMixed((node) => {
15
+ let { name } = node.type;
16
+
17
+ switch (name) {
18
+ case 'Template':
19
+ return null;
20
+ case 'Comment':
21
+ case 'MoustacheExpression':
22
+ case 'NamedBlock':
23
+ case 'Splattributes':
24
+ case 'Argument':
25
+ return { parser };
26
+ }
27
+
28
+ return { parser: htmlParser };
29
+ }),
30
+
31
+ props: [
32
+ indentNodeProp.add({
33
+ Element: (context) => {
34
+ let after = /^(\s*)(<\/)?/.exec(context.textAfter);
35
+
36
+ if (context.node.to <= context.pos + after[0].length) return context.continue();
37
+
38
+ return context.lineIndent(context.node.from) + (after[2] ? 0 : context.unit);
39
+ },
40
+
41
+ Block: (context) => {
42
+ const node = context.node;
43
+ const text = context.textAfter.trim();
44
+
45
+ if (text.startsWith('{/')) {
46
+ const name = node.name;
47
+
48
+ if (
49
+ (name === 'IfBlock' && text.startsWith('{/if')) ||
50
+ (name === 'EachBlock' && text.startsWith('{/each')) ||
51
+ (name === 'KeyBlock' && text.startsWith('{/key'))
52
+ ) {
53
+ return context.lineIndent(context.node.from);
54
+ }
55
+
56
+ return null;
57
+ }
58
+
59
+ if (node.name === 'IfBlock' || node.name === 'EachBlock') {
60
+ if (text.startsWith('{{:else')) return context.lineIndent(node.from);
61
+ if (text.startsWith('{{else')) return context.lineIndent(node.from);
62
+ }
63
+
64
+ // not sure if this needed to be duplicated
65
+ let after = /^(\s*)(<\/)?/.exec(context.textAfter);
66
+
67
+ if (context.node.to <= context.pos + after[0].length) return context.continue();
68
+
69
+ return context.lineIndent(context.node.from) + (after[2] ? 0 : context.unit);
70
+ },
71
+
72
+ 'BlockOpen BlockClose BlockInline': (context) => {
73
+ return context.column(context.node.from) + context.unit;
74
+ },
75
+
76
+ 'OpenTag CloseTag SelfClosingTag': (context) => {
77
+ return context.column(context.node.from) + context.unit;
78
+ },
79
+
80
+ Document: (context) => {
81
+ if (context.pos + /\s*/.exec(context.textAfter)[0].length < context.node.to) {
82
+ return context.continue();
83
+ }
84
+
85
+ let endElt: SyntaxNode | null = null;
86
+ let close: SyntaxNode;
87
+
88
+ for (let cur = context.node; ; ) {
89
+ let last = cur.lastChild;
90
+
91
+ if (!last || last.name != 'Element' || last.to != cur.to) break;
92
+ endElt = cur = last;
93
+ }
94
+
95
+ if (
96
+ endElt &&
97
+ !(
98
+ (close = endElt.lastChild) &&
99
+ (close.name === 'CloseTag' || close.name === 'SelfClosingTag')
100
+ )
101
+ ) {
102
+ return context.lineIndent(endElt.from) + context.unit;
103
+ }
104
+
105
+ return null;
106
+ },
107
+ }),
108
+
109
+ foldNodeProp.add({
110
+ Block: (node) => {
111
+ const open = `${node.name}Open`;
112
+ const close = `${node.name}Close`;
113
+
114
+ const first = node.firstChild;
115
+ const last = node.lastChild;
116
+
117
+ if (!first || first.name !== open) return null;
118
+
119
+ return {
120
+ from: first.to,
121
+ to: last?.name === close ? last.from : node.to,
122
+ };
123
+ },
124
+
125
+ Element: (node) => {
126
+ let first = node.firstChild;
127
+ let last = node.lastChild;
128
+
129
+ if (!first || first.name != 'OpenTag') return null;
130
+
131
+ return {
132
+ from: first.to,
133
+ to: last.name === 'CloseTag' ? last.from : node.to,
134
+ };
135
+ },
136
+ }),
137
+ ],
138
+ }),
139
+
140
+ languageData: {
141
+ commentTokens: {
142
+ block: { open: '<!--', close: '-->' },
143
+ devBlock: { open: '{{!--', close: '--}}' },
144
+ devBlockShort: { open: '{{!', close: '}}' },
145
+ },
146
+ indentOnInput: /^\s*((<\/\w+\W)|(\{:(else|then|catch))|(\{\/(if|unless|each|await|key)))$/,
147
+ wordChars: '-._',
148
+ autocomplete: htmlCompletionSource,
149
+ },
150
+ });
151
+
152
+ export function glimmer() {
153
+ return new LanguageSupport(glimmerLanguage, [html().support]);
154
+ }
@@ -0,0 +1,139 @@
1
+ @top Template { entity* }
2
+
3
+ @skip { whitespace }
4
+
5
+ GlimmerTemplateTag {
6
+ templateTagStart templateTagContent* templateTagEnd
7
+ }
8
+
9
+ entity {
10
+ GlimmerTemplateTag
11
+ | Comment
12
+ | MoustacheExpression
13
+ | Splattributes
14
+ | NamedBlock
15
+ // Arguments needs to be "anywhere", beacuse it can be in Element-space.
16
+ // TODO: only allow within Element-space or in an SExpression
17
+ // TODO: define what Element-space is
18
+ // (more parseMixed logic?)
19
+ | Argument
20
+ }
21
+
22
+ Splattributes { "...attributes" }
23
+ NamedBlock { ":" identifier }
24
+ Argument { "@" identifier }
25
+ Function { ~identifier }
26
+
27
+ CallExpression { "(" ~Function SExpression? (identifier Is SExpression)? ")" }
28
+
29
+ SExpression {
30
+ CallExpression
31
+ | identifier
32
+ | Is
33
+ | Pipe
34
+ | Argument
35
+ //| String
36
+ | keywords
37
+ }
38
+
39
+ @external tokens shortCommentContent from "./tokens.js" { shortCommentContent }
40
+ @external tokens longCommentContent from "./tokens.js" { longCommentContent }
41
+ @external tokens htmlCommentContent from "./tokens.js" { htmlCommentContent }
42
+ @external tokens templateTagContent from "./tokens.js" { templateTagContent }
43
+
44
+ Comment { ShortComment | LongComment | HtmlComment }
45
+ ShortComment { shortCommentStart shortCommentContent* shortCommentEnd }
46
+ LongComment { longCommentStart longCommentContent* longCommentEnd }
47
+ HtmlComment { htmlCommentStart (htmlCommentContent | MoustacheExpression)* htmlCommentEnd }
48
+
49
+
50
+ String {
51
+ "\"" attributeValueContentDouble? "\"" |
52
+ "\'" attributeValueContentSingle? "\'"
53
+ }
54
+
55
+
56
+ attributeValueContentDouble[@name=AttributeValueContent] {
57
+ (attributeValueContentCharDouble | attributeValueContentEntity)+
58
+ }
59
+
60
+ attributeValueContentSingle[@name=AttributeValueContent] {
61
+ (attributeValueContentCharSingle | attributeValueContentEntity)+
62
+ }
63
+
64
+ attributeValueContentEntity {
65
+ MoustacheExpression
66
+ }
67
+
68
+
69
+ @skip {} {
70
+ MoustacheExpression {
71
+ "{{" SExpression* "}}"
72
+ | "{{#" SExpression* "}}"
73
+ | "{{/" identifier "}}"
74
+
75
+ }
76
+ }
77
+
78
+
79
+ keywords {
80
+ kw<"this">
81
+ | kw<"as">
82
+ | kw<"if"> | kw<"unless">
83
+ | kw<"else">
84
+ | kw<"let">
85
+ | kw<"and"> | kw<"or"> | kw<"not">
86
+ | kw<"eq"> | kw<"neq"> | kw<"not-eq">
87
+ | kw<"gt"> | kw<"gte">
88
+ | kw<"lt"> | kw<"lte">
89
+ | kw<"array">
90
+ | kw<"concat">
91
+ | kw<"hash">
92
+ | kw<"each"> | kw<"each-in">
93
+ | kw<"on">
94
+ | kw<"modifier"> | kw<"component"> | kw<"helper">
95
+ | kw<"in-element">
96
+ | kw<"yield"> | kw<"outlet">
97
+ }
98
+
99
+ kw<term> { @specialize[@name={term}]<identifier, term> }
100
+
101
+ @tokens {
102
+ Text { ![{] Text? | "{" (@eof | ![%{] Text?) }
103
+ space { @whitespace+ }
104
+ whitespace { (std.whitespace | ",")+ }
105
+
106
+
107
+ "{{"[closedBy="}}"]
108
+ "}}"[openedBy="{{"]
109
+ "("[closedBy="("]
110
+ ")"[openedBy=")"]
111
+ Is { "=" }
112
+ Pipe { "|" }
113
+
114
+ templateTagStart[@name="TemplateTag", closedBy=templateTagEnd] { "<template>" }
115
+ templateTagEnd[@name="TemplateTag", openedBy=templateTagStart] { "</template>" }
116
+
117
+
118
+ identifierChar { @asciiLetter }
119
+ word { identifierChar (identifierChar | @digit)* }
120
+ identifier { word }
121
+
122
+ attributeValueContentCharDouble { !["&{] }
123
+ attributeValueContentCharSingle { !['&{] }
124
+
125
+ @precedence { attributeValueContentCharDouble whitespace }
126
+ @precedence { attributeValueContentCharSingle whitespace }
127
+
128
+ htmlCommentStart { "<!--" }
129
+ htmlCommentEnd { "-->" }
130
+
131
+ shortCommentStart { "{{!" }
132
+ shortCommentEnd { "}}" }
133
+
134
+ longCommentStart { "{{!--" }
135
+ longCommentEnd { "--}}" }
136
+
137
+ }
138
+
139
+ @external propSource glimmerHighlighting from "./highlight"
@@ -0,0 +1,3 @@
1
+ import type { Parser } from '@lezer/common';
2
+
3
+ export declare const parser: Parser;
@@ -0,0 +1,14 @@
1
+ export const Comment: 2,
2
+ HtmlComment: 5,
3
+ LongComment: 4,
4
+ MoustacheExpression: 6,
5
+ SExpression: 8,
6
+ ShortComment: 3,
7
+ String: 9,
8
+ Template: 1,
9
+ attributeValueContentDouble: 10,
10
+ attributeValueContentSingle: 11,
11
+ htmlCommentContent: 50,
12
+ longCommentContent: 49,
13
+ shortCommentContent: 48,
14
+ templateTagContent: 55;
package/src/tokens.ts ADDED
@@ -0,0 +1,277 @@
1
+ import { ExternalTokenizer } from '@lezer/lr';
2
+
3
+ import {
4
+ htmlCommentContent as htmlCommentToken,
5
+ longCommentContent as longCommentToken,
6
+ MoustacheExpression as longExprToken,
7
+ shortCommentContent as shortCommentToken,
8
+ templateTagContent as templateTagToken,
9
+ } from './syntax.grammar.terms';
10
+
11
+ import type { InputStream } from '@lezer/lr';
12
+
13
+ // Ascii values
14
+ const parenOpen = 40;
15
+ const parenClose = 41;
16
+ const squareOpen = 91;
17
+ const squareClose = 93;
18
+ const curlyOpen = 123;
19
+ const curlyClose = 125;
20
+ const colon = 58;
21
+ const hash = 35;
22
+ const at = 64;
23
+ const slash = 47;
24
+ const greaterThan = 62;
25
+ const dash = 45;
26
+ const quoteDouble = 34;
27
+ const quoteSingle = 39;
28
+ const backslash = 92;
29
+ const newline = 10;
30
+ const asterisk = 42;
31
+ const tick = 96;
32
+
33
+ const prefixes = [colon, hash, at, slash];
34
+
35
+ const shortCommentEnd = [curlyClose, curlyClose];
36
+ const longCommentEnd = [dash, dash, curlyClose, curlyClose];
37
+ const htmlCommentEnd = [dash, dash, greaterThan];
38
+
39
+ // Is there a way to make this a build time (rather than eval-time?) operation?
40
+ const closingTemplateTag = '</template>'.split('').map((char) => char.charCodeAt(0));
41
+
42
+ export function matchForComment(commentEndPattern, commentToken, input) {
43
+ for (let found = 0, i = 0; ; i++) {
44
+ if (input.next < 0) {
45
+ if (i) input.acceptToken(commentToken);
46
+
47
+ break;
48
+ }
49
+
50
+ let hasMatch = commentEndPattern[found] === input.next;
51
+
52
+ if (!hasMatch) {
53
+ found = 0;
54
+ input.advance();
55
+
56
+ break;
57
+ }
58
+
59
+ if (found === commentEndPattern.length - 1) {
60
+ if (i > commentEndPattern.length - 1) {
61
+ input.acceptToken(commentToken, 1 - commentEndPattern.length);
62
+
63
+ break;
64
+ } else {
65
+ console.warn('Reached end of comment but there is still content left');
66
+ }
67
+
68
+ break;
69
+ }
70
+
71
+ found++;
72
+ input.advance();
73
+ }
74
+ }
75
+
76
+ export const shortCommentContent = new ExternalTokenizer((input) => {
77
+ return matchForComment(shortCommentEnd, shortCommentToken, input);
78
+ });
79
+
80
+ export const longCommentContent = new ExternalTokenizer((input) => {
81
+ return matchForComment(longCommentEnd, longCommentToken, input);
82
+ });
83
+
84
+ export const htmlCommentContent = new ExternalTokenizer((input) => {
85
+ return matchForComment(htmlCommentEnd, htmlCommentToken, input);
86
+ });
87
+
88
+ export const templateTagContent = new ExternalTokenizer((input) => {
89
+ return matchForComment(closingTemplateTag, templateTagToken, input);
90
+ });
91
+
92
+ // TODO: string handler does not handle interpolation
93
+
94
+ function createStringHandler(input: InputStream) {
95
+ let inString = false;
96
+ let inStringType: 'double' | 'single' | 'template' | null = null;
97
+ let inStringIgnoreNext = false;
98
+
99
+ return () => {
100
+ if (inString) {
101
+ if (inStringIgnoreNext) {
102
+ inStringIgnoreNext = false;
103
+
104
+ return true;
105
+ }
106
+
107
+ if (input.next === backslash) {
108
+ inStringIgnoreNext = true;
109
+
110
+ return true;
111
+ }
112
+
113
+ if (inStringType === 'double' && input.next === quoteDouble) {
114
+ inString = false;
115
+ inStringType = null;
116
+
117
+ return true;
118
+ }
119
+
120
+ if (inStringType === 'single' && input.next === quoteSingle) {
121
+ inString = false;
122
+ inStringType = null;
123
+
124
+ return true;
125
+ }
126
+
127
+ if (inStringType === 'template' && input.next === tick) {
128
+ inString = false;
129
+ inStringType = null;
130
+
131
+ return true;
132
+ }
133
+
134
+ return true;
135
+ }
136
+
137
+ if (input.next === quoteDouble) {
138
+ inString = true;
139
+ inStringType = 'double';
140
+
141
+ return true;
142
+ }
143
+
144
+ if (input.next === quoteSingle) {
145
+ inString = true;
146
+ inStringType = 'single';
147
+
148
+ return true;
149
+ }
150
+
151
+ if (input.next === tick) {
152
+ inString = true;
153
+ inStringType = 'template';
154
+
155
+ return true;
156
+ }
157
+
158
+ return false;
159
+ };
160
+ }
161
+
162
+ function createCommentHandler(input: InputStream) {
163
+ let inLineComment = false;
164
+ let inBlockComment = false;
165
+
166
+ return () => {
167
+ if (inLineComment) {
168
+ if (input.next === newline) {
169
+ inLineComment = false;
170
+
171
+ return true;
172
+ }
173
+
174
+ return true;
175
+ }
176
+
177
+ if (inBlockComment) {
178
+ if (input.next === asterisk && input.peek(1) === slash) {
179
+ inBlockComment = false;
180
+
181
+ return true;
182
+ }
183
+
184
+ return true;
185
+ }
186
+
187
+ if (input.next === slash && input.peek(1) === slash) {
188
+ inLineComment = true;
189
+
190
+ return true;
191
+ }
192
+
193
+ if (input.next === slash && input.peek(1) === asterisk) {
194
+ inBlockComment = true;
195
+
196
+ return true;
197
+ }
198
+
199
+ return false;
200
+ };
201
+ }
202
+
203
+ type ExpressionStack = '(' | '{' | '{{' | '[';
204
+
205
+ // closes on a delimiter that probably isn't in the expression
206
+ export const expression = new ExternalTokenizer((input) => {
207
+ if (prefixes.includes(input.next)) {
208
+ return;
209
+ }
210
+
211
+ const commentHandler = createCommentHandler(input);
212
+ const stringHandler = createStringHandler(input);
213
+
214
+ let stack: ExpressionStack[] = [];
215
+
216
+ const popIfMatch = (match: ExpressionStack) => {
217
+ const idx = stack.lastIndexOf(match);
218
+
219
+ if (idx !== -1) {
220
+ while (stack.length > idx) {
221
+ stack.pop();
222
+ }
223
+ }
224
+ };
225
+
226
+ for (let pos = 0; ; pos++) {
227
+ // end of input
228
+ if (input.next < 0) {
229
+ if (pos > 0) input.acceptToken(longExprToken);
230
+
231
+ break;
232
+ }
233
+
234
+ if (commentHandler() || stringHandler()) {
235
+ input.advance();
236
+ continue;
237
+ }
238
+
239
+ if (
240
+ stack.length === 0 &&
241
+ (input.next === curlyClose || input.next === parenClose || input.next === squareClose)
242
+ ) {
243
+ input.acceptToken(longExprToken);
244
+
245
+ break;
246
+ }
247
+
248
+ switch (input.next) {
249
+ case parenOpen:
250
+ stack.push('(');
251
+
252
+ break;
253
+ case parenClose:
254
+ popIfMatch('(');
255
+
256
+ break;
257
+ case squareOpen:
258
+ stack.push('[');
259
+
260
+ break;
261
+ case squareClose:
262
+ popIfMatch('[');
263
+
264
+ break;
265
+ case curlyOpen:
266
+ stack.push('{');
267
+
268
+ break;
269
+ case curlyClose:
270
+ popIfMatch('{');
271
+
272
+ break;
273
+ }
274
+
275
+ input.advance();
276
+ }
277
+ });
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ const { configs } = require("@nullvoxpopuli/eslint-configs");
4
+
5
+ module.exports = configs.crossPlatform();
@@ -0,0 +1,116 @@
1
+ # Long
2
+
3
+ {{!-- text here --}}
4
+
5
+ ==>
6
+
7
+ Glimmer(
8
+ BlockComment(
9
+ LongComment(
10
+ StartLongComment,
11
+ Text,
12
+ EndLongComment
13
+ )
14
+ )
15
+ )
16
+
17
+
18
+ # Short
19
+
20
+ {{! text here }}
21
+
22
+ ==>
23
+
24
+ Glimmer(
25
+ BlockComment(
26
+ ShortComment(
27
+ StartShortComment,
28
+ Text,
29
+ EndStache
30
+ )
31
+ )
32
+ )
33
+
34
+
35
+
36
+ # Long multiline
37
+
38
+ {{!--
39
+ line 1
40
+ line 2
41
+
42
+ weird indent
43
+
44
+ line 4
45
+
46
+ --}}
47
+
48
+ ==>
49
+
50
+ Glimmer(
51
+ BlockComment(
52
+ LongComment(
53
+ StartLongComment,
54
+ Text,
55
+ EndLongComment
56
+ )
57
+ )
58
+ )
59
+
60
+ # Short multiline
61
+
62
+ {{!
63
+ line 1
64
+ line 2
65
+
66
+ weird indent
67
+
68
+ line 4
69
+
70
+ }}
71
+
72
+ ==>
73
+
74
+ Glimmer(
75
+ BlockComment(
76
+ ShortComment(
77
+ StartShortComment,
78
+ Text,
79
+ EndStache
80
+ )
81
+ )
82
+ )
83
+
84
+
85
+ # Long - empty
86
+
87
+ {{!-- --}}
88
+
89
+ ==>
90
+
91
+ Glimmer(
92
+ BlockComment(
93
+ LongComment(
94
+ StartLongComment,
95
+ Text,
96
+ EndLongComment
97
+ )
98
+ )
99
+ )
100
+
101
+
102
+ # Short - empty
103
+
104
+ {{! }}
105
+
106
+ ==>
107
+
108
+ Glimmer(
109
+ BlockComment(
110
+ ShortComment(
111
+ StartShortComment,
112
+ Text,
113
+ EndStache
114
+ )
115
+ )
116
+ )