xquery.js 15 KB

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