soy.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var indentingTags = ["template", "literal", "msg", "fallbackmsg", "let", "if", "elseif",
  13. "else", "switch", "case", "default", "foreach", "ifempty", "for",
  14. "call", "param", "deltemplate", "delcall", "log"];
  15. CodeMirror.defineMode("soy", function(config) {
  16. var textMode = CodeMirror.getMode(config, "text/plain");
  17. var modes = {
  18. html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false}),
  19. attributes: textMode,
  20. text: textMode,
  21. uri: textMode,
  22. trusted_resource_uri: textMode,
  23. css: CodeMirror.getMode(config, "text/css"),
  24. js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
  25. };
  26. function last(array) {
  27. return array[array.length - 1];
  28. }
  29. function tokenUntil(stream, state, untilRegExp) {
  30. if (stream.sol()) {
  31. for (var indent = 0; indent < state.indent; indent++) {
  32. if (!stream.eat(/\s/)) break;
  33. }
  34. if (indent) return null;
  35. }
  36. var oldString = stream.string;
  37. var match = untilRegExp.exec(oldString.substr(stream.pos));
  38. if (match) {
  39. // We don't use backUp because it backs up just the position, not the state.
  40. // This uses an undocumented API.
  41. stream.string = oldString.substr(0, stream.pos + match.index);
  42. }
  43. var result = stream.hideFirstChars(state.indent, function() {
  44. var localState = last(state.localStates);
  45. return localState.mode.token(stream, localState.state);
  46. });
  47. stream.string = oldString;
  48. return result;
  49. }
  50. function contains(list, element) {
  51. while (list) {
  52. if (list.element === element) return true;
  53. list = list.next;
  54. }
  55. return false;
  56. }
  57. function prepend(list, element) {
  58. return {
  59. element: element,
  60. next: list
  61. };
  62. }
  63. // Reference a variable `name` in `list`.
  64. // Let `loose` be truthy to ignore missing identifiers.
  65. function ref(list, name, loose) {
  66. return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error");
  67. }
  68. function popscope(state) {
  69. if (state.scopes) {
  70. state.variables = state.scopes.element;
  71. state.scopes = state.scopes.next;
  72. }
  73. }
  74. return {
  75. startState: function() {
  76. return {
  77. kind: [],
  78. kindTag: [],
  79. soyState: [],
  80. templates: null,
  81. variables: prepend(null, 'ij'),
  82. scopes: null,
  83. indent: 0,
  84. quoteKind: null,
  85. localStates: [{
  86. mode: modes.html,
  87. state: CodeMirror.startState(modes.html)
  88. }]
  89. };
  90. },
  91. copyState: function(state) {
  92. return {
  93. tag: state.tag, // Last seen Soy tag.
  94. kind: state.kind.concat([]), // Values of kind="" attributes.
  95. kindTag: state.kindTag.concat([]), // Opened tags with kind="" attributes.
  96. soyState: state.soyState.concat([]),
  97. templates: state.templates,
  98. variables: state.variables,
  99. scopes: state.scopes,
  100. indent: state.indent, // Indentation of the following line.
  101. quoteKind: state.quoteKind,
  102. localStates: state.localStates.map(function(localState) {
  103. return {
  104. mode: localState.mode,
  105. state: CodeMirror.copyState(localState.mode, localState.state)
  106. };
  107. })
  108. };
  109. },
  110. token: function(stream, state) {
  111. var match;
  112. switch (last(state.soyState)) {
  113. case "comment":
  114. if (stream.match(/^.*?\*\//)) {
  115. state.soyState.pop();
  116. } else {
  117. stream.skipToEnd();
  118. }
  119. if (!state.scopes) {
  120. var paramRe = /@param\??\s+(\S+)/g;
  121. var current = stream.current();
  122. for (var match; (match = paramRe.exec(current)); ) {
  123. state.variables = prepend(state.variables, match[1]);
  124. }
  125. }
  126. return "comment";
  127. case "string":
  128. var match = stream.match(/^.*?(["']|\\[\s\S])/);
  129. if (!match) {
  130. stream.skipToEnd();
  131. } else if (match[1] == state.quoteKind) {
  132. state.quoteKind = null;
  133. state.soyState.pop();
  134. }
  135. return "string";
  136. }
  137. if (!state.soyState.length || last(state.soyState) != "literal") {
  138. if (stream.match(/^\/\*/)) {
  139. state.soyState.push("comment");
  140. return "comment";
  141. } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  142. return "comment";
  143. }
  144. }
  145. switch (last(state.soyState)) {
  146. case "templ-def":
  147. if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
  148. state.templates = prepend(state.templates, match[1]);
  149. state.scopes = prepend(state.scopes, state.variables);
  150. state.soyState.pop();
  151. return "def";
  152. }
  153. stream.next();
  154. return null;
  155. case "templ-ref":
  156. if (match = stream.match(/^\.?([\w]+)/)) {
  157. state.soyState.pop();
  158. // If the first character is '.', try to match against a local template name.
  159. if (match[0][0] == '.') {
  160. return ref(state.templates, match[1], true);
  161. }
  162. // Otherwise
  163. return "variable";
  164. }
  165. stream.next();
  166. return null;
  167. case "param-def":
  168. if (match = stream.match(/^\w+/)) {
  169. state.variables = prepend(state.variables, match[0]);
  170. state.soyState.pop();
  171. state.soyState.push("param-type");
  172. return "def";
  173. }
  174. stream.next();
  175. return null;
  176. case "param-type":
  177. if (stream.peek() == "}") {
  178. state.soyState.pop();
  179. return null;
  180. }
  181. if (stream.eatWhile(/^[\w]+/)) {
  182. return "variable-3";
  183. }
  184. stream.next();
  185. return null;
  186. case "var-def":
  187. if (match = stream.match(/^\$([\w]+)/)) {
  188. state.variables = prepend(state.variables, match[1]);
  189. state.soyState.pop();
  190. return "def";
  191. }
  192. stream.next();
  193. return null;
  194. case "tag":
  195. if (stream.match(/^\/?}/)) {
  196. if (state.tag == "/template" || state.tag == "/deltemplate") {
  197. popscope(state);
  198. state.variables = prepend(null, 'ij');
  199. state.indent = 0;
  200. } else {
  201. if (state.tag == "/for" || state.tag == "/foreach") {
  202. popscope(state);
  203. }
  204. state.indent -= config.indentUnit *
  205. (stream.current() == "/}" || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
  206. }
  207. state.soyState.pop();
  208. return "keyword";
  209. } else if (stream.match(/^([\w?]+)(?==)/)) {
  210. if (stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  211. var kind = match[1];
  212. state.kind.push(kind);
  213. state.kindTag.push(state.tag);
  214. var mode = modes[kind] || modes.html;
  215. var localState = last(state.localStates);
  216. if (localState.mode.indent) {
  217. state.indent += localState.mode.indent(localState.state, "");
  218. }
  219. state.localStates.push({
  220. mode: mode,
  221. state: CodeMirror.startState(mode)
  222. });
  223. }
  224. return "attribute";
  225. } else if (match = stream.match(/^["']/)) {
  226. state.soyState.push("string");
  227. state.quoteKind = match;
  228. return "string";
  229. }
  230. if (match = stream.match(/^\$([\w]+)/)) {
  231. return ref(state.variables, match[1]);
  232. }
  233. if (match = stream.match(/^\w+/)) {
  234. return /^(?:as|and|or|not|in)$/.test(match[0]) ? "keyword" : null;
  235. }
  236. stream.next();
  237. return null;
  238. case "literal":
  239. if (stream.match(/^(?=\{\/literal})/)) {
  240. state.indent -= config.indentUnit;
  241. state.soyState.pop();
  242. return this.token(stream, state);
  243. }
  244. return tokenUntil(stream, state, /\{\/literal}/);
  245. }
  246. if (stream.match(/^\{literal}/)) {
  247. state.indent += config.indentUnit;
  248. state.soyState.push("literal");
  249. return "keyword";
  250. // A tag-keyword must be followed by whitespace, comment or a closing tag.
  251. } else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) {
  252. if (match[1] != "/switch")
  253. state.indent += (/^(\/|(else|elseif|ifempty|case|fallbackmsg|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
  254. state.tag = match[1];
  255. if (state.tag == "/" + last(state.kindTag)) {
  256. // We found the tag that opened the current kind="".
  257. state.kind.pop();
  258. state.kindTag.pop();
  259. state.localStates.pop();
  260. var localState = last(state.localStates);
  261. if (localState.mode.indent) {
  262. state.indent -= localState.mode.indent(localState.state, "");
  263. }
  264. }
  265. state.soyState.push("tag");
  266. if (state.tag == "template" || state.tag == "deltemplate") {
  267. state.soyState.push("templ-def");
  268. } else if (state.tag == "call" || state.tag == "delcall") {
  269. state.soyState.push("templ-ref");
  270. } else if (state.tag == "let") {
  271. state.soyState.push("var-def");
  272. } else if (state.tag == "for" || state.tag == "foreach") {
  273. state.scopes = prepend(state.scopes, state.variables);
  274. state.soyState.push("var-def");
  275. } else if (state.tag == "namespace") {
  276. if (!state.scopes) {
  277. state.variables = prepend(null, 'ij');
  278. }
  279. } else if (state.tag.match(/^@(?:param\??|inject)/)) {
  280. state.soyState.push("param-def");
  281. }
  282. return "keyword";
  283. // Not a tag-keyword; it's an implicit print tag.
  284. } else if (stream.eat('{')) {
  285. state.tag = "print";
  286. state.indent += 2 * config.indentUnit;
  287. state.soyState.push("tag");
  288. return "keyword";
  289. }
  290. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  291. },
  292. indent: function(state, textAfter) {
  293. var indent = state.indent, top = last(state.soyState);
  294. if (top == "comment") return CodeMirror.Pass;
  295. if (top == "literal") {
  296. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  297. } else {
  298. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  299. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  300. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  301. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  302. }
  303. var localState = last(state.localStates);
  304. if (indent && localState.mode.indent) {
  305. indent += localState.mode.indent(localState.state, textAfter);
  306. }
  307. return indent;
  308. },
  309. innerMode: function(state) {
  310. if (state.soyState.length && last(state.soyState) != "literal") return null;
  311. else return last(state.localStates);
  312. },
  313. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  314. lineComment: "//",
  315. blockCommentStart: "/*",
  316. blockCommentEnd: "*/",
  317. blockCommentContinue: " * ",
  318. useInnerComments: false,
  319. fold: "indent"
  320. };
  321. }, "htmlmixed");
  322. CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
  323. ["delpackage", "namespace", "alias", "print", "css", "debugger"]));
  324. CodeMirror.defineMIME("text/x-soy", "soy");
  325. });