generated from tree-sitter-grammars/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
159 lines (129 loc) · 4 KB
/
grammar.js
File metadata and controls
159 lines (129 loc) · 4 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @file .Xresources grammar for tree-sitter
* @author Omar Valdez <omarantoniovaldezf2@gmail.com>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
const ANY_CHAR = /[^\x00\n]/;
const WHITE_SPACE = /[ \t]/;
const NEWLINE = /\n/;
const C_IDENTIFIER = /[a-zA-Z_]\w*/;
export default grammar({
name: 'xresources',
extras: $ => [WHITE_SPACE, $.preprocessor_comment],
rules: {
resources: $ => repeat($._line),
_line: $ => seq(optional($._statement), NEWLINE),
_statement: $ => choice(
$.comment,
$.define_directive,
$.define_function_directive,
$.if_directive,
$.ifdef_directive,
$.include_directive,
$.resource,
$.simple_directive,
$.undef_directive,
),
comment: _ => token.immediate(seq('!', repeat(ANY_CHAR))),
// C style comments handled by the C preprocessor,
// rule definition taken from tree-sitter-c
preprocessor_comment: _ => token(choice(
seq('//', /(\\+(.|\r?\n)|[^\\\n])*/),
seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
)),
resource: $ => seq(
field('name', $.components),
':',
repeat(WHITE_SPACE),
field('value', $.resource_value),
),
components: $ => seq(
repeat($.binding),
repeat(seq(
choice($.component, $.any_component),
repeat1($.binding)),
),
$.component,
),
binding: _ => choice('.', '*'),
component: _ => /[a-zA-Z0-9_-]+/,
any_component: _ => '?',
escape_sequence: _ => token(seq('\\', choice('n', '\\', '\t', '\n', ' ', /[0-7]{3}/))),
resource_value: $ => repeat1(choice(ANY_CHAR, $.escape_sequence)),
string: $ => seq('"', alias(/[^\n"]*/, $.string_content), '"'),
// Preprocessor directives
include_directive: $ => seq(directive('include'), field('file', $.string)),
define_directive: $ => seq(
directive('define'),
field('name', $.identifier),
field('value', optional($.expansion)),
),
define_function_directive: $ => seq(
directive('define'),
field('name', $.identifier),
field('parameters', $.parameters),
field('value', optional($.expansion)),
),
parameters: $ => seq(
token.immediate('('),
optional(sep1(choice($.identifier, '...'), ',')),
')',
),
expansion: _ => token(prec(-1, /\S([^/\n]|\/[^*]|\\\r?\n)*/)),
undef_directive: $ => seq(directive('undef'), field('name', $.identifier)),
if_directive: $ => seq(
directive('if'),
field('condition', $.expansion),
NEWLINE,
$._if_directive_body,
),
ifdef_directive: $ => seq(
choice(directive('ifdef'), directive('ifndef')),
field('condition', $.identifier),
NEWLINE,
$._if_directive_body,
),
_if_directive_body: $ => seq(
field('consequence', alias(repeat($._line), $.body)),
repeat(field('alternative', choice($.elif_directive, $.elifdef_directive))),
optional(field('alternative', $.else_directive)),
directive('endif'),
),
elif_directive: $ => seq(
directive('elif'),
field('condition', $.expansion),
repeat($._line),
),
elifdef_directive: $ => seq(
choice(directive('elifdef'), directive('elifndef')),
field('condition', $.identifier),
repeat($._line),
),
else_directive: $ => seq(directive('else'), NEWLINE, repeat($._line)),
simple_directive: $ => seq(
field('name', $.directive),
field('value', optional($.expansion)),
),
directive: _ => token(seq('#', repeat(WHITE_SPACE), C_IDENTIFIER)),
identifier: _ => C_IDENTIFIER,
},
});
/**
* @param {string} name Name of the preprocessor directive
* @returns {Rule} Preprocessor rule
*/
function directive(name) {
return alias(token(seq('#', repeat(WHITE_SPACE), name)), '#' + name);
}
/**
* `rule` one or more times separated by `separator`.
*
* @param {Rule} rule
* @param {String} separator
* @returns {Rule}
*/
function sep1(rule, separator) {
return seq(rule, repeat(seq(separator, rule)));
}