soy.js 22 KB

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