Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

548 lines
18 KiB

5 years ago
  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 (match = stream.match(/^([\w]+|[?])/)) {
  278. if (match[0] == "map" || match[0] == "list") {
  279. state.soyState.push('param-type-map-list');
  280. }
  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-map-list":
  298. var peekChar = stream.peek();
  299. if (stream.match(/^[>]/)) {
  300. state.soyState.pop();
  301. return null;
  302. }
  303. if (stream.match(/^[<,]/)) {
  304. state.soyState.push('param-type');
  305. return null;
  306. }
  307. stream.next();
  308. return null;
  309. case "var-def":
  310. if (match = stream.match(/^\$([\w]+)/)) {
  311. state.variables = prepend(state.variables, match[1]);
  312. state.soyState.pop();
  313. return "def";
  314. }
  315. stream.next();
  316. return null;
  317. case "record-literal":
  318. if (stream.match(/^[)]/)) {
  319. state.soyState.pop();
  320. return null;
  321. }
  322. if (stream.match(/[(,]/)) {
  323. state.soyState.push("map-value")
  324. state.soyState.push("record-key")
  325. return null;
  326. }
  327. stream.next()
  328. return null;
  329. case "map-literal":
  330. if (stream.match(/^[)]/)) {
  331. state.soyState.pop();
  332. return null;
  333. }
  334. if (stream.match(/[(,]/)) {
  335. state.soyState.push("map-value")
  336. state.soyState.push("map-value")
  337. return null;
  338. }
  339. stream.next()
  340. return null;
  341. case "list-literal":
  342. if (stream.match(/\]/)) {
  343. state.soyState.pop();
  344. state.lookupVariables = true;
  345. return null;
  346. }
  347. if (stream.match(/for\b/)) {
  348. state.soyState.push("var-def")
  349. return "keyword";
  350. } else if (stream.match(/in\b/)) {
  351. state.lookupVariables = true;
  352. return "keyword";
  353. }
  354. return expression(stream, state);
  355. case "record-key":
  356. if (stream.match(/[\w]+/)) {
  357. return "property";
  358. }
  359. if (stream.match(/^[:]/)) {
  360. state.soyState.pop();
  361. return null;
  362. }
  363. stream.next();
  364. return null;
  365. case "map-value":
  366. if (stream.peek() == ")" || stream.peek() == "," || stream.match(/^[:)]/)) {
  367. state.soyState.pop();
  368. return null;
  369. }
  370. return expression(stream, state);
  371. case "tag":
  372. var endTag = state.tag[0] == "/";
  373. var tagName = endTag ? state.tag.substring(1) : state.tag;
  374. var tag = tags[tagName];
  375. if (stream.match(/^\/?}/)) {
  376. var selfClosed = stream.current() == "/}";
  377. if (selfClosed && !endTag) {
  378. popcontext(state);
  379. }
  380. if (state.tag == "/template" || state.tag == "/deltemplate") {
  381. state.variables = prepend(null, 'ij');
  382. state.indent = 0;
  383. } else {
  384. state.indent -= config.indentUnit *
  385. (selfClosed || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
  386. }
  387. state.soyState.pop();
  388. return "keyword";
  389. } else if (stream.match(/^([\w?]+)(?==)/)) {
  390. if (state.context && state.context.tag == tagName && stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  391. var kind = match[1];
  392. state.context.kind = kind;
  393. var mode = modes[kind] || modes.html;
  394. var localState = last(state.localStates);
  395. if (localState.mode.indent) {
  396. state.indent += localState.mode.indent(localState.state, "", "");
  397. }
  398. state.localStates.push({
  399. mode: mode,
  400. state: CodeMirror.startState(mode)
  401. });
  402. }
  403. return "attribute";
  404. }
  405. return expression(stream, state);
  406. case "literal":
  407. if (stream.match(/^(?=\{\/literal})/)) {
  408. state.soyState.pop();
  409. return this.token(stream, state);
  410. }
  411. return tokenUntil(stream, state, /\{\/literal}/);
  412. }
  413. if (stream.match(/^\{literal}/)) {
  414. state.indent += config.indentUnit;
  415. state.soyState.push("literal");
  416. state.context = new Context(state.context, "literal", state.variables);
  417. return "keyword";
  418. // A tag-keyword must be followed by whitespace, comment or a closing tag.
  419. } else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) {
  420. var prevTag = state.tag;
  421. state.tag = match[1];
  422. var endTag = state.tag[0] == "/";
  423. var indentingTag = !!tags[state.tag];
  424. var tagName = endTag ? state.tag.substring(1) : state.tag;
  425. var tag = tags[tagName];
  426. if (state.tag != "/switch")
  427. state.indent += ((endTag || tag && tag.reduceIndent) && prevTag != "switch" ? 1 : 2) * config.indentUnit;
  428. state.soyState.push("tag");
  429. var tagError = false;
  430. if (tag) {
  431. if (!endTag) {
  432. if (tag.soyState) state.soyState.push(tag.soyState);
  433. }
  434. // If a new tag, open a new context.
  435. if (!tag.noEndTag && (indentingTag || !endTag)) {
  436. state.context = new Context(state.context, state.tag, tag.variableScope ? state.variables : null);
  437. // Otherwise close the current context.
  438. } else if (endTag) {
  439. if (!state.context || state.context.tag != tagName) {
  440. tagError = true;
  441. } else if (state.context) {
  442. if (state.context.kind) {
  443. state.localStates.pop();
  444. var localState = last(state.localStates);
  445. if (localState.mode.indent) {
  446. state.indent -= localState.mode.indent(localState.state, "", "");
  447. }
  448. }
  449. popcontext(state);
  450. }
  451. }
  452. } else if (endTag) {
  453. // Assume all tags with a closing tag are defined in the config.
  454. tagError = true;
  455. }
  456. return (tagError ? "error " : "") + "keyword";
  457. // Not a tag-keyword; it's an implicit print tag.
  458. } else if (stream.eat('{')) {
  459. state.tag = "print";
  460. state.indent += 2 * config.indentUnit;
  461. state.soyState.push("tag");
  462. return "keyword";
  463. }
  464. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  465. },
  466. indent: function(state, textAfter, line) {
  467. var indent = state.indent, top = last(state.soyState);
  468. if (top == "comment") return CodeMirror.Pass;
  469. if (top == "literal") {
  470. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  471. } else {
  472. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  473. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  474. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  475. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  476. }
  477. var localState = last(state.localStates);
  478. if (indent && localState.mode.indent) {
  479. indent += localState.mode.indent(localState.state, textAfter, line);
  480. }
  481. return indent;
  482. },
  483. innerMode: function(state) {
  484. if (state.soyState.length && last(state.soyState) != "literal") return null;
  485. else return last(state.localStates);
  486. },
  487. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  488. lineComment: "//",
  489. blockCommentStart: "/*",
  490. blockCommentEnd: "*/",
  491. blockCommentContinue: " * ",
  492. useInnerComments: false,
  493. fold: "indent"
  494. };
  495. }, "htmlmixed");
  496. CodeMirror.registerHelper("wordChars", "soy", /[\w$]/);
  497. CodeMirror.registerHelper("hintWords", "soy", Object.keys(tags).concat(
  498. ["css", "debugger"]));
  499. CodeMirror.defineMIME("text/x-soy", "soy");
  500. });