Skip to content

Commit

Permalink
Added ability to configure parser/lexer class names.
Browse files Browse the repository at this point in the history
  • Loading branch information
akovari committed Aug 5, 2022
1 parent ec8a844 commit 45c5a16
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ base parser/lexer classes, your custom code will *not* be run during live previe
As of 1.17, this limitation is partially not true. The configuration window of a grammar has a new option
that allows using the generated parser code in the preview. In this case, the grammar must be compiled into
a Java class (just to be on Project's target classpath). Any changes made to such grammar are not immediately
reflected in the preview and the project must be recompiled instead.
reflected in the preview and the project must be recompiled instead. It is also possible to specify the name of the
compiler parser/lexer class. By default the name of the grammar is used (parser and lexer grammar).

## History

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class ANTLRv4PluginController implements ProjectComponent {
private ProgressIndicator parsingProgressIndicator;
private UrlClassLoader projectClassLoader;

private Class<?> tokenStreamClass;
private Class<?> charStreamClass;

public ANTLRv4PluginController(Project project) {
this.project = project;
}
Expand Down Expand Up @@ -217,12 +220,19 @@ private void initClassLoader() {
for (String path : compiledClassUrls) {
try {
urls.add(new File(FileUtil.toSystemIndependentName(path)).toURI().toURL());
} catch (MalformedURLException e1) {
} catch (MalformedURLException e1) {
LOG.error(e1);
}
}

this.projectClassLoader = UrlClassLoader.build().parent(TokenStream.class.getClassLoader()).urls(urls).get();

try {
this.tokenStreamClass = Class.forName(TokenStream.class.getName(), true, this.projectClassLoader);
this.charStreamClass = Class.forName(CharStream.class.getName(), true, this.projectClassLoader);
} catch (ClassNotFoundException e) {
LOG.error(e);
}
}

// ------------------------------
Expand Down Expand Up @@ -426,13 +436,10 @@ private String updateGrammarObjectsFromFile_(VirtualFile grammarFile) {

ANTLRv4GrammarProperties grammarProperties = ANTLRv4GrammarPropertiesStore.getGrammarProperties(project, grammarFile);
if (grammarProperties.isUseGeneratedParserCodeCheckBox()) {
String parserClassName = grammarFile.getNameWithoutExtension();
String lexerClassName = lg.name;
String parserClassName = grammarProperties.getGeneratedParserClassName() != null ? grammarProperties.getGeneratedParserClassName() : grammarFile.getNameWithoutExtension();
String lexerClassName = grammarProperties.getGeneratedLexerClassName() != null ? grammarProperties.getGeneratedLexerClassName() : lg.name;

try {
Class<?> tokenStreamClass = Class.forName(TokenStream.class.getName(), true, this.projectClassLoader);
Class<?> charStreamClass = Class.forName(CharStream.class.getName(), true, this.projectClassLoader);

Class<Parser> parserClass = (Class<Parser>) Class.forName(parserClassName, true, this.projectClassLoader);
Class<Lexer> lexerClass = (Class<Lexer>) Class.forName(lexerClassName, true, this.projectClassLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public class ANTLRv4GrammarProperties implements Cloneable {
@Property
boolean useGeneratedParserCodeCheckBox = false;

@Property
String generatedParserClassName;

@Property
String generatedLexerClassName;

public ANTLRv4GrammarProperties() {
}

Expand All @@ -96,6 +102,8 @@ public ANTLRv4GrammarProperties(ANTLRv4GrammarProperties source) {
this.generateVisitor = source.generateVisitor;
this.caseChangingStrategy = source.caseChangingStrategy;
this.useGeneratedParserCodeCheckBox = source.useGeneratedParserCodeCheckBox;
this.generatedParserClassName = source.generatedParserClassName;
this.generatedLexerClassName = source.generatedLexerClassName;
}

public boolean shouldAutoGenerateParser() {
Expand Down Expand Up @@ -134,6 +142,14 @@ public boolean isUseGeneratedParserCodeCheckBox() {
return useGeneratedParserCodeCheckBox;
}

public String getGeneratedParserClassName() {
return generatedParserClassName;
}

public String getGeneratedLexerClassName() {
return generatedLexerClassName;
}

public CaseChangingStrategy getCaseChangingStrategy() {
return caseChangingStrategy;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.antlr.intellij.plugin.configdialogs.ConfigANTLRPerGrammar">
<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">
<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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="630" height="333"/>
<xy x="20" y="20" width="630" height="403"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -134,9 +134,45 @@
</component>
<vspacer id="39a11">
<constraints>
<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"/>
<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"/>
</constraints>
</vspacer>
<component id="523de" class="javax.swing.JTextField" binding="generatedParserClassName">
<constraints>
<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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
</component>
<component id="a48c0" class="javax.swing.JTextField" binding="generatedLexerClassName">
<constraints>
<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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
</component>
<component id="da6cb" class="javax.swing.JLabel">
<constraints>
<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"/>
</constraints>
<properties>
<text value="Generated parser class name"/>
</properties>
</component>
<component id="a83b5" class="javax.swing.JLabel">
<constraints>
<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"/>
</constraints>
<properties>
<text value="Generated lexer class name"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public class ConfigANTLRPerGrammar extends DialogWrapper {
protected JTextField languageField;
private JComboBox<CaseChangingStrategy> caseTransformation;
private JCheckBox useGeneratedParserCodeCheckBox;
private JTextField generatedParserClassName;
private JTextField generatedLexerClassName;

private ConfigANTLRPerGrammar(final Project project) {
private ConfigANTLRPerGrammar(final Project project) {
super(project, false);
}

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

loadValues(project, qualFileName);

useGeneratedParserCodeCheckBox.addActionListener(e -> toggleGeneratedClassNames());
toggleGeneratedClassNames();
}

private void toggleGeneratedClassNames() {
boolean enabled = useGeneratedParserCodeCheckBox.isSelected();
generatedParserClassName.setEnabled(enabled);
generatedLexerClassName.setEnabled(enabled);
}

public void loadValues(Project project, String qualFileName) {
Expand All @@ -81,6 +92,8 @@ public void loadValues(Project project, String qualFileName) {
generateParseTreeListenerCheckBox.setSelected(grammarProperties.shouldGenerateParseTreeListener());
generateParseTreeVisitorCheckBox.setSelected(grammarProperties.shouldGenerateParseTreeVisitor());
useGeneratedParserCodeCheckBox.setSelected(grammarProperties.isUseGeneratedParserCodeCheckBox());
generatedParserClassName.setText(grammarProperties.generatedParserClassName);
generatedLexerClassName.setText(grammarProperties.generatedLexerClassName);
}

public void saveValues(Project project, String qualFileName) {
Expand All @@ -96,6 +109,8 @@ public void saveValues(Project project, String qualFileName) {
grammarProperties.generateListener = generateParseTreeListenerCheckBox.isSelected();
grammarProperties.generateVisitor = generateParseTreeVisitorCheckBox.isSelected();
grammarProperties.useGeneratedParserCodeCheckBox = useGeneratedParserCodeCheckBox.isSelected();
grammarProperties.generatedParserClassName = getGeneratedParserClassName();
grammarProperties.generatedLexerClassName = getGeneratedLexerClassName();
}

boolean isModified(ANTLRv4GrammarProperties originalProperties) {
Expand All @@ -104,7 +119,10 @@ boolean isModified(ANTLRv4GrammarProperties originalProperties) {
|| !Objects.equals(originalProperties.getEncoding(), getFileEncodingText())
|| !Objects.equals(originalProperties.getPackage(), getPackageFieldText())
|| !Objects.equals(originalProperties.getLanguage(), getLanguageText())
|| !Objects.equals(originalProperties.caseChangingStrategy, getCaseChangingStrategy());
|| !Objects.equals(originalProperties.caseChangingStrategy, getCaseChangingStrategy())
|| !Objects.equals(originalProperties.isUseGeneratedParserCodeCheckBox(), isUseGeneratedParserCode())
|| !Objects.equals(originalProperties.generatedParserClassName, getGeneratedParserClassName())
|| !Objects.equals(originalProperties.generatedLexerClassName, getGeneratedLexerClassName());
}

String getLanguageText() {
Expand All @@ -127,6 +145,18 @@ String getOutputDirText() {
return outputDirField.getText();
}

boolean isUseGeneratedParserCode() {
return useGeneratedParserCodeCheckBox.isSelected();
}

String getGeneratedParserClassName() {
return generatedParserClassName.getText();
}

String getGeneratedLexerClassName() {
return generatedLexerClassName.getText();
}

private CaseChangingStrategy getCaseChangingStrategy() {
return (CaseChangingStrategy) caseTransformation.getSelectedItem();
}
Expand All @@ -146,6 +176,8 @@ public String toString() {
", packageField=" + packageField +
", outputDirField=" + outputDirField +
", libDirField=" + libDirField +
", generatedParserClassName=" + generatedParserClassName +
", generatedLexerClassName=" + generatedLexerClassName +
'}';
}

Expand Down

0 comments on commit 45c5a16

Please sign in to comment.