soy.js 18 KB

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