soy.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 (stream.match(/^\/\*/)) {
  138. state.soyState.push("comment");
  139. return "comment";
  140. } else if (stream.match(stream.sol() || (state.soyState.length && last(state.soyState) != "literal") ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  141. return "comment";
  142. }
  143. switch (last(state.soyState)) {
  144. case "templ-def":
  145. if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
  146. state.templates = prepend(state.templates, match[1]);
  147. state.scopes = prepend(state.scopes, state.variables);
  148. state.soyState.pop();
  149. return "def";
  150. }
  151. stream.next();
  152. return null;
  153. case "templ-ref":
  154. if (match = stream.match(/^\.?([\w]+)/)) {
  155. state.soyState.pop();
  156. // If the first character is '.', try to match against a local template name.
  157. if (match[0][0] == '.') {
  158. return ref(state.templates, match[1], true);
  159. }
  160. // Otherwise
  161. return "variable";
  162. }
  163. stream.next();
  164. return null;
  165. case "param-def":
  166. if (match = stream.match(/^\w+/)) {
  167. state.variables = prepend(state.variables, match[0]);
  168. state.soyState.pop();
  169. state.soyState.push("param-type");
  170. return "def";
  171. }
  172. stream.next();
  173. return null;
  174. case "param-type":
  175. if (stream.peek() == "}") {
  176. state.soyState.pop();
  177. return null;
  178. }
  179. if (stream.eatWhile(/^[\w]+/)) {
  180. return "variable-3";
  181. }
  182. stream.next();
  183. return null;
  184. case "var-def":
  185. if (match = stream.match(/^\$([\w]+)/)) {
  186. state.variables = prepend(state.variables, match[1]);
  187. state.soyState.pop();
  188. return "def";
  189. }
  190. stream.next();
  191. return null;
  192. case "tag":
  193. if (stream.match(/^\/?}/)) {
  194. if (state.tag == "/template" || state.tag == "/deltemplate") {
  195. popscope(state);
  196. state.variables = prepend(null, 'ij');
  197. state.indent = 0;
  198. } else {
  199. if (state.tag == "/for" || state.tag == "/foreach") {
  200. popscope(state);
  201. }
  202. state.indent -= config.indentUnit *
  203. (stream.current() == "/}" || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
  204. }
  205. state.soyState.pop();
  206. return "keyword";
  207. } else if (stream.match(/^([\w?]+)(?==)/)) {
  208. if (stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  209. var kind = match[1];
  210. state.kind.push(kind);
  211. state.kindTag.push(state.tag);
  212. var mode = modes[kind] || modes.html;
  213. var localState = last(state.localStates);
  214. if (localState.mode.indent) {
  215. state.indent += localState.mode.indent(localState.state, "");
  216. }
  217. state.localStates.push({
  218. mode: mode,
  219. state: CodeMirror.startState(mode)
  220. });
  221. }
  222. return "attribute";
  223. } else if (match = stream.match(/^["']/)) {
  224. state.soyState.push("string");
  225. state.quoteKind = match;
  226. return "string";
  227. }
  228. if (match = stream.match(/^\$([\w]+)/)) {
  229. return ref(state.variables, match[1]);
  230. }
  231. if (match = stream.match(/^\w+/)) {
  232. return /^(?:as|and|or|not|in)$/.test(match[0]) ? "keyword" : null;
  233. }
  234. stream.next();
  235. return null;
  236. case "literal":
  237. if (stream.match(/^(?=\{\/literal})/)) {
  238. state.indent -= config.indentUnit;
  239. state.soyState.pop();
  240. return this.token(stream, state);
  241. }
  242. return tokenUntil(stream, state, /\{\/literal}/);
  243. }
  244. if (stream.match(/^\{literal}/)) {
  245. state.indent += config.indentUnit;
  246. state.soyState.push("literal");
  247. return "keyword";
  248. // A tag-keyword must be followed by whitespace, comment or a closing tag.
  249. } else if (match = stream.match(/^\{([\/@\\]?\w+\??)(?=[\s\}]|\/[/*])/)) {
  250. if (match[1] != "/switch")
  251. state.indent += (/^(\/|(else|elseif|ifempty|case|fallbackmsg|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
  252. state.tag = match[1];
  253. if (state.tag == "/" + last(state.kindTag)) {
  254. // We found the tag that opened the current kind="".
  255. state.kind.pop();
  256. state.kindTag.pop();
  257. state.localStates.pop();
  258. var localState = last(state.localStates);
  259. if (localState.mode.indent) {
  260. state.indent -= localState.mode.indent(localState.state, "");
  261. }
  262. }
  263. state.soyState.push("tag");
  264. if (state.tag == "template" || state.tag == "deltemplate") {
  265. state.soyState.push("templ-def");
  266. } else if (state.tag == "call" || state.tag == "delcall") {
  267. state.soyState.push("templ-ref");
  268. } else if (state.tag == "let") {
  269. state.soyState.push("var-def");
  270. } else if (state.tag == "for" || state.tag == "foreach") {
  271. state.scopes = prepend(state.scopes, state.variables);
  272. state.soyState.push("var-def");
  273. } else if (state.tag == "namespace") {
  274. if (!state.scopes) {
  275. state.variables = prepend(null, 'ij');
  276. }
  277. } else if (state.tag.match(/^@(?:param\??|inject)/)) {
  278. state.soyState.push("param-def");
  279. }
  280. return "keyword";
  281. // Not a tag-keyword; it's an implicit print tag.
  282. } else if (stream.eat('{')) {
  283. state.tag = "print";
  284. state.indent += 2 * config.indentUnit;
  285. state.soyState.push("tag");
  286. return "keyword";
  287. }
  288. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  289. },
  290. indent: function(state, textAfter) {
  291. var indent = state.indent, top = last(state.soyState);
  292. if (top == "comment") return CodeMirror.Pass;
  293. if (top == "literal") {
  294. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  295. } else {
  296. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  297. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  298. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  299. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  300. }
  301. var localState = last(state.localStates);
  302. if (indent && localState.mode.indent) {
  303. indent += localState.mode.indent(localState.state, textAfter);
  304. }
  305. return indent;
  306. },
  307. innerMode: function(state) {
  308. if (state.soyState.length && last(state.soyState) != "literal") return null;
  309. else return last(state.localStates);
  310. },
  311. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  312. lineComment: "//",
  313. blockCommentStart: "/*",
  314. blockCommentEnd: "*/",
  315. blockCommentContinue: " * ",
  316. useInnerComments: false,
  317. fold: "indent"
  318. };
  319. }, "htmlmixed");
  320. CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
  321. ["delpackage", "namespace", "alias", "print", "css", "debugger"]));
  322. CodeMirror.defineMIME("text/x-soy", "soy");
  323. });