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.

225 lines
6.7 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. /**
  4. * Smarty 2 and 3 mode.
  5. */
  6. (function(mod) {
  7. if (typeof exports == "object" && typeof module == "object") // CommonJS
  8. mod(require("../../lib/codemirror"));
  9. else if (typeof define == "function" && define.amd) // AMD
  10. define(["../../lib/codemirror"], mod);
  11. else // Plain browser env
  12. mod(CodeMirror);
  13. })(function(CodeMirror) {
  14. "use strict";
  15. CodeMirror.defineMode("smarty", function(config, parserConf) {
  16. var rightDelimiter = parserConf.rightDelimiter || "}";
  17. var leftDelimiter = parserConf.leftDelimiter || "{";
  18. var version = parserConf.version || 2;
  19. var baseMode = CodeMirror.getMode(config, parserConf.baseMode || "null");
  20. var keyFunctions = ["debug", "extends", "function", "include", "literal"];
  21. var regs = {
  22. operatorChars: /[+\-*&%=<>!?]/,
  23. validIdentifier: /[a-zA-Z0-9_]/,
  24. stringChar: /['"]/
  25. };
  26. var last;
  27. function cont(style, lastType) {
  28. last = lastType;
  29. return style;
  30. }
  31. function chain(stream, state, parser) {
  32. state.tokenize = parser;
  33. return parser(stream, state);
  34. }
  35. // Smarty 3 allows { and } surrounded by whitespace to NOT slip into Smarty mode
  36. function doesNotCount(stream, pos) {
  37. if (pos == null) pos = stream.pos;
  38. return version === 3 && leftDelimiter == "{" &&
  39. (pos == stream.string.length || /\s/.test(stream.string.charAt(pos)));
  40. }
  41. function tokenTop(stream, state) {
  42. var string = stream.string;
  43. for (var scan = stream.pos;;) {
  44. var nextMatch = string.indexOf(leftDelimiter, scan);
  45. scan = nextMatch + leftDelimiter.length;
  46. if (nextMatch == -1 || !doesNotCount(stream, nextMatch + leftDelimiter.length)) break;
  47. }
  48. if (nextMatch == stream.pos) {
  49. stream.match(leftDelimiter);
  50. if (stream.eat("*")) {
  51. return chain(stream, state, tokenBlock("comment", "*" + rightDelimiter));
  52. } else {
  53. state.depth++;
  54. state.tokenize = tokenSmarty;
  55. last = "startTag";
  56. return "tag";
  57. }
  58. }
  59. if (nextMatch > -1) stream.string = string.slice(0, nextMatch);
  60. var token = baseMode.token(stream, state.base);
  61. if (nextMatch > -1) stream.string = string;
  62. return token;
  63. }
  64. // parsing Smarty content
  65. function tokenSmarty(stream, state) {
  66. if (stream.match(rightDelimiter, true)) {
  67. if (version === 3) {
  68. state.depth--;
  69. if (state.depth <= 0) {
  70. state.tokenize = tokenTop;
  71. }
  72. } else {
  73. state.tokenize = tokenTop;
  74. }
  75. return cont("tag", null);
  76. }
  77. if (stream.match(leftDelimiter, true)) {
  78. state.depth++;
  79. return cont("tag", "startTag");
  80. }
  81. var ch = stream.next();
  82. if (ch == "$") {
  83. stream.eatWhile(regs.validIdentifier);
  84. return cont("variable-2", "variable");
  85. } else if (ch == "|") {
  86. return cont("operator", "pipe");
  87. } else if (ch == ".") {
  88. return cont("operator", "property");
  89. } else if (regs.stringChar.test(ch)) {
  90. state.tokenize = tokenAttribute(ch);
  91. return cont("string", "string");
  92. } else if (regs.operatorChars.test(ch)) {
  93. stream.eatWhile(regs.operatorChars);
  94. return cont("operator", "operator");
  95. } else if (ch == "[" || ch == "]") {
  96. return cont("bracket", "bracket");
  97. } else if (ch == "(" || ch == ")") {
  98. return cont("bracket", "operator");
  99. } else if (/\d/.test(ch)) {
  100. stream.eatWhile(/\d/);
  101. return cont("number", "number");
  102. } else {
  103. if (state.last == "variable") {
  104. if (ch == "@") {
  105. stream.eatWhile(regs.validIdentifier);
  106. return cont("property", "property");
  107. } else if (ch == "|") {
  108. stream.eatWhile(regs.validIdentifier);
  109. return cont("qualifier", "modifier");
  110. }
  111. } else if (state.last == "pipe") {
  112. stream.eatWhile(regs.validIdentifier);
  113. return cont("qualifier", "modifier");
  114. } else if (state.last == "whitespace") {
  115. stream.eatWhile(regs.validIdentifier);
  116. return cont("attribute", "modifier");
  117. } if (state.last == "property") {
  118. stream.eatWhile(regs.validIdentifier);
  119. return cont("property", null);
  120. } else if (/\s/.test(ch)) {
  121. last = "whitespace";
  122. return null;
  123. }
  124. var str = "";
  125. if (ch != "/") {
  126. str += ch;
  127. }
  128. var c = null;
  129. while (c = stream.eat(regs.validIdentifier)) {
  130. str += c;
  131. }
  132. for (var i=0, j=keyFunctions.length; i<j; i++) {
  133. if (keyFunctions[i] == str) {
  134. return cont("keyword", "keyword");
  135. }
  136. }
  137. if (/\s/.test(ch)) {
  138. return null;
  139. }
  140. return cont("tag", "tag");
  141. }
  142. }
  143. function tokenAttribute(quote) {
  144. return function(stream, state) {
  145. var prevChar = null;
  146. var currChar = null;
  147. while (!stream.eol()) {
  148. currChar = stream.peek();
  149. if (stream.next() == quote && prevChar !== '\\') {
  150. state.tokenize = tokenSmarty;
  151. break;
  152. }
  153. prevChar = currChar;
  154. }
  155. return "string";
  156. };
  157. }
  158. function tokenBlock(style, terminator) {
  159. return function(stream, state) {
  160. while (!stream.eol()) {
  161. if (stream.match(terminator)) {
  162. state.tokenize = tokenTop;
  163. break;
  164. }
  165. stream.next();
  166. }
  167. return style;
  168. };
  169. }
  170. return {
  171. startState: function() {
  172. return {
  173. base: CodeMirror.startState(baseMode),
  174. tokenize: tokenTop,
  175. last: null,
  176. depth: 0
  177. };
  178. },
  179. copyState: function(state) {
  180. return {
  181. base: CodeMirror.copyState(baseMode, state.base),
  182. tokenize: state.tokenize,
  183. last: state.last,
  184. depth: state.depth
  185. };
  186. },
  187. innerMode: function(state) {
  188. if (state.tokenize == tokenTop)
  189. return {mode: baseMode, state: state.base};
  190. },
  191. token: function(stream, state) {
  192. var style = state.tokenize(stream, state);
  193. state.last = last;
  194. return style;
  195. },
  196. indent: function(state, text, line) {
  197. if (state.tokenize == tokenTop && baseMode.indent)
  198. return baseMode.indent(state.base, text, line);
  199. else
  200. return CodeMirror.Pass;
  201. },
  202. blockCommentStart: leftDelimiter + "*",
  203. blockCommentEnd: "*" + rightDelimiter
  204. };
  205. });
  206. CodeMirror.defineMIME("text/x-smarty", "smarty");
  207. });