soy.js 22 KB

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