scripts/CodeMirror/mode/soy/soy.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12 "use strict";
13
14 var paramData = { noEndTag: true, soyState: "param-def" };
15 var tags = {
16 "alias": { noEndTag: true },
17 "delpackage": { noEndTag: true },
18 "namespace": { noEndTag: true, soyState: "namespace-def" },
19 "@param": paramData,
20 "@param?": paramData,
21 "@inject": paramData,
22 "@inject?": paramData,
23 "@state": paramData,
24 "template": { soyState: "templ-def", variableScope: true},
25 "literal": { },
26 "msg": {},
27 "fallbackmsg": { noEndTag: true, reduceIndent: true},
28 "select": {},
29 "plural": {},
30 "let": { soyState: "var-def" },
31 "if": {},
32 "elseif": { noEndTag: true, reduceIndent: true},
33 "else": { noEndTag: true, reduceIndent: true},
34 "switch": {},
35 "case": { noEndTag: true, reduceIndent: true},
36 "default": { noEndTag: true, reduceIndent: true},
37 "foreach": { variableScope: true, soyState: "for-loop" },
38 "ifempty": { noEndTag: true, reduceIndent: true},
39 "for": { variableScope: true, soyState: "for-loop" },
40 "call": { soyState: "templ-ref" },
41 "param": { soyState: "param-ref"},
42 "print": { noEndTag: true },
43 "deltemplate": { soyState: "templ-def", variableScope: true},
44 "delcall": { soyState: "templ-ref" },
45 "log": {},
46 "element": { variableScope: true },
47 };
48
49 var indentingTags = Object.keys(tags).filter(function(tag) {
50 return !tags[tag].noEndTag || tags[tag].reduceIndent;
51 });
52
53 CodeMirror.defineMode("soy", function(config) {
54 var textMode = CodeMirror.getMode(config, "text/plain");
55 var modes = {
56 html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false}),
57 attributes: textMode,
58 text: textMode,
59 uri: textMode,
60 trusted_resource_uri: textMode,
61 css: CodeMirror.getMode(config, "text/css"),
62 js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
63 };
64
65 function last(array) {
66 return array[array.length - 1];
67 }
68
69 function tokenUntil(stream, state, untilRegExp) {
70 if (stream.sol()) {
71 for (var indent = 0; indent < state.indent; indent++) {
72 if (!stream.eat(/\s/)) break;
73 }
74 if (indent) return null;
75 }
76 var oldString = stream.string;
77 var match = untilRegExp.exec(oldString.substr(stream.pos));
78 if (match) {
79 // We don't use backUp because it backs up just the position, not the state.
80 // This uses an undocumented API.
81 stream.string = oldString.substr(0, stream.pos + match.index);
82 }
83 var result = stream.hideFirstChars(state.indent, function() {
84 var localState = last(state.localStates);
85 return localState.mode.token(stream, localState.state);
86 });
87 stream.string = oldString;
88 return result;
89 }
90
91 function contains(list, element) {
92 while (list) {
93 if (list.element === element) return true;
94 list = list.next;
95 }
96 return false;
97 }
98
99 function prepend(list, element) {
100 return {
101 element: element,
102 next: list
103 };
104 }
105
106 function popcontext(state) {
107 if (!state.context) return;
108 if (state.context.scope) {
109 state.variables = state.context.scope;
110 }
111 state.context = state.context.previousContext;
112 }
113
114 // Reference a variable `name` in `list`.
115 // Let `loose` be truthy to ignore missing identifiers.
116 function ref(list, name, loose) {
117 return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error");
118 }
119
120 // Data for an open soy tag.
121 function Context(previousContext, tag, scope) {
122 this.previousContext = previousContext;
123 this.tag = tag;
124 this.kind = null;
125 this.scope = scope;
126 }
127
128 function expression(stream, state) {
129 var match;
130 if (stream.match(/[[]/)) {
131 state.soyState.push("list-literal");
132 state.context = new Context(state.context, "list-literal", state.variables);
133 state.lookupVariables = false;
134 return null;
135 } else if (stream.match(/map\b/)) {
136 state.soyState.push("map-literal");
137 return "keyword";
138 } else if (stream.match(/record\b/)) {
139 state.soyState.push("record-literal");
140 return "keyword";
141 } else if (stream.match(/([\w]+)(?=\()/)) {
142 return "variable callee";
143 } else if (match = stream.match(/^["']/)) {
144 state.soyState.push("string");
145 state.quoteKind = match[0];
146 return "string";
147 } else if (stream.match(/^[(]/)) {
148 state.soyState.push("open-parentheses");
149 return null;
150 } else if (stream.match(/(null|true|false)(?!\w)/) ||
151 stream.match(/0x([0-9a-fA-F]{2,})/) ||
152 stream.match(/-?([0-9]*[.])?[0-9]+(e[0-9]*)?/)) {
153 return "atom";
154 } else if (stream.match(/(\||[+\-*\/%]|[=!]=|\?:|[<>]=?)/)) {
155 // Tokenize filter, binary, null propagator, and equality operators.
156 return "operator";
157 } else if (match = stream.match(/^\$([\w]+)/)) {
158 return ref(state.variables, match[1], !state.lookupVariables);
159 } else if (match = stream.match(/^\w+/)) {
160 return /^(?:as|and|or|not|in|if)$/.test(match[0]) ? "keyword" : null;
161 }
162
163 stream.next();
164 return null;
165 }
166
167 return {
168 startState: function() {
169 return {
170 soyState: [],
171 variables: prepend(null, 'ij'),
172 scopes: null,
173 indent: 0,
174 quoteKind: null,
175 context: null,
176 lookupVariables: true, // Is unknown variables considered an error
177 localStates: [{
178 mode: modes.html,
179 state: CodeMirror.startState(modes.html)
180 }]
181 };
182 },
183
184 copyState: function(state) {
185 return {
186 tag: state.tag, // Last seen Soy tag.
187 soyState: state.soyState.concat([]),
188 variables: state.variables,
189 context: state.context,
190 indent: state.indent, // Indentation of the following line.
191 quoteKind: state.quoteKind,
192 lookupVariables: state.lookupVariables,
193 localStates: state.localStates.map(function(localState) {
194 return {
195 mode: localState.mode,
196 state: CodeMirror.copyState(localState.mode, localState.state)
197 };
198 })
199 };
200 },
201
202 token: function(stream, state) {
203 var match;
204
205 switch (last(state.soyState)) {
206 case "comment":
207 if (stream.match(/^.*?\*\//)) {
208 state.soyState.pop();
209 } else {
210 stream.skipToEnd();
211 }
212 if (!state.context || !state.context.scope) {
213 var paramRe = /@param\??\s+(\S+)/g;
214 var current = stream.current();
215 for (var match; (match = paramRe.exec(current)); ) {
216 state.variables = prepend(state.variables, match[1]);
217 }
218 }
219 return "comment";
220
221 case "string":
222 var match = stream.match(/^.*?(["']|\\[\s\S])/);
223 if (!match) {
224 stream.skipToEnd();
225 } else if (match[1] == state.quoteKind) {
226 state.quoteKind = null;
227 state.soyState.pop();
228 }
229 return "string";
230 }
231
232 if (!state.soyState.length || last(state.soyState) != "literal") {
233 if (stream.match(/^\/\*/)) {
234 state.soyState.push("comment");
235 return "comment";
236 } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
237 return "comment";
238 }
239 }
240
241 switch (last(state.soyState)) {
242 case "templ-def":
243 if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
244 state.soyState.pop();
245 return "def";
246 }
247 stream.next();
248 return null;
249
250 case "templ-ref":
251 if (match = stream.match(/(\.?[a-zA-Z_][a-zA-Z_0-9]+)+/)) {
252 state.soyState.pop();
253 // If the first character is '.', it can only be a local template.
254 if (match[0][0] == '.') {
255 return "variable-2"
256 }
257 // Otherwise
258 return "variable";
259 }
260 if (match = stream.match(/^\$([\w]+)/)) {
261 state.soyState.pop();
262 return ref(state.variables, match[1], !state.lookupVariables);
263 }
264
265 stream.next();
266 return null;
267
268 case "namespace-def":
269 if (match = stream.match(/^\.?([\w\.]+)/)) {
270 state.soyState.pop();
271 return "variable";
272 }
273 stream.next();
274 return null;
275
276 case "param-def":
277 if (match = stream.match(/^\w+/)) {
278 state.variables = prepend(state.variables, match[0]);
279 state.soyState.pop();
280 state.soyState.push("param-type");
281 return "def";
282 }
283 stream.next();
284 return null;
285
286 case "param-ref":
287 if (match = stream.match(/^\w+/)) {
288 state.soyState.pop();
289 return "property";
290 }
291 stream.next();
292 return null;
293
294 case "open-parentheses":
295 if (stream.match(/[)]/)) {
296 state.soyState.pop();
297 return null;
298 }
299 return expression(stream, state);
300
301 case "param-type":
302 var peekChar = stream.peek();
303 if ("}]=>,".indexOf(peekChar) != -1) {
304 state.soyState.pop();
305 return null;
306 } else if (peekChar == "[") {
307 state.soyState.push('param-type-record');
308 return null;
309 } else if (peekChar == "(") {
310 state.soyState.push('param-type-template');
311 return null;
312 } else if (peekChar == "<") {
313 state.soyState.push('param-type-parameter');
314 return null;
315 } else if (match = stream.match(/^([\w]+|[?])/)) {
316 return "type";
317 }
318 stream.next();
319 return null;
320
321 case "param-type-record":
322 var peekChar = stream.peek();
323 if (peekChar == "]") {
324 state.soyState.pop();
325 return null;
326 }
327 if (stream.match(/^\w+/)) {
328 state.soyState.push('param-type');
329 return "property";
330 }
331 stream.next();
332 return null;
333
334 case "param-type-parameter":
335 if (stream.match(/^[>]/)) {
336 state.soyState.pop();
337 return null;
338 }
339 if (stream.match(/^[<,]/)) {
340 state.soyState.push('param-type');
341 return null;
342 }
343 stream.next();
344 return null;
345
346 case "param-type-template":
347 if (stream.match(/[>]/)) {
348 state.soyState.pop();
349 state.soyState.push('param-type');
350 return null;
351 }
352 if (stream.match(/^\w+/)) {
353 state.soyState.push('param-type');
354 return "def";
355 }
356 stream.next();
357 return null;
358
359 case "var-def":
360 if (match = stream.match(/^\$([\w]+)/)) {
361 state.variables = prepend(state.variables, match[1]);
362 state.soyState.pop();
363 return "def";
364 }
365 stream.next();
366 return null;
367
368 case "for-loop":
369 if (stream.match(/\bin\b/)) {
370 state.soyState.pop();
371 return "keyword";
372 }
373 if (stream.peek() == "$") {
374 state.soyState.push('var-def');
375 return null;
376 }
377 stream.next();
378 return null;
379
380 case "record-literal":
381 if (stream.match(/^[)]/)) {
382 state.soyState.pop();
383 return null;
384 }
385 if (stream.match(/[(,]/)) {
386 state.soyState.push("map-value")
387 state.soyState.push("record-key")
388 return null;
389 }
390 stream.next()
391 return null;
392
393 case "map-literal":
394 if (stream.match(/^[)]/)) {
395 state.soyState.pop();
396 return null;
397 }
398 if (stream.match(/[(,]/)) {
399 state.soyState.push("map-value")
400 state.soyState.push("map-value")
401 return null;
402 }
403 stream.next()
404 return null;
405
406 case "list-literal":
407 if (stream.match(/\]/)) {
408 state.soyState.pop();
409 state.lookupVariables = true;
410 popcontext(state);
411 return null;
412 }
413 if (stream.match(/\bfor\b/)) {
414 state.lookupVariables = true;
415 state.soyState.push('for-loop');
416 return "keyword";
417 }
418 return expression(stream, state);
419
420 case "record-key":
421 if (stream.match(/[\w]+/)) {
422 return "property";
423 }
424 if (stream.match(/^[:]/)) {
425 state.soyState.pop();
426 return null;
427 }
428 stream.next();
429 return null;
430
431 case "map-value":
432 if (stream.peek() == ")" || stream.peek() == "," || stream.match(/^[:)]/)) {
433 state.soyState.pop();
434 return null;
435 }
436 return expression(stream, state);
437
438 case "import":
439 if (stream.eat(";")) {
440 state.soyState.pop();
441 state.indent -= 2 * config.indentUnit;
442 return null;
443 }
444 if (stream.match(/\w+(?=\s+as)/)) {
445 return "variable";
446 }
447 if (match = stream.match(/\w+/)) {
448 return /(from|as)/.test(match[0]) ? "keyword" : "def";
449 }
450 if (match = stream.match(/^["']/)) {
451 state.soyState.push("string");
452 state.quoteKind = match[0];
453 return "string";
454 }
455 stream.next();
456 return null;
457
458 case "tag":
459 var endTag = state.tag[0] == "/";
460 var tagName = endTag ? state.tag.substring(1) : state.tag;
461 var tag = tags[tagName];
462 if (stream.match(/^\/?}/)) {
463 var selfClosed = stream.current() == "/}";
464 if (selfClosed && !endTag) {
465 popcontext(state);
466 }
467 if (state.tag == "/template" || state.tag == "/deltemplate") {
468 state.variables = prepend(null, 'ij');
469 state.indent = 0;
470 } else {
471 state.indent -= config.indentUnit *
472 (selfClosed || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
473 }
474 state.soyState.pop();
475 return "keyword";
476 } else if (stream.match(/^([\w?]+)(?==)/)) {
477 if (state.context && state.context.tag == tagName && stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
478 var kind = match[1];
479 state.context.kind = kind;
480 var mode = modes[kind] || modes.html;
481 var localState = last(state.localStates);
482 if (localState.mode.indent) {
483 state.indent += localState.mode.indent(localState.state, "", "");
484 }
485 state.localStates.push({
486 mode: mode,
487 state: CodeMirror.startState(mode)
488 });
489 }
490 return "attribute";
491 }
492 return expression(stream, state);
493
494 case "literal":
495 if (stream.match(/^(?=\{\/literal})/)) {
496 state.soyState.pop();
497 return this.token(stream, state);
498 }
499 return tokenUntil(stream, state, /\{\/literal}/);
500 }
501
502 if (stream.match(/^\{literal}/)) {
503 state.indent += config.indentUnit;
504 state.soyState.push("literal");
505 state.context = new Context(state.context, "literal", state.variables);
506 return "keyword";
507
508 // A tag-keyword must be followed by whitespace, comment or a closing tag.
509 } else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) {
510 var prevTag = state.tag;
511 state.tag = match[1];
512 var endTag = state.tag[0] == "/";
513 var indentingTag = !!tags[state.tag];
514 var tagName = endTag ? state.tag.substring(1) : state.tag;
515 var tag = tags[tagName];
516 if (state.tag != "/switch")
517 state.indent += ((endTag || tag && tag.reduceIndent) && prevTag != "switch" ? 1 : 2) * config.indentUnit;
518
519 state.soyState.push("tag");
520 var tagError = false;
521 if (tag) {
522 if (!endTag) {
523 if (tag.soyState) state.soyState.push(tag.soyState);
524 }
525 // If a new tag, open a new context.
526 if (!tag.noEndTag && (indentingTag || !endTag)) {
527 state.context = new Context(state.context, state.tag, tag.variableScope ? state.variables : null);
528 // Otherwise close the current context.
529 } else if (endTag) {
530 if (!state.context || state.context.tag != tagName) {
531 tagError = true;
532 } else if (state.context) {
533 if (state.context.kind) {
534 state.localStates.pop();
535 var localState = last(state.localStates);
536 if (localState.mode.indent) {
537 state.indent -= localState.mode.indent(localState.state, "", "");
538 }
539 }
540 popcontext(state);
541 }
542 }
543 } else if (endTag) {
544 // Assume all tags with a closing tag are defined in the config.
545 tagError = true;
546 }
547 return (tagError ? "error " : "") + "keyword";
548
549 // Not a tag-keyword; it's an implicit print tag.
550 } else if (stream.eat('{')) {
551 state.tag = "print";
552 state.indent += 2 * config.indentUnit;
553 state.soyState.push("tag");
554 return "keyword";
555 } else if (!state.context && stream.match(/\bimport\b/)) {
556 state.soyState.push("import");
557 state.indent += 2 * config.indentUnit;
558 return "keyword";
559 }
560
561 return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
562 },
563
564 indent: function(state, textAfter, line) {
565 var indent = state.indent, top = last(state.soyState);
566 if (top == "comment") return CodeMirror.Pass;
567
568 if (top == "literal") {
569 if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
570 } else {
571 if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
572 if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
573 if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
574 if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
575 }
576 var localState = last(state.localStates);
577 if (indent && localState.mode.indent) {
578 indent += localState.mode.indent(localState.state, textAfter, line);
579 }
580 return indent;
581 },
582
583 innerMode: function(state) {
584 if (state.soyState.length && last(state.soyState) != "literal") return null;
585 else return last(state.localStates);
586 },
587
588 electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
589 lineComment: "//",
590 blockCommentStart: "/*",
591 blockCommentEnd: "*/",
592 blockCommentContinue: " * ",
593 useInnerComments: false,
594 fold: "indent"
595 };
596 }, "htmlmixed");
597
598 CodeMirror.registerHelper("wordChars", "soy", /[\w$]/);
599
600 CodeMirror.registerHelper("hintWords", "soy", Object.keys(tags).concat(
601 ["css", "debugger"]));
602
603 CodeMirror.defineMIME("text/x-soy", "soy");
604});