1 | (function (Prism) {
|
2 |
|
3 | Prism.languages.xquery = Prism.languages.extend('markup', {
|
4 | 'xquery-comment': {
|
5 | pattern: /\(:[\s\S]*?:\)/,
|
6 | greedy: true,
|
7 | alias: "comment"
|
8 | },
|
9 | 'string': {
|
10 | pattern: /(["'])(?:\1\1|(?!\1)[\s\S])*\1/,
|
11 | greedy: true
|
12 | },
|
13 | 'extension': {
|
14 | pattern: /\(#.+?#\)/,
|
15 | alias: 'symbol'
|
16 | },
|
17 | 'variable': /\$[\w-:]+/,
|
18 | 'axis': {
|
19 | pattern: /(^|[^-])(?:ancestor(?:-or-self)?|attribute|child|descendant(?:-or-self)?|following(?:-sibling)?|parent|preceding(?:-sibling)?|self)(?=::)/,
|
20 | lookbehind: true,
|
21 | alias: 'operator'
|
22 | },
|
23 | 'keyword-operator': {
|
24 | pattern: /(^|[^:-])\b(?:and|castable as|div|eq|except|ge|gt|idiv|instance of|intersect|is|le|lt|mod|ne|or|union)\b(?=$|[^:-])/,
|
25 | lookbehind: true,
|
26 | alias: 'operator'
|
27 | },
|
28 | 'keyword': {
|
29 | pattern: /(^|[^:-])\b(?:as|ascending|at|base-uri|boundary-space|case|cast as|collation|construction|copy-namespaces|declare|default|descending|else|empty (?:greatest|least)|encoding|every|external|for|function|if|import|in|inherit|lax|let|map|module|namespace|no-inherit|no-preserve|option|order(?: by|ed|ing)?|preserve|return|satisfies|schema|some|stable|strict|strip|then|to|treat as|typeswitch|unordered|validate|variable|version|where|xquery)\b(?=$|[^:-])/,
|
30 | lookbehind: true
|
31 | },
|
32 | 'function': /[\w-]+(?::[\w-]+)*(?=\s*\()/,
|
33 | 'xquery-element': {
|
34 | pattern: /(element\s+)[\w-]+(?::[\w-]+)*/,
|
35 | lookbehind: true,
|
36 | alias: 'tag'
|
37 | },
|
38 | 'xquery-attribute': {
|
39 | pattern: /(attribute\s+)[\w-]+(?::[\w-]+)*/,
|
40 | lookbehind: true,
|
41 | alias: 'attr-name'
|
42 | },
|
43 | 'builtin': {
|
44 | pattern: /(^|[^:-])\b(?:attribute|comment|document|element|processing-instruction|text|xs:(?:anyAtomicType|anyType|anyURI|base64Binary|boolean|byte|date|dateTime|dayTimeDuration|decimal|double|duration|ENTITIES|ENTITY|float|gDay|gMonth|gMonthDay|gYear|gYearMonth|hexBinary|ID|IDREFS?|int|integer|language|long|Name|NCName|negativeInteger|NMTOKENS?|nonNegativeInteger|nonPositiveInteger|normalizedString|NOTATION|positiveInteger|QName|short|string|time|token|unsigned(?:Byte|Int|Long|Short)|untyped(?:Atomic)?|yearMonthDuration))\b(?=$|[^:-])/,
|
45 | lookbehind: true
|
46 | },
|
47 | 'number': /\b\d+(?:\.\d+)?(?:E[+-]?\d+)?/,
|
48 | 'operator': [
|
49 | /[+*=?|@]|\.\.?|:=|!=|<[=<]?|>[=>]?/,
|
50 | {
|
51 | pattern: /(\s)-(?=\s)/,
|
52 | lookbehind: true
|
53 | }
|
54 | ],
|
55 | 'punctuation': /[[\](){},;:/]/
|
56 | });
|
57 |
|
58 | Prism.languages.xquery.tag.pattern = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|{(?!{)(?:{(?:{[^}]*}|[^}])*}|[^}])+}|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/i;
|
59 | Prism.languages.xquery['tag'].inside['attr-value'].pattern = /=(?:("|')(?:\\[\s\S]|{(?!{)(?:{(?:{[^}]*}|[^}])*}|[^}])+}|(?!\1)[^\\])*\1|[^\s'">=]+)/i;
|
60 | Prism.languages.xquery['tag'].inside['attr-value'].inside['punctuation'] = /^="|"$/;
|
61 | Prism.languages.xquery['tag'].inside['attr-value'].inside['expression'] = {
|
62 | // Allow for two levels of nesting
|
63 | pattern: /{(?!{)(?:{(?:{[^}]*}|[^}])*}|[^}])+}/,
|
64 | inside: {
|
65 | rest: Prism.languages.xquery
|
66 | },
|
67 | 'alias': 'language-xquery'
|
68 | };
|
69 |
|
70 | // The following will handle plain text inside tags
|
71 | var stringifyToken = function (token) {
|
72 | if (typeof token === 'string') {
|
73 | return token;
|
74 | }
|
75 | if (typeof token.content === 'string') {
|
76 | return token.content;
|
77 | }
|
78 | return token.content.map(stringifyToken).join('');
|
79 | };
|
80 |
|
81 | var walkTokens = function (tokens) {
|
82 | var openedTags = [];
|
83 | for (var i = 0; i < tokens.length; i++) {
|
84 | var token = tokens[i];
|
85 | var notTagNorBrace = false;
|
86 |
|
87 | if (typeof token !== 'string') {
|
88 | if (token.type === 'tag' && token.content[0] && token.content[0].type === 'tag') {
|
89 | // We found a tag, now find its kind
|
90 |
|
91 | if (token.content[0].content[0].content === '</') {
|
92 | // Closing tag
|
93 | if (openedTags.length > 0 && openedTags[openedTags.length - 1].tagName === stringifyToken(token.content[0].content[1])) {
|
94 | // Pop matching opening tag
|
95 | openedTags.pop();
|
96 | }
|
97 | } else {
|
98 | if (token.content[token.content.length - 1].content === '/>') {
|
99 | // Autoclosed tag, ignore
|
100 | } else {
|
101 | // Opening tag
|
102 | openedTags.push({
|
103 | tagName: stringifyToken(token.content[0].content[1]),
|
104 | openedBraces: 0
|
105 | });
|
106 | }
|
107 | }
|
108 | } else if (
|
109 | openedTags.length > 0 && token.type === 'punctuation' && token.content === '{' &&
|
110 | // Ignore `{{`
|
111 | (!tokens[i + 1] || tokens[i + 1].type !== 'punctuation' || tokens[i + 1].content !== '{') &&
|
112 | (!tokens[i - 1] || tokens[i - 1].type !== 'plain-text' || tokens[i - 1].content !== '{')
|
113 | ) {
|
114 | // Here we might have entered an XQuery expression inside a tag
|
115 | openedTags[openedTags.length - 1].openedBraces++;
|
116 |
|
117 | } else if (openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces > 0 && token.type === 'punctuation' && token.content === '}') {
|
118 |
|
119 | // Here we might have left an XQuery expression inside a tag
|
120 | openedTags[openedTags.length - 1].openedBraces--;
|
121 |
|
122 | } else if (token.type !== 'comment') {
|
123 | notTagNorBrace = true
|
124 | }
|
125 | }
|
126 | if (notTagNorBrace || typeof token === 'string') {
|
127 | if (openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces === 0) {
|
128 | // Here we are inside a tag, and not inside an XQuery expression.
|
129 | // That's plain text: drop any tokens matched.
|
130 | var plainText = stringifyToken(token);
|
131 |
|
132 | // And merge text with adjacent text
|
133 | if (i < tokens.length - 1 && (typeof tokens[i + 1] === 'string' || tokens[i + 1].type === 'plain-text')) {
|
134 | plainText += stringifyToken(tokens[i + 1]);
|
135 | tokens.splice(i + 1, 1);
|
136 | }
|
137 | if (i > 0 && (typeof tokens[i - 1] === 'string' || tokens[i - 1].type === 'plain-text')) {
|
138 | plainText = stringifyToken(tokens[i - 1]) + plainText;
|
139 | tokens.splice(i - 1, 1);
|
140 | i--;
|
141 | }
|
142 |
|
143 | if (/^\s+$/.test(plainText)) {
|
144 | tokens[i] = plainText;
|
145 | } else {
|
146 | tokens[i] = new Prism.Token('plain-text', plainText, null, plainText);
|
147 | }
|
148 | }
|
149 | }
|
150 |
|
151 | if (token.content && typeof token.content !== 'string') {
|
152 | walkTokens(token.content);
|
153 | }
|
154 | }
|
155 | };
|
156 |
|
157 | Prism.hooks.add('after-tokenize', function (env) {
|
158 | if (env.language !== 'xquery') {
|
159 | return;
|
160 | }
|
161 | walkTokens(env.tokens);
|
162 | });
|
163 |
|
164 | }(Prism)); |
\ | No newline at end of file |