velocity.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. CodeMirror.defineMode("velocity", function() {
  2. function parseWords(str) {
  3. var obj = {}, words = str.split(" ");
  4. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  5. return obj;
  6. }
  7. var keywords = parseWords("#end #else #break #stop #[[ #]] " +
  8. "#{end} #{else} #{break} #{stop}");
  9. var functions = parseWords("#if #elseif #foreach #set #include #parse #macro #define #evaluate " +
  10. "#{if} #{elseif} #{foreach} #{set} #{include} #{parse} #{macro} #{define} #{evaluate}");
  11. var specials = parseWords("$foreach.count $foreach.hasNext $foreach.first $foreach.last $foreach.topmost $foreach.parent $velocityCount");
  12. var isOperatorChar = /[+\-*&%=<>!?:\/|]/;
  13. function chain(stream, state, f) {
  14. state.tokenize = f;
  15. return f(stream, state);
  16. }
  17. function tokenBase(stream, state) {
  18. var beforeParams = state.beforeParams;
  19. state.beforeParams = false;
  20. var ch = stream.next();
  21. // start of string?
  22. if ((ch == '"' || ch == "'") && state.inParams)
  23. return chain(stream, state, tokenString(ch));
  24. // is it one of the special signs []{}().,;? Seperator?
  25. else if (/[\[\]{}\(\),;\.]/.test(ch)) {
  26. if (ch == "(" && beforeParams) state.inParams = true;
  27. else if (ch == ")") state.inParams = false;
  28. return null;
  29. }
  30. // start of a number value?
  31. else if (/\d/.test(ch)) {
  32. stream.eatWhile(/[\w\.]/);
  33. return "number";
  34. }
  35. // multi line comment?
  36. else if (ch == "#" && stream.eat("*")) {
  37. return chain(stream, state, tokenComment);
  38. }
  39. // unparsed content?
  40. else if (ch == "#" && stream.match(/ *\[ *\[/)) {
  41. return chain(stream, state, tokenUnparsed);
  42. }
  43. // single line comment?
  44. else if (ch == "#" && stream.eat("#")) {
  45. stream.skipToEnd();
  46. return "comment";
  47. }
  48. // variable?
  49. else if (ch == "$") {
  50. stream.eatWhile(/[\w\d\$_\.{}]/);
  51. // is it one of the specials?
  52. if (specials && specials.propertyIsEnumerable(stream.current().toLowerCase())) {
  53. return "keyword";
  54. }
  55. else {
  56. state.beforeParams = true;
  57. return "builtin";
  58. }
  59. }
  60. // is it a operator?
  61. else if (isOperatorChar.test(ch)) {
  62. stream.eatWhile(isOperatorChar);
  63. return "operator";
  64. }
  65. else {
  66. // get the whole word
  67. stream.eatWhile(/[\w\$_{}]/);
  68. var word = stream.current().toLowerCase();
  69. // is it one of the listed keywords?
  70. if (keywords && keywords.propertyIsEnumerable(word))
  71. return "keyword";
  72. // is it one of the listed functions?
  73. if (functions && functions.propertyIsEnumerable(word) ||
  74. stream.current().match(/^#[a-z0-9_]+ *$/i) && stream.peek()=="(") {
  75. state.beforeParams = true;
  76. return "keyword";
  77. }
  78. // default: just a "word"
  79. return null;
  80. }
  81. }
  82. function tokenString(quote) {
  83. return function(stream, state) {
  84. var escaped = false, next, end = false;
  85. while ((next = stream.next()) != null) {
  86. if (next == quote && !escaped) {
  87. end = true;
  88. break;
  89. }
  90. escaped = !escaped && next == "\\";
  91. }
  92. if (end) state.tokenize = tokenBase;
  93. return "string";
  94. };
  95. }
  96. function tokenComment(stream, state) {
  97. var maybeEnd = false, ch;
  98. while (ch = stream.next()) {
  99. if (ch == "#" && maybeEnd) {
  100. state.tokenize = tokenBase;
  101. break;
  102. }
  103. maybeEnd = (ch == "*");
  104. }
  105. return "comment";
  106. }
  107. function tokenUnparsed(stream, state) {
  108. var maybeEnd = 0, ch;
  109. while (ch = stream.next()) {
  110. if (ch == "#" && maybeEnd == 2) {
  111. state.tokenize = tokenBase;
  112. break;
  113. }
  114. if (ch == "]")
  115. maybeEnd++;
  116. else if (ch != " ")
  117. maybeEnd = 0;
  118. }
  119. return "meta";
  120. }
  121. // Interface
  122. return {
  123. startState: function() {
  124. return {
  125. tokenize: tokenBase,
  126. beforeParams: false,
  127. inParams: false
  128. };
  129. },
  130. token: function(stream, state) {
  131. if (stream.eatSpace()) return null;
  132. return state.tokenize(stream, state);
  133. }
  134. };
  135. });
  136. CodeMirror.defineMIME("text/velocity", "velocity");