forked from zealot128/tree-sitter-pug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
75 lines (75 loc) · 2.21 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
72
73
74
75
module.exports = grammar({
name: "pug",
externals: ($) => [$._newline, $._indent, $._dedent],
rules: {
source_file: ($) => repeat(choice($.comment, $.tag, $.doctype)),
doctype: ($) =>
seq("doctype", alias(choice("html", "strict", "xml"), $.doctype_name)),
pipe_content: ($) =>
seq("|", optional($._content_or_javascript), $._newline),
tag: ($) =>
seq(
choice($.tag_name, $.id, $.class),
optional(repeat1(choice($.id, $.class))),
optional($.attributes),
choice(
seq(":", $.tag),
$._content_after_dot,
seq(
optional(seq(" ", $._content_or_javascript)),
$._newline,
optional($.children)
)
)
),
_content_after_dot: ($) =>
seq(
".",
$._newline,
$._indent,
alias(
repeat1(seq($._content_or_javascript, $._newline)),
$.children
),
$._dedent
),
attributes: ($) =>
seq(
"(",
repeat(seq($.attribute, choice(",", " "))),
optional($.attribute),
")"
),
attribute: ($) =>
seq(
$.attribute_name,
optional(repeat1(seq(".", alias(/[\w@\-:]+/, $.attribute_modifier)))),
optional(seq("=", $.quoted_attribute_value))
),
children: ($) => seq($._indent, repeat1($._children_choice), $._dedent),
_children_choice: ($) => choice($.pipe_content, $.tag),
comment: ($) =>
seq(
"//",
optional($._comment_content),
$._newline,
optional(
seq($._indent, repeat(seq($._comment_content, $._newline)), $._dedent)
)
),
tag_name: ($) => /\w(?:[-:\w]*\w)?/,
class: ($) => /\.[_a-z0-9\-]*[_a-z][_a-z0-9\-]*/i,
id: ($) => /#[\w-]+/,
attribute_name: ($) => /[\w@\-:]+/,
quoted_attribute_value: ($) =>
choice(
seq("'", optional(alias(/[^']+/, $.attribute_value)), "'"),
seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"')
),
javascript: ($) => /[^\n}]+/,
content: ($) => /[^\n\{]+/,
_content_or_javascript: ($) =>
repeat1(choice(seq("{{", $.javascript, "}}"), $.content)),
_comment_content: ($) => /[^ ][^\n]*/,
},
});