本站源代码
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.

438 lines
15KB

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