xquery.js 15 KB

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