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("xquery", function() {
19
var keywords = function(){
21
function kw(type) {return {type: type, style: "keyword"};}
22
var operator = kw("operator")
23
, atom = {type: "atom", style: "atom"}
24
, punctuation = {type: "punctuation", style: null}
25
, qualifier = {type: "axis_specifier", style: "qualifier"};
34
var basic = ['after', 'all', 'allowing', 'ancestor', 'ancestor-or-self', 'any', 'array', 'as',
35
'ascending', 'at', 'attribute', 'base-uri', 'before', 'boundary-space', 'by', 'case', 'cast',
36
'castable', 'catch', 'child', 'collation', 'comment', 'construction', 'contains', 'content',
37
'context', 'copy', 'copy-namespaces', 'count', 'decimal-format', 'declare', 'default', 'delete',
38
'descendant', 'descendant-or-self', 'descending', 'diacritics', 'different', 'distance',
39
'document', 'document-node', 'element', 'else', 'empty', 'empty-sequence', 'encoding', 'end',
40
'entire', 'every', 'exactly', 'except', 'external', 'first', 'following', 'following-sibling',
41
'for', 'from', 'ftand', 'ftnot', 'ft-option', 'ftor', 'function', 'fuzzy', 'greatest', 'group',
42
'if', 'import', 'in', 'inherit', 'insensitive', 'insert', 'instance', 'intersect', 'into',
43
'invoke', 'is', 'item', 'language', 'last', 'lax', 'least', 'let', 'levels', 'lowercase', 'map',
44
'modify', 'module', 'most', 'namespace', 'next', 'no', 'node', 'nodes', 'no-inherit',
45
'no-preserve', 'not', 'occurs', 'of', 'only', 'option', 'order', 'ordered', 'ordering',
46
'paragraph', 'paragraphs', 'parent', 'phrase', 'preceding', 'preceding-sibling', 'preserve',
47
'previous', 'processing-instruction', 'relationship', 'rename', 'replace', 'return',
48
'revalidation', 'same', 'satisfies', 'schema', 'schema-attribute', 'schema-element', 'score',
49
'self', 'sensitive', 'sentence', 'sentences', 'sequence', 'skip', 'sliding', 'some', 'stable',
50
'start', 'stemming', 'stop', 'strict', 'strip', 'switch', 'text', 'then', 'thesaurus', 'times',
51
'to', 'transform', 'treat', 'try', 'tumbling', 'type', 'typeswitch', 'union', 'unordered',
52
'update', 'updating', 'uppercase', 'using', 'validate', 'value', 'variable', 'version',
53
'weight', 'when', 'where', 'wildcards', 'window', 'with', 'without', 'word', 'words', 'xquery'];
54
for(var i=0, l=basic.length; i < l; i++) { kwObj[basic[i]] = kw(basic[i]);};
58
var types = ['xs:anyAtomicType', 'xs:anySimpleType', 'xs:anyType', 'xs:anyURI',
59
'xs:base64Binary', 'xs:boolean', 'xs:byte', 'xs:date', 'xs:dateTime', 'xs:dateTimeStamp',
60
'xs:dayTimeDuration', 'xs:decimal', 'xs:double', 'xs:duration', 'xs:ENTITIES', 'xs:ENTITY',
61
'xs:float', 'xs:gDay', 'xs:gMonth', 'xs:gMonthDay', 'xs:gYear', 'xs:gYearMonth', 'xs:hexBinary',
62
'xs:ID', 'xs:IDREF', 'xs:IDREFS', 'xs:int', 'xs:integer', 'xs:item', 'xs:java', 'xs:language',
63
'xs:long', 'xs:Name', 'xs:NCName', 'xs:negativeInteger', 'xs:NMTOKEN', 'xs:NMTOKENS',
64
'xs:nonNegativeInteger', 'xs:nonPositiveInteger', 'xs:normalizedString', 'xs:NOTATION',
65
'xs:numeric', 'xs:positiveInteger', 'xs:precisionDecimal', 'xs:QName', 'xs:short', 'xs:string',
66
'xs:time', 'xs:token', 'xs:unsignedByte', 'xs:unsignedInt', 'xs:unsignedLong',
67
'xs:unsignedShort', 'xs:untyped', 'xs:untypedAtomic', 'xs:yearMonthDuration'];
68
for(var i=0, l=types.length; i < l; i++) { kwObj[types[i]] = atom;};
71
var operators = ['eq', 'ne', 'lt', 'le', 'gt', 'ge', ':=', '=', '>', '>=', '<', '<=', '.', '|', '?', 'and', 'or', 'div', 'idiv', 'mod', '*', '/', '+', '-'];
72
for(var i=0, l=operators.length; i < l; i++) { kwObj[operators[i]] = operator;};
75
var axis_specifiers = ["self::", "attribute::", "child::", "descendant::", "descendant-or-self::", "parent::",
76
"ancestor::", "ancestor-or-self::", "following::", "preceding::", "following-sibling::", "preceding-sibling::"];
77
for(var i=0, l=axis_specifiers.length; i < l; i++) { kwObj[axis_specifiers[i]] = qualifier; };
82
function chain(stream, state, f) {
84
return f(stream, state);
88
function tokenBase(stream, state) {
89
var ch = stream.next(),
90
mightBeFunction = false,
91
isEQName = isEQNameAhead(stream);
95
if(stream.match("!--", true))
96
return chain(stream, state, tokenXMLComment);
98
if(stream.match("![CDATA", false)) {
99
state.tokenize = tokenCDATA;
103
if(stream.match("?", false)) {
104
return chain(stream, state, tokenPreProcessing);
107
var isclose = stream.eat("/");
110
while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
112
return chain(stream, state, tokenTag(tagName, isclose));
116
pushStateStack(state, { type: "codeblock"});
121
popStateStack(state);
125
else if(isInXmlBlock(state)) {
128
else if(ch == "/" && stream.eat(">")) {
129
popStateStack(state);
136
else if (/\d/.test(ch)) {
137
stream.match(/^\d*(?:\.\d*)?(?:E[+\-]?\d+)?/);
141
else if (ch === "(" && stream.eat(":")) {
142
pushStateStack(state, { type: "comment"});
143
return chain(stream, state, tokenComment);
146
else if (!isEQName && (ch === '"' || ch === "'"))
147
return chain(stream, state, tokenString(ch));
149
else if(ch === "$") {
150
return chain(stream, state, tokenVariable);
153
else if(ch ===":" && stream.eat("=")) {
157
else if(ch === "(") {
158
pushStateStack(state, { type: "paren"});
162
else if(ch === ")") {
163
popStateStack(state);
167
else if(ch === "[") {
168
pushStateStack(state, { type: "bracket"});
172
else if(ch === "]") {
173
popStateStack(state);
177
var known = keywords.propertyIsEnumerable(ch) && keywords[ch];
180
if(isEQName && ch === '\"') while(stream.next() !== '"'){}
181
if(isEQName && ch === '\'') while(stream.next() !== '\''){}
184
if(!known) stream.eatWhile(/[\w\$_-]/);
187
var foundColon = stream.eat(":");
191
if(!stream.eat(":") && foundColon) {
192
stream.eatWhile(/[\w\$_-]/);
195
if(stream.match(/^[ \t]*\(/, false)) {
196
mightBeFunction = true;
199
var word = stream.current();
200
known = keywords.propertyIsEnumerable(word) && keywords[word];
204
if(mightBeFunction && !known) known = {type: "function_call", style: "variable def"};
207
if(isInXmlConstructor(state)) {
208
popStateStack(state);
213
if(word == "element" || word == "attribute" || known.type == "axis_specifier") pushStateStack(state, {type: "xmlconstructor"});
216
return known ? known.style : "variable";
221
function tokenComment(stream, state) {
222
var maybeEnd = false, maybeNested = false, nestedCount = 0, ch;
223
while (ch = stream.next()) {
224
if (ch == ")" && maybeEnd) {
228
popStateStack(state);
232
else if(ch == ":" && maybeNested) {
235
maybeEnd = (ch == ":");
236
maybeNested = (ch == "(");
244
function tokenString(quote, f) {
245
return function(stream, state) {
248
if(isInString(state) && stream.current() == quote) {
249
popStateStack(state);
250
if(f) state.tokenize = f;
254
pushStateStack(state, { type: "string", name: quote, tokenize: tokenString(quote, f) });
257
if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
258
state.tokenize = tokenBase;
263
while (ch = stream.next()) {
265
popStateStack(state);
266
if(f) state.tokenize = f;
271
if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
272
state.tokenize = tokenBase;
284
function tokenVariable(stream, state) {
285
var isVariableChar = /[\w\$_-]/;
288
if(stream.eat("\"")) {
289
while(stream.next() !== '\"'){};
292
stream.eatWhile(isVariableChar);
293
if(!stream.match(":=", false)) stream.eat(":");
295
stream.eatWhile(isVariableChar);
296
state.tokenize = tokenBase;
301
function tokenTag(name, isclose) {
302
return function(stream, state) {
304
if(isclose && stream.eat(">")) {
305
popStateStack(state);
306
state.tokenize = tokenBase;
311
pushStateStack(state, { type: "tag", name: name, tokenize: tokenBase});
312
if(!stream.eat(">")) {
313
state.tokenize = tokenAttribute;
317
state.tokenize = tokenBase;
324
function tokenAttribute(stream, state) {
325
var ch = stream.next();
327
if(ch == "/" && stream.eat(">")) {
328
if(isInXmlAttributeBlock(state)) popStateStack(state);
329
if(isInXmlBlock(state)) popStateStack(state);
333
if(isInXmlAttributeBlock(state)) popStateStack(state);
339
if (ch == '"' || ch == "'")
340
return chain(stream, state, tokenString(ch, tokenAttribute));
342
if(!isInXmlAttributeBlock(state))
343
pushStateStack(state, { type: "attribute", tokenize: tokenAttribute});
345
stream.eat(/[a-zA-Z_:]/);
346
stream.eatWhile(/[-a-zA-Z0-9_:.]/);
350
if(stream.match(">", false) || stream.match("/", false)) {
351
popStateStack(state);
352
state.tokenize = tokenBase;
359
function tokenXMLComment(stream, state) {
361
while (ch = stream.next()) {
362
if (ch == "-" && stream.match("->", true)) {
363
state.tokenize = tokenBase;
371
function tokenCDATA(stream, state) {
373
while (ch = stream.next()) {
374
if (ch == "]" && stream.match("]", true)) {
375
state.tokenize = tokenBase;
382
function tokenPreProcessing(stream, state) {
384
while (ch = stream.next()) {
385
if (ch == "?" && stream.match(">", true)) {
386
state.tokenize = tokenBase;
387
return "comment meta";
394
function isInXmlBlock(state) { return isIn(state, "tag"); }
395
function isInXmlAttributeBlock(state) { return isIn(state, "attribute"); }
396
function isInXmlConstructor(state) { return isIn(state, "xmlconstructor"); }
397
function isInString(state) { return isIn(state, "string"); }
399
function isEQNameAhead(stream) {
401
if(stream.current() === '"')
402
return stream.match(/^[^\"]+\"\:/, false);
403
else if(stream.current() === '\'')
404
return stream.match(/^[^\"]+\'\:/, false);
409
function isIn(state, type) {
410
return (state.stack.length && state.stack[state.stack.length - 1].type == type);
413
function pushStateStack(state, newState) {
414
state.stack.push(newState);
417
function popStateStack(state) {
419
var reinstateTokenize = state.stack.length && state.stack[state.stack.length-1].tokenize;
420
state.tokenize = reinstateTokenize || tokenBase;
425
startState: function() {
433
token: function(stream, state) {
434
if (stream.eatSpace()) return null;
435
var style = state.tokenize(stream, state);
439
blockCommentStart: "(:",
440
blockCommentEnd: ":)"
446
CodeMirror.defineMIME("application/xquery", "xquery");