Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / velocity / velocity.js
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
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 = /[+\-*&%=<>!?:\/|]/;
14
15     function chain(stream, state, f) {
16         state.tokenize = f;
17         return f(stream, state);
18     }
19     function tokenBase(stream, state) {
20         var beforeParams = state.beforeParams;
21         state.beforeParams = false;
22         var ch = stream.next();
23         // start of string?
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;
30             return null;
31         }
32         // start of a number value?
33         else if (/\d/.test(ch)) {
34             stream.eatWhile(/[\w\.]/);
35             return "number";
36         }
37         // multi line comment?
38         else if (ch == "#" && stream.eat("*")) {
39             return chain(stream, state, tokenComment);
40         }
41         // unparsed content?
42         else if (ch == "#" && stream.match(/ *\[ *\[/)) {
43             return chain(stream, state, tokenUnparsed);
44         }
45         // single line comment?
46         else if (ch == "#" && stream.eat("#")) {
47             stream.skipToEnd();
48             return "comment";
49         }
50         // variable?
51         else if (ch == "$") {
52             stream.eatWhile(/[\w\d\$_\.{}]/);
53             // is it one of the specials?
54             if (specials && specials.propertyIsEnumerable(stream.current().toLowerCase())) {
55                 return "keyword";
56             }
57             else {
58                 state.beforeParams = true;
59                 return "builtin";
60             }
61         }
62         // is it a operator?
63         else if (isOperatorChar.test(ch)) {
64             stream.eatWhile(isOperatorChar);
65             return "operator";
66         }
67         else {
68             // get the whole word
69             stream.eatWhile(/[\w\$_{}]/);
70             var word = stream.current().toLowerCase();
71             // is it one of the listed keywords?
72             if (keywords && keywords.propertyIsEnumerable(word))
73                 return "keyword";
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;
78                 return "keyword";
79             }
80             // default: just a "word"
81             return null;
82         }
83     }
84
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) {
90                     end = true;
91                     break;
92                 }
93                 escaped = !escaped && next == "\\";
94             }
95             if (end) state.tokenize = tokenBase;
96             return "string";
97         };
98     }
99
100     function tokenComment(stream, state) {
101         var maybeEnd = false, ch;
102         while (ch = stream.next()) {
103             if (ch == "#" && maybeEnd) {
104                 state.tokenize = tokenBase;
105                 break;
106             }
107             maybeEnd = (ch == "*");
108         }
109         return "comment";
110     }
111
112     function tokenUnparsed(stream, state) {
113         var maybeEnd = 0, ch;
114         while (ch = stream.next()) {
115             if (ch == "#" && maybeEnd == 2) {
116                 state.tokenize = tokenBase;
117                 break;
118             }
119             if (ch == "]")
120                 maybeEnd++;
121             else if (ch != " ")
122                 maybeEnd = 0;
123         }
124         return "meta";
125     }
126     // Interface
127
128     return {
129         startState: function() {
130             return {
131                 tokenize: tokenBase,
132                 beforeParams: false,
133                 inParams: false
134             };
135         },
136
137         token: function(stream, state) {
138             if (stream.eatSpace()) return null;
139             return state.tokenize(stream, state);
140         }
141     };
142 });
143
144 CodeMirror.defineMIME("text/velocity", "velocity");