]> luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/xquery.js
Initial commit.
[flow-web.git] / static / highlight / languages / xquery.js
1 /*! `xquery` grammar compiled for Highlight.js 11.11.1 */
2 (function(){
3 var hljsGrammar = (function () {
4 'use strict';
5
6 /*
7 Language: XQuery
8 Author: Dirk Kirsten <dk@basex.org>
9 Contributor: Duncan Paterson
10 Description: Supports XQuery 3.1 including XQuery Update 3, so also XPath (as it is a superset)
11 Refactored to process xml constructor syntax and function-bodies. Added missing data-types, xpath operands, inbuilt functions, and query prologs
12 Website: https://www.w3.org/XML/Query/
13 Category: functional
14 Audit: 2020
15 */
16
17 /** @type LanguageFn */
18 function xquery(_hljs) {
19 // see https://www.w3.org/TR/xquery/#id-terminal-delimitation
20 const KEYWORDS = [
21 "module",
22 "schema",
23 "namespace",
24 "boundary-space",
25 "preserve",
26 "no-preserve",
27 "strip",
28 "default",
29 "collation",
30 "base-uri",
31 "ordering",
32 "context",
33 "decimal-format",
34 "decimal-separator",
35 "copy-namespaces",
36 "empty-sequence",
37 "except",
38 "exponent-separator",
39 "external",
40 "grouping-separator",
41 "inherit",
42 "no-inherit",
43 "lax",
44 "minus-sign",
45 "per-mille",
46 "percent",
47 "schema-attribute",
48 "schema-element",
49 "strict",
50 "unordered",
51 "zero-digit",
52 "declare",
53 "import",
54 "option",
55 "function",
56 "validate",
57 "variable",
58 "for",
59 "at",
60 "in",
61 "let",
62 "where",
63 "order",
64 "group",
65 "by",
66 "return",
67 "if",
68 "then",
69 "else",
70 "tumbling",
71 "sliding",
72 "window",
73 "start",
74 "when",
75 "only",
76 "end",
77 "previous",
78 "next",
79 "stable",
80 "ascending",
81 "descending",
82 "allowing",
83 "empty",
84 "greatest",
85 "least",
86 "some",
87 "every",
88 "satisfies",
89 "switch",
90 "case",
91 "typeswitch",
92 "try",
93 "catch",
94 "and",
95 "or",
96 "to",
97 "union",
98 "intersect",
99 "instance",
100 "of",
101 "treat",
102 "as",
103 "castable",
104 "cast",
105 "map",
106 "array",
107 "delete",
108 "insert",
109 "into",
110 "replace",
111 "value",
112 "rename",
113 "copy",
114 "modify",
115 "update"
116 ];
117
118 // Node Types (sorted by inheritance)
119 // atomic types (sorted by inheritance)
120 const TYPES = [
121 "item",
122 "document-node",
123 "node",
124 "attribute",
125 "document",
126 "element",
127 "comment",
128 "namespace",
129 "namespace-node",
130 "processing-instruction",
131 "text",
132 "construction",
133 "xs:anyAtomicType",
134 "xs:untypedAtomic",
135 "xs:duration",
136 "xs:time",
137 "xs:decimal",
138 "xs:float",
139 "xs:double",
140 "xs:gYearMonth",
141 "xs:gYear",
142 "xs:gMonthDay",
143 "xs:gMonth",
144 "xs:gDay",
145 "xs:boolean",
146 "xs:base64Binary",
147 "xs:hexBinary",
148 "xs:anyURI",
149 "xs:QName",
150 "xs:NOTATION",
151 "xs:dateTime",
152 "xs:dateTimeStamp",
153 "xs:date",
154 "xs:string",
155 "xs:normalizedString",
156 "xs:token",
157 "xs:language",
158 "xs:NMTOKEN",
159 "xs:Name",
160 "xs:NCName",
161 "xs:ID",
162 "xs:IDREF",
163 "xs:ENTITY",
164 "xs:integer",
165 "xs:nonPositiveInteger",
166 "xs:negativeInteger",
167 "xs:long",
168 "xs:int",
169 "xs:short",
170 "xs:byte",
171 "xs:nonNegativeInteger",
172 "xs:unisignedLong",
173 "xs:unsignedInt",
174 "xs:unsignedShort",
175 "xs:unsignedByte",
176 "xs:positiveInteger",
177 "xs:yearMonthDuration",
178 "xs:dayTimeDuration"
179 ];
180
181 const LITERALS = [
182 "eq",
183 "ne",
184 "lt",
185 "le",
186 "gt",
187 "ge",
188 "is",
189 "self::",
190 "child::",
191 "descendant::",
192 "descendant-or-self::",
193 "attribute::",
194 "following::",
195 "following-sibling::",
196 "parent::",
197 "ancestor::",
198 "ancestor-or-self::",
199 "preceding::",
200 "preceding-sibling::",
201 "NaN"
202 ];
203
204 // functions (TODO: find regex for op: without breaking build)
205 const BUILT_IN = {
206 className: 'built_in',
207 variants: [
208 {
209 begin: /\barray:/,
210 end: /(?:append|filter|flatten|fold-(?:left|right)|for-each(?:-pair)?|get|head|insert-before|join|put|remove|reverse|size|sort|subarray|tail)\b/
211 },
212 {
213 begin: /\bmap:/,
214 end: /(?:contains|entry|find|for-each|get|keys|merge|put|remove|size)\b/
215 },
216 {
217 begin: /\bmath:/,
218 end: /(?:a(?:cos|sin|tan[2]?)|cos|exp(?:10)?|log(?:10)?|pi|pow|sin|sqrt|tan)\b/
219 },
220 {
221 begin: /\bop:/,
222 end: /\(/,
223 excludeEnd: true
224 },
225 {
226 begin: /\bfn:/,
227 end: /\(/,
228 excludeEnd: true
229 },
230 // do not highlight inbuilt strings as variable or xml element names
231 { begin: /[^</$:'"-]\b(?:abs|accumulator-(?:after|before)|adjust-(?:date(?:Time)?|time)-to-timezone|analyze-string|apply|available-(?:environment-variables|system-properties)|avg|base-uri|boolean|ceiling|codepoints?-(?:equal|to-string)|collation-key|collection|compare|concat|contains(?:-token)?|copy-of|count|current(?:-)?(?:date(?:Time)?|time|group(?:ing-key)?|output-uri|merge-(?:group|key))?data|dateTime|days?-from-(?:date(?:Time)?|duration)|deep-equal|default-(?:collation|language)|distinct-values|document(?:-uri)?|doc(?:-available)?|element-(?:available|with-id)|empty|encode-for-uri|ends-with|environment-variable|error|escape-html-uri|exactly-one|exists|false|filter|floor|fold-(?:left|right)|for-each(?:-pair)?|format-(?:date(?:Time)?|time|integer|number)|function-(?:arity|available|lookup|name)|generate-id|has-children|head|hours-from-(?:dateTime|duration|time)|id(?:ref)?|implicit-timezone|in-scope-prefixes|index-of|innermost|insert-before|iri-to-uri|json-(?:doc|to-xml)|key|lang|last|load-xquery-module|local-name(?:-from-QName)?|(?:lower|upper)-case|matches|max|minutes-from-(?:dateTime|duration|time)|min|months?-from-(?:date(?:Time)?|duration)|name(?:space-uri-?(?:for-prefix|from-QName)?)?|nilled|node-name|normalize-(?:space|unicode)|not|number|one-or-more|outermost|parse-(?:ietf-date|json)|path|position|(?:prefix-from-)?QName|random-number-generator|regex-group|remove|replace|resolve-(?:QName|uri)|reverse|root|round(?:-half-to-even)?|seconds-from-(?:dateTime|duration|time)|snapshot|sort|starts-with|static-base-uri|stream-available|string-?(?:join|length|to-codepoints)?|subsequence|substring-?(?:after|before)?|sum|system-property|tail|timezone-from-(?:date(?:Time)?|time)|tokenize|trace|trans(?:form|late)|true|type-available|unordered|unparsed-(?:entity|text)?-?(?:public-id|uri|available|lines)?|uri-collection|xml-to-json|years?-from-(?:date(?:Time)?|duration)|zero-or-one)\b/ },
232 {
233 begin: /\blocal:/,
234 end: /\(/,
235 excludeEnd: true
236 },
237 {
238 begin: /\bzip:/,
239 end: /(?:zip-file|(?:xml|html|text|binary)-entry| (?:update-)?entries)\b/
240 },
241 {
242 begin: /\b(?:util|db|functx|app|xdmp|xmldb):/,
243 end: /\(/,
244 excludeEnd: true
245 }
246 ]
247 };
248
249 const TITLE = {
250 className: 'title',
251 begin: /\bxquery version "[13]\.[01]"\s?(?:encoding ".+")?/,
252 end: /;/
253 };
254
255 const VAR = {
256 className: 'variable',
257 begin: /[$][\w\-:]+/
258 };
259
260 const NUMBER = {
261 className: 'number',
262 begin: /(\b0[0-7_]+)|(\b0x[0-9a-fA-F_]+)|(\b[1-9][0-9_]*(\.[0-9_]+)?)|[0_]\b/,
263 relevance: 0
264 };
265
266 const STRING = {
267 className: 'string',
268 variants: [
269 {
270 begin: /"/,
271 end: /"/,
272 contains: [
273 {
274 begin: /""/,
275 relevance: 0
276 }
277 ]
278 },
279 {
280 begin: /'/,
281 end: /'/,
282 contains: [
283 {
284 begin: /''/,
285 relevance: 0
286 }
287 ]
288 }
289 ]
290 };
291
292 const ANNOTATION = {
293 className: 'meta',
294 begin: /%[\w\-:]+/
295 };
296
297 const COMMENT = {
298 className: 'comment',
299 begin: /\(:/,
300 end: /:\)/,
301 relevance: 10,
302 contains: [
303 {
304 className: 'doctag',
305 begin: /@\w+/
306 }
307 ]
308 };
309
310 // see https://www.w3.org/TR/xquery/#id-computedConstructors
311 // mocha: computed_inbuilt
312 // see https://www.regexpal.com/?fam=99749
313 const COMPUTED = {
314 beginKeywords: 'element attribute comment document processing-instruction',
315 end: /\{/,
316 excludeEnd: true
317 };
318
319 // mocha: direct_method
320 const DIRECT = {
321 begin: /<([\w._:-]+)(\s+\S*=('|").*('|"))?>/,
322 end: /(\/[\w._:-]+>)/,
323 subLanguage: 'xml',
324 contains: [
325 {
326 begin: /\{/,
327 end: /\}/,
328 subLanguage: 'xquery'
329 },
330 'self'
331 ]
332 };
333
334 const CONTAINS = [
335 VAR,
336 BUILT_IN,
337 STRING,
338 NUMBER,
339 COMMENT,
340 ANNOTATION,
341 TITLE,
342 COMPUTED,
343 DIRECT
344 ];
345
346 return {
347 name: 'XQuery',
348 aliases: [
349 'xpath',
350 'xq',
351 'xqm'
352 ],
353 case_insensitive: false,
354 illegal: /(proc)|(abstract)|(extends)|(until)|(#)/,
355 keywords: {
356 $pattern: /[a-zA-Z$][a-zA-Z0-9_:-]*/,
357 keyword: KEYWORDS,
358 type: TYPES,
359 literal: LITERALS
360 },
361 contains: CONTAINS
362 };
363 }
364
365 return xquery;
366
367 })();
368
369 hljs.registerLanguage('xquery', hljsGrammar);
370 })();