xquery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 A = kw("keyword a")
  20. , B = kw("keyword b")
  21. , C = kw("keyword c")
  22. , operator = kw("operator")
  23. , atom = {type: "atom", style: "atom"}
  24. , punctuation = {type: "punctuation", style: null}
  25. , qualifier = {type: "axis_specifier", style: "qualifier"};
  26. // kwObj is what is return from this function at the end
  27. var kwObj = {
  28. 'if': A, 'switch': A, 'while': A, 'for': A,
  29. 'else': B, 'then': B, 'try': B, 'finally': B, 'catch': B,
  30. 'element': C, 'attribute': C, 'let': C, 'implements': C, 'import': C, 'module': C, 'namespace': C,
  31. 'return': C, 'super': C, 'this': C, 'throws': C, 'where': C, 'private': C,
  32. ',': punctuation,
  33. 'null': atom, 'fn:false()': atom, 'fn:true()': atom
  34. };
  35. // a list of 'basic' keywords. For each add a property to kwObj with the value of
  36. // {type: basic[i], style: "keyword"} e.g. 'after' --> {type: "after", style: "keyword"}
  37. var basic = ['after','ancestor','ancestor-or-self','and','as','ascending','assert','attribute','before',
  38. 'by','case','cast','child','comment','declare','default','define','descendant','descendant-or-self',
  39. 'descending','document','document-node','element','else','eq','every','except','external','following',
  40. 'following-sibling','follows','for','function','if','import','in','instance','intersect','item',
  41. 'let','module','namespace','node','node','of','only','or','order','parent','precedes','preceding',
  42. 'preceding-sibling','processing-instruction','ref','return','returns','satisfies','schema','schema-element',
  43. 'self','some','sortby','stable','text','then','to','treat','typeswitch','union','variable','version','where',
  44. 'xquery', 'empty-sequence'];
  45. for(var i=0, l=basic.length; i < l; i++) { kwObj[basic[i]] = kw(basic[i]);};
  46. // a list of types. For each add a property to kwObj with the value of
  47. // {type: "atom", style: "atom"}
  48. var types = ['xs:string', 'xs:float', 'xs:decimal', 'xs:double', 'xs:integer', 'xs:boolean', 'xs:date', 'xs:dateTime',
  49. 'xs:time', 'xs:duration', 'xs:dayTimeDuration', 'xs:time', 'xs:yearMonthDuration', 'numeric', 'xs:hexBinary',
  50. 'xs:base64Binary', 'xs:anyURI', 'xs:QName', 'xs:byte','xs:boolean','xs:anyURI','xf:yearMonthDuration'];
  51. for(var i=0, l=types.length; i < l; i++) { kwObj[types[i]] = atom;};
  52. // each operator will add a property to kwObj with value of {type: "operator", style: "keyword"}
  53. var operators = ['eq', 'ne', 'lt', 'le', 'gt', 'ge', ':=', '=', '>', '>=', '<', '<=', '.', '|', '?', 'and', 'or', 'div', 'idiv', 'mod', '*', '/', '+', '-'];
  54. for(var i=0, l=operators.length; i < l; i++) { kwObj[operators[i]] = operator;};
  55. // each axis_specifiers will add a property to kwObj with value of {type: "axis_specifier", style: "qualifier"}
  56. var axis_specifiers = ["self::", "attribute::", "child::", "descendant::", "descendant-or-self::", "parent::",
  57. "ancestor::", "ancestor-or-self::", "following::", "preceding::", "following-sibling::", "preceding-sibling::"];
  58. for(var i=0, l=axis_specifiers.length; i < l; i++) { kwObj[axis_specifiers[i]] = qualifier; };
  59. return kwObj;
  60. }();
  61. function chain(stream, state, f) {
  62. state.tokenize = f;
  63. return f(stream, state);
  64. }
  65. // the primary mode tokenizer
  66. function tokenBase(stream, state) {
  67. var ch = stream.next(),
  68. mightBeFunction = false,
  69. isEQName = isEQNameAhead(stream);
  70. // an XML tag (if not in some sub, chained tokenizer)
  71. if (ch == "<") {
  72. if(stream.match("!--", true))
  73. return chain(stream, state, tokenXMLComment);
  74. if(stream.match("![CDATA", false)) {
  75. state.tokenize = tokenCDATA;
  76. return "tag";
  77. }
  78. if(stream.match("?", false)) {
  79. return chain(stream, state, tokenPreProcessing);
  80. }
  81. var isclose = stream.eat("/");
  82. stream.eatSpace();
  83. var tagName = "", c;
  84. while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
  85. return chain(stream, state, tokenTag(tagName, isclose));
  86. }
  87. // start code block
  88. else if(ch == "{") {
  89. pushStateStack(state,{ type: "codeblock"});
  90. return null;
  91. }
  92. // end code block
  93. else if(ch == "}") {
  94. popStateStack(state);
  95. return null;
  96. }
  97. // if we're in an XML block
  98. else if(isInXmlBlock(state)) {
  99. if(ch == ">")
  100. return "tag";
  101. else if(ch == "/" && stream.eat(">")) {
  102. popStateStack(state);
  103. return "tag";
  104. }
  105. else
  106. return "variable";
  107. }
  108. // if a number
  109. else if (/\d/.test(ch)) {
  110. stream.match(/^\d*(?:\.\d*)?(?:E[+\-]?\d+)?/);
  111. return "atom";
  112. }
  113. // comment start
  114. else if (ch === "(" && stream.eat(":")) {
  115. pushStateStack(state, { type: "comment"});
  116. return chain(stream, state, tokenComment);
  117. }
  118. // quoted string
  119. else if ( !isEQName && (ch === '"' || ch === "'"))
  120. return chain(stream, state, tokenString(ch));
  121. // variable
  122. else if(ch === "$") {
  123. return chain(stream, state, tokenVariable);
  124. }
  125. // assignment
  126. else if(ch ===":" && stream.eat("=")) {
  127. return "keyword";
  128. }
  129. // open paren
  130. else if(ch === "(") {
  131. pushStateStack(state, { type: "paren"});
  132. return null;
  133. }
  134. // close paren
  135. else if(ch === ")") {
  136. popStateStack(state);
  137. return null;
  138. }
  139. // open paren
  140. else if(ch === "[") {
  141. pushStateStack(state, { type: "bracket"});
  142. return null;
  143. }
  144. // close paren
  145. else if(ch === "]") {
  146. popStateStack(state);
  147. return null;
  148. }
  149. else {
  150. var known = keywords.propertyIsEnumerable(ch) && keywords[ch];
  151. // if there's a EQName ahead, consume the rest of the string portion, it's likely a function
  152. if(isEQName && ch === '\"') while(stream.next() !== '"'){}
  153. if(isEQName && ch === '\'') while(stream.next() !== '\''){}
  154. // gobble up a word if the character is not known
  155. if(!known) stream.eatWhile(/[\w\$_-]/);
  156. // gobble a colon in the case that is a lib func type call fn:doc
  157. var foundColon = stream.eat(":");
  158. // if there's not a second colon, gobble another word. Otherwise, it's probably an axis specifier
  159. // which should get matched as a keyword
  160. if(!stream.eat(":") && foundColon) {
  161. stream.eatWhile(/[\w\$_-]/);
  162. }
  163. // if the next non whitespace character is an open paren, this is probably a function (if not a keyword of other sort)
  164. if(stream.match(/^[ \t]*\(/, false)) {
  165. mightBeFunction = true;
  166. }
  167. // is the word a keyword?
  168. var word = stream.current();
  169. known = keywords.propertyIsEnumerable(word) && keywords[word];
  170. // if we think it's a function call but not yet known,
  171. // set style to variable for now for lack of something better
  172. if(mightBeFunction && !known) known = {type: "function_call", style: "variable def"};
  173. // if the previous word was element, attribute, axis specifier, this word should be the name of that
  174. if(isInXmlConstructor(state)) {
  175. popStateStack(state);
  176. return "variable";
  177. }
  178. // as previously checked, if the word is element,attribute, axis specifier, call it an "xmlconstructor" and
  179. // push the stack so we know to look for it on the next word
  180. if(word == "element" || word == "attribute" || known.type == "axis_specifier") pushStateStack(state, {type: "xmlconstructor"});
  181. // if the word is known, return the details of that else just call this a generic 'word'
  182. return known ? known.style : "variable";
  183. }
  184. }
  185. // handle comments, including nested
  186. function tokenComment(stream, state) {
  187. var maybeEnd = false, maybeNested = false, nestedCount = 0, ch;
  188. while (ch = stream.next()) {
  189. if (ch == ")" && maybeEnd) {
  190. if(nestedCount > 0)
  191. nestedCount--;
  192. else {
  193. popStateStack(state);
  194. break;
  195. }
  196. }
  197. else if(ch == ":" && maybeNested) {
  198. nestedCount++;
  199. }
  200. maybeEnd = (ch == ":");
  201. maybeNested = (ch == "(");
  202. }
  203. return "comment";
  204. }
  205. // tokenizer for string literals
  206. // optionally pass a tokenizer function to set state.tokenize back to when finished
  207. function tokenString(quote, f) {
  208. return function(stream, state) {
  209. var ch;
  210. if(isInString(state) && stream.current() == quote) {
  211. popStateStack(state);
  212. if(f) state.tokenize = f;
  213. return "string";
  214. }
  215. pushStateStack(state, { type: "string", name: quote, tokenize: tokenString(quote, f) });
  216. // if we're in a string and in an XML block, allow an embedded code block
  217. if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
  218. state.tokenize = tokenBase;
  219. return "string";
  220. }
  221. while (ch = stream.next()) {
  222. if (ch == quote) {
  223. popStateStack(state);
  224. if(f) state.tokenize = f;
  225. break;
  226. }
  227. else {
  228. // if we're in a string and in an XML block, allow an embedded code block in an attribute
  229. if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
  230. state.tokenize = tokenBase;
  231. return "string";
  232. }
  233. }
  234. }
  235. return "string";
  236. };
  237. }
  238. // tokenizer for variables
  239. function tokenVariable(stream, state) {
  240. var isVariableChar = /[\w\$_-]/;
  241. // a variable may start with a quoted EQName so if the next character is quote, consume to the next quote
  242. if(stream.eat("\"")) {
  243. while(stream.next() !== '\"'){};
  244. stream.eat(":");
  245. } else {
  246. stream.eatWhile(isVariableChar);
  247. if(!stream.match(":=", false)) stream.eat(":");
  248. }
  249. stream.eatWhile(isVariableChar);
  250. state.tokenize = tokenBase;
  251. return "variable";
  252. }
  253. // tokenizer for XML tags
  254. function tokenTag(name, isclose) {
  255. return function(stream, state) {
  256. stream.eatSpace();
  257. if(isclose && stream.eat(">")) {
  258. popStateStack(state);
  259. state.tokenize = tokenBase;
  260. return "tag";
  261. }
  262. // self closing tag without attributes?
  263. if(!stream.eat("/"))
  264. pushStateStack(state, { type: "tag", name: name, tokenize: tokenBase});
  265. if(!stream.eat(">")) {
  266. state.tokenize = tokenAttribute;
  267. return "tag";
  268. }
  269. else {
  270. state.tokenize = tokenBase;
  271. }
  272. return "tag";
  273. };
  274. }
  275. // tokenizer for XML attributes
  276. function tokenAttribute(stream, state) {
  277. var ch = stream.next();
  278. if(ch == "/" && stream.eat(">")) {
  279. if(isInXmlAttributeBlock(state)) popStateStack(state);
  280. if(isInXmlBlock(state)) popStateStack(state);
  281. return "tag";
  282. }
  283. if(ch == ">") {
  284. if(isInXmlAttributeBlock(state)) popStateStack(state);
  285. return "tag";
  286. }
  287. if(ch == "=")
  288. return null;
  289. // quoted string
  290. if (ch == '"' || ch == "'")
  291. return chain(stream, state, tokenString(ch, tokenAttribute));
  292. if(!isInXmlAttributeBlock(state))
  293. pushStateStack(state, { type: "attribute", tokenize: tokenAttribute});
  294. stream.eat(/[a-zA-Z_:]/);
  295. stream.eatWhile(/[-a-zA-Z0-9_:.]/);
  296. stream.eatSpace();
  297. // the case where the attribute has not value and the tag was closed
  298. if(stream.match(">", false) || stream.match("/", false)) {
  299. popStateStack(state);
  300. state.tokenize = tokenBase;
  301. }
  302. return "attribute";
  303. }
  304. // handle comments, including nested
  305. function tokenXMLComment(stream, state) {
  306. var ch;
  307. while (ch = stream.next()) {
  308. if (ch == "-" && stream.match("->", true)) {
  309. state.tokenize = tokenBase;
  310. return "comment";
  311. }
  312. }
  313. }
  314. // handle CDATA
  315. function tokenCDATA(stream, state) {
  316. var ch;
  317. while (ch = stream.next()) {
  318. if (ch == "]" && stream.match("]", true)) {
  319. state.tokenize = tokenBase;
  320. return "comment";
  321. }
  322. }
  323. }
  324. // handle preprocessing instructions
  325. function tokenPreProcessing(stream, state) {
  326. var ch;
  327. while (ch = stream.next()) {
  328. if (ch == "?" && stream.match(">", true)) {
  329. state.tokenize = tokenBase;
  330. return "comment meta";
  331. }
  332. }
  333. }
  334. // functions to test the current context of the state
  335. function isInXmlBlock(state) { return isIn(state, "tag"); }
  336. function isInXmlAttributeBlock(state) { return isIn(state, "attribute"); }
  337. function isInXmlConstructor(state) { return isIn(state, "xmlconstructor"); }
  338. function isInString(state) { return isIn(state, "string"); }
  339. function isEQNameAhead(stream) {
  340. // assume we've already eaten a quote (")
  341. if(stream.current() === '"')
  342. return stream.match(/^[^\"]+\"\:/, false);
  343. else if(stream.current() === '\'')
  344. return stream.match(/^[^\"]+\'\:/, false);
  345. else
  346. return false;
  347. }
  348. function isIn(state, type) {
  349. return (state.stack.length && state.stack[state.stack.length - 1].type == type);
  350. }
  351. function pushStateStack(state, newState) {
  352. state.stack.push(newState);
  353. }
  354. function popStateStack(state) {
  355. state.stack.pop();
  356. var reinstateTokenize = state.stack.length && state.stack[state.stack.length-1].tokenize;
  357. state.tokenize = reinstateTokenize || tokenBase;
  358. }
  359. // the interface for the mode API
  360. return {
  361. startState: function() {
  362. return {
  363. tokenize: tokenBase,
  364. cc: [],
  365. stack: []
  366. };
  367. },
  368. token: function(stream, state) {
  369. if (stream.eatSpace()) return null;
  370. var style = state.tokenize(stream, state);
  371. return style;
  372. },
  373. blockCommentStart: "(:",
  374. blockCommentEnd: ":)"
  375. };
  376. });
  377. CodeMirror.defineMIME("application/xquery", "xquery");
  378. });