xquery.js 15 KB

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