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;
8 var keywords = parseWords("#end #else #break #stop #[[ #]] " +
9 "#{end} #{else} #{break} #{stop}");
10 var functions = parseWords("#if #elseif #foreach #set #include #parse #macro #define #evaluate " +
11 "#{if} #{elseif} #{foreach} #{set} #{include} #{parse} #{macro} #{define} #{evaluate}");
12 var specials = parseWords("$foreach.count $foreach.hasNext $foreach.first $foreach.last $foreach.topmost $foreach.parent $velocityCount");
13 var isOperatorChar = /[+\-*&%=<>!?:\/|]/;
15 function chain(stream, state, f) {
17 return f(stream, state);
19 function tokenBase(stream, state) {
20 var beforeParams = state.beforeParams;
21 state.beforeParams = false;
22 var ch = stream.next();
24 if ((ch == '"' || ch == "'") && state.inParams)
25 return chain(stream, state, tokenString(ch));
26 // is it one of the special signs []{}().,;? Seperator?
27 else if (/[\[\]{}\(\),;\.]/.test(ch)) {
28 if (ch == "(" && beforeParams) state.inParams = true;
29 else if (ch == ")") state.inParams = false;
32 // start of a number value?
33 else if (/\d/.test(ch)) {
34 stream.eatWhile(/[\w\.]/);
37 // multi line comment?
38 else if (ch == "#" && stream.eat("*")) {
39 return chain(stream, state, tokenComment);
42 else if (ch == "#" && stream.match(/ *\[ *\[/)) {
43 return chain(stream, state, tokenUnparsed);
45 // single line comment?
46 else if (ch == "#" && stream.eat("#")) {
52 stream.eatWhile(/[\w\d\$_\.{}]/);
53 // is it one of the specials?
54 if (specials && specials.propertyIsEnumerable(stream.current().toLowerCase())) {
58 state.beforeParams = true;
63 else if (isOperatorChar.test(ch)) {
64 stream.eatWhile(isOperatorChar);
69 stream.eatWhile(/[\w\$_{}]/);
70 var word = stream.current().toLowerCase();
71 // is it one of the listed keywords?
72 if (keywords && keywords.propertyIsEnumerable(word))
74 // is it one of the listed functions?
75 if (functions && functions.propertyIsEnumerable(word) ||
76 stream.current().match(/^#[a-z0-9_]+ *$/i) && stream.peek()=="(") {
77 state.beforeParams = true;
80 // default: just a "word"
85 function tokenString(quote) {
86 return function(stream, state) {
87 var escaped = false, next, end = false;
88 while ((next = stream.next()) != null) {
89 if (next == quote && !escaped) {
93 escaped = !escaped && next == "\\";
95 if (end) state.tokenize = tokenBase;
100 function tokenComment(stream, state) {
101 var maybeEnd = false, ch;
102 while (ch = stream.next()) {
103 if (ch == "#" && maybeEnd) {
104 state.tokenize = tokenBase;
107 maybeEnd = (ch == "*");
112 function tokenUnparsed(stream, state) {
113 var maybeEnd = 0, ch;
114 while (ch = stream.next()) {
115 if (ch == "#" && maybeEnd == 2) {
116 state.tokenize = tokenBase;
129 startState: function() {
137 token: function(stream, state) {
138 if (stream.eatSpace()) return null;
139 return state.tokenize(stream, state);
144 CodeMirror.defineMIME("text/velocity", "velocity");