Skip to content

Commit e167fab

Browse files
committed
Added ability to configure parser/lexer class names.
1 parent 3de00ef commit e167fab

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ base parser/lexer classes, your custom code will *not* be run during live previe
6767
As of 1.17, this limitation is partially not true. The configuration window of a grammar has a new option
6868
that allows using the generated parser code in the preview. In this case, the grammar must be compiled into
6969
a Java class (just to be on Project's target classpath). Any changes made to such grammar are not immediately
70-
reflected in the preview and the project must be recompiled instead.
70+
reflected in the preview and the project must be recompiled instead. It is also possible to specify the name of the
71+
compiler parser/lexer class. By default the name of the grammar is used (parser and lexer grammar).
7172

7273
## History
7374

src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public class ANTLRv4PluginController implements ProjectComponent {
101101
private ProgressIndicator parsingProgressIndicator;
102102
private UrlClassLoader projectClassLoader;
103103

104+
private Class<?> tokenStreamClass;
105+
private Class<?> charStreamClass;
106+
104107
public ANTLRv4PluginController(Project project) {
105108
this.project = project;
106109
}
@@ -212,12 +215,19 @@ private void initClassLoader() {
212215
for (String path : compiledClassUrls) {
213216
try {
214217
urls.add(new File(FileUtil.toSystemIndependentName(path)).toURI().toURL());
215-
} catch (MalformedURLException e1) {
218+
} catch (MalformedURLException e1) {
216219
LOG.error(e1);
217220
}
218221
}
219222

220223
this.projectClassLoader = UrlClassLoader.build().parent(TokenStream.class.getClassLoader()).urls(urls).get();
224+
225+
try {
226+
this.tokenStreamClass = Class.forName(TokenStream.class.getName(), true, this.projectClassLoader);
227+
this.charStreamClass = Class.forName(CharStream.class.getName(), true, this.projectClassLoader);
228+
} catch (ClassNotFoundException e) {
229+
LOG.error(e);
230+
}
221231
}
222232

223233
// ------------------------------
@@ -420,13 +430,10 @@ private String updateGrammarObjectsFromFile_(VirtualFile grammarFile) {
420430

421431
ANTLRv4GrammarProperties grammarProperties = ANTLRv4GrammarPropertiesStore.getGrammarProperties(project, grammarFile);
422432
if (grammarProperties.isUseGeneratedParserCodeCheckBox()) {
423-
String parserClassName = grammarFile.getNameWithoutExtension();
424-
String lexerClassName = lg.name;
433+
String parserClassName = grammarProperties.getGeneratedParserClassName() != null ? grammarProperties.getGeneratedParserClassName() : grammarFile.getNameWithoutExtension();
434+
String lexerClassName = grammarProperties.getGeneratedLexerClassName() != null ? grammarProperties.getGeneratedLexerClassName() : lg.name;
425435

426436
try {
427-
Class<?> tokenStreamClass = Class.forName(TokenStream.class.getName(), true, this.projectClassLoader);
428-
Class<?> charStreamClass = Class.forName(CharStream.class.getName(), true, this.projectClassLoader);
429-
430437
Class<Parser> parserClass = (Class<Parser>) Class.forName(parserClassName, true, this.projectClassLoader);
431438
Class<Lexer> lexerClass = (Class<Lexer>) Class.forName(lexerClassName, true, this.projectClassLoader);
432439

src/main/java/org/antlr/intellij/plugin/configdialogs/ANTLRv4GrammarProperties.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public class ANTLRv4GrammarProperties implements Cloneable {
6060
@Property
6161
boolean useGeneratedParserCodeCheckBox = false;
6262

63+
@Property
64+
String generatedParserClassName;
65+
66+
@Property
67+
String generatedLexerClassName;
68+
6369
public ANTLRv4GrammarProperties() {
6470
}
6571

@@ -75,6 +81,8 @@ public ANTLRv4GrammarProperties(ANTLRv4GrammarProperties source) {
7581
this.generateVisitor = source.generateVisitor;
7682
this.caseChangingStrategy = source.caseChangingStrategy;
7783
this.useGeneratedParserCodeCheckBox = source.useGeneratedParserCodeCheckBox;
84+
this.generatedParserClassName = source.generatedParserClassName;
85+
this.generatedLexerClassName = source.generatedLexerClassName;
7886
}
7987

8088
public boolean shouldAutoGenerateParser() {
@@ -113,6 +121,14 @@ public boolean isUseGeneratedParserCodeCheckBox() {
113121
return useGeneratedParserCodeCheckBox;
114122
}
115123

124+
public String getGeneratedParserClassName() {
125+
return generatedParserClassName;
126+
}
127+
128+
public String getGeneratedLexerClassName() {
129+
return generatedLexerClassName;
130+
}
131+
116132
public CaseChangingStrategy getCaseChangingStrategy() {
117133
return caseChangingStrategy;
118134
}

src/main/java/org/antlr/intellij/plugin/configdialogs/ConfigANTLRDialogPanel.form

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.antlr.intellij.plugin.configdialogs.ConfigANTLRPerGrammar">
3-
<grid id="27dc6" binding="dialogContents" layout-manager="GridLayoutManager" row-count="11" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="dialogContents" layout-manager="GridLayoutManager" row-count="13" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="630" height="333"/>
6+
<xy x="20" y="20" width="630" height="403"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
@@ -134,9 +134,45 @@
134134
</component>
135135
<vspacer id="39a11">
136136
<constraints>
137-
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
137+
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
138138
</constraints>
139139
</vspacer>
140+
<component id="523de" class="javax.swing.JTextField" binding="generatedParserClassName">
141+
<constraints>
142+
<grid row="10" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
143+
<preferred-size width="150" height="-1"/>
144+
</grid>
145+
</constraints>
146+
<properties>
147+
<enabled value="false"/>
148+
</properties>
149+
</component>
150+
<component id="a48c0" class="javax.swing.JTextField" binding="generatedLexerClassName">
151+
<constraints>
152+
<grid row="11" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
153+
<preferred-size width="150" height="-1"/>
154+
</grid>
155+
</constraints>
156+
<properties>
157+
<enabled value="false"/>
158+
</properties>
159+
</component>
160+
<component id="da6cb" class="javax.swing.JLabel">
161+
<constraints>
162+
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
163+
</constraints>
164+
<properties>
165+
<text value="Generated parser class name"/>
166+
</properties>
167+
</component>
168+
<component id="a83b5" class="javax.swing.JLabel">
169+
<constraints>
170+
<grid row="11" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
171+
</constraints>
172+
<properties>
173+
<text value="Generated lexer class name"/>
174+
</properties>
175+
</component>
140176
</children>
141177
</grid>
142178
</form>

src/main/java/org/antlr/intellij/plugin/configdialogs/ConfigANTLRPerGrammar.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public class ConfigANTLRPerGrammar extends DialogWrapper {
3333
protected JTextField languageField;
3434
private JComboBox<CaseChangingStrategy> caseTransformation;
3535
private JCheckBox useGeneratedParserCodeCheckBox;
36+
private JTextField generatedParserClassName;
37+
private JTextField generatedLexerClassName;
3638

37-
private ConfigANTLRPerGrammar(final Project project) {
39+
private ConfigANTLRPerGrammar(final Project project) {
3840
super(project, false);
3941
}
4042

@@ -66,6 +68,15 @@ private void initAntlrFields(Project project, String qualFileName) {
6668
libDirField.setTextFieldPreferredWidth(50);
6769

6870
loadValues(project, qualFileName);
71+
72+
useGeneratedParserCodeCheckBox.addActionListener(e -> toggleGeneratedClassNames());
73+
toggleGeneratedClassNames();
74+
}
75+
76+
private void toggleGeneratedClassNames() {
77+
boolean enabled = useGeneratedParserCodeCheckBox.isSelected();
78+
generatedParserClassName.setEnabled(enabled);
79+
generatedLexerClassName.setEnabled(enabled);
6980
}
7081

7182
public void loadValues(Project project, String qualFileName) {
@@ -81,6 +92,8 @@ public void loadValues(Project project, String qualFileName) {
8192
generateParseTreeListenerCheckBox.setSelected(grammarProperties.shouldGenerateParseTreeListener());
8293
generateParseTreeVisitorCheckBox.setSelected(grammarProperties.shouldGenerateParseTreeVisitor());
8394
useGeneratedParserCodeCheckBox.setSelected(grammarProperties.isUseGeneratedParserCodeCheckBox());
95+
generatedParserClassName.setText(grammarProperties.generatedParserClassName);
96+
generatedLexerClassName.setText(grammarProperties.generatedLexerClassName);
8497
}
8598

8699
public void saveValues(Project project, String qualFileName) {
@@ -96,6 +109,8 @@ public void saveValues(Project project, String qualFileName) {
96109
grammarProperties.generateListener = generateParseTreeListenerCheckBox.isSelected();
97110
grammarProperties.generateVisitor = generateParseTreeVisitorCheckBox.isSelected();
98111
grammarProperties.useGeneratedParserCodeCheckBox = useGeneratedParserCodeCheckBox.isSelected();
112+
grammarProperties.generatedParserClassName = getGeneratedParserClassName();
113+
grammarProperties.generatedLexerClassName = getGeneratedLexerClassName();
99114
}
100115

101116
boolean isModified(ANTLRv4GrammarProperties originalProperties) {
@@ -104,7 +119,10 @@ boolean isModified(ANTLRv4GrammarProperties originalProperties) {
104119
|| !Objects.equals(originalProperties.getEncoding(), getFileEncodingText())
105120
|| !Objects.equals(originalProperties.getPackage(), getPackageFieldText())
106121
|| !Objects.equals(originalProperties.getLanguage(), getLanguageText())
107-
|| !Objects.equals(originalProperties.caseChangingStrategy, getCaseChangingStrategy());
122+
|| !Objects.equals(originalProperties.caseChangingStrategy, getCaseChangingStrategy())
123+
|| !Objects.equals(originalProperties.isUseGeneratedParserCodeCheckBox(), isUseGeneratedParserCode())
124+
|| !Objects.equals(originalProperties.generatedParserClassName, getGeneratedParserClassName())
125+
|| !Objects.equals(originalProperties.generatedLexerClassName, getGeneratedLexerClassName());
108126
}
109127

110128
String getLanguageText() {
@@ -127,6 +145,18 @@ String getOutputDirText() {
127145
return outputDirField.getText();
128146
}
129147

148+
boolean isUseGeneratedParserCode() {
149+
return useGeneratedParserCodeCheckBox.isSelected();
150+
}
151+
152+
String getGeneratedParserClassName() {
153+
return generatedParserClassName.getText();
154+
}
155+
156+
String getGeneratedLexerClassName() {
157+
return generatedLexerClassName.getText();
158+
}
159+
130160
private CaseChangingStrategy getCaseChangingStrategy() {
131161
return (CaseChangingStrategy) caseTransformation.getSelectedItem();
132162
}
@@ -146,6 +176,8 @@ public String toString() {
146176
", packageField=" + packageField +
147177
", outputDirField=" + outputDirField +
148178
", libDirField=" + libDirField +
179+
", generatedParserClassName=" + generatedParserClassName +
180+
", generatedLexerClassName=" + generatedLexerClassName +
149181
'}';
150182
}
151183

0 commit comments

Comments
 (0)