xquery.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineMode('xquery', function () {
  15. // The keywords object is set to the result of this self executing
  16. // function. Each keyword is a property of the keywords object whose
  17. // value is {type: atype, style: astyle}
  18. var keywords = (function () {
  19. // convenience functions used to build keywords object
  20. function kw(type) {
  21. return { type: type, style: 'keyword' }
  22. }
  23. var operator = kw('operator'),
  24. atom = { type: 'atom', style: 'atom' },
  25. punctuation = { type: 'punctuation', style: null },
  26. qualifier = { type: 'axis_specifier', style: 'qualifier' }
  27. // kwObj is what is return from this function at the end
  28. var kwObj = {
  29. ',': punctuation,
  30. }
  31. // a list of 'basic' keywords. For each add a property to kwObj with the value of
  32. // {type: basic[i], style: "keyword"} e.g. 'after' --> {type: "after", style: "keyword"}
  33. var basic = [
  34. 'after',
  35. 'all',
  36. 'allowing',
  37. 'ancestor',
  38. 'ancestor-or-self',
  39. 'any',
  40. 'array',
  41. 'as',
  42. 'ascending',
  43. 'at',
  44. 'attribute',
  45. 'base-uri',
  46. 'before',
  47. 'boundary-space',
  48. 'by',
  49. 'case',
  50. 'cast',
  51. 'castable',
  52. 'catch',
  53. 'child',
  54. 'collation',
  55. 'comment',
  56. 'construction',
  57. 'contains',
  58. 'content',
  59. 'context',
  60. 'copy',
  61. 'copy-namespaces',
  62. 'count',
  63. 'decimal-format',
  64. 'declare',
  65. 'default',
  66. 'delete',
  67. 'descendant',
  68. 'descendant-or-self',
  69. 'descending',
  70. 'diacritics',
  71. 'different',
  72. 'distance',
  73. 'document',
  74. 'document-node',
  75. 'element',
  76. 'else',
  77. 'empty',
  78. 'empty-sequence',
  79. 'encoding',
  80. 'end',
  81. 'entire',
  82. 'every',
  83. 'exactly',
  84. 'except',
  85. 'external',
  86. 'first',
  87. 'following',
  88. 'following-sibling',
  89. 'for',
  90. 'from',
  91. 'ftand',
  92. 'ftnot',
  93. 'ft-option',
  94. 'ftor',
  95. 'function',
  96. 'fuzzy',
  97. 'greatest',
  98. 'group',
  99. 'if',
  100. 'import',
  101. 'in',
  102. 'inherit',
  103. 'insensitive',
  104. 'insert',
  105. 'instance',
  106. 'intersect',
  107. 'into',
  108. 'invoke',
  109. 'is',
  110. 'item',
  111. 'language',
  112. 'last',
  113. 'lax',
  114. 'least',
  115. 'let',
  116. 'levels',
  117. 'lowercase',
  118. 'map',
  119. 'modify',
  120. 'module',
  121. 'most',
  122. 'namespace',
  123. 'next',
  124. 'no',
  125. 'node',
  126. 'nodes',
  127. 'no-inherit',
  128. 'no-preserve',
  129. 'not',
  130. 'occurs',
  131. 'of',
  132. 'only',
  133. 'option',
  134. 'order',
  135. 'ordered',
  136. 'ordering',
  137. 'paragraph',
  138. 'paragraphs',
  139. 'parent',
  140. 'phrase',
  141. 'preceding',
  142. 'preceding-sibling',
  143. 'preserve',
  144. 'previous',
  145. 'processing-instruction',
  146. 'relationship',
  147. 'rename',
  148. 'replace',
  149. 'return',
  150. 'revalidation',
  151. 'same',
  152. 'satisfies',
  153. 'schema',
  154. 'schema-attribute',
  155. 'schema-element',
  156. 'score',
  157. 'self',
  158. 'sensitive',
  159. 'sentence',
  160. 'sentences',
  161. 'sequence',
  162. 'skip',
  163. 'sliding',
  164. 'some',
  165. 'stable',
  166. 'start',
  167. 'stemming',
  168. 'stop',
  169. 'strict',
  170. 'strip',
  171. 'switch',
  172. 'text',
  173. 'then',
  174. 'thesaurus',
  175. 'times',
  176. 'to',
  177. 'transform',
  178. 'treat',
  179. 'try',
  180. 'tumbling',
  181. 'type',
  182. 'typeswitch',
  183. 'union',
  184. 'unordered',
  185. 'update',
  186. 'updating',
  187. 'uppercase',
  188. 'using',
  189. 'validate',
  190. 'value',
  191. 'variable',
  192. 'version',
  193. 'weight',
  194. 'when',
  195. 'where',
  196. 'wildcards',
  197. 'window',
  198. 'with',
  199. 'without',
  200. 'word',
  201. 'words',
  202. 'xquery',
  203. ]
  204. for (var i = 0, l = basic.length; i < l; i++) {
  205. kwObj[basic[i]] = kw(basic[i])
  206. }
  207. // a list of types. For each add a property to kwObj with the value of
  208. // {type: "atom", style: "atom"}
  209. var types = [
  210. 'xs:anyAtomicType',
  211. 'xs:anySimpleType',
  212. 'xs:anyType',
  213. 'xs:anyURI',
  214. 'xs:base64Binary',
  215. 'xs:boolean',
  216. 'xs:byte',
  217. 'xs:date',
  218. 'xs:dateTime',
  219. 'xs:dateTimeStamp',
  220. 'xs:dayTimeDuration',
  221. 'xs:decimal',
  222. 'xs:double',
  223. 'xs:duration',
  224. 'xs:ENTITIES',
  225. 'xs:ENTITY',
  226. 'xs:float',
  227. 'xs:gDay',
  228. 'xs:gMonth',
  229. 'xs:gMonthDay',
  230. 'xs:gYear',
  231. 'xs:gYearMonth',
  232. 'xs:hexBinary',
  233. 'xs:ID',
  234. 'xs:IDREF',
  235. 'xs:IDREFS',
  236. 'xs:int',
  237. 'xs:integer',
  238. 'xs:item',
  239. 'xs:java',
  240. 'xs:language',
  241. 'xs:long',
  242. 'xs:Name',
  243. 'xs:NCName',
  244. 'xs:negativeInteger',
  245. 'xs:NMTOKEN',
  246. 'xs:NMTOKENS',
  247. 'xs:nonNegativeInteger',
  248. 'xs:nonPositiveInteger',
  249. 'xs:normalizedString',
  250. 'xs:NOTATION',
  251. 'xs:numeric',
  252. 'xs:positiveInteger',
  253. 'xs:precisionDecimal',
  254. 'xs:QName',
  255. 'xs:short',
  256. 'xs:string',
  257. 'xs:time',
  258. 'xs:token',
  259. 'xs:unsignedByte',
  260. 'xs:unsignedInt',
  261. 'xs:unsignedLong',
  262. 'xs:unsignedShort',
  263. 'xs:untyped',
  264. 'xs:untypedAtomic',
  265. 'xs:yearMonthDuration',
  266. ]
  267. for (var i = 0, l = types.length; i < l; i++) {
  268. kwObj[types[i]] = atom
  269. }
  270. // each operator will add a property to kwObj with value of {type: "operator", style: "keyword"}
  271. var operators = ['eq', 'ne', 'lt', 'le', 'gt', 'ge', ':=', '=', '>', '>=', '<', '<=', '.', '|', '?', 'and', 'or', 'div', 'idiv', 'mod', '*', '/', '+', '-']
  272. for (var i = 0, l = operators.length; i < l; i++) {
  273. kwObj[operators[i]] = operator
  274. }
  275. // each axis_specifiers will add a property to kwObj with value of {type: "axis_specifier", style: "qualifier"}
  276. var axis_specifiers = [
  277. 'self::',
  278. 'attribute::',
  279. 'child::',
  280. 'descendant::',
  281. 'descendant-or-self::',
  282. 'parent::',
  283. 'ancestor::',
  284. 'ancestor-or-self::',
  285. 'following::',
  286. 'preceding::',
  287. 'following-sibling::',
  288. 'preceding-sibling::',
  289. ]
  290. for (var i = 0, l = axis_specifiers.length; i < l; i++) {
  291. kwObj[axis_specifiers[i]] = qualifier
  292. }
  293. return kwObj
  294. })()
  295. function chain(stream, state, f) {
  296. state.tokenize = f
  297. return f(stream, state)
  298. }
  299. // the primary mode tokenizer
  300. function tokenBase(stream, state) {
  301. var ch = stream.next(),
  302. mightBeFunction = false,
  303. isEQName = isEQNameAhead(stream)
  304. // an XML tag (if not in some sub, chained tokenizer)
  305. if (ch == '<') {
  306. if (stream.match('!--', true)) return chain(stream, state, tokenXMLComment)
  307. if (stream.match('![CDATA', false)) {
  308. state.tokenize = tokenCDATA
  309. return 'tag'
  310. }
  311. if (stream.match('?', false)) {
  312. return chain(stream, state, tokenPreProcessing)
  313. }
  314. var isclose = stream.eat('/')
  315. stream.eatSpace()
  316. var tagName = '',
  317. c
  318. while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c
  319. return chain(stream, state, tokenTag(tagName, isclose))
  320. }
  321. // start code block
  322. else if (ch == '{') {
  323. pushStateStack(state, { type: 'codeblock' })
  324. return null
  325. }
  326. // end code block
  327. else if (ch == '}') {
  328. popStateStack(state)
  329. return null
  330. }
  331. // if we're in an XML block
  332. else if (isInXmlBlock(state)) {
  333. if (ch == '>') return 'tag'
  334. else if (ch == '/' && stream.eat('>')) {
  335. popStateStack(state)
  336. return 'tag'
  337. } else return 'variable'
  338. }
  339. // if a number
  340. else if (/\d/.test(ch)) {
  341. stream.match(/^\d*(?:\.\d*)?(?:E[+\-]?\d+)?/)
  342. return 'atom'
  343. }
  344. // comment start
  345. else if (ch === '(' && stream.eat(':')) {
  346. pushStateStack(state, { type: 'comment' })
  347. return chain(stream, state, tokenComment)
  348. }
  349. // quoted string
  350. else if (!isEQName && (ch === '"' || ch === "'")) return chain(stream, state, tokenString(ch))
  351. // variable
  352. else if (ch === '$') {
  353. return chain(stream, state, tokenVariable)
  354. }
  355. // assignment
  356. else if (ch === ':' && stream.eat('=')) {
  357. return 'keyword'
  358. }
  359. // open paren
  360. else if (ch === '(') {
  361. pushStateStack(state, { type: 'paren' })
  362. return null
  363. }
  364. // close paren
  365. else if (ch === ')') {
  366. popStateStack(state)
  367. return null
  368. }
  369. // open paren
  370. else if (ch === '[') {
  371. pushStateStack(state, { type: 'bracket' })
  372. return null
  373. }
  374. // close paren
  375. else if (ch === ']') {
  376. popStateStack(state)
  377. return null
  378. } else {
  379. var known = keywords.propertyIsEnumerable(ch) && keywords[ch]
  380. // if there's a EQName ahead, consume the rest of the string portion, it's likely a function
  381. if (isEQName && ch === '"') while (stream.next() !== '"') {}
  382. if (isEQName && ch === "'") while (stream.next() !== "'") {}
  383. // gobble up a word if the character is not known
  384. if (!known) stream.eatWhile(/[\w\$_-]/)
  385. // gobble a colon in the case that is a lib func type call fn:doc
  386. var foundColon = stream.eat(':')
  387. // if there's not a second colon, gobble another word. Otherwise, it's probably an axis specifier
  388. // which should get matched as a keyword
  389. if (!stream.eat(':') && foundColon) {
  390. stream.eatWhile(/[\w\$_-]/)
  391. }
  392. // if the next non whitespace character is an open paren, this is probably a function (if not a keyword of other sort)
  393. if (stream.match(/^[ \t]*\(/, false)) {
  394. mightBeFunction = true
  395. }
  396. // is the word a keyword?
  397. var word = stream.current()
  398. known = keywords.propertyIsEnumerable(word) && keywords[word]
  399. // if we think it's a function call but not yet known,
  400. // set style to variable for now for lack of something better
  401. if (mightBeFunction && !known) known = { type: 'function_call', style: 'variable def' }
  402. // if the previous word was element, attribute, axis specifier, this word should be the name of that
  403. if (isInXmlConstructor(state)) {
  404. popStateStack(state)
  405. return 'variable'
  406. }
  407. // as previously checked, if the word is element,attribute, axis specifier, call it an "xmlconstructor" and
  408. // push the stack so we know to look for it on the next word
  409. if (word == 'element' || word == 'attribute' || known.type == 'axis_specifier') pushStateStack(state, { type: 'xmlconstructor' })
  410. // if the word is known, return the details of that else just call this a generic 'word'
  411. return known ? known.style : 'variable'
  412. }
  413. }
  414. // handle comments, including nested
  415. function tokenComment(stream, state) {
  416. var maybeEnd = false,
  417. maybeNested = false,
  418. nestedCount = 0,
  419. ch
  420. while ((ch = stream.next())) {
  421. if (ch == ')' && maybeEnd) {
  422. if (nestedCount > 0) nestedCount--
  423. else {
  424. popStateStack(state)
  425. break
  426. }
  427. } else if (ch == ':' && maybeNested) {
  428. nestedCount++
  429. }
  430. maybeEnd = ch == ':'
  431. maybeNested = ch == '('
  432. }
  433. return 'comment'
  434. }
  435. // tokenizer for string literals
  436. // optionally pass a tokenizer function to set state.tokenize back to when finished
  437. function tokenString(quote, f) {
  438. return function (stream, state) {
  439. var ch
  440. if (isInString(state) && stream.current() == quote) {
  441. popStateStack(state)
  442. if (f) state.tokenize = f
  443. return 'string'
  444. }
  445. pushStateStack(state, { type: 'string', name: quote, tokenize: tokenString(quote, f) })
  446. // if we're in a string and in an XML block, allow an embedded code block
  447. if (stream.match('{', false) && isInXmlAttributeBlock(state)) {
  448. state.tokenize = tokenBase
  449. return 'string'
  450. }
  451. while ((ch = stream.next())) {
  452. if (ch == quote) {
  453. popStateStack(state)
  454. if (f) state.tokenize = f
  455. break
  456. } else {
  457. // if we're in a string and in an XML block, allow an embedded code block in an attribute
  458. if (stream.match('{', false) && isInXmlAttributeBlock(state)) {
  459. state.tokenize = tokenBase
  460. return 'string'
  461. }
  462. }
  463. }
  464. return 'string'
  465. }
  466. }
  467. // tokenizer for variables
  468. function tokenVariable(stream, state) {
  469. var isVariableChar = /[\w\$_-]/
  470. // a variable may start with a quoted EQName so if the next character is quote, consume to the next quote
  471. if (stream.eat('"')) {
  472. while (stream.next() !== '"') {}
  473. stream.eat(':')
  474. } else {
  475. stream.eatWhile(isVariableChar)
  476. if (!stream.match(':=', false)) stream.eat(':')
  477. }
  478. stream.eatWhile(isVariableChar)
  479. state.tokenize = tokenBase
  480. return 'variable'
  481. }
  482. // tokenizer for XML tags
  483. function tokenTag(name, isclose) {
  484. return function (stream, state) {
  485. stream.eatSpace()
  486. if (isclose && stream.eat('>')) {
  487. popStateStack(state)
  488. state.tokenize = tokenBase
  489. return 'tag'
  490. }
  491. // self closing tag without attributes?
  492. if (!stream.eat('/')) pushStateStack(state, { type: 'tag', name: name, tokenize: tokenBase })
  493. if (!stream.eat('>')) {
  494. state.tokenize = tokenAttribute
  495. return 'tag'
  496. } else {
  497. state.tokenize = tokenBase
  498. }
  499. return 'tag'
  500. }
  501. }
  502. // tokenizer for XML attributes
  503. function tokenAttribute(stream, state) {
  504. var ch = stream.next()
  505. if (ch == '/' && stream.eat('>')) {
  506. if (isInXmlAttributeBlock(state)) popStateStack(state)
  507. if (isInXmlBlock(state)) popStateStack(state)
  508. return 'tag'
  509. }
  510. if (ch == '>') {
  511. if (isInXmlAttributeBlock(state)) popStateStack(state)
  512. return 'tag'
  513. }
  514. if (ch == '=') return null
  515. // quoted string
  516. if (ch == '"' || ch == "'") return chain(stream, state, tokenString(ch, tokenAttribute))
  517. if (!isInXmlAttributeBlock(state)) pushStateStack(state, { type: 'attribute', tokenize: tokenAttribute })
  518. stream.eat(/[a-zA-Z_:]/)
  519. stream.eatWhile(/[-a-zA-Z0-9_:.]/)
  520. stream.eatSpace()
  521. // the case where the attribute has not value and the tag was closed
  522. if (stream.match('>', false) || stream.match('/', false)) {
  523. popStateStack(state)
  524. state.tokenize = tokenBase
  525. }
  526. return 'attribute'
  527. }
  528. // handle comments, including nested
  529. function tokenXMLComment(stream, state) {
  530. var ch
  531. while ((ch = stream.next())) {
  532. if (ch == '-' && stream.match('->', true)) {
  533. state.tokenize = tokenBase
  534. return 'comment'
  535. }
  536. }
  537. }
  538. // handle CDATA
  539. function tokenCDATA(stream, state) {
  540. var ch
  541. while ((ch = stream.next())) {
  542. if (ch == ']' && stream.match(']', true)) {
  543. state.tokenize = tokenBase
  544. return 'comment'
  545. }
  546. }
  547. }
  548. // handle preprocessing instructions
  549. function tokenPreProcessing(stream, state) {
  550. var ch
  551. while ((ch = stream.next())) {
  552. if (ch == '?' && stream.match('>', true)) {
  553. state.tokenize = tokenBase
  554. return 'comment meta'
  555. }
  556. }
  557. }
  558. // functions to test the current context of the state
  559. function isInXmlBlock(state) {
  560. return isIn(state, 'tag')
  561. }
  562. function isInXmlAttributeBlock(state) {
  563. return isIn(state, 'attribute')
  564. }
  565. function isInXmlConstructor(state) {
  566. return isIn(state, 'xmlconstructor')
  567. }
  568. function isInString(state) {
  569. return isIn(state, 'string')
  570. }
  571. function isEQNameAhead(stream) {
  572. // assume we've already eaten a quote (")
  573. if (stream.current() === '"') return stream.match(/^[^\"]+\"\:/, false)
  574. else if (stream.current() === "'") return stream.match(/^[^\"]+\'\:/, false)
  575. else return false
  576. }
  577. function isIn(state, type) {
  578. return state.stack.length && state.stack[state.stack.length - 1].type == type
  579. }
  580. function pushStateStack(state, newState) {
  581. state.stack.push(newState)
  582. }
  583. function popStateStack(state) {
  584. state.stack.pop()
  585. var reinstateTokenize = state.stack.length && state.stack[state.stack.length - 1].tokenize
  586. state.tokenize = reinstateTokenize || tokenBase
  587. }
  588. // the interface for the mode API
  589. return {
  590. startState: function () {
  591. return {
  592. tokenize: tokenBase,
  593. cc: [],
  594. stack: [],
  595. }
  596. },
  597. token: function (stream, state) {
  598. if (stream.eatSpace()) return null
  599. var style = state.tokenize(stream, state)
  600. return style
  601. },
  602. blockCommentStart: '(:',
  603. blockCommentEnd: ':)',
  604. }
  605. })
  606. CodeMirror.defineMIME('application/xquery', 'xquery')
  607. })