aboutsummaryrefslogtreecommitdiff
path: root/grammar.js
blob: 0f13c6be8ed1670af2426cedd2e2c51f004f5ab8 (plain) (blame)
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
module.exports = grammar({
	name: 'gsp',
	extras: $ => [$._S],
	rules: {
		source_file: $ => repeat(choice($.comment, $.node)),

		comment: $ => seq(
      '/',
			optional($.node_name),
			optional($.attribute_list),
			'{',
			optional($.node_body),
			'}',
		),

		node: $ => seq(
			optional('>'),
			$.node_name,
			optional($.attribute_list),
			'{',
			optional($.node_body),
			'}',
		),

		node_body: $ => choice(
			repeat1(choice($.comment, $.node)),
			seq(choice('-', '='), optional($.text)),
		),

		node_name: $ => /[a-zA-Z:_\u{000C0}-\u{000D6}\u{000D8}-\u{000F6}\u{000F8}-\u{002FF}\u{00370}-\u{0037D}\u{0037F}-\u{01FFF}\u{0200C}-\u{0200D}\u{02070}-\u{0218F}\u{02C00}-\u{02FEF}\u{03001}-\u{0D7FF}\u{0F900}-\u{0FDCF}\u{0FDF0}-\u{0FFFD}\u{10000}-\u{EFFFF}][a-zA-Z0-9:_\-.ยท\u{00300}-\u{0036F}\u{0203F}-\u{02040}\u{000C0}-\u{000D6}\u{000D8}-\u{000F6}\u{000F8}-\u{002FF}\u{00370}-\u{0037D}\u{0037F}-\u{01FFF}\u{0200C}-\u{0200D}\u{02070}-\u{0218F}\u{02C00}-\u{02FEF}\u{03001}-\u{0D7FF}\u{0F900}-\u{0FDCF}\u{0FDF0}-\u{0FFFD}\u{10000}-\u{EFFFF}]*/u,

		text: $ => repeat1(
			choice(
				$.literal_text,
				seq('@', choice($.comment, $.node)),
			),
		),

		literal_text: $ => /(\\[@}\\]|[^@}\\])+/,

		attribute_list: $ => repeat1($.attribute),

		attribute: $ => choice(
			$.class_shorthand,
			$.id_shorthand,
			seq(
				$.attribute_name,
				optional(seq('=', $.attribute_value)),
			),
		),

		class_shorthand: $ => /\.\P{White_Space}+/u,
		id_shorthand: $ => /#\P{White_Space}+/u,

		attribute_name: $ => /[a-zA-Z0-9_-]+/,
		attribute_value: $ => /"(\\.|[^"\\])*"/,

		_S: $ => /\p{White_Space}+/u,
	},
})