soy.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. css: CodeMirror.getMode(config, "text/css"),
  23. js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
  24. };
  25. function last(array) {
  26. return array[array.length - 1];
  27. }
  28. function tokenUntil(stream, state, untilRegExp) {
  29. var oldString = stream.string;
  30. var match = untilRegExp.exec(oldString.substr(stream.pos));
  31. if (match) {
  32. // We don't use backUp because it backs up just the position, not the state.
  33. // This uses an undocumented API.
  34. stream.string = oldString.substr(0, stream.pos + match.index);
  35. }
  36. var result = stream.hideFirstChars(state.indent, function() {
  37. return state.localMode.token(stream, state.localState);
  38. });
  39. stream.string = oldString;
  40. return result;
  41. }
  42. function contains(list, element) {
  43. while (list) {
  44. if (list.element === element) return true;
  45. list = list.next;
  46. }
  47. return false;
  48. }
  49. function prepend(list, element) {
  50. return {
  51. element: element,
  52. next: list
  53. };
  54. }
  55. // Reference a variable `name` in `list`.
  56. // Let `loose` be truthy to ignore missing identifiers.
  57. function ref(list, name, loose) {
  58. return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error");
  59. }
  60. function popscope(state) {
  61. if (state.scopes) {
  62. state.variables = state.scopes.element;
  63. state.scopes = state.scopes.next;
  64. }
  65. }
  66. return {
  67. startState: function() {
  68. return {
  69. kind: [],
  70. kindTag: [],
  71. soyState: [],
  72. templates: null,
  73. variables: null,
  74. scopes: null,
  75. indent: 0,
  76. localMode: modes.html,
  77. localState: CodeMirror.startState(modes.html)
  78. };
  79. },
  80. copyState: function(state) {
  81. return {
  82. tag: state.tag, // Last seen Soy tag.
  83. kind: state.kind.concat([]), // Values of kind="" attributes.
  84. kindTag: state.kindTag.concat([]), // Opened tags with kind="" attributes.
  85. soyState: state.soyState.concat([]),
  86. templates: state.templates,
  87. variables: state.variables,
  88. scopes: state.scopes,
  89. indent: state.indent, // Indentation of the following line.
  90. localMode: state.localMode,
  91. localState: CodeMirror.copyState(state.localMode, state.localState)
  92. };
  93. },
  94. token: function(stream, state) {
  95. var match;
  96. switch (last(state.soyState)) {
  97. case "comment":
  98. if (stream.match(/^.*?\*\//)) {
  99. state.soyState.pop();
  100. } else {
  101. stream.skipToEnd();
  102. }
  103. return "comment";
  104. case "templ-def":
  105. if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
  106. state.templates = prepend(state.templates, match[1]);
  107. state.scopes = prepend(state.scopes, state.variables);
  108. state.soyState.pop();
  109. return "def";
  110. }
  111. stream.next();
  112. return null;
  113. case "templ-ref":
  114. if (match = stream.match(/^\.?([\w]+)/)) {
  115. state.soyState.pop();
  116. // If the first character is '.', try to match against a local template name.
  117. if (match[0][0] == '.') {
  118. return ref(state.templates, match[1], true);
  119. }
  120. // Otherwise
  121. return "variable";
  122. }
  123. stream.next();
  124. return null;
  125. case "param-def":
  126. if (match = stream.match(/^([\w]+)(?=:)/)) {
  127. state.variables = prepend(state.variables, match[1]);
  128. state.soyState.pop();
  129. state.soyState.push("param-type");
  130. return "def";
  131. }
  132. stream.next();
  133. return null;
  134. case "param-type":
  135. if (stream.peek() == "}") {
  136. state.soyState.pop();
  137. return null;
  138. }
  139. if (stream.eatWhile(/^[\w]+/)) {
  140. return "variable-3";
  141. }
  142. stream.next();
  143. return null;
  144. case "var-def":
  145. if (match = stream.match(/^\$([\w]+)/)) {
  146. state.variables = prepend(state.variables, match[1]);
  147. state.soyState.pop();
  148. return "def";
  149. }
  150. stream.next();
  151. return null;
  152. case "tag":
  153. if (stream.match(/^\/?}/)) {
  154. if (state.tag == "/template" || state.tag == "/deltemplate") {
  155. popscope(state);
  156. state.indent = 0;
  157. } else {
  158. if (state.tag == "/for" || state.tag == "/foreach") {
  159. popscope(state);
  160. }
  161. state.indent -= config.indentUnit *
  162. (stream.current() == "/}" || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
  163. }
  164. state.soyState.pop();
  165. return "keyword";
  166. } else if (stream.match(/^([\w?]+)(?==)/)) {
  167. if (stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  168. var kind = match[1];
  169. state.kind.push(kind);
  170. state.kindTag.push(state.tag);
  171. state.localMode = modes[kind] || modes.html;
  172. state.localState = CodeMirror.startState(state.localMode);
  173. }
  174. return "attribute";
  175. } else if (stream.match(/^"/)) {
  176. state.soyState.push("string");
  177. return "string";
  178. }
  179. if (match = stream.match(/^\$([\w]+)/)) {
  180. return ref(state.variables, match[1]);
  181. }
  182. if (match = stream.match(/^\w+/)) {
  183. return /^(?:as|and|or|not|in)$/.test(match[0]) ? "keyword" : null;
  184. }
  185. stream.next();
  186. return null;
  187. case "literal":
  188. if (stream.match(/^(?=\{\/literal})/)) {
  189. state.indent -= config.indentUnit;
  190. state.soyState.pop();
  191. return this.token(stream, state);
  192. }
  193. return tokenUntil(stream, state, /\{\/literal}/);
  194. case "string":
  195. var match = stream.match(/^.*?("|\\[\s\S])/);
  196. if (!match) {
  197. stream.skipToEnd();
  198. } else if (match[1] == "\"") {
  199. state.soyState.pop();
  200. }
  201. return "string";
  202. }
  203. if (stream.match(/^\/\*/)) {
  204. state.soyState.push("comment");
  205. return "comment";
  206. } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  207. return "comment";
  208. } else if (stream.match(/^\{literal}/)) {
  209. state.indent += config.indentUnit;
  210. state.soyState.push("literal");
  211. return "keyword";
  212. } else if (match = stream.match(/^\{([\/@\\]?[\w?]*)/)) {
  213. if (match[1] != "/switch")
  214. state.indent += (/^(\/|(else|elseif|ifempty|case|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
  215. state.tag = match[1];
  216. if (state.tag == "/" + last(state.kindTag)) {
  217. // We found the tag that opened the current kind="".
  218. state.kind.pop();
  219. state.kindTag.pop();
  220. state.localMode = modes[last(state.kind)] || modes.html;
  221. state.localState = CodeMirror.startState(state.localMode);
  222. }
  223. state.soyState.push("tag");
  224. if (state.tag == "template" || state.tag == "deltemplate") {
  225. state.soyState.push("templ-def");
  226. }
  227. if (state.tag == "call" || state.tag == "delcall") {
  228. state.soyState.push("templ-ref");
  229. }
  230. if (state.tag == "let") {
  231. state.soyState.push("var-def");
  232. }
  233. if (state.tag == "for" || state.tag == "foreach") {
  234. state.scopes = prepend(state.scopes, state.variables);
  235. state.soyState.push("var-def");
  236. }
  237. if (state.tag.match(/^@param\??/)) {
  238. state.soyState.push("param-def");
  239. }
  240. return "keyword";
  241. }
  242. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  243. },
  244. indent: function(state, textAfter) {
  245. var indent = state.indent, top = last(state.soyState);
  246. if (top == "comment") return CodeMirror.Pass;
  247. if (top == "literal") {
  248. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  249. } else {
  250. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  251. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  252. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  253. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  254. }
  255. if (indent && state.localMode.indent)
  256. indent += state.localMode.indent(state.localState, textAfter);
  257. return indent;
  258. },
  259. innerMode: function(state) {
  260. if (state.soyState.length && last(state.soyState) != "literal") return null;
  261. else return {state: state.localState, mode: state.localMode};
  262. },
  263. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  264. lineComment: "//",
  265. blockCommentStart: "/*",
  266. blockCommentEnd: "*/",
  267. blockCommentContinue: " * ",
  268. fold: "indent"
  269. };
  270. }, "htmlmixed");
  271. CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
  272. ["delpackage", "namespace", "alias", "print", "css", "debugger"]));
  273. CodeMirror.defineMIME("text/x-soy", "soy");
  274. });