-
Notifications
You must be signed in to change notification settings - Fork 316
/
TerminalRuleToTextMateRule.java
191 lines (171 loc) · 4.92 KB
/
TerminalRuleToTextMateRule.java
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
/**
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.textmate;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.Alternatives;
import org.eclipse.xtext.CharacterRange;
import org.eclipse.xtext.EOF;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.NegatedToken;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.UntilToken;
import org.eclipse.xtext.Wildcard;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.util.XtextSwitch;
/**
* Converter from Xtext {@link TerminalRule} to TextMate regular expressions.
*
* @author David Medina
* @author Sebastian Zarnekow
* @since 2.35
*/
public class TerminalRuleToTextMateRule extends XtextSwitch<String> {
private final StringBuilder match;
private boolean negationMode = false;
public TerminalRuleToTextMateRule() {
this.match = new StringBuilder();
}
public String getMatchRegEx(TerminalRule rule) {
doSwitch(rule.getAlternatives());
String result = match.toString();
match.setLength(0);
return result;
}
public String getMatchRegEx(AbstractElement element) {
doSwitch(element);
String result = match.toString();
match.setLength(0);
return result;
}
@Override
public String caseAlternatives(Alternatives object) {
if (negationMode) {
for (var elem : object.getElements()) {
doSwitch(elem);
}
} else {
match.append("(");
boolean first = true;
for (var elem : object.getElements()) {
if (!first) match.append("|");
first = false;
doSwitch(elem);
}
match.append(')');
match.append(Strings.emptyIfNull(object.getCardinality()));
}
return "";
}
@Override
public String caseWildcard(Wildcard object) {
match.append(".");
match.append(Strings.emptyIfNull(object.getCardinality()));
return "";
}
@Override
public String caseTerminalRule(TerminalRule object) {
doSwitch(object.getAlternatives());
return "";
}
@Override
public String caseCharacterRange(CharacterRange object) {
if (!Strings.isEmpty(object.getCardinality())) {
match.append('(');
}
match.append("[");
doSwitch(object.getLeft());
match.append("-");
doSwitch(object.getRight());
match.append("]");
if (!Strings.isEmpty(object.getCardinality())) {
match.append(')');
match.append(Strings.emptyIfNull(object.getCardinality()));
}
return "";
}
@Override
public String caseRuleCall(RuleCall object) {
if (!Strings.isEmpty(object.getCardinality())) {
match.append("(");
}
doSwitch(object.getRule());
if (!Strings.isEmpty(object.getCardinality())) {
match.append(")");
match.append(Strings.emptyIfNull(object.getCardinality()));
}
return "";
}
@Override
public String caseGroup(Group object) {
if (negationMode) {
throw new UnsupportedOperationException("Negation is not supported for group rules");
}
if (!Strings.isEmpty(object.getCardinality())) {
match.append("(");
}
for (var elem : object.getElements()) {
doSwitch(elem);
}
if (!Strings.isEmpty(object.getCardinality())) {
match.append(")");
}
match.append(Strings.emptyIfNull(object.getCardinality()));
return "";
}
@Override
public String caseNegatedToken(NegatedToken object) {
match.append("[^");
negationMode = true;
doSwitch(object.getTerminal());
negationMode = false;
match.append("]").append(Strings.emptyIfNull(object.getCardinality()));
return "";
}
@Override
public String caseUntilToken(UntilToken object) {
throw new UnsupportedOperationException("Until token not supported, use begin and end TextMate rules");
}
@Override
public String defaultCase(EObject object) {
throw new UnsupportedOperationException(
"Encountered a non terminal rule or a terminal rule that is not supported: " + object.toString());
}
@Override
public String caseKeyword(Keyword object) {
String value = object.getValue();
match.append(toTextMateString(value));
match.append(Strings.emptyIfNull(object.getCardinality()));
return "";
}
// Do nothing
@Override
public String caseEOF(EOF object) {
return "";
}
// see https://macromates.com/manual/en/regular_expressions
private String toTextMateString(String base) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < base.length(); i++) {
char c = base.charAt(i);
if (c == '*' || c == '+' || c == '.' || c == '?' || c == '\\' || c == '\r' || c == '\n' || c == '"' || c == '\t'
|| c == '$' || c == '^' || c == '|' || c == '{' || c == '}'|| c == '[' || c == ']') {
builder.append("\\");
}
if (c == ' ') {
builder.append("\\s");
} else {
builder.append(c);
}
}
return builder.toString();
}
}