You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
15 KiB

3 years ago
  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. // conveinence 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. // Used as scratch variables to communicate multiple values without
  62. // consing up tons of objects.
  63. var type, content;
  64. function ret(tp, style, cont) {
  65. type = tp; content = cont;
  66. return style;
  67. }
  68. function chain(stream, state, f) {
  69. state.tokenize = f;
  70. return f(stream, state);
  71. }
  72. // the primary mode tokenizer
  73. function tokenBase(stream, state) {
  74. var ch = stream.next(),
  75. mightBeFunction = false,
  76. isEQName = isEQNameAhead(stream);
  77. // an XML tag (if not in some sub, chained tokenizer)
  78. if (ch == "<") {
  79. if(stream.match("!--", true))
  80. return chain(stream, state, tokenXMLComment);
  81. if(stream.match("![CDATA", false)) {
  82. state.tokenize = tokenCDATA;
  83. return ret("tag", "tag");
  84. }
  85. if(stream.match("?", false)) {
  86. return chain(stream, state, tokenPreProcessing);
  87. }
  88. var isclose = stream.eat("/");
  89. stream.eatSpace();
  90. var tagName = "", c;
  91. while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
  92. return chain(stream, state, tokenTag(tagName, isclose));
  93. }
  94. // start code block
  95. else if(ch == "{") {
  96. pushStateStack(state,{ type: "codeblock"});
  97. return ret("", null);
  98. }
  99. // end code block
  100. else if(ch == "}") {
  101. popStateStack(state);
  102. return ret("", null);
  103. }
  104. // if we're in an XML block
  105. else if(isInXmlBlock(state)) {
  106. if(ch == ">")
  107. return ret("tag", "tag");
  108. else if(ch == "/" && stream.eat(">")) {
  109. popStateStack(state);
  110. return ret("tag", "tag");
  111. }
  112. else
  113. return ret("word", "variable");
  114. }
  115. // if a number
  116. else if (/\d/.test(ch)) {
  117. stream.match(/^\d*(?:\.\d*)?(?:E[+\-]?\d+)?/);
  118. return ret("number", "atom");
  119. }
  120. // comment start
  121. else if (ch === "(" && stream.eat(":")) {
  122. pushStateStack(state, { type: "comment"});
  123. return chain(stream, state, tokenComment);
  124. }
  125. // quoted string
  126. else if ( !isEQName && (ch === '"' || ch === "'"))
  127. return chain(stream, state, tokenString(ch));
  128. // variable
  129. else if(ch === "$") {
  130. return chain(stream, state, tokenVariable);
  131. }
  132. // assignment
  133. else if(ch ===":" && stream.eat("=")) {
  134. return ret("operator", "keyword");
  135. }
  136. // open paren
  137. else if(ch === "(") {
  138. pushStateStack(state, { type: "paren"});
  139. return ret("", null);
  140. }
  141. // close paren
  142. else if(ch === ")") {
  143. popStateStack(state);
  144. return ret("", null);
  145. }
  146. // open paren
  147. else if(ch === "[") {
  148. pushStateStack(state, { type: "bracket"});
  149. return ret("", null);
  150. }
  151. // close paren
  152. else if(ch === "]") {
  153. popStateStack(state);
  154. return ret("", null);
  155. }
  156. else {
  157. var known = keywords.propertyIsEnumerable(ch) && keywords[ch];
  158. // if there's a EQName ahead, consume the rest of the string portion, it's likely a function
  159. if(isEQName && ch === '\"') while(stream.next() !== '"'){}
  160. if(isEQName && ch === '\'') while(stream.next() !== '\''){}
  161. // gobble up a word if the character is not known
  162. if(!known) stream.eatWhile(/[\w\$_-]/);
  163. // gobble a colon in the case that is a lib func type call fn:doc
  164. var foundColon = stream.eat(":");
  165. // if there's not a second colon, gobble another word. Otherwise, it's probably an axis specifier
  166. // which should get matched as a keyword
  167. if(!stream.eat(":") && foundColon) {
  168. stream.eatWhile(/[\w\$_-]/);
  169. }
  170. // if the next non whitespace character is an open paren, this is probably a function (if not a keyword of other sort)
  171. if(stream.match(/^[ \t]*\(/, false)) {
  172. mightBeFunction = true;
  173. }
  174. // is the word a keyword?
  175. var word = stream.current();
  176. known = keywords.propertyIsEnumerable(word) && keywords[word];
  177. // if we think it's a function call but not yet known,
  178. // set style to variable for now for lack of something better
  179. if(mightBeFunction && !known) known = {type: "function_call", style: "variable def"};
  180. // if the previous word was element, attribute, axis specifier, this word should be the name of that
  181. if(isInXmlConstructor(state)) {
  182. popStateStack(state);
  183. return ret("word", "variable", word);
  184. }
  185. // as previously checked, if the word is element,attribute, axis specifier, call it an "xmlconstructor" and
  186. // push the stack so we know to look for it on the next word
  187. if(word == "element" || word == "attribute" || known.type == "axis_specifier") pushStateStack(state, {type: "xmlconstructor"});
  188. // if the word is known, return the details of that else just call this a generic 'word'
  189. return known ? ret(known.type, known.style, word) :
  190. ret("word", "variable", word);
  191. }
  192. }
  193. // handle comments, including nested
  194. function tokenComment(stream, state) {
  195. var maybeEnd = false, maybeNested = false, nestedCount = 0, ch;
  196. while (ch = stream.next()) {
  197. if (ch == ")" && maybeEnd) {
  198. if(nestedCount > 0)
  199. nestedCount--;
  200. else {
  201. popStateStack(state);
  202. break;
  203. }
  204. }
  205. else if(ch == ":" && maybeNested) {
  206. nestedCount++;
  207. }
  208. maybeEnd = (ch == ":");
  209. maybeNested = (ch == "(");
  210. }
  211. return ret("comment", "comment");
  212. }
  213. // tokenizer for string literals
  214. // optionally pass a tokenizer function to set state.tokenize back to when finished
  215. function tokenString(quote, f) {
  216. return function(stream, state) {
  217. var ch;
  218. if(isInString(state) && stream.current() == quote) {
  219. popStateStack(state);
  220. if(f) state.tokenize = f;
  221. return ret("string", "string");
  222. }
  223. pushStateStack(state, { type: "string", name: quote, tokenize: tokenString(quote, f) });
  224. // if we're in a string and in an XML block, allow an embedded code block
  225. if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
  226. state.tokenize = tokenBase;
  227. return ret("string", "string");
  228. }
  229. while (ch = stream.next()) {
  230. if (ch == quote) {
  231. popStateStack(state);
  232. if(f) state.tokenize = f;
  233. break;
  234. }
  235. else {
  236. // if we're in a string and in an XML block, allow an embedded code block in an attribute
  237. if(stream.match("{", false) && isInXmlAttributeBlock(state)) {
  238. state.tokenize = tokenBase;
  239. return ret("string", "string");
  240. }
  241. }
  242. }
  243. return ret("string", "string");
  244. };
  245. }
  246. // tokenizer for variables
  247. function tokenVariable(stream, state) {
  248. var isVariableChar = /[\w\$_-]/;
  249. // a variable may start with a quoted EQName so if the next character is quote, consume to the next quote
  250. if(stream.eat("\"")) {
  251. while(stream.next() !== '\"'){};
  252. stream.eat(":");
  253. } else {
  254. stream.eatWhile(isVariableChar);
  255. if(!stream.match(":=", false)) stream.eat(":");
  256. }
  257. stream.eatWhile(isVariableChar);
  258. state.tokenize = tokenBase;
  259. return ret("variable", "variable");
  260. }
  261. // tokenizer for XML tags
  262. function tokenTag(name, isclose) {
  263. return function(stream, state) {
  264. stream.eatSpace();
  265. if(isclose && stream.eat(">")) {
  266. popStateStack(state);
  267. state.tokenize = tokenBase;
  268. return ret("tag", "tag");
  269. }
  270. // self closing tag without attributes?
  271. if(!stream.eat("/"))
  272. pushStateStack(state, { type: "tag", name: name, tokenize: tokenBase});
  273. if(!stream.eat(">")) {
  274. state.tokenize = tokenAttribute;
  275. return ret("tag", "tag");
  276. }
  277. else {
  278. state.tokenize = tokenBase;
  279. }
  280. return ret("tag", "tag");
  281. };
  282. }
  283. // tokenizer for XML attributes
  284. function tokenAttribute(stream, state) {
  285. var ch = stream.next();
  286. if(ch == "/" && stream.eat(">")) {
  287. if(isInXmlAttributeBlock(state)) popStateStack(state);
  288. if(isInXmlBlock(state)) popStateStack(state);
  289. return ret("tag", "tag");
  290. }
  291. if(ch == ">") {
  292. if(isInXmlAttributeBlock(state)) popStateStack(state);
  293. return ret("tag", "tag");
  294. }
  295. if(ch == "=")
  296. return ret("", null);
  297. // quoted string
  298. if (ch == '"' || ch == "'")
  299. return chain(stream, state, tokenString(ch, tokenAttribute));
  300. if(!isInXmlAttributeBlock(state))
  301. pushStateStack(state, { type: "attribute", tokenize: tokenAttribute});
  302. stream.eat(/[a-zA-Z_:]/);
  303. stream.eatWhile(/[-a-zA-Z0-9_:.]/);
  304. stream.eatSpace();
  305. // the case where the attribute has not value and the tag was closed
  306. if(stream.match(">", false) || stream.match("/", false)) {
  307. popStateStack(state);
  308. state.tokenize = tokenBase;
  309. }
  310. return ret("attribute", "attribute");
  311. }
  312. // handle comments, including nested
  313. function tokenXMLComment(stream, state) {
  314. var ch;
  315. while (ch = stream.next()) {
  316. if (ch == "-" && stream.match("->", true)) {
  317. state.tokenize = tokenBase;
  318. return ret("comment", "comment");
  319. }
  320. }
  321. }
  322. // handle CDATA
  323. function tokenCDATA(stream, state) {
  324. var ch;
  325. while (ch = stream.next()) {
  326. if (ch == "]" && stream.match("]", true)) {
  327. state.tokenize = tokenBase;
  328. return ret("comment", "comment");
  329. }
  330. }
  331. }
  332. // handle preprocessing instructions
  333. function tokenPreProcessing(stream, state) {
  334. var ch;
  335. while (ch = stream.next()) {
  336. if (ch == "?" && stream.match(">", true)) {
  337. state.tokenize = tokenBase;
  338. return ret("comment", "comment meta");
  339. }
  340. }
  341. }
  342. // functions to test the current context of the state
  343. function isInXmlBlock(state) { return isIn(state, "tag"); }
  344. function isInXmlAttributeBlock(state) { return isIn(state, "attribute"); }
  345. function isInXmlConstructor(state) { return isIn(state, "xmlconstructor"); }
  346. function isInString(state) { return isIn(state, "string"); }
  347. function isEQNameAhead(stream) {
  348. // assume we've already eaten a quote (")
  349. if(stream.current() === '"')
  350. return stream.match(/^[^\"]+\"\:/, false);
  351. else if(stream.current() === '\'')
  352. return stream.match(/^[^\"]+\'\:/, false);
  353. else
  354. return false;
  355. }
  356. function isIn(state, type) {
  357. return (state.stack.length && state.stack[state.stack.length - 1].type == type);
  358. }
  359. function pushStateStack(state, newState) {
  360. state.stack.push(newState);
  361. }
  362. function popStateStack(state) {
  363. state.stack.pop();
  364. var reinstateTokenize = state.stack.length && state.stack[state.stack.length-1].tokenize;
  365. state.tokenize = reinstateTokenize || tokenBase;
  366. }
  367. // the interface for the mode API
  368. return {
  369. startState: function() {
  370. return {
  371. tokenize: tokenBase,
  372. cc: [],
  373. stack: []
  374. };
  375. },
  376. token: function(stream, state) {
  377. if (stream.eatSpace()) return null;
  378. var style = state.tokenize(stream, state);
  379. return style;
  380. },
  381. blockCommentStart: "(:",
  382. blockCommentEnd: ":)"
  383. };
  384. });
  385. CodeMirror.defineMIME("application/xquery", "xquery");
  386. });