Debian Codegraph @

codemirror-legacy-modes/mode/xquery.js

raw
1 // The keywords object is set to the result of this self executing
2 // function. Each keyword is a property of the keywords object whose
3 // value is {type: atype, style: astyle}
4 var keywords = function(){
5 // convenience functions used to build keywords object
6 function kw(type) {return {type: type, style: "keyword"};}
7 var operator = kw("operator")
8 , atom = {type: "atom", style: "atom"}
9 , punctuation = {type: "punctuation", style: null}
10 , qualifier = {type: "axis_specifier", style: "qualifier"};
11
12 // kwObj is what is return from this function at the end
13 var kwObj = {
14 ',': punctuation
15 };
16
17 // a list of 'basic' keywords. For each add a property to kwObj with the value of
18 // {type: basic[i], style: "keyword"} e.g. 'after' --> {type: "after", style: "keyword"}
19 var basic = ['after', 'all', 'allowing', 'ancestor', 'ancestor-or-self', 'any', 'array', 'as',
20 'ascending', 'at', 'attribute', 'base-uri', 'before', 'boundary-space', 'by', 'case', 'cast',
21 'castable', 'catch', 'child', 'collation', 'comment', 'construction', 'contains', 'content',
22 'context', 'copy', 'copy-namespaces', 'count', 'decimal-format', 'declare', 'default', 'delete',
23 'descendant', 'descendant-or-self', 'descending', 'diacritics', 'different', 'distance',
24 'document', 'document-node', 'element', 'else', 'empty', 'empty-sequence', 'encoding', 'end',
25 'entire', 'every', 'exactly', 'except', 'external', 'first', 'following', 'following-sibling',
26 'for', 'from', 'ftand', 'ftnot', 'ft-option', 'ftor', 'function', 'fuzzy', 'greatest', 'group',
27 'if', 'import', 'in', 'inherit', 'insensitive', 'insert', 'instance', 'intersect', 'into',
28 'invoke', 'is', 'item', 'language', 'last', 'lax', 'least', 'let', 'levels', 'lowercase', 'map',
29 'modify', 'module', 'most', 'namespace', 'next', 'no', 'node', 'nodes', 'no-inherit',
30 'no-preserve', 'not', 'occurs', 'of', 'only', 'option', 'order', 'ordered', 'ordering',
31 'paragraph', 'paragraphs', 'parent', 'phrase', 'preceding', 'preceding-sibling', 'preserve',
32 'previous', 'processing-instruction', 'relationship', 'rename', 'replace', 'return',
33 'revalidation', 'same', 'satisfies', 'schema', 'schema-attribute', 'schema-element', 'score',
34 'self', 'sensitive', 'sentence', 'sentences', 'sequence', 'skip', 'sliding', 'some', 'stable',
35 'start', 'stemming', 'stop', 'strict', 'strip', 'switch', 'text', 'then', 'thesaurus', 'times',
36 'to', 'transform', 'treat', 'try', 'tumbling', 'type', 'typeswitch', 'union', 'unordered',
37 'update', 'updating', 'uppercase', 'using', 'validate', 'value', 'variable', 'version',
38 'weight', 'when', 'where', 'wildcards', 'window', 'with', 'without', 'word', 'words', 'xquery'];
39 for(var i=0, l=basic.length; i < l; i++) { kwObj[basic[i]] = kw(basic[i]);};
40
41 // a list of types. For each add a property to kwObj with the value of
42 // {type: "atom", style: "atom"}
43 var types = ['xs:anyAtomicType', 'xs:anySimpleType', 'xs:anyType', 'xs:anyURI',
44 'xs:base64Binary', 'xs:boolean', 'xs:byte', 'xs:date', 'xs:dateTime', 'xs:dateTimeStamp',
45 'xs:dayTimeDuration', 'xs:decimal', 'xs:double', 'xs:duration', 'xs:ENTITIES', 'xs:ENTITY',
46 'xs:float', 'xs:gDay', 'xs:gMonth', 'xs:gMonthDay', 'xs:gYear', 'xs:gYearMonth', 'xs:hexBinary',
47 'xs:ID', 'xs:IDREF', 'xs:IDREFS', 'xs:int', 'xs:integer', 'xs:item', 'xs:java', 'xs:language',
48 'xs:long', 'xs:Name', 'xs:NCName', 'xs:negativeInteger', 'xs:NMTOKEN', 'xs:NMTOKENS',
49 'xs:nonNegativeInteger', 'xs:nonPositiveInteger', 'xs:normalizedString', 'xs:NOTATION',
50 'xs:numeric', 'xs:positiveInteger', 'xs:precisionDecimal', 'xs:QName', 'xs:short', 'xs:string',
51 'xs:time', 'xs:token', 'xs:unsignedByte', 'xs:unsignedInt', 'xs:unsignedLong',
52 'xs:unsignedShort', 'xs:untyped', 'xs:untypedAtomic', 'xs:yearMonthDuration'];
53 for(var i=0, l=types.length; i < l; i++) { kwObj[types[i]] = atom;};
54
55 // each operator will add a property to kwObj with value of {type: "operator", style: "keyword"}
56 var operators = ['eq', 'ne', 'lt', 'le', 'gt', 'ge', ':=', '=', '>', '>=', '<', '<=', '.', '|', '?', 'and', 'or', 'div', 'idiv', 'mod', '*', '/', '+', '-'];
57 for(var i=0, l=operators.length; i < l; i++) { kwObj[operators[i]] = operator;};
58
59 // each axis_specifiers will add a property to kwObj with value of {type: "axis_specifier", style: "qualifier"}
60 var axis_specifiers = ["self::", "attribute::", "child::", "descendant::", "descendant-or-self::", "parent::",
61 "ancestor::", "ancestor-or-self::", "following::", "preceding::", "following-sibling::", "preceding-sibling::"];
62 for(var i=0, l=axis_specifiers.length; i < l; i++) { kwObj[axis_specifiers[i]] = qualifier; };
63
64 return kwObj;
65 }();
66
67 function chain(stream, state, f) {
68 state.tokenize = f;
69 return f(stream, state);
70 }
71
72 // the primary mode tokenizer
73 function tokenBase(stream, state) {
74 var ch = stream.next(),
75 mightBeFunction = false,
76 isEQName = isEQNameAhead(stream);
77
78 // an XML tag (if not in some sub, chained tokenizer)
79 if (ch == "<") {
80 if(stream.match("!--", true))
81 return chain(stream, state, tokenXMLComment);
82
83 if(stream.match("![CDATA", false)) {
84 state.tokenize = tokenCDATA;
85 return "tag";
86 }
87
88 if(stream.match("?", false)) {
89 return chain(stream, state, tokenPreProcessing);
90 }
91
92 var isclose = stream.eat("/");
93 stream.eatSpace();
94 var tagName = "", c;
95 while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
96
97 return chain(stream, state, tokenTag(tagName, isclose));
98 }
99 // start code block
100 else if(ch == "{") {
101 pushStateStack(state, { type: "codeblock"});
102 return null;
103 }
104 // end code block
105 else if(ch == "}") {
106 popStateStack(state);
107 return null;
108 }
109 // if we're in an XML block
110 else if(isInXmlBlock(state)) {
111 if(ch == ">")
112 return "tag";
113 else if(ch == "/" && stream.eat(">")) {
114 popStateStack(state);
115 return "tag";
116 }
117 else
118 return "variable";
119 }
120 // if a number
121 else if (/\d/.test(ch)) {
122 stream.match(/^\d*(?:\.\d*)?(?:E[+\-]?\d+)?/);
123 return "atom";
124 }
125 // comment start
126 else if (ch === "(" && stream.eat(":")) {
127 pushStateStack(state, { type: "comment"});
128 return chain(stream, state, tokenComment);
129 }
130 // quoted string
131 else if (!isEQName && (ch === '"' || ch === "'"))
132 return startString(stream, state, ch);
133 // variable
134 else if(ch === "$") {
135 return chain(stream, state, tokenVariable);
136 }
137 // assignment
138 else if(ch ===":" && stream.eat("=")) {
139 return "keyword";
140 }
141 // open paren
142 else if(ch === "(") {
143 pushStateStack(state, { type: "paren"});
144 return null;
145 }
146 // close paren
147 else if(ch === ")") {
148 popStateStack(state);
149 return null;
150 }
151 // open paren
152 else if(ch === "[") {
153 pushStateStack(state, { type: "bracket"});
154 return null;
155 }
156 // close paren
157 else if(ch === "]") {
158 popStateStack(state);
159 return null;
160 }
161 else {
162 var known = keywords.propertyIsEnumerable(ch) && keywords[ch];
163
164 // if there's a EQName ahead, consume the rest of the string portion, it's likely a function
165 if(isEQName && ch === '\"') while(stream.next() !== '"'){}
166 if(isEQName && ch === '\'') while(stream.next() !== '\''){}
167
168 // gobble up a word if the character is not known
169 if(!known) stream.eatWhile(/[\w\$_-]/);
170
171 // gobble a colon in the case that is a lib func type call fn:doc
172 var foundColon = stream.eat(":");
173
174 // if there's not a second colon, gobble another word. Otherwise, it's probably an axis specifier
175 // which should get matched as a keyword
176 if(!stream.eat(":") && foundColon) {
177 stream.eatWhile(/[\w\$_-]/);
178 }
179 // if the next non whitespace character is an open paren, this is probably a function (if not a keyword of other sort)
180 if(stream.match(/^[ \t]*\(/, false)) {
181 mightBeFunction = true;
182 }
183 // is the word a keyword?
184 var word = stream.current();
185 known = keywords.propertyIsEnumerable(word) && keywords[word];
186
187 // if we think it's a function call but not yet known,
188 // set style to variable for now for lack of something better
189 if(mightBeFunction && !known) known = {type: "function_call", style: "def"};
190
191 // if the previous word was element, attribute, axis specifier, this word should be the name of that
192 if(isInXmlConstructor(state)) {
193 popStateStack(state);
194 return "variable";
195 }
196 // as previously checked, if the word is element,attribute, axis specifier, call it an "xmlconstructor" and
197 // push the stack so we know to look for it on the next word
198 if(word == "element" || word == "attribute" || known.type == "axis_specifier") pushStateStack(state, {type: "xmlconstructor"});
199
200 // if the word is known, return the details of that else just call this a generic 'word'
201 return known ? known.style : "variable";
202 }
203 }
204
205 // handle comments, including nested
206 function tokenComment(stream, state) {
207 var maybeEnd = false, maybeNested = false, nestedCount = 0, ch;
208 while (ch = stream.next()) {
209 if (ch == ")" && maybeEnd) {
210 if(nestedCount > 0)
211 nestedCount--;
212 else {
213 popStateStack(state);
214 break;
215 }
216 }
217 else if(ch == ":" && maybeNested) {
218 nestedCount++;
219 }
220 maybeEnd = (ch == ":");
221 maybeNested = (ch == "(");
222 }
223
224 return "comment";
225 }
226
227 // tokenizer for string literals
228 // optionally pass a tokenizer function to set state.tokenize back to when finished
229 function tokenString(quote, f) {
230 return function(stream, state) {
231 var ch;
232 while (ch = stream.next()) {
233 if (ch == quote) {
234 popStateStack(state);
235 if (f) state.tokenize = f;
236 break;
237 } else if (stream.match("{", false) && isInXmlAttributeBlock(state)) {
238 // if we're in a string and in an XML block, allow an embedded code block in an attribute
239 pushStateStack(state, { type: "codeblock"});
240 state.tokenize = tokenBase;
241 return "string";
242 }
243 }
244
245 return "string";
246 };
247 }
248
249 function startString(stream, state, quote, f) {
250 let tokenize = tokenString(quote, f);
251 pushStateStack(state, { type: "string", name: quote, tokenize });
252 return chain(stream, state, tokenize);
253 }
254
255 // tokenizer for variables
256 function tokenVariable(stream, state) {
257 var isVariableChar = /[\w\$_-]/;
258
259 // a variable may start with a quoted EQName so if the next character is quote, consume to the next quote
260 if(stream.eat("\"")) {
261 while(stream.next() !== '\"'){};
262 stream.eat(":");
263 } else {
264 stream.eatWhile(isVariableChar);
265 if(!stream.match(":=", false)) stream.eat(":");
266 }
267 stream.eatWhile(isVariableChar);
268 state.tokenize = tokenBase;
269 return "variable";
270 }
271
272 // tokenizer for XML tags
273 function tokenTag(name, isclose) {
274 return function(stream, state) {
275 stream.eatSpace();
276 if(isclose && stream.eat(">")) {
277 popStateStack(state);
278 state.tokenize = tokenBase;
279 return "tag";
280 }
281 // self closing tag without attributes?
282 if(!stream.eat("/"))
283 pushStateStack(state, { type: "tag", name: name, tokenize: tokenBase});
284 if(!stream.eat(">")) {
285 state.tokenize = tokenAttribute;
286 return "tag";
287 }
288 else {
289 state.tokenize = tokenBase;
290 }
291 return "tag";
292 };
293 }
294
295 // tokenizer for XML attributes
296 function tokenAttribute(stream, state) {
297 var ch = stream.next();
298
299 if(ch == "/" && stream.eat(">")) {
300 if(isInXmlAttributeBlock(state)) popStateStack(state);
301 if(isInXmlBlock(state)) popStateStack(state);
302 return "tag";
303 }
304 if(ch == ">") {
305 if(isInXmlAttributeBlock(state)) popStateStack(state);
306 return "tag";
307 }
308 if(ch == "=")
309 return null;
310 // quoted string
311 if (ch == '"' || ch == "'")
312 return startString(stream, state, ch, tokenAttribute);
313
314 if(!isInXmlAttributeBlock(state))
315 pushStateStack(state, { type: "attribute", tokenize: tokenAttribute});
316
317 stream.eat(/[a-zA-Z_:]/);
318 stream.eatWhile(/[-a-zA-Z0-9_:.]/);
319 stream.eatSpace();
320
321 // the case where the attribute has not value and the tag was closed
322 if(stream.match(">", false) || stream.match("/", false)) {
323 popStateStack(state);
324 state.tokenize = tokenBase;
325 }
326
327 return "attribute";
328 }
329
330 // handle comments, including nested
331 function tokenXMLComment(stream, state) {
332 var ch;
333 while (ch = stream.next()) {
334 if (ch == "-" && stream.match("->", true)) {
335 state.tokenize = tokenBase;
336 return "comment";
337 }
338 }
339 }
340
341
342 // handle CDATA
343 function tokenCDATA(stream, state) {
344 var ch;
345 while (ch = stream.next()) {
346 if (ch == "]" && stream.match("]", true)) {
347 state.tokenize = tokenBase;
348 return "comment";
349 }
350 }
351 }
352
353 // handle preprocessing instructions
354 function tokenPreProcessing(stream, state) {
355 var ch;
356 while (ch = stream.next()) {
357 if (ch == "?" && stream.match(">", true)) {
358 state.tokenize = tokenBase;
359 return "processingInstruction";
360 }
361 }
362 }
363
364
365 // functions to test the current context of the state
366 function isInXmlBlock(state) { return isIn(state, "tag"); }
367 function isInXmlAttributeBlock(state) { return isIn(state, "attribute"); }
368 function isInXmlConstructor(state) { return isIn(state, "xmlconstructor"); }
369 function isInString(state) { return isIn(state, "string"); }
370
371 function isEQNameAhead(stream) {
372 // assume we've already eaten a quote (")
373 if(stream.current() === '"')
374 return stream.match(/^[^\"]+\"\:/, false);
375 else if(stream.current() === '\'')
376 return stream.match(/^[^\"]+\'\:/, false);
377 else
378 return false;
379 }
380
381 function isIn(state, type) {
382 return (state.stack.length && state.stack[state.stack.length - 1].type == type);
383 }
384
385 function pushStateStack(state, newState) {
386 state.stack.push(newState);
387 }
388
389 function popStateStack(state) {
390 state.stack.pop();
391 var reinstateTokenize = state.stack.length && state.stack[state.stack.length-1].tokenize;
392 state.tokenize = reinstateTokenize || tokenBase;
393 }
394
395 // the interface for the mode API
396 export const xQuery = {
397 name: "xquery",
398 startState: function() {
399 return {
400 tokenize: tokenBase,
401 cc: [],
402 stack: []
403 };
404 },
405
406 token: function(stream, state) {
407 if (stream.eatSpace()) return null;
408 var style = state.tokenize(stream, state);
409 return style;
410 },
411
412 languageData: {
413 commentTokens: {block: {open: "(:", close: ":)"}}
414 }
415 };