forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtree-sitter-grammar.js
194 lines (165 loc) · 5.38 KB
/
tree-sitter-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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const path = require('path');
const SyntaxScopeMap = require('./syntax-scope-map');
const Module = require('module');
module.exports = class TreeSitterGrammar {
constructor(registry, filePath, params) {
this.registry = registry;
this.name = params.name;
this.scopeName = params.scopeName;
// TODO - Remove the `RegExp` spelling and only support `Regex`, once all of the existing
// Tree-sitter grammars are updated to spell it `Regex`.
this.contentRegex = buildRegex(params.contentRegex || params.contentRegExp);
this.injectionRegex = buildRegex(
params.injectionRegex || params.injectionRegExp
);
this.firstLineRegex = buildRegex(params.firstLineRegex);
this.folds = params.folds || [];
this.folds.forEach(normalizeFoldSpecification);
this.commentStrings = {
commentStartString: params.comments && params.comments.start,
commentEndString: params.comments && params.comments.end
};
const scopeSelectors = {};
for (const key in params.scopes || {}) {
const classes = preprocessScopes(params.scopes[key]);
const selectors = key.split(/,\s+/);
for (let selector of selectors) {
selector = selector.trim();
if (!selector) continue;
if (scopeSelectors[selector]) {
scopeSelectors[selector] = [].concat(
scopeSelectors[selector],
classes
);
} else {
scopeSelectors[selector] = classes;
}
}
}
this.scopeMap = new SyntaxScopeMap(scopeSelectors);
this.fileTypes = params.fileTypes || [];
this.injectionPointsByType = {};
for (const injectionPoint of params.injectionPoints || []) {
this.addInjectionPoint(injectionPoint);
}
// TODO - When we upgrade to a new enough version of node, use `require.resolve`
// with the new `paths` option instead of this private API.
const languageModulePath = Module._resolveFilename(params.parser, {
id: filePath,
filename: filePath,
paths: Module._nodeModulePaths(path.dirname(filePath))
});
this.languageModule = require(languageModulePath);
this.classNamesById = new Map();
this.scopeNamesById = new Map();
this.idsByScope = Object.create(null);
this.nextScopeId = 256 + 1;
this.registration = null;
}
inspect() {
return `TreeSitterGrammar {scopeName: ${this.scopeName}}`;
}
idForScope(scopeName) {
if (!scopeName) {
return undefined;
}
let id = this.idsByScope[scopeName];
if (!id) {
id = this.nextScopeId += 2;
const className = scopeName
.split('.')
.map(s => `syntax--${s}`)
.join(' ');
this.idsByScope[scopeName] = id;
this.classNamesById.set(id, className);
this.scopeNamesById.set(id, scopeName);
}
return id;
}
classNameForScopeId(id) {
return this.classNamesById.get(id);
}
scopeNameForScopeId(id) {
return this.scopeNamesById.get(id);
}
activate() {
this.registration = this.registry.addGrammar(this);
}
deactivate() {
if (this.registration) this.registration.dispose();
}
addInjectionPoint(injectionPoint) {
let injectionPoints = this.injectionPointsByType[injectionPoint.type];
if (!injectionPoints) {
injectionPoints = this.injectionPointsByType[injectionPoint.type] = [];
}
injectionPoints.push(injectionPoint);
}
removeInjectionPoint(injectionPoint) {
const injectionPoints = this.injectionPointsByType[injectionPoint.type];
if (injectionPoints) {
const index = injectionPoints.indexOf(injectionPoint);
if (index !== -1) injectionPoints.splice(index, 1);
if (injectionPoints.length === 0) {
delete this.injectionPointsByType[injectionPoint.type];
}
}
}
/*
Section - Backward compatibility shims
*/
onDidUpdate(callback) {
// do nothing
}
tokenizeLines(text, compatibilityMode = true) {
return text.split('\n').map(line => this.tokenizeLine(line, null, false));
}
tokenizeLine(line, ruleStack, firstLine) {
return {
value: line,
scopes: [this.scopeName]
};
}
};
const preprocessScopes = value =>
typeof value === 'string'
? value
: Array.isArray(value)
? value.map(preprocessScopes)
: value.match
? { match: new RegExp(value.match), scopes: preprocessScopes(value.scopes) }
: Object.assign({}, value, { scopes: preprocessScopes(value.scopes) });
const NODE_NAME_REGEX = /[\w_]+/;
function matcherForSpec(spec) {
if (typeof spec === 'string') {
if (spec[0] === '"' && spec[spec.length - 1] === '"') {
return {
type: spec.substr(1, spec.length - 2),
named: false
};
}
if (!NODE_NAME_REGEX.test(spec)) {
return { type: spec, named: false };
}
return { type: spec, named: true };
}
return spec;
}
function normalizeFoldSpecification(spec) {
if (spec.type) {
if (Array.isArray(spec.type)) {
spec.matchers = spec.type.map(matcherForSpec);
} else {
spec.matchers = [matcherForSpec(spec.type)];
}
}
if (spec.start) normalizeFoldSpecification(spec.start);
if (spec.end) normalizeFoldSpecification(spec.end);
}
function buildRegex(value) {
// Allow multiple alternatives to be specified via an array, for
// readability of the grammar file
if (Array.isArray(value)) value = value.map(_ => `(${_})`).join('|');
if (typeof value === 'string') return new RegExp(value);
return null;
}