1 | /**
|
---|
2 | * @param {string} value
|
---|
3 | * @returns {RegExp}
|
---|
4 | * */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @param {RegExp | string } re
|
---|
8 | * @returns {string}
|
---|
9 | */
|
---|
10 | function source(re) {
|
---|
11 | if (!re) return null;
|
---|
12 | if (typeof re === "string") return re;
|
---|
13 |
|
---|
14 | return re.source;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @param {...(RegExp | string) } args
|
---|
19 | * @returns {string}
|
---|
20 | */
|
---|
21 | function concat(...args) {
|
---|
22 | const joined = args.map((x) => source(x)).join("");
|
---|
23 | return joined;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /*
|
---|
27 | Language: QML
|
---|
28 | Requires: javascript.js, xml.js
|
---|
29 | Author: John Foster <jfoster@esri.com>
|
---|
30 | Description: Syntax highlighting for the Qt Quick QML scripting language, based mostly off
|
---|
31 | the JavaScript parser.
|
---|
32 | Website: https://doc.qt.io/qt-5/qmlapplications.html
|
---|
33 | Category: scripting
|
---|
34 | */
|
---|
35 |
|
---|
36 | function qml(hljs) {
|
---|
37 | const KEYWORDS = {
|
---|
38 | keyword:
|
---|
39 | 'in of on if for while finally var new function do return void else break catch ' +
|
---|
40 | 'instanceof with throw case default try this switch continue typeof delete ' +
|
---|
41 | 'let yield const export super debugger as async await import',
|
---|
42 | literal:
|
---|
43 | 'true false null undefined NaN Infinity',
|
---|
44 | built_in:
|
---|
45 | 'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' +
|
---|
46 | 'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' +
|
---|
47 | 'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' +
|
---|
48 | 'TypeError URIError Number Math Date String RegExp Array Float32Array ' +
|
---|
49 | 'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
|
---|
50 | 'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
|
---|
51 | 'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' +
|
---|
52 | 'Behavior bool color coordinate date double enumeration font geocircle georectangle ' +
|
---|
53 | 'geoshape int list matrix4x4 parent point quaternion real rect ' +
|
---|
54 | 'size string url variant vector2d vector3d vector4d ' +
|
---|
55 | 'Promise'
|
---|
56 | };
|
---|
57 |
|
---|
58 | const QML_IDENT_RE = '[a-zA-Z_][a-zA-Z0-9\\._]*';
|
---|
59 |
|
---|
60 | // Isolate property statements. Ends at a :, =, ;, ,, a comment or end of line.
|
---|
61 | // Use property class.
|
---|
62 | const PROPERTY = {
|
---|
63 | className: 'keyword',
|
---|
64 | begin: '\\bproperty\\b',
|
---|
65 | starts: {
|
---|
66 | className: 'string',
|
---|
67 | end: '(:|=|;|,|//|/\\*|$)',
|
---|
68 | returnEnd: true
|
---|
69 | }
|
---|
70 | };
|
---|
71 |
|
---|
72 | // Isolate signal statements. Ends at a ) a comment or end of line.
|
---|
73 | // Use property class.
|
---|
74 | const SIGNAL = {
|
---|
75 | className: 'keyword',
|
---|
76 | begin: '\\bsignal\\b',
|
---|
77 | starts: {
|
---|
78 | className: 'string',
|
---|
79 | end: '(\\(|:|=|;|,|//|/\\*|$)',
|
---|
80 | returnEnd: true
|
---|
81 | }
|
---|
82 | };
|
---|
83 |
|
---|
84 | // id: is special in QML. When we see id: we want to mark the id: as attribute and
|
---|
85 | // emphasize the token following.
|
---|
86 | const ID_ID = {
|
---|
87 | className: 'attribute',
|
---|
88 | begin: '\\bid\\s*:',
|
---|
89 | starts: {
|
---|
90 | className: 'string',
|
---|
91 | end: QML_IDENT_RE,
|
---|
92 | returnEnd: false
|
---|
93 | }
|
---|
94 | };
|
---|
95 |
|
---|
96 | // Find QML object attribute. An attribute is a QML identifier followed by :.
|
---|
97 | // Unfortunately it's hard to know where it ends, as it may contain scalars,
|
---|
98 | // objects, object definitions, or javascript. The true end is either when the parent
|
---|
99 | // ends or the next attribute is detected.
|
---|
100 | const QML_ATTRIBUTE = {
|
---|
101 | begin: QML_IDENT_RE + '\\s*:',
|
---|
102 | returnBegin: true,
|
---|
103 | contains: [
|
---|
104 | {
|
---|
105 | className: 'attribute',
|
---|
106 | begin: QML_IDENT_RE,
|
---|
107 | end: '\\s*:',
|
---|
108 | excludeEnd: true,
|
---|
109 | relevance: 0
|
---|
110 | }
|
---|
111 | ],
|
---|
112 | relevance: 0
|
---|
113 | };
|
---|
114 |
|
---|
115 | // Find QML object. A QML object is a QML identifier followed by { and ends at the matching }.
|
---|
116 | // All we really care about is finding IDENT followed by { and just mark up the IDENT and ignore the {.
|
---|
117 | const QML_OBJECT = {
|
---|
118 | begin: concat(QML_IDENT_RE, /\s*\{/),
|
---|
119 | end: /\{/,
|
---|
120 | returnBegin: true,
|
---|
121 | relevance: 0,
|
---|
122 | contains: [
|
---|
123 | hljs.inherit(hljs.TITLE_MODE, {
|
---|
124 | begin: QML_IDENT_RE
|
---|
125 | })
|
---|
126 | ]
|
---|
127 | };
|
---|
128 |
|
---|
129 | return {
|
---|
130 | name: 'QML',
|
---|
131 | aliases: [ 'qt' ],
|
---|
132 | case_insensitive: false,
|
---|
133 | keywords: KEYWORDS,
|
---|
134 | contains: [
|
---|
135 | {
|
---|
136 | className: 'meta',
|
---|
137 | begin: /^\s*['"]use (strict|asm)['"]/
|
---|
138 | },
|
---|
139 | hljs.APOS_STRING_MODE,
|
---|
140 | hljs.QUOTE_STRING_MODE,
|
---|
141 | { // template string
|
---|
142 | className: 'string',
|
---|
143 | begin: '`',
|
---|
144 | end: '`',
|
---|
145 | contains: [
|
---|
146 | hljs.BACKSLASH_ESCAPE,
|
---|
147 | {
|
---|
148 | className: 'subst',
|
---|
149 | begin: '\\$\\{',
|
---|
150 | end: '\\}'
|
---|
151 | }
|
---|
152 | ]
|
---|
153 | },
|
---|
154 | hljs.C_LINE_COMMENT_MODE,
|
---|
155 | hljs.C_BLOCK_COMMENT_MODE,
|
---|
156 | {
|
---|
157 | className: 'number',
|
---|
158 | variants: [
|
---|
159 | {
|
---|
160 | begin: '\\b(0[bB][01]+)'
|
---|
161 | },
|
---|
162 | {
|
---|
163 | begin: '\\b(0[oO][0-7]+)'
|
---|
164 | },
|
---|
165 | {
|
---|
166 | begin: hljs.C_NUMBER_RE
|
---|
167 | }
|
---|
168 | ],
|
---|
169 | relevance: 0
|
---|
170 | },
|
---|
171 | { // "value" container
|
---|
172 | begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
|
---|
173 | keywords: 'return throw case',
|
---|
174 | contains: [
|
---|
175 | hljs.C_LINE_COMMENT_MODE,
|
---|
176 | hljs.C_BLOCK_COMMENT_MODE,
|
---|
177 | hljs.REGEXP_MODE,
|
---|
178 | { // E4X / JSX
|
---|
179 | begin: /</,
|
---|
180 | end: />\s*[);\]]/,
|
---|
181 | relevance: 0,
|
---|
182 | subLanguage: 'xml'
|
---|
183 | }
|
---|
184 | ],
|
---|
185 | relevance: 0
|
---|
186 | },
|
---|
187 | SIGNAL,
|
---|
188 | PROPERTY,
|
---|
189 | {
|
---|
190 | className: 'function',
|
---|
191 | beginKeywords: 'function',
|
---|
192 | end: /\{/,
|
---|
193 | excludeEnd: true,
|
---|
194 | contains: [
|
---|
195 | hljs.inherit(hljs.TITLE_MODE, {
|
---|
196 | begin: /[A-Za-z$_][0-9A-Za-z$_]*/
|
---|
197 | }),
|
---|
198 | {
|
---|
199 | className: 'params',
|
---|
200 | begin: /\(/,
|
---|
201 | end: /\)/,
|
---|
202 | excludeBegin: true,
|
---|
203 | excludeEnd: true,
|
---|
204 | contains: [
|
---|
205 | hljs.C_LINE_COMMENT_MODE,
|
---|
206 | hljs.C_BLOCK_COMMENT_MODE
|
---|
207 | ]
|
---|
208 | }
|
---|
209 | ],
|
---|
210 | illegal: /\[|%/
|
---|
211 | },
|
---|
212 | {
|
---|
213 | // hack: prevents detection of keywords after dots
|
---|
214 | begin: '\\.' + hljs.IDENT_RE,
|
---|
215 | relevance: 0
|
---|
216 | },
|
---|
217 | ID_ID,
|
---|
218 | QML_ATTRIBUTE,
|
---|
219 | QML_OBJECT
|
---|
220 | ],
|
---|
221 | illegal: /#/
|
---|
222 | };
|
---|
223 | }
|
---|
224 |
|
---|
225 | module.exports = qml;
|
---|