This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
ExternalAntlrLexerFragment.java
178 lines (151 loc) · 6.61 KB
/
ExternalAntlrLexerFragment.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
/*******************************************************************************
* Copyright (c) 2019 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.parser.antlr.ex;
import org.eclipse.xtend2.lib.StringConcatenationClient;
import org.eclipse.xtext.parser.antlr.Lexer;
import org.eclipse.xtext.xtext.generator.Issues;
import org.eclipse.xtext.xtext.generator.model.GuiceModuleAccess;
import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference;
import org.eclipse.xtext.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment2;
import com.google.common.annotations.Beta;
/**
* Use this fragment if you want to use an external lexer , e.g: in case the generated lexer has to be customized.
*
* @since 2.20
* @author dietrich - Initial contribution and API
*/
@Beta
public class ExternalAntlrLexerFragment extends AbstractAntlrGeneratorFragment2 {
private String lexerGrammar;
private boolean highlighting;
private boolean runtime;
private boolean contentAssist;
@Override
protected void doGenerate() {
if (runtime) {
StringConcatenationClient binding = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation target) {
target.append("binder.bind(" + Lexer.class.getName() + ".class)"+
".annotatedWith(com.google.inject.name.Names.named(" +
"org.eclipse.xtext.parser.antlr.LexerBindings.RUNTIME" +
")).to(" + lexerGrammar +".class);");
}
};
new GuiceModuleAccess.BindingFactory().addConfiguredBinding("RuntimeLexer", binding).contributeTo(getLanguage().getRuntimeGenModule());
}
if (highlighting) {
if (this.getProjectConfig().getEclipsePlugin().getRoot() != null) {
StringConcatenationClient binding = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation target) {
target.append("binder.bind(" + Lexer.class.getName() + ".class)"+
".annotatedWith(com.google.inject.name.Names.named(" +
"org.eclipse.xtext.ide.LexerIdeBindings.HIGHLIGHTING" +
")).to(" + lexerGrammar +".class);");
}
};
new GuiceModuleAccess.BindingFactory().addConfiguredBinding("HighlightingLexer", binding).contributeTo(getLanguage().getEclipsePluginGenModule());
}
}
if (contentAssist) {
if (this.getProjectConfig().getGenericIde().getRoot() != null) {
StringConcatenationClient binding = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation target) {
target.append("binder.bind(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.Lexer.class)"+
".annotatedWith(com.google.inject.name.Names.named(" +
"org.eclipse.xtext.ide.LexerIdeBindings.CONTENT_ASSIST" +
")).to(" + lexerGrammar +".class);");
}
};
new GuiceModuleAccess.BindingFactory().addConfiguredBinding("ContentAssistLexer", binding).contributeTo(getLanguage().getIdeGenModule());
}
if (this.getProjectConfig().getEclipsePlugin().getRoot() != null) {
StringConcatenationClient binding = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation target) {
target.append("binder.bind(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.Lexer.class)"+
".annotatedWith(com.google.inject.name.Names.named(" +
"org.eclipse.xtext.ide.LexerIdeBindings.CONTENT_ASSIST" +
")).to(" + lexerGrammar +".class);");
}
};
new GuiceModuleAccess.BindingFactory().addConfiguredBinding("ContentAssistLexer", binding).contributeTo(getLanguage().getEclipsePluginGenModule());
}
}
IXtextGeneratorFileSystemAccess srcGenFsa = this.getProjectConfig().getRuntime().getSrcGen();
IXtextGeneratorFileSystemAccess srcFsa = this.getProjectConfig().getRuntime().getSrc();
if (contentAssist || highlighting) {
if (this.getProjectConfig().getGenericIde().getRoot() != null) {
srcGenFsa = this.getProjectConfig().getGenericIde().getSrcGen();
srcFsa = this.getProjectConfig().getGenericIde().getSrc();
} else {
srcGenFsa = this.getProjectConfig().getEclipsePlugin().getSrcGen();
srcFsa = this.getProjectConfig().getEclipsePlugin().getSrc();
}
}
String srcGenPath = srcGenFsa.getPath();
String srcPath = srcFsa.getPath();
String grammarFile = srcPath + "/" + getLexerGrammar().replace('.', '/') + ".g";
String generateTo = "";
if (getLexerGrammar().lastIndexOf('.') != -1) {
generateTo = getLexerGrammar().substring(0, getLexerGrammar().lastIndexOf('.'));
}
generateTo = srcGenPath + "/" + generateTo.replace('.', '/');
addAntlrParam("-fo");
addAntlrParam(generateTo);
final String encoding = getCodeConfig().getEncoding();
getAntlrTool().runWithEncodingAndParams(grammarFile, encoding, getAntlrParams());
TypeReference lexerType = new TypeReference(getLexerGrammar());
splitParserAndLexerIfEnabled(srcGenFsa, null /* parser */, lexerType);
normalizeTokens(srcGenFsa, getLexerGrammar().replace('.', '/') + ".tokens");
suppressWarnings(srcGenFsa, lexerType);
normalizeLineDelimiters(srcGenFsa, lexerType);
}
@Override
protected void splitParserClassFile(IXtextGeneratorFileSystemAccess fsa, TypeReference parser) {
// no-op
}
@Override
protected void improveParserCodeQuality(IXtextGeneratorFileSystemAccess fsa, TypeReference parser) {
// no-op
}
@Override
public void checkConfiguration(Issues issues) {
super.checkConfiguration(issues);
if (contentAssist && highlighting || runtime && highlighting || contentAssist && runtime) {
issues.addError("Only one of those flags is allowed: contentAssist, runtime, highlighting flag");
}
}
public void setLexerGrammar(String lexerGrammar) {
this.lexerGrammar = lexerGrammar;
}
public String getLexerGrammar() {
return lexerGrammar;
}
public void setHighlighting(boolean highlighting) {
this.highlighting = highlighting;
}
public boolean isHighlighting() {
return highlighting;
}
public void setRuntime(boolean runtime) {
this.runtime = runtime;
}
public boolean isRuntime() {
return runtime;
}
public void setContentAssist(boolean contentAssist) {
this.contentAssist = contentAssist;
}
public boolean isContentAssist() {
return contentAssist;
}
}