5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("velocity", function() {
15
function parseWords(str) {
16
var obj = {}, words = str.split(" ");
17
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
21
var keywords = parseWords("#end #else #break #stop #[[ #]] " +
22
"#{end} #{else} #{break} #{stop}");
23
var functions = parseWords("#if #elseif #foreach #set #include #parse #macro #define #evaluate " +
24
"#{if} #{elseif} #{foreach} #{set} #{include} #{parse} #{macro} #{define} #{evaluate}");
25
var specials = parseWords("$foreach.count $foreach.hasNext $foreach.first $foreach.last $foreach.topmost $foreach.parent.count $foreach.parent.hasNext $foreach.parent.first $foreach.parent.last $foreach.parent $velocityCount $!bodyContent $bodyContent");
26
var isOperatorChar = /[+\-*&%=<>!?:\/|]/;
28
function chain(stream, state, f) {
30
return f(stream, state);
32
function tokenBase(stream, state) {
33
var beforeParams = state.beforeParams;
34
state.beforeParams = false;
35
var ch = stream.next();
37
if ((ch == "'") && !state.inString && state.inParams) {
38
state.lastTokenWasBuiltin = false;
39
return chain(stream, state, tokenString(ch));
42
else if ((ch == '"')) {
43
state.lastTokenWasBuiltin = false;
45
state.inString = false;
48
else if (state.inParams)
49
return chain(stream, state, tokenString(ch));
52
else if (/[\[\]{}\(\),;\.]/.test(ch)) {
53
if (ch == "(" && beforeParams)
54
state.inParams = true;
56
state.inParams = false;
57
state.lastTokenWasBuiltin = true;
62
else if (/\d/.test(ch)) {
63
state.lastTokenWasBuiltin = false;
64
stream.eatWhile(/[\w\.]/);
68
else if (ch == "#" && stream.eat("*")) {
69
state.lastTokenWasBuiltin = false;
70
return chain(stream, state, tokenComment);
73
else if (ch == "#" && stream.match(/ *\[ *\[/)) {
74
state.lastTokenWasBuiltin = false;
75
return chain(stream, state, tokenUnparsed);
78
else if (ch == "#" && stream.eat("#")) {
79
state.lastTokenWasBuiltin = false;
86
stream.eatWhile(/[\w\d\$_\.{}-]/);
88
if (specials && specials.propertyIsEnumerable(stream.current())) {
92
state.lastTokenWasBuiltin = true;
93
state.beforeParams = true;
98
else if (isOperatorChar.test(ch)) {
99
state.lastTokenWasBuiltin = false;
100
stream.eatWhile(isOperatorChar);
105
stream.eatWhile(/[\w\$_{}@]/);
106
var word = stream.current();
108
if (keywords && keywords.propertyIsEnumerable(word))
111
if (functions && functions.propertyIsEnumerable(word) ||
112
(stream.current().match(/^#@?[a-z0-9_]+ *$/i) && stream.peek()=="(") &&
113
!(functions && functions.propertyIsEnumerable(word.toLowerCase()))) {
114
state.beforeParams = true;
115
state.lastTokenWasBuiltin = false;
118
if (state.inString) {
119
state.lastTokenWasBuiltin = false;
122
if (stream.pos > word.length && stream.string.charAt(stream.pos-word.length-1)=="." && state.lastTokenWasBuiltin)
125
state.lastTokenWasBuiltin = false;
130
function tokenString(quote) {
131
return function(stream, state) {
132
var escaped = false, next, end = false;
133
while ((next = stream.next()) != null) {
134
if ((next == quote) && !escaped) {
138
if (quote=='"' && stream.peek() == '$' && !escaped) {
139
state.inString = true;
143
escaped = !escaped && next == "\\";
145
if (end) state.tokenize = tokenBase;
150
function tokenComment(stream, state) {
151
var maybeEnd = false, ch;
152
while (ch = stream.next()) {
153
if (ch == "#" && maybeEnd) {
154
state.tokenize = tokenBase;
157
maybeEnd = (ch == "*");
162
function tokenUnparsed(stream, state) {
163
var maybeEnd = 0, ch;
164
while (ch = stream.next()) {
165
if (ch == "#" && maybeEnd == 2) {
166
state.tokenize = tokenBase;
179
startState: function() {
185
lastTokenWasBuiltin: false
189
token: function(stream, state) {
190
if (stream.eatSpace()) return null;
191
return state.tokenize(stream, state);
193
blockCommentStart: "#*",
194
blockCommentEnd: "*#",
200
CodeMirror.defineMIME("text/velocity", "velocity");