-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
71 lines (61 loc) · 2 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module.exports = grammar({
name: 'jqtpl',
extras: $ => [],
rules: {
template: $ => repeat($._node),
_node: $ => choice(
$.comment_directive,
$.content,
$.each_directive,
$.html_directive,
$.if_directive,
$.output_directive,
$.partial_directive,
$.var_directive,
),
code: $ => repeat1(/}[^\n\r\}]+|[^\n\r\}]+/),
// code: $ => repeat1(/[^\n\r\}]+}[^\n\r\}]+|}[^\n\r\}]+|[^\n\r\}]+/),
content: $ => prec.right(repeat1(/[^\{](\{?[^\{]+)*/)),
output_directive: $ => seq(
'{{', token.immediate('='), optional($.code), '}}',
),
html_directive: $ => seq(
'{{', token.immediate('html'), optional($.code), '}}',
),
comment_directive: $ => seq(
'{{', token.immediate('!'), optional(alias($.code, $.comment)), '}}',
),
var_directive: $ => seq(
'{{', token.immediate('var'), ' ', field('var_name', alias(/[a-zA-Z0-9_\.]+/, $.code)), ' = ', field('var_value', $.code), '}}',
),
if_directive: $ => seq(
'{{', token.immediate('if'), field('condition', $.code), '}}',
field('consequence', repeat($._node)),
optional(field('alternative', $.else_directive)),
'{{/', token.immediate('if'), token.immediate('}}'),
),
else_directive: $ => prec.left(seq(
'{{', token.immediate('else'), optional(field('condition', $.code)), '}}',
field('consequence', repeat($._node)),
optional(field('alternative', $.else_directive)),
)),
each_directive: $ => seq(
'{{', token.immediate('each'), token.immediate('('), /[^,]+/, ',', /[^)]+/, ')', optional($.code), '}}',
repeat($._node),
'{{/', token.immediate('each'), token.immediate('}}'),
),
partial_directive: $ => seq(
'{{', token.immediate('tmpl'), token.immediate('('),
field(
'bindings',
alias(
token(repeat(choice(/[^\(\)]*/, seq('(', /[^\(\)]*/, ')')))),
$.code,
),
),
')',
field('path', $.code),
'}}',
),
},
});