|
1 | | -import { insertBefore } from '../util/language-util.js'; |
2 | 1 | import markup from './markup.js'; |
3 | 2 |
|
4 | 3 | /** @type {import('../types.d.ts').LanguageProto<'parser'>} */ |
5 | 4 | export default { |
6 | 5 | id: 'parser', |
7 | 6 | base: markup, |
8 | | - grammar ({ base }) { |
| 7 | + grammar () { |
9 | 8 | const punctuation = /[\[\](){};]/; |
10 | 9 |
|
11 | 10 | const parser = { |
| 11 | + 'expression': { |
| 12 | + // Allow for 3 levels of depth |
| 13 | + pattern: /(^|[^^])\((?:[^()]|\((?:[^()]|\((?:[^()])*\))*\))*\)/, |
| 14 | + greedy: true, |
| 15 | + lookbehind: true, |
| 16 | + inside: {}, // See below |
| 17 | + }, |
12 | 18 | 'keyword': { |
13 | 19 | pattern: |
14 | 20 | /(^|[^^])(?:\^(?:case|eval|for|if|switch|throw)\b|@(?:BASE|CLASS|GET(?:_DEFAULT)?|OPTIONS|SET_DEFAULT|USE)\b)/, |
@@ -39,62 +45,44 @@ export default { |
39 | 45 | 'punctuation': punctuation, |
40 | 46 | }; |
41 | 47 |
|
42 | | - insertBefore(parser, 'keyword', { |
43 | | - 'parser-comment': { |
44 | | - pattern: /(\s)#.*/, |
| 48 | + parser['expression'].inside = { |
| 49 | + 'string': { |
| 50 | + pattern: /(^|[^^])(["'])(?:(?!\2)[^^]|\^[\s\S])*\2/, |
45 | 51 | lookbehind: true, |
46 | | - alias: 'comment', |
47 | 52 | }, |
48 | | - 'expression': { |
49 | | - // Allow for 3 levels of depth |
50 | | - pattern: /(^|[^^])\((?:[^()]|\((?:[^()]|\((?:[^()])*\))*\))*\)/, |
51 | | - greedy: true, |
52 | | - lookbehind: true, |
53 | | - inside: { |
54 | | - 'string': { |
55 | | - pattern: /(^|[^^])(["'])(?:(?!\2)[^^]|\^[\s\S])*\2/, |
| 53 | + 'keyword': parser.keyword, |
| 54 | + 'variable': parser.variable, |
| 55 | + 'function': parser.function, |
| 56 | + 'boolean': /\b(?:false|true)\b/, |
| 57 | + 'number': /\b(?:0x[a-f\d]+|\d+(?:\.\d*)?(?:e[+-]?\d+)?)\b/i, |
| 58 | + 'escape': parser.escape, |
| 59 | + 'operator': |
| 60 | + /[~+*\/\\%]|!(?:\|\|?|=)?|&&?|\|\|?|==|<[<=]?|>[>=]?|-[fd]?|\b(?:def|eq|ge|gt|in|is|le|lt|ne)\b/, |
| 61 | + 'punctuation': punctuation, |
| 62 | + }; |
| 63 | + |
| 64 | + return { |
| 65 | + ...parser, |
| 66 | + $insertBefore: { |
| 67 | + 'keyword': { |
| 68 | + 'parser-comment': { |
| 69 | + pattern: /(\s)#.*/, |
56 | 70 | lookbehind: true, |
| 71 | + alias: 'comment', |
57 | 72 | }, |
| 73 | + }, |
| 74 | + 'tag/attr-value/punctuation': { |
| 75 | + 'expression': parser.expression, |
58 | 76 | 'keyword': parser.keyword, |
59 | 77 | 'variable': parser.variable, |
60 | 78 | 'function': parser.function, |
61 | | - 'boolean': /\b(?:false|true)\b/, |
62 | | - 'number': /\b(?:0x[a-f\d]+|\d+(?:\.\d*)?(?:e[+-]?\d+)?)\b/i, |
63 | 79 | 'escape': parser.escape, |
64 | | - 'operator': |
65 | | - /[~+*\/\\%]|!(?:\|\|?|=)?|&&?|\|\|?|==|<[<=]?|>[>=]?|-[fd]?|\b(?:def|eq|ge|gt|in|is|le|lt|ne)\b/, |
66 | | - 'punctuation': punctuation, |
| 80 | + 'parser-punctuation': { |
| 81 | + pattern: punctuation, |
| 82 | + alias: 'punctuation', |
| 83 | + }, |
67 | 84 | }, |
68 | 85 | }, |
69 | | - }); |
70 | | - |
71 | | - insertBefore( |
72 | | - /** @type {Grammar} */ ( |
73 | | - /** @type {GrammarToken} */ ( |
74 | | - /** @type {Grammar} */ (/** @type {GrammarToken} */ (base['tag']).inside)[ |
75 | | - 'attr-value' |
76 | | - ] |
77 | | - ).inside |
78 | | - ), |
79 | | - 'punctuation', |
80 | | - { |
81 | | - 'expression': parser.expression, |
82 | | - 'keyword': parser.keyword, |
83 | | - 'variable': parser.variable, |
84 | | - 'function': parser.function, |
85 | | - 'escape': parser.escape, |
86 | | - 'parser-punctuation': { |
87 | | - pattern: punctuation, |
88 | | - alias: 'punctuation', |
89 | | - }, |
90 | | - } |
91 | | - ); |
92 | | - |
93 | | - return parser; |
| 86 | + }; |
94 | 87 | }, |
95 | 88 | }; |
96 | | - |
97 | | -/** |
98 | | - * @typedef {import('../types.d.ts').Grammar} Grammar |
99 | | - * @typedef {import('../types.d.ts').GrammarToken} GrammarToken |
100 | | - */ |
0 commit comments