Skip to content

Commit 014d325

Browse files
parser
1 parent 8985d72 commit 014d325

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/languages/parser.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { insertBefore } from '../util/language-util.js';
21
import markup from './markup.js';
32

43
/** @type {import('../types.d.ts').LanguageProto<'parser'>} */
54
export default {
65
id: 'parser',
76
base: markup,
8-
grammar ({ base }) {
7+
grammar () {
98
const punctuation = /[\[\](){};]/;
109

1110
const parser = {
11+
'expression': {
12+
// Allow for 3 levels of depth
13+
pattern: /(^|[^^])\((?:[^()]|\((?:[^()]|\((?:[^()])*\))*\))*\)/,
14+
greedy: true,
15+
lookbehind: true,
16+
inside: {}, // See below
17+
},
1218
'keyword': {
1319
pattern:
1420
/(^|[^^])(?:\^(?:case|eval|for|if|switch|throw)\b|@(?:BASE|CLASS|GET(?:_DEFAULT)?|OPTIONS|SET_DEFAULT|USE)\b)/,
@@ -39,62 +45,44 @@ export default {
3945
'punctuation': punctuation,
4046
};
4147

42-
insertBefore(parser, 'keyword', {
43-
'parser-comment': {
44-
pattern: /(\s)#.*/,
48+
parser['expression'].inside = {
49+
'string': {
50+
pattern: /(^|[^^])(["'])(?:(?!\2)[^^]|\^[\s\S])*\2/,
4551
lookbehind: true,
46-
alias: 'comment',
4752
},
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)#.*/,
5670
lookbehind: true,
71+
alias: 'comment',
5772
},
73+
},
74+
'tag/attr-value/punctuation': {
75+
'expression': parser.expression,
5876
'keyword': parser.keyword,
5977
'variable': parser.variable,
6078
'function': parser.function,
61-
'boolean': /\b(?:false|true)\b/,
62-
'number': /\b(?:0x[a-f\d]+|\d+(?:\.\d*)?(?:e[+-]?\d+)?)\b/i,
6379
'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+
},
6784
},
6885
},
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+
};
9487
},
9588
};
96-
97-
/**
98-
* @typedef {import('../types.d.ts').Grammar} Grammar
99-
* @typedef {import('../types.d.ts').GrammarToken} GrammarToken
100-
*/

0 commit comments

Comments
 (0)