-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuildSyntax.ts
More file actions
108 lines (99 loc) · 3.48 KB
/
buildSyntax.ts
File metadata and controls
108 lines (99 loc) · 3.48 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
import { readFileSync, writeFileSync } from 'fs'
import { parse } from 'yaml'
declare interface MapLike<T> {
[s: string]: T;
}
declare interface TmGrammarRuleName {
name: string;
}
declare interface TmGrammarRule {
name?: string;
match: string;
captures: MapLike<TmGrammarRuleName>;
contentName?: string;
begin: string;
end: string;
beginCaptures?: MapLike<TmGrammarRuleName>;
endCaptures?: MapLike<TmGrammarRuleName>;
patterns: TmGrammarRule[];
include: string;
}
declare interface TmGrammarRepositoryPatterns {
patterns: TmGrammarRule[];
}
declare interface TmGrammar {
name: string;
scopeName: string;
fileTypes: string[];
uuid: string;
variables?: MapLike<string>;
patterns?: TmGrammarRule[];
repository: MapLike<TmGrammarRule>;
}
declare interface TmThemeSetting {
scope: string;
settings: { vsclassificationtype: string; };
}
declare interface TmTheme {
name: string;
uuid: string;
settings: TmThemeSetting[];
}
type VariableReplacer = [RegExp, string]
const variableReplacers: VariableReplacer[] = []
function updateVariableReplacers(variables: MapLike<string>) {
for (const variableName in variables) {
// Replace the pattern with earlier variables
const pattern = replacePatternVariables(variables[variableName], variableReplacers);
variableReplacers.push([new RegExp(`{{${variableName}}}`, "gim"), pattern]);
}
}
function replacePatternVariables(pattern: string, variableReplacers: VariableReplacer[]) {
let result = pattern;
for (const [variableName, value] of variableReplacers) {
result = result.replace(variableName, value);
}
return result;
}
function updateGrammarVariables(grammar: TmGrammar, variables: MapLike<string>) {
const variableReplacers: VariableReplacer[] = [];
for (const variableName in variables) {
// Replace the pattern with earlier variables
const pattern = replacePatternVariables(variables[variableName], variableReplacers);
variableReplacers.push([new RegExp(`{{${variableName}}}`, "gim"), pattern]);
}
transformGrammarRepository(
grammar,
["begin", "end", "match"],
pattern => replacePatternVariables(pattern, variableReplacers)
);
return grammar;
}
function transformGrammarRepository(grammar: TmGrammar, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
const repository = grammar.repository;
for (let key in repository) {
transformGrammarRule(repository[key], propertyNames, transformProperty);
}
}
function transformGrammarRule(rule: any, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
for (const propertyName of propertyNames) {
const value = rule[propertyName];
if (typeof value === 'string') {
rule[propertyName] = transformProperty(value);
}
}
for (var propertyName in rule) {
const value = rule[propertyName];
if (typeof value === 'object') {
transformGrammarRule(value, propertyNames, transformProperty);
}
}
}
const yamlContent = readFileSync("syntaxes/teo.tmLanguage.yaml").toString()
const yamlObject: TmGrammar = parse(yamlContent)
const variables = yamlObject.variables
delete yamlObject['variables']
updateVariableReplacers(variables!)
updateGrammarVariables(yamlObject, variables!)
const jsonContent = JSON.stringify(yamlObject, null, 2)
writeFileSync("syntaxes/teo.tmLanguage.json", jsonContent)