Debian Codegraph @

codemirror-legacy-modes/mode/pug.js

raw
1 import {javascript} from "./javascript.js"
2
3 var ATTRS_NEST = {
4 '{': '}',
5 '(': ')',
6 '[': ']'
7 }
8
9 function defaultCopyState(state) {
10 if (typeof state != "object") return state
11 let newState = {}
12 for (let prop in state) {
13 let val = state[prop]
14 newState[prop] = val instanceof Array ? val.slice() : val
15 }
16 return newState
17 }
18
19 class State {
20 constructor(indentUnit) {
21 this.indentUnit = indentUnit
22
23 this.javaScriptLine = false
24 this.javaScriptLineExcludesColon = false
25
26 this.javaScriptArguments = false
27 this.javaScriptArgumentsDepth = 0
28
29 this.isInterpolating = false
30 this.interpolationNesting = 0
31
32 this.jsState = javascript.startState(indentUnit)
33
34 this.restOfLine = ''
35
36 this.isIncludeFiltered = false
37 this.isEach = false
38
39 this.lastTag = ''
40
41 // Attributes Mode
42 this.isAttrs = false
43 this.attrsNest = []
44 this.inAttributeName = true
45 this.attributeIsType = false
46 this.attrValue = ''
47
48 // Indented Mode
49 this.indentOf = Infinity
50 this.indentToken = ''
51 }
52
53 copy() {
54 var res = new State(this.indentUnit)
55 res.javaScriptLine = this.javaScriptLine
56 res.javaScriptLineExcludesColon = this.javaScriptLineExcludesColon
57 res.javaScriptArguments = this.javaScriptArguments
58 res.javaScriptArgumentsDepth = this.javaScriptArgumentsDepth
59 res.isInterpolating = this.isInterpolating
60 res.interpolationNesting = this.interpolationNesting
61
62 res.jsState = (javascript.copyState || defaultCopyState)(this.jsState)
63
64 res.restOfLine = this.restOfLine
65
66 res.isIncludeFiltered = this.isIncludeFiltered
67 res.isEach = this.isEach
68 res.lastTag = this.lastTag
69 res.isAttrs = this.isAttrs
70 res.attrsNest = this.attrsNest.slice()
71 res.inAttributeName = this.inAttributeName
72 res.attributeIsType = this.attributeIsType
73 res.attrValue = this.attrValue
74 res.indentOf = this.indentOf
75 res.indentToken = this.indentToken
76
77 return res
78 }
79 }
80
81 function javaScript(stream, state) {
82 if (stream.sol()) {
83 // if javaScriptLine was set at end of line, ignore it
84 state.javaScriptLine = false
85 state.javaScriptLineExcludesColon = false
86 }
87 if (state.javaScriptLine) {
88 if (state.javaScriptLineExcludesColon && stream.peek() === ':') {
89 state.javaScriptLine = false
90 state.javaScriptLineExcludesColon = false
91 return
92 }
93 var tok = javascript.token(stream, state.jsState)
94 if (stream.eol()) state.javaScriptLine = false
95 return tok || true
96 }
97 }
98 function javaScriptArguments(stream, state) {
99 if (state.javaScriptArguments) {
100 if (state.javaScriptArgumentsDepth === 0 && stream.peek() !== '(') {
101 state.javaScriptArguments = false
102 return
103 }
104 if (stream.peek() === '(') {
105 state.javaScriptArgumentsDepth++
106 } else if (stream.peek() === ')') {
107 state.javaScriptArgumentsDepth--
108 }
109 if (state.javaScriptArgumentsDepth === 0) {
110 state.javaScriptArguments = false
111 return
112 }
113
114 var tok = javascript.token(stream, state.jsState)
115 return tok || true
116 }
117 }
118
119 function yieldStatement(stream) {
120 if (stream.match(/^yield\b/)) {
121 return 'keyword'
122 }
123 }
124
125 function doctype(stream) {
126 if (stream.match(/^(?:doctype) *([^\n]+)?/)) return 'meta'
127 }
128
129 function interpolation(stream, state) {
130 if (stream.match('#{')) {
131 state.isInterpolating = true
132 state.interpolationNesting = 0
133 return 'punctuation'
134 }
135 }
136
137 function interpolationContinued(stream, state) {
138 if (state.isInterpolating) {
139 if (stream.peek() === '}') {
140 state.interpolationNesting--
141 if (state.interpolationNesting < 0) {
142 stream.next()
143 state.isInterpolating = false
144 return 'punctuation'
145 }
146 } else if (stream.peek() === '{') {
147 state.interpolationNesting++
148 }
149 return javascript.token(stream, state.jsState) || true
150 }
151 }
152
153 function caseStatement(stream, state) {
154 if (stream.match(/^case\b/)) {
155 state.javaScriptLine = true
156 return 'keyword'
157 }
158 }
159
160 function when(stream, state) {
161 if (stream.match(/^when\b/)) {
162 state.javaScriptLine = true
163 state.javaScriptLineExcludesColon = true
164 return 'keyword'
165 }
166 }
167
168 function defaultStatement(stream) {
169 if (stream.match(/^default\b/)) {
170 return 'keyword'
171 }
172 }
173
174 function extendsStatement(stream, state) {
175 if (stream.match(/^extends?\b/)) {
176 state.restOfLine = 'string'
177 return 'keyword'
178 }
179 }
180
181 function append(stream, state) {
182 if (stream.match(/^append\b/)) {
183 state.restOfLine = 'variable'
184 return 'keyword'
185 }
186 }
187 function prepend(stream, state) {
188 if (stream.match(/^prepend\b/)) {
189 state.restOfLine = 'variable'
190 return 'keyword'
191 }
192 }
193 function block(stream, state) {
194 if (stream.match(/^block\b *(?:(prepend|append)\b)?/)) {
195 state.restOfLine = 'variable'
196 return 'keyword'
197 }
198 }
199
200 function include(stream, state) {
201 if (stream.match(/^include\b/)) {
202 state.restOfLine = 'string'
203 return 'keyword'
204 }
205 }
206
207 function includeFiltered(stream, state) {
208 if (stream.match(/^include:([a-zA-Z0-9\-]+)/, false) && stream.match('include')) {
209 state.isIncludeFiltered = true
210 return 'keyword'
211 }
212 }
213
214 function includeFilteredContinued(stream, state) {
215 if (state.isIncludeFiltered) {
216 var tok = filter(stream, state)
217 state.isIncludeFiltered = false
218 state.restOfLine = 'string'
219 return tok
220 }
221 }
222
223 function mixin(stream, state) {
224 if (stream.match(/^mixin\b/)) {
225 state.javaScriptLine = true
226 return 'keyword'
227 }
228 }
229
230 function call(stream, state) {
231 if (stream.match(/^\+([-\w]+)/)) {
232 if (!stream.match(/^\( *[-\w]+ *=/, false)) {
233 state.javaScriptArguments = true
234 state.javaScriptArgumentsDepth = 0
235 }
236 return 'variable'
237 }
238 if (stream.match('+#{', false)) {
239 stream.next()
240 state.mixinCallAfter = true
241 return interpolation(stream, state)
242 }
243 }
244 function callArguments(stream, state) {
245 if (state.mixinCallAfter) {
246 state.mixinCallAfter = false
247 if (!stream.match(/^\( *[-\w]+ *=/, false)) {
248 state.javaScriptArguments = true
249 state.javaScriptArgumentsDepth = 0
250 }
251 return true
252 }
253 }
254
255 function conditional(stream, state) {
256 if (stream.match(/^(if|unless|else if|else)\b/)) {
257 state.javaScriptLine = true
258 return 'keyword'
259 }
260 }
261
262 function each(stream, state) {
263 if (stream.match(/^(- *)?(each|for)\b/)) {
264 state.isEach = true
265 return 'keyword'
266 }
267 }
268 function eachContinued(stream, state) {
269 if (state.isEach) {
270 if (stream.match(/^ in\b/)) {
271 state.javaScriptLine = true
272 state.isEach = false
273 return 'keyword'
274 } else if (stream.sol() || stream.eol()) {
275 state.isEach = false
276 } else if (stream.next()) {
277 while (!stream.match(/^ in\b/, false) && stream.next()) {}
278 return 'variable'
279 }
280 }
281 }
282
283 function whileStatement(stream, state) {
284 if (stream.match(/^while\b/)) {
285 state.javaScriptLine = true
286 return 'keyword'
287 }
288 }
289
290 function tag(stream, state) {
291 var captures
292 if (captures = stream.match(/^(\w(?:[-:\w]*\w)?)\/?/)) {
293 state.lastTag = captures[1].toLowerCase()
294 return 'tag'
295 }
296 }
297
298 function filter(stream, state) {
299 if (stream.match(/^:([\w\-]+)/)) {
300 setStringMode(stream, state)
301 return 'atom'
302 }
303 }
304
305 function code(stream, state) {
306 if (stream.match(/^(!?=|-)/)) {
307 state.javaScriptLine = true
308 return 'punctuation'
309 }
310 }
311
312 function id(stream) {
313 if (stream.match(/^#([\w-]+)/)) {
314 return 'builtin'
315 }
316 }
317
318 function className(stream) {
319 if (stream.match(/^\.([\w-]+)/)) {
320 return 'className'
321 }
322 }
323
324 function attrs(stream, state) {
325 if (stream.peek() == '(') {
326 stream.next()
327 state.isAttrs = true
328 state.attrsNest = []
329 state.inAttributeName = true
330 state.attrValue = ''
331 state.attributeIsType = false
332 return 'punctuation'
333 }
334 }
335
336 function attrsContinued(stream, state) {
337 if (state.isAttrs) {
338 if (ATTRS_NEST[stream.peek()]) {
339 state.attrsNest.push(ATTRS_NEST[stream.peek()])
340 }
341 if (state.attrsNest[state.attrsNest.length - 1] === stream.peek()) {
342 state.attrsNest.pop()
343 } else if (stream.eat(')')) {
344 state.isAttrs = false
345 return 'punctuation'
346 }
347 if (state.inAttributeName && stream.match(/^[^=,\)!]+/)) {
348 if (stream.peek() === '=' || stream.peek() === '!') {
349 state.inAttributeName = false
350 state.jsState = javascript.startState(2)
351 if (state.lastTag === 'script' && stream.current().trim().toLowerCase() === 'type') {
352 state.attributeIsType = true
353 } else {
354 state.attributeIsType = false
355 }
356 }
357 return 'attribute'
358 }
359
360 var tok = javascript.token(stream, state.jsState)
361 if (state.attrsNest.length === 0 && (tok === 'string' || tok === 'variable' || tok === 'keyword')) {
362 try {
363 Function('', 'var x ' + state.attrValue.replace(/,\s*$/, '').replace(/^!/, ''))
364 state.inAttributeName = true
365 state.attrValue = ''
366 stream.backUp(stream.current().length)
367 return attrsContinued(stream, state)
368 } catch (ex) {
369 //not the end of an attribute
370 }
371 }
372 state.attrValue += stream.current()
373 return tok || true
374 }
375 }
376
377 function attributesBlock(stream, state) {
378 if (stream.match(/^&attributes\b/)) {
379 state.javaScriptArguments = true
380 state.javaScriptArgumentsDepth = 0
381 return 'keyword'
382 }
383 }
384
385 function indent(stream) {
386 if (stream.sol() && stream.eatSpace()) {
387 return 'indent'
388 }
389 }
390
391 function comment(stream, state) {
392 if (stream.match(/^ *\/\/(-)?([^\n]*)/)) {
393 state.indentOf = stream.indentation()
394 state.indentToken = 'comment'
395 return 'comment'
396 }
397 }
398
399 function colon(stream) {
400 if (stream.match(/^: */)) {
401 return 'colon'
402 }
403 }
404
405 function text(stream, state) {
406 if (stream.match(/^(?:\| ?| )([^\n]+)/)) {
407 return 'string'
408 }
409 if (stream.match(/^(<[^\n]*)/, false)) {
410 // html string
411 setStringMode(stream, state)
412 stream.skipToEnd()
413 return state.indentToken
414 }
415 }
416
417 function dot(stream, state) {
418 if (stream.eat('.')) {
419 setStringMode(stream, state)
420 return 'dot'
421 }
422 }
423
424 function fail(stream) {
425 stream.next()
426 return null
427 }
428
429
430 function setStringMode(stream, state) {
431 state.indentOf = stream.indentation()
432 state.indentToken = 'string'
433 }
434 function restOfLine(stream, state) {
435 if (stream.sol()) {
436 // if restOfLine was set at end of line, ignore it
437 state.restOfLine = ''
438 }
439 if (state.restOfLine) {
440 stream.skipToEnd()
441 var tok = state.restOfLine
442 state.restOfLine = ''
443 return tok
444 }
445 }
446
447
448 function startState(indentUnit) {
449 return new State(indentUnit)
450 }
451 function copyState(state) {
452 return state.copy()
453 }
454 function nextToken(stream, state) {
455 var tok = restOfLine(stream, state)
456 || interpolationContinued(stream, state)
457 || includeFilteredContinued(stream, state)
458 || eachContinued(stream, state)
459 || attrsContinued(stream, state)
460 || javaScript(stream, state)
461 || javaScriptArguments(stream, state)
462 || callArguments(stream, state)
463
464 || yieldStatement(stream)
465 || doctype(stream)
466 || interpolation(stream, state)
467 || caseStatement(stream, state)
468 || when(stream, state)
469 || defaultStatement(stream)
470 || extendsStatement(stream, state)
471 || append(stream, state)
472 || prepend(stream, state)
473 || block(stream, state)
474 || include(stream, state)
475 || includeFiltered(stream, state)
476 || mixin(stream, state)
477 || call(stream, state)
478 || conditional(stream, state)
479 || each(stream, state)
480 || whileStatement(stream, state)
481 || tag(stream, state)
482 || filter(stream, state)
483 || code(stream, state)
484 || id(stream)
485 || className(stream)
486 || attrs(stream, state)
487 || attributesBlock(stream, state)
488 || indent(stream)
489 || text(stream, state)
490 || comment(stream, state)
491 || colon(stream)
492 || dot(stream, state)
493 || fail(stream)
494
495 return tok === true ? null : tok
496 }
497
498 export const pug = {
499 startState: startState,
500 copyState: copyState,
501 token: nextToken
502 }