source: node_modules/refractor/lang/xquery.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[d24f17c]1'use strict'
2
3module.exports = xquery
4xquery.displayName = 'xquery'
5xquery.aliases = []
6function xquery(Prism) {
7 ;(function (Prism) {
8 Prism.languages.xquery = Prism.languages.extend('markup', {
9 'xquery-comment': {
10 pattern: /\(:[\s\S]*?:\)/,
11 greedy: true,
12 alias: 'comment'
13 },
14 string: {
15 pattern: /(["'])(?:\1\1|(?!\1)[\s\S])*\1/,
16 greedy: true
17 },
18 extension: {
19 pattern: /\(#.+?#\)/,
20 alias: 'symbol'
21 },
22 variable: /\$[-\w:]+/,
23 axis: {
24 pattern:
25 /(^|[^-])(?:ancestor(?:-or-self)?|attribute|child|descendant(?:-or-self)?|following(?:-sibling)?|parent|preceding(?:-sibling)?|self)(?=::)/,
26 lookbehind: true,
27 alias: 'operator'
28 },
29 'keyword-operator': {
30 pattern:
31 /(^|[^:-])\b(?:and|castable as|div|eq|except|ge|gt|idiv|instance of|intersect|is|le|lt|mod|ne|or|union)\b(?=$|[^:-])/,
32 lookbehind: true,
33 alias: 'operator'
34 },
35 keyword: {
36 pattern:
37 /(^|[^:-])\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(?=$|[^:-])/,
38 lookbehind: true
39 },
40 function: /[\w-]+(?::[\w-]+)*(?=\s*\()/,
41 'xquery-element': {
42 pattern: /(element\s+)[\w-]+(?::[\w-]+)*/,
43 lookbehind: true,
44 alias: 'tag'
45 },
46 'xquery-attribute': {
47 pattern: /(attribute\s+)[\w-]+(?::[\w-]+)*/,
48 lookbehind: true,
49 alias: 'attr-name'
50 },
51 builtin: {
52 pattern:
53 /(^|[^:-])\b(?:attribute|comment|document|element|processing-instruction|text|xs:(?:ENTITIES|ENTITY|ID|IDREFS?|NCName|NMTOKENS?|NOTATION|Name|QName|anyAtomicType|anyType|anyURI|base64Binary|boolean|byte|date|dateTime|dayTimeDuration|decimal|double|duration|float|gDay|gMonth|gMonthDay|gYear|gYearMonth|hexBinary|int|integer|language|long|negativeInteger|nonNegativeInteger|nonPositiveInteger|normalizedString|positiveInteger|short|string|time|token|unsigned(?:Byte|Int|Long|Short)|untyped(?:Atomic)?|yearMonthDuration))\b(?=$|[^:-])/,
54 lookbehind: true
55 },
56 number: /\b\d+(?:\.\d+)?(?:E[+-]?\d+)?/,
57 operator: [
58 /[+*=?|@]|\.\.?|:=|!=|<[=<]?|>[=>]?/,
59 {
60 pattern: /(\s)-(?=\s)/,
61 lookbehind: true
62 }
63 ],
64 punctuation: /[[\](){},;:/]/
65 })
66 Prism.languages.xquery.tag.pattern =
67 /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/
68 Prism.languages.xquery['tag'].inside['attr-value'].pattern =
69 /=(?:("|')(?:\\[\s\S]|\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}|(?!\1)[^\\])*\1|[^\s'">=]+)/
70 Prism.languages.xquery['tag'].inside['attr-value'].inside['punctuation'] =
71 /^="|"$/
72 Prism.languages.xquery['tag'].inside['attr-value'].inside['expression'] = {
73 // Allow for two levels of nesting
74 pattern: /\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}/,
75 inside: Prism.languages.xquery,
76 alias: 'language-xquery'
77 } // The following will handle plain text inside tags
78 var stringifyToken = function (token) {
79 if (typeof token === 'string') {
80 return token
81 }
82 if (typeof token.content === 'string') {
83 return token.content
84 }
85 return token.content.map(stringifyToken).join('')
86 }
87 var walkTokens = function (tokens) {
88 var openedTags = []
89 for (var i = 0; i < tokens.length; i++) {
90 var token = tokens[i]
91 var notTagNorBrace = false
92 if (typeof token !== 'string') {
93 if (
94 token.type === 'tag' &&
95 token.content[0] &&
96 token.content[0].type === 'tag'
97 ) {
98 // We found a tag, now find its kind
99 if (token.content[0].content[0].content === '</') {
100 // Closing tag
101 if (
102 openedTags.length > 0 &&
103 openedTags[openedTags.length - 1].tagName ===
104 stringifyToken(token.content[0].content[1])
105 ) {
106 // Pop matching opening tag
107 openedTags.pop()
108 }
109 } else {
110 if (token.content[token.content.length - 1].content === '/>') {
111 // Autoclosed tag, ignore
112 } else {
113 // Opening tag
114 openedTags.push({
115 tagName: stringifyToken(token.content[0].content[1]),
116 openedBraces: 0
117 })
118 }
119 }
120 } else if (
121 openedTags.length > 0 &&
122 token.type === 'punctuation' &&
123 token.content === '{' && // Ignore `{{`
124 (!tokens[i + 1] ||
125 tokens[i + 1].type !== 'punctuation' ||
126 tokens[i + 1].content !== '{') &&
127 (!tokens[i - 1] ||
128 tokens[i - 1].type !== 'plain-text' ||
129 tokens[i - 1].content !== '{')
130 ) {
131 // Here we might have entered an XQuery expression inside a tag
132 openedTags[openedTags.length - 1].openedBraces++
133 } else if (
134 openedTags.length > 0 &&
135 openedTags[openedTags.length - 1].openedBraces > 0 &&
136 token.type === 'punctuation' &&
137 token.content === '}'
138 ) {
139 // Here we might have left an XQuery expression inside a tag
140 openedTags[openedTags.length - 1].openedBraces--
141 } else if (token.type !== 'comment') {
142 notTagNorBrace = true
143 }
144 }
145 if (notTagNorBrace || typeof token === 'string') {
146 if (
147 openedTags.length > 0 &&
148 openedTags[openedTags.length - 1].openedBraces === 0
149 ) {
150 // Here we are inside a tag, and not inside an XQuery expression.
151 // That's plain text: drop any tokens matched.
152 var plainText = stringifyToken(token) // And merge text with adjacent text
153 if (
154 i < tokens.length - 1 &&
155 (typeof tokens[i + 1] === 'string' ||
156 tokens[i + 1].type === 'plain-text')
157 ) {
158 plainText += stringifyToken(tokens[i + 1])
159 tokens.splice(i + 1, 1)
160 }
161 if (
162 i > 0 &&
163 (typeof tokens[i - 1] === 'string' ||
164 tokens[i - 1].type === 'plain-text')
165 ) {
166 plainText = stringifyToken(tokens[i - 1]) + plainText
167 tokens.splice(i - 1, 1)
168 i--
169 }
170 if (/^\s+$/.test(plainText)) {
171 tokens[i] = plainText
172 } else {
173 tokens[i] = new Prism.Token(
174 'plain-text',
175 plainText,
176 null,
177 plainText
178 )
179 }
180 }
181 }
182 if (token.content && typeof token.content !== 'string') {
183 walkTokens(token.content)
184 }
185 }
186 }
187 Prism.hooks.add('after-tokenize', function (env) {
188 if (env.language !== 'xquery') {
189 return
190 }
191 walkTokens(env.tokens)
192 })
193 })(Prism)
194}
Note: See TracBrowser for help on using the repository browser.