diff --git a/LICENSE b/LICENSE
index 6fc16004..510e84ef 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,21 +1,20 @@
BSD 3-Clause License
-Copyright (c) 2024, the respective contributors, as shown by the AUTHORS file.
-All rights reserved.
+Copyright (c) 2024 - Present HiveVM Contributors
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
-* Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
-* Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
-* Neither the name of the copyright holder nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -26,4 +25,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.md b/README.md
index 4ecf0501..c9b08f24 100644
--- a/README.md
+++ b/README.md
@@ -32,21 +32,21 @@ plugins {
id "org.hivevm.cc" version "1.0.0"
}
-hiveCC {
+parserProject {
target = 'java'
- step {
+ task {
name = 'tree'
+ output = 'src/main/generated'
jjtFile = 'src/main/resources/JJTree.jjt'
- directory = 'src/main/generated2/org/hivevm/cc/jjtree'
excludes = [ 'BNF', 'BNFAction', 'BNFDeclaration', 'BNFNodeScope',
'ExpansionNodeScope', 'NodeDescriptor', 'OptionBinding' ]
}
- step {
+ task {
name = 'parser'
+ output = 'src/main/generated'
jjFile = 'src/main/resources/JavaCC.jj'
- directory = 'src/main/generated2/org/hivevm/cc/parser'
}
}
~~~
diff --git a/bootstrap/javacc.jar b/bootstrap/javacc.jar
index 8b5a7845..fc3cd7fb 100644
Binary files a/bootstrap/javacc.jar and b/bootstrap/javacc.jar differ
diff --git a/build.gradle b/build.gradle
index 011f3aca..b1900412 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,6 +1,6 @@
plugins {
id 'com.gradle.plugin-publish' version '1.3.0'
- id "org.hivevm.cc" version "1.0.0"
+ id "org.hivevm.cc" version "1.0.1"
id 'java-gradle-plugin'
id 'maven-publish'
}
@@ -18,7 +18,7 @@ repositories {
}
-version = '1.0.0'
+version = '1.0.1'
group = 'org.hivevm'
@@ -87,33 +87,33 @@ gradlePlugin {
}
-hiveCC {
+parserProject {
target = 'java'
- step {
+ task {
name = 'tree'
+ output = 'src/main/generated'
jjtFile = 'src/main/resources/JJTree.jjt'
- directory = 'src/main/generated/org/hivevm/cc/jjtree'
excludes = [ 'BNF', 'BNFAction', 'BNFDeclaration', 'BNFNodeScope',
'ExpansionNodeScope', 'NodeDescriptor', 'OptionBinding' ]
}
- step {
+ task {
name = 'parser'
- jjFile = 'src/main/resources/JavaCC.jj'
- directory = 'src/main/generated/org/hivevm/cc/parser'
+ output = 'src/main/generated'
+ jjFile = 'src/main/resources/JavaCC.jj'
}
- // step {
+ // task {
// name = 'oql4java'
- // directory = '/data/smartIO/develop/parser/parser/src/main/java/it/smartio/text/parser'
+ // output = '/data/smartIO/develop/parser/parser/src/main/java'
// jjtFile = '/data/smartIO/develop/parser/parser/src/main/resources/it/smartio/text/parser/OQL.jjt'
// }
- // step {
+ // task {
// name = 'oql4cpp'
// target = 'cpp'
- // directory = '/data/smartIO/develop/core-cpp/text/parser'
+ // output = '/data/smartIO/develop/core-cpp/text/parser'
// jjtFile = '/data/smartIO/develop/parser/parser/src/main/resources/it/smartio/text/parser/OQL-Cpp.jjt'
// }
-}
\ No newline at end of file
+}
diff --git a/build.xml b/build.xml
index 723f291e..bea3a58b 100644
--- a/build.xml
+++ b/build.xml
@@ -3,7 +3,6 @@
-
@@ -72,19 +71,19 @@
-
+
-
+
-
-
+
+
diff --git a/src/main/gradle/org/hivevm/cc/GradlePlugin.java b/src/main/gradle/org/hivevm/cc/GradlePlugin.java
index a2923156..668776b1 100644
--- a/src/main/gradle/org/hivevm/cc/GradlePlugin.java
+++ b/src/main/gradle/org/hivevm/cc/GradlePlugin.java
@@ -12,14 +12,13 @@
*/
public class GradlePlugin implements Plugin {
- public static final String CONFIG = "hiveCC";
-
+ private static final String CONFIG = "parserProject";
@Override
public void apply(Project project) {
ExtensionContainer extension = project.getExtensions();
- extension.create(GradlePlugin.CONFIG, GeneratorConfig.class);
+ extension.create(GradlePlugin.CONFIG, ParserProject.class);
- project.getTasks().register("generateParser", GeneratorTask.class);
+ project.getTasks().register("generateParser", ParserGenerator.class);
}
}
\ No newline at end of file
diff --git a/src/main/gradle/org/hivevm/cc/GeneratorTask.java b/src/main/gradle/org/hivevm/cc/ParserGenerator.java
similarity index 72%
rename from src/main/gradle/org/hivevm/cc/GeneratorTask.java
rename to src/main/gradle/org/hivevm/cc/ParserGenerator.java
index d374e705..7b62691b 100644
--- a/src/main/gradle/org/hivevm/cc/GeneratorTask.java
+++ b/src/main/gradle/org/hivevm/cc/ParserGenerator.java
@@ -3,13 +3,6 @@
package org.hivevm.cc;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import javax.inject.Inject;
-
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
@@ -18,13 +11,20 @@
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.inject.Inject;
+
/**
- * The {@link GeneratorTask} class.
+ * The {@link ParserGenerator} class.
*/
-public abstract class GeneratorTask extends DefaultTask {
+public abstract class ParserGenerator extends DefaultTask {
@Inject
- public GeneratorTask() {
+ public ParserGenerator() {
setGroup("HiveVM");
setDescription("Generates a parser");
}
@@ -41,7 +41,7 @@ public List getAvailableOutputTypes() {
@TaskAction
public void process() {
- GeneratorConfig config = getProject().getExtensions().findByType(GeneratorConfig.class);
+ ParserProject config = getProject().getExtensions().findByType(ParserProject.class);
if (config == null) {
getProject().getLogger().error("No configuration defined");
@@ -49,8 +49,7 @@ public void process() {
}
Language defaultTarget = (config.target == null) ? Language.JAVA : config.target;
-
- config.getSteps().forEach(s -> process(s, defaultTarget));
+ config.getTasks().forEach(s -> process(s, defaultTarget));
}
protected File getFile(String pathname) {
@@ -67,14 +66,13 @@ protected File getFile(String pathname) {
return new File(projectDir, pathname);
}
-
- protected void process(GeneratorStep step, Language target) {
- Language language = (step.target == null) ? target : step.target;
+ protected void process(ParserTask task, Language target) {
+ Language language = (task.target == null) ? target : task.target;
HiveCCBuilder builder = HiveCCBuilder.of(language);
- builder.setTargetDir(getFile(step.directory));
- builder.setJJTreeFile(getFile(step.jjtFile));
- builder.setJJFile(getFile(step.jjFile));
- builder.setExcludes(step.excludes);
+ builder.setTargetDir(getFile(task.output));
+ builder.setJJTreeFile(getFile(task.jjtFile));
+ builder.setJJFile(getFile(task.jjFile));
+ builder.setExcludes(task.excludes);
builder.build();
}
}
\ No newline at end of file
diff --git a/src/main/gradle/org/hivevm/cc/GeneratorConfig.java b/src/main/gradle/org/hivevm/cc/ParserProject.java
similarity index 62%
rename from src/main/gradle/org/hivevm/cc/GeneratorConfig.java
rename to src/main/gradle/org/hivevm/cc/ParserProject.java
index 8e6088b5..bef2eff6 100644
--- a/src/main/gradle/org/hivevm/cc/GeneratorConfig.java
+++ b/src/main/gradle/org/hivevm/cc/ParserProject.java
@@ -3,28 +3,28 @@
package org.hivevm.cc;
-import java.util.List;
-
-import javax.inject.Inject;
-
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Nested;
-public abstract class GeneratorConfig {
+import java.util.List;
+
+import javax.inject.Inject;
+
+public abstract class ParserProject {
private final Project project;
public Language target;
- private final ListProperty steps;
+ private final ListProperty tasks;
@Inject
- public GeneratorConfig(Project project) {
+ public ParserProject(Project project) {
this.project = project;
- this.steps = project.getObjects().listProperty(GeneratorStep.class).empty();
+ this.tasks = project.getObjects().listProperty(ParserTask.class).empty();
}
protected final Project getProject() {
@@ -32,12 +32,12 @@ protected final Project getProject() {
}
@Nested
- public final List getSteps() {
- return this.steps.get();
+ public final List getTasks() {
+ return this.tasks.get();
}
- public final void step(Action super GeneratorStep> action) {
- this.steps.add(newInstance(GeneratorStep.class, action));
+ public final void task(Action super ParserTask> action) {
+ this.tasks.add(newInstance(ParserTask.class, action));
}
private C newInstance(Class clazz, Action super C> action) {
diff --git a/src/main/gradle/org/hivevm/cc/GeneratorStep.java b/src/main/gradle/org/hivevm/cc/ParserTask.java
similarity index 82%
rename from src/main/gradle/org/hivevm/cc/GeneratorStep.java
rename to src/main/gradle/org/hivevm/cc/ParserTask.java
index b720f706..1fa19df9 100644
--- a/src/main/gradle/org/hivevm/cc/GeneratorStep.java
+++ b/src/main/gradle/org/hivevm/cc/ParserTask.java
@@ -3,13 +3,13 @@
package org.hivevm.cc;
+import org.gradle.api.Project;
+
import java.util.List;
import javax.inject.Inject;
-import org.gradle.api.Project;
-
-public class GeneratorStep {
+public class ParserTask {
private final Project project;
@@ -20,12 +20,12 @@ public class GeneratorStep {
public String jjFile;
public String jjtFile;
- public String directory;
+ public String output;
public List excludes;
@Inject
- public GeneratorStep(Project project) {
+ public ParserTask(Project project) {
this.project = project;
}
diff --git a/src/main/java/javacc.java b/src/main/java/javacc.java
index 252d63da..824f662e 100644
--- a/src/main/java/javacc.java
+++ b/src/main/java/javacc.java
@@ -24,11 +24,9 @@
* Intermediary between OS script and main program of application. Having this intermediary allows
* the OS scripts to be package name independent.
*/
-public final class javacc {
+public interface javacc {
- private javacc() {}
-
- public static void main(String[] args) throws Exception {
+ static void main(String[] args) throws Exception {
org.hivevm.cc.JJParser.main(args);
}
}
diff --git a/src/main/java/jjdoc.java b/src/main/java/jjdoc.java
index a035197b..ad58c25d 100644
--- a/src/main/java/jjdoc.java
+++ b/src/main/java/jjdoc.java
@@ -24,11 +24,9 @@
* Intermediary between OS script and main program of application. Having this intermediary allows
* the OS scripts to be package name independent.
*/
-public final class jjdoc {
+public interface jjdoc {
- private jjdoc() {}
-
- public static void main(String[] args) throws Exception {
+ static void main(String[] args) throws Exception {
org.hivevm.cc.doc.JJDocMain.main(args);
}
}
diff --git a/src/main/java/jjtree.java b/src/main/java/jjtree.java
index 73d2669b..ee541071 100644
--- a/src/main/java/jjtree.java
+++ b/src/main/java/jjtree.java
@@ -24,12 +24,9 @@
* Intermediary between OS script and main program of application. Having this intermediary allows
* the OS scripts to be package name independent.
*/
-public final class jjtree {
+public interface jjtree {
- private jjtree() {}
-
- public static void main(String[] args) {
+ static void main(String[] args) {
org.hivevm.cc.JJTree.main(args);
}
-
}
diff --git a/src/main/java/org/hivevm/cc/HiveCC.java b/src/main/java/org/hivevm/cc/HiveCC.java
index 12ac49ee..c0c688c7 100644
--- a/src/main/java/org/hivevm/cc/HiveCC.java
+++ b/src/main/java/org/hivevm/cc/HiveCC.java
@@ -1,14 +1,14 @@
-// Copyright 2024 HiveVM.ORG. All rights reserved.
+// Copyright 2024 HiveVM.org. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package org.hivevm.cc;
+import org.hivevm.cc.utils.Version;
+
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
-import org.hivevm.cc.utils.Version;
-
/**
* This package contains data created as a result of parsing and semanticizing a JavaCC input file.
* This data is what is used by the back-ends of JavaCC as well as any other back-end of JavaCC
@@ -69,20 +69,21 @@ public interface HiveCC {
Version VERSION = HiveCC.load();
+ static int getInt(Properties properties, String name) {
+ return properties.containsKey(name) ? Integer.parseInt(properties.getProperty(name)) : 0;
+ }
+
static Version load() {
- String major = "??";
- String minor = "??";
- String patch = "??";
-
- try (InputStream is = HiveCC.class.getResourceAsStream("/version.properties")) {
- Properties properties = new Properties();
- properties.load(is);
- major = properties.getProperty("version.major", major);
- minor = properties.getProperty("version.minor", minor);
- patch = properties.getProperty("version.patch", patch);
+ Properties properties = new Properties();
+ try (InputStream stream = HiveCC.class.getResourceAsStream("/version.properties")) {
+ properties.load(stream);
} catch (IOException e) {
System.err.println("Could not read version.properties: " + e);
}
- return Version.of(Integer.parseInt(major), Integer.parseInt(minor), patch.equals("") ? 0 : Integer.parseInt(patch));
+
+ int major = HiveCC.getInt(properties, "version.major");
+ int minor = HiveCC.getInt(properties, "version.minor");
+ int patch = HiveCC.getInt(properties, "version.patch");
+ return Version.of(major, minor, patch);
}
}
diff --git a/src/main/java/org/hivevm/cc/JJCodeBlock.java b/src/main/java/org/hivevm/cc/JJCodeBlock.java
deleted file mode 100644
index 6de4940f..00000000
--- a/src/main/java/org/hivevm/cc/JJCodeBlock.java
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2024 HiveVM.ORG. All rights reserved.
-// SPDX-License-Identifier: BSD-3-Clause
-
-package org.hivevm.cc;
-
-
-/**
- * The {@link JJCodeBlock} class.
- */
-public enum JJCodeBlock {
-
- Code;
-
- public final String CODE;
-
- private JJCodeBlock() {
- this.CODE = "@" + name().toLowerCase();
- }
-
- public final String strip(String text) {
- return text.substring(name().length() + 1);
- }
-}
diff --git a/src/main/java/org/hivevm/cc/generator/JJTreeCodeGenerator.java b/src/main/java/org/hivevm/cc/generator/ASTCodeGenerator.java
similarity index 70%
rename from src/main/java/org/hivevm/cc/generator/JJTreeCodeGenerator.java
rename to src/main/java/org/hivevm/cc/generator/ASTCodeGenerator.java
index 4b3813d4..11e66d46 100644
--- a/src/main/java/org/hivevm/cc/generator/JJTreeCodeGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/ASTCodeGenerator.java
@@ -3,11 +3,6 @@
package org.hivevm.cc.generator;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.Set;
-
import org.hivevm.cc.jjtree.ASTBNFAction;
import org.hivevm.cc.jjtree.ASTBNFDeclaration;
import org.hivevm.cc.jjtree.ASTBNFNodeScope;
@@ -30,7 +25,12 @@
import org.hivevm.cc.jjtree.NodeScope;
import org.hivevm.cc.jjtree.Token;
-public abstract class JJTreeCodeGenerator extends JJTreeParserDefaultVisitor {
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+public abstract class ASTCodeGenerator extends JJTreeParserDefaultVisitor {
private final Set nodesToGenerate = new HashSet<>();
@@ -44,12 +44,6 @@ protected final Iterable nodesToGenerate() {
return this.nodesToGenerate;
}
- protected abstract String getPointer();
-
- protected abstract String getBoolean();
-
- protected abstract String getTryFinally();
-
@Override
public final Object defaultVisit(Node node, ASTWriter data) {
data.handleJJTreeNode((ASTNode) node, this);
@@ -61,15 +55,14 @@ public final Object visit(ASTGrammar node, ASTWriter data) {
return node.childrenAccept(this, data);
}
+ /**
+ * Assume that this action requires an early node close, and then try to decide whether this
+ * assumption is false. Do this by looking outwards through the enclosing expansion units. If we
+ * ever find that we are enclosed in a unit which is not the final unit in a sequence we know that
+ * an early close is not required.
+ */
@Override
public final Object visit(ASTBNFAction node, ASTWriter writer) {
- /*
- * Assume that this action requires an early node close, and then try to decide whether this
- * assumption is false. Do this by looking outwards through the enclosing expansion units. If we
- * ever find that we are enclosed in a unit which is not the final unit in a sequence we know
- * that an early close is not required.
- */
-
NodeScope ns = NodeScope.getEnclosingNodeScope(node);
if ((ns != null) && !ns.isVoid()) {
boolean needClose = true;
@@ -182,66 +175,29 @@ private final String getIndentation(ASTNode n) {
protected abstract void insertOpenNodeCode(NodeScope ns, ASTWriter writer, String indent, JJTreeOptions options);
- private final void insertCloseNodeCode(NodeScope ns, ASTWriter writer, String indent, boolean isFinal,
- JJTreeOptions options) {
- String closeNode = ns.getNodeDescriptor().closeNode(ns.nodeVar);
- writer.println(indent + closeNode);
- if (ns.usesCloseNodeVar() && !isFinal) {
- writer.println(indent + ns.closedVar + " = false;");
- }
- if (options.getNodeScopeHook()) {
- writer.println(indent + "if (jjtree.nodeCreated()) {");
- writer.println(indent + " jjtreeCloseNodeScope(" + ns.nodeVar + ");");
- writer.println(indent + "}");
- }
-
- if (options.getTrackTokens()) {
- writer.println(indent + ns.nodeVar + getPointer() + "jjtSetLastToken(getToken(0));");
- }
- }
+ protected abstract void insertCloseNodeCode(NodeScope ns, ASTWriter writer, String indent, boolean isFinal,
+ JJTreeOptions options);
protected abstract void insertCatchBlocks(NodeScope ns, ASTWriter writer, Enumeration thrown_names,
String indent);
+ protected abstract void catchExpansionUnit(NodeScope ns, ASTWriter writer, String indent, ASTNode expansion_unit);
+
- private static void findThrown(NodeScope ns, Hashtable thrown_set, ASTNode expansion_unit) {
+ protected final void findThrown(NodeScope ns, Hashtable thrown_set, ASTNode expansion_unit) {
if (expansion_unit instanceof ASTBNFNonTerminal) {
- /*
- * Should really make the nonterminal explicitly maintain its name.
- */
+ // Should really make the nonterminal explicitly maintain its name.
String nt = expansion_unit.getFirstToken().image;
ASTProduction prod = JJTreeGlobals.productions.get(nt);
if (prod != null) {
- Enumeration e = prod.throws_list.elements();
- while (e.hasMoreElements()) {
- String t = e.nextElement();
- thrown_set.put(t, t);
- }
+ prod.throwElements().forEach(t -> thrown_set.put(t, t));
}
}
for (int i = 0; i < expansion_unit.jjtGetNumChildren(); ++i) {
ASTNode n = (ASTNode) expansion_unit.jjtGetChild(i);
- JJTreeCodeGenerator.findThrown(ns, thrown_set, n);
+ findThrown(ns, thrown_set, n);
}
}
- private void catchExpansionUnit(NodeScope ns, ASTWriter writer, String indent, ASTNode expansion_unit) {
- writer.openCodeBlock(null);
-
- Hashtable thrown_set = new Hashtable<>();
- JJTreeCodeGenerator.findThrown(ns, thrown_set, expansion_unit);
- Enumeration thrown_names = thrown_set.elements();
- insertCatchBlocks(ns, writer, thrown_names, indent);
-
- writer.println(indent + "} " + getTryFinally() + "{");
- if (ns.usesCloseNodeVar()) {
- writer.println(indent + " if (" + ns.closedVar + ") {");
- insertCloseNodeCode(ns, writer, indent + " ", true, expansion_unit.jjtOptions());
- writer.println(indent + " }");
- }
- writer.print(indent + "}");
- writer.closeCodeBlock();
- }
-
public abstract void generateJJTree(JJTreeOptions options);
}
diff --git a/src/main/java/org/hivevm/cc/generator/AbstractFileGenerator.java b/src/main/java/org/hivevm/cc/generator/AbstractFileGenerator.java
deleted file mode 100644
index 0c185c26..00000000
--- a/src/main/java/org/hivevm/cc/generator/AbstractFileGenerator.java
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2024 HiveVM.ORG. All rights reserved.
-// SPDX-License-Identifier: BSD-3-Clause
-
-package org.hivevm.cc.generator;
-
-import java.io.File;
-import java.io.IOException;
-
-import org.hivevm.cc.parser.JavaCCErrors;
-import org.hivevm.cc.utils.DigestOptions;
-import org.hivevm.cc.utils.DigestWriter;
-import org.hivevm.cc.utils.Template;
-import org.hivevm.cc.utils.TemplateProvider;
-
-/**
- * The {@link AbstractFileGenerator} class.
- */
-public abstract class AbstractFileGenerator {
-
- /**
- * Generates a {@link File} from a template.
- *
- * @param tpl
- * @param options
- */
- protected final void generateFile(TemplateProvider tpl, DigestOptions options) {
- generateFile(tpl, null, options);
- }
-
- /**
- * Generates a {@link File} from a template.
- *
- * @param tpl
- * @param name
- * @param options
- */
- protected final void generateFile(TemplateProvider tpl, String name, DigestOptions options) {
- String filename = tpl.getFilename(name);
- try (DigestWriter writer = tpl.createDigestWriter(name, options)) {
- Template template = Template.of(tpl, writer.options());
- template.render(writer);
- } catch (IOException e) {
- System.err.println("Failed to create file: " + filename + " " + e);
- JavaCCErrors.semantic_error("Could not open file: " + filename + " for writing.");
- throw new Error();
- }
- }
-}
diff --git a/src/main/java/org/hivevm/cc/generator/CodeGenerator.java b/src/main/java/org/hivevm/cc/generator/CodeGenerator.java
index c0eb2c6e..61351513 100644
--- a/src/main/java/org/hivevm/cc/generator/CodeGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/CodeGenerator.java
@@ -3,7 +3,7 @@
package org.hivevm.cc.generator;
-import org.hivevm.cc.JJCodeBlock;
+import org.hivevm.cc.jjtree.ASTWriter;
import org.hivevm.cc.parser.JavaCCParserConstants;
import org.hivevm.cc.parser.Token;
import org.hivevm.cc.utils.Encoding;
@@ -75,8 +75,8 @@ protected String getStringForTokenOnly(Token t) {
}
if ((t.kind == JavaCCParserConstants.STRING_LITERAL) || (t.kind == JavaCCParserConstants.CHARACTER_LITERAL)) {
retval += Encoding.escapeUnicode(t.image);
- } else if (t.image.startsWith(JJCodeBlock.Code.CODE)) {
- retval += JJCodeBlock.Code.strip(t.image);
+ } else if (t.image.startsWith(ASTWriter.CodeBlock.CODE.image)) {
+ retval += ASTWriter.CodeBlock.CODE.strip(t.image);
} else {
retval += t.image;
}
diff --git a/src/main/java/org/hivevm/cc/generator/Lexer.java b/src/main/java/org/hivevm/cc/generator/Lexer.java
new file mode 100644
index 00000000..b9ee2a00
--- /dev/null
+++ b/src/main/java/org/hivevm/cc/generator/Lexer.java
@@ -0,0 +1,15 @@
+// Copyright 2024 HiveVM.ORG. All rights reserved.
+// SPDX-License-Identifier: BSD-3-Clause
+
+package org.hivevm.cc.generator;
+
+
+/**
+ * The {@link Lexer} class.
+ */
+public interface Lexer {
+
+ String LOHI_BYTES = "LOHI_BYTES";
+ String STATES = "STATES";
+ String NON_ASCII_TABLE = "NON_ASCII_TABLE";
+}
diff --git a/src/main/java/org/hivevm/cc/generator/LexerBuilder.java b/src/main/java/org/hivevm/cc/generator/LexerBuilder.java
index ce451c19..a7eb1724 100644
--- a/src/main/java/org/hivevm/cc/generator/LexerBuilder.java
+++ b/src/main/java/org/hivevm/cc/generator/LexerBuilder.java
@@ -1551,10 +1551,6 @@ private final void GetNonAsciiMoves(LexerData data, NfaState state) {
data.lohiByteTab.put(tmp, ind = Integer.valueOf(data.lohiByteCnt++));
}
- if (state.loByteVec == null) {
- state.loByteVec = new Vector<>();
- }
-
state.loByteVec.add(Integer.valueOf(i));
state.loByteVec.add(ind);
}
diff --git a/src/main/java/org/hivevm/cc/generator/LexerGenerator.java b/src/main/java/org/hivevm/cc/generator/LexerGenerator.java
index 845fead9..a7d9f93d 100644
--- a/src/main/java/org/hivevm/cc/generator/LexerGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/LexerGenerator.java
@@ -3,6 +3,13 @@
package org.hivevm.cc.generator;
+import org.hivevm.cc.lexer.NfaState;
+import org.hivevm.cc.parser.JavaCCErrors;
+import org.hivevm.cc.parser.RStringLiteral;
+import org.hivevm.cc.parser.RegularExpression;
+import org.hivevm.cc.parser.Token;
+import org.hivevm.cc.utils.Encoding;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@@ -11,13 +18,6 @@
import java.util.List;
import java.util.Vector;
-import org.hivevm.cc.lexer.NfaState;
-import org.hivevm.cc.parser.JavaCCErrors;
-import org.hivevm.cc.parser.RStringLiteral;
-import org.hivevm.cc.parser.RegularExpression;
-import org.hivevm.cc.parser.Token;
-import org.hivevm.cc.utils.Encoding;
-
/**
* The {@link LexerGenerator} class.
*/
@@ -543,7 +543,7 @@ private void DumpAsciiMove(PrintWriter writer, LexerStateData data, NfaState sta
if (oneBit != -1) {
writer.println(" if (curChar == " + ((64 * byteNum) + oneBit) + kindCheck + ")");
} else {
- writer.println(" if ((0x" + Long.toHexString(state.asciiMoves[byteNum]) + "L & l) != 0L"
+ writer.println(" if ((" + toHexString(state.asciiMoves[byteNum]) + " & l) != 0L"
+ kindCheck + ")");
}
@@ -566,7 +566,7 @@ private void DumpAsciiMove(PrintWriter writer, LexerStateData data, NfaState sta
writer.println(" if (curChar != " + ((64 * byteNum) + oneBit) + ")");
writer.println(" break;");
} else if (state.asciiMoves[byteNum] != 0xffffffffffffffffL) {
- writer.println(" if ((0x" + Long.toHexString(state.asciiMoves[byteNum]) + "L & l) == 0L)");
+ writer.println(" if ((" + toHexString(state.asciiMoves[byteNum]) + " & l) == 0L)");
writer.println(" break;");
}
@@ -580,7 +580,7 @@ private void DumpAsciiMove(PrintWriter writer, LexerStateData data, NfaState sta
writer.println(" if (curChar == " + ((64 * byteNum) + oneBit) + ")");
prefix = " ";
} else if (state.asciiMoves[byteNum] != 0xffffffffffffffffL) {
- writer.println(" if ((0x" + Long.toHexString(state.asciiMoves[byteNum]) + "L & l) != 0L)");
+ writer.println(" if ((" + toHexString(state.asciiMoves[byteNum]) + " & l) != 0L)");
prefix = " ";
}
@@ -648,8 +648,8 @@ private void DumpAsciiMoveForCompositeState(PrintWriter writer, LexerStateData d
writer.println(
" " + (elseNeeded ? "else " : "") + "if (curChar == " + ((64 * byteNum) + oneBit) + ")");
} else {
- writer.println(" " + (elseNeeded ? "else " : "") + "if ((0x"
- + Long.toHexString(state.asciiMoves[byteNum]) + "L & l) != 0L)");
+ writer.println(" " + (elseNeeded ? "else " : "") + "if (("
+ + toHexString(state.asciiMoves[byteNum]) + " & l) != 0L)");
}
prefix = " ";
}
@@ -1059,4 +1059,23 @@ protected final void DumpCharAndRangeMoves(PrintWriter writer, LexerStateData da
writer.println(" }");
writer.println(" } while(i != startsAt);");
}
+
+ // Assumes l != 0L
+ protected static char MaxChar(long l) {
+ for (int i = 64; i-- > 0;) {
+ if ((l & (1L << i)) != 0L) {
+ return (char) i;
+ }
+ }
+ return 0xffff;
+ }
+
+ protected String toHexString(long value) {
+ return "0x" + Long.toHexString(value) + "L";
+ }
+
+ protected String getLohiBytes(LexerData data, int i) {
+ return String.join(", ", toHexString(data.lohiByte.get(i)[0]), toHexString(data.lohiByte.get(i)[1]),
+ toHexString(data.lohiByte.get(i)[2]), toHexString(data.lohiByte.get(i)[3]));
+ }
}
diff --git a/src/main/java/org/hivevm/cc/generator/ParserEngine.java b/src/main/java/org/hivevm/cc/generator/ParserEngine.java
index 67f06bd6..1711d51d 100644
--- a/src/main/java/org/hivevm/cc/generator/ParserEngine.java
+++ b/src/main/java/org/hivevm/cc/generator/ParserEngine.java
@@ -27,14 +27,14 @@ public class ParserEngine {
private final LexerGenerator lexerGenerator;
private final ParserGenerator parserGenerator;
- private final JJTreeCodeGenerator treeGenerator;
+ private final ASTCodeGenerator treeGenerator;
private final FileGenerator otherFilesGenerator;
/**
* Constructs an instance of {@link ParserEngine}.
*/
private ParserEngine(LexerGenerator lexerGenerator, ParserGenerator parserGenerator,
- JJTreeCodeGenerator treeGenerator, FileGenerator otherFilesGenerator) {
+ ASTCodeGenerator treeGenerator, FileGenerator otherFilesGenerator) {
this.lexerGenerator = lexerGenerator;
this.parserGenerator = parserGenerator;
this.treeGenerator = treeGenerator;
diff --git a/src/main/java/org/hivevm/cc/generator/cpp/CppFileGenerator.java b/src/main/java/org/hivevm/cc/generator/cpp/CppFileGenerator.java
index c46d684d..4583c131 100644
--- a/src/main/java/org/hivevm/cc/generator/cpp/CppFileGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/cpp/CppFileGenerator.java
@@ -3,12 +3,7 @@
package org.hivevm.cc.generator.cpp;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.List;
-
import org.hivevm.cc.JavaCCRequest;
-import org.hivevm.cc.generator.AbstractFileGenerator;
import org.hivevm.cc.generator.FileGenerator;
import org.hivevm.cc.generator.LexerData;
import org.hivevm.cc.parser.JavaCCErrors;
@@ -17,13 +12,17 @@
import org.hivevm.cc.parser.RegExprSpec;
import org.hivevm.cc.parser.RegularExpression;
import org.hivevm.cc.parser.TokenProduction;
-import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
/**
* Generates the Constants file.
*/
-public class CppFileGenerator extends AbstractFileGenerator implements FileGenerator {
+public class CppFileGenerator implements FileGenerator {
@Override
public final void handleRequest(JavaCCRequest request, LexerData context) throws ParseException {
@@ -43,26 +42,26 @@ public final void handleRequest(JavaCCRequest request, LexerData context) throws
.set("image", (i, w) -> CppFileGenerator.getRegExp(w, true, i, expressions));
- generateFile(CppTemplate.JAVACC, new DigestOptions(context.options()));
+ TemplateProvider.render(CppTemplate.JAVACC, context.options());
- generateFile(CppTemplate.TOKEN, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKEN_H, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKENMANAGER, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKENNANAGERERROR, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKENNANAGERERROR_H, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKENNANAGERHANDLER, new DigestOptions(context.options()));
- generateFile(CppTemplate.TOKENNANAGERHANDLER_H, new DigestOptions(context.options()));
+ TemplateProvider.render(CppTemplate.TOKEN, context.options());
+ TemplateProvider.render(CppTemplate.TOKEN_H, context.options());
+ TemplateProvider.render(CppTemplate.TOKENMANAGER, context.options());
+ TemplateProvider.render(CppTemplate.TOKENNANAGERERROR, context.options());
+ TemplateProvider.render(CppTemplate.TOKENNANAGERERROR_H, context.options());
+ TemplateProvider.render(CppTemplate.TOKENNANAGERHANDLER, context.options());
+ TemplateProvider.render(CppTemplate.TOKENNANAGERHANDLER_H, context.options());
- generateFile(CppTemplate.READER, new DigestOptions(context.options()));
- generateFile(CppTemplate.STRINGREADER, new DigestOptions(context.options()));
- generateFile(CppTemplate.STRINGREADER_H, new DigestOptions(context.options()));
+ TemplateProvider.render(CppTemplate.READER, context.options());
+ TemplateProvider.render(CppTemplate.STRINGREADER, context.options());
+ TemplateProvider.render(CppTemplate.STRINGREADER_H, context.options());
- generateFile(CppTemplate.PARSEEXCEPTION, new DigestOptions(context.options()));
- generateFile(CppTemplate.PARSEEXCEPTION_H, new DigestOptions(context.options()));
- generateFile(CppTemplate.PARSERHANDLER, new DigestOptions(context.options()));
- generateFile(CppTemplate.PARSERHANDLER_H, new DigestOptions(context.options()));
+ TemplateProvider.render(CppTemplate.PARSEEXCEPTION, context.options());
+ TemplateProvider.render(CppTemplate.PARSEEXCEPTION_H, context.options());
+ TemplateProvider.render(CppTemplate.PARSERHANDLER, context.options());
+ TemplateProvider.render(CppTemplate.PARSERHANDLER_H, context.options());
- generateFile(CppTemplate.PARSER_CONSTANTS, request.getParserName(), new DigestOptions(context.options(), options));
+ TemplateProvider.render(CppTemplate.PARSER_CONSTANTS, context.options(), options, request.getParserName());
}
private static void getRegExp(PrintWriter writer, boolean isImage, int i, List expressions) {
@@ -88,9 +87,18 @@ private static void getRegExp(PrintWriter writer, boolean isImage, int i, List CppLexerGenerator.getLohiBytes(data, i));
- options.add("STATES", data.stateNames).set("head", (i, w) -> dumpNfaAndDfaHeader(data.getStateData(i), w))
+ options.add("PARSER_NAME_UPPER", data.getParserName().toUpperCase());
+ options.add(Lexer.LOHI_BYTES, data.lohiByte.keySet()).set("bytes", i -> getLohiBytes(data, i));
+ options.add(Lexer.STATES, data.stateNames).set("head", (i, w) -> dumpNfaAndDfaHeader(data.getStateData(i), w))
.set("body", (i, w) -> dumpNfaAndDfa(data.getStateData(i), w));
- options.add("NON_ASCII_TABLE", data.nonAsciiTableForMethod).set("offset", i -> ((NfaState) i).nonAsciiMethod)
+ options.add(Lexer.NON_ASCII_TABLE, data.nonAsciiTableForMethod).set("offset", i -> ((NfaState) i).nonAsciiMethod)
.set("method", (k, w) -> DumpNonAsciiMoveMethod(data, k, w));
options.setWriter("DumpStrLiteralImages", w -> DumpStrLiteralImages(w, data));
@@ -67,23 +72,16 @@ protected final void dumpAll(LexerData data) throws IOException {
options.set("jjCheckNAddStatesDualNeeded", data.jjCheckNAddStatesDualNeeded);
options.set("jjCheckNAddStatesUnaryNeeded", data.jjCheckNAddStatesUnaryNeeded);
- DigestOptions digest = new DigestOptions(data.options(), options);
- try (CppWriter writer = new CppWriter(data.getParserName() + "TokenManager", CppTemplate.LEXER, digest)) {
- writer.writeTemplate();
- writer.switchToHeader();
- data.boilerPlateDumped = false;
- writer.writeTemplateHeader();
- }
- }
+ TemplateProvider provider = CppTemplate.LEXER;
+ provider.render(data.options(), options, data.getParserName());
+ data.boilerPlateDumped = false;
- private static String getLohiBytes(LexerData data, int i) {
- return "0x" + Long.toHexString(data.lohiByte.get(i)[0]) + "L, " + "0x" + Long.toHexString(data.lohiByte.get(i)[1])
- + "L, " + "0x" + Long.toHexString(data.lohiByte.get(i)[2]) + "L, " + "0x"
- + Long.toHexString(data.lohiByte.get(i)[3]) + "L";
+ provider = CppTemplate.LEXER_H;
+ provider.render(data.options(), options, data.getParserName());
}
- private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data) {
+ private void DumpStaticVarDeclarations(PrintWriter writer, LexerData data) {
int i;
if (data.maxLexStates > 1) {
@@ -112,7 +110,7 @@ private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data
if ((i % 4) == 0) {
writer.print("\n ");
}
- writer.print("0x" + Long.toHexString(data.toToken[i]) + "L, ");
+ writer.print(toHexString(data.toToken[i]) + ", ");
}
writer.println("\n};");
}
@@ -124,7 +122,7 @@ private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data
if ((i % 4) == 0) {
writer.print("\n ");
}
- writer.print("0x" + Long.toHexString(data.toSkip[i]) + "L, ");
+ writer.print(toHexString(data.toSkip[i]) + ", ");
}
writer.println("\n};");
}
@@ -136,7 +134,7 @@ private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data
if ((i % 4) == 0) {
writer.print("\n ");
}
- writer.print("0x" + Long.toHexString(data.toSpecial[i]) + "L, ");
+ writer.print(toHexString(data.toSpecial[i]) + ", ");
}
writer.println("\n};");
}
@@ -148,7 +146,7 @@ private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data
if ((i % 4) == 0) {
writer.print("\n ");
}
- writer.print("0x" + Long.toHexString(data.toMore[i]) + "L, ");
+ writer.print(toHexString(data.toMore[i]) + ", ");
}
writer.println("\n};");
}
@@ -214,17 +212,17 @@ private void DumpGetNextToken(PrintWriter writer, LexerData data) {
writer.println(prefix + "{ reader->backup(0);");
if ((data.singlesToSkip[i].asciiMoves[0] != 0L) && (data.singlesToSkip[i].asciiMoves[1] != 0L)) {
writer.println(
- prefix + " while ((curChar < 64" + " && (0x" + Long.toHexString(data.singlesToSkip[i].asciiMoves[0])
- + "L & (1L << curChar)) != 0L) || \n" + prefix + " (curChar >> 6) == 1" + " && (0x"
- + Long.toHexString(data.singlesToSkip[i].asciiMoves[1]) + "L & (1L << (curChar & 077))) != 0L)");
+ prefix + " while ((curChar < 64" + " && (" + Long.toHexString(data.singlesToSkip[i].asciiMoves[0])
+ + " & (1L << curChar)) != 0L) || \n" + prefix + " (curChar >> 6) == 1" + " && ("
+ + toHexString(data.singlesToSkip[i].asciiMoves[1]) + " & (1L << (curChar & 077))) != 0L)");
} else if (data.singlesToSkip[i].asciiMoves[1] == 0L) {
writer.println(
- prefix + " while (curChar <= " + (int) CppLexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[0])
- + " && (0x" + Long.toHexString(data.singlesToSkip[i].asciiMoves[0]) + "L & (1L << curChar)) != 0L)");
+ prefix + " while (curChar <= " + (int) LexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[0])
+ + " && (" + toHexString(data.singlesToSkip[i].asciiMoves[0]) + " & (1L << curChar)) != 0L)");
} else if (data.singlesToSkip[i].asciiMoves[0] == 0L) {
writer.println(prefix + " while (curChar > 63 && curChar <= "
- + (CppLexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[1]) + 64) + " && (0x"
- + Long.toHexString(data.singlesToSkip[i].asciiMoves[1]) + "L & (1L << (curChar & 077))) != 0L)");
+ + (LexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[1]) + 64) + " && ("
+ + toHexString(data.singlesToSkip[i].asciiMoves[1]) + " & (1L << (curChar & 077))) != 0L)");
}
writer.println(prefix + "{");
@@ -328,8 +326,8 @@ private void DumpGetNextToken(PrintWriter writer, LexerData data) {
}
if (data.hasSkip || data.hasMore || data.hasSpecial) {
- writer
- .println(prefix + " if ((jjtoToken[jjmatchedKind >> 6] & " + "(1L << (jjmatchedKind & 077))) != 0L)");
+ writer.println(
+ prefix + " if ((jjtoToken[jjmatchedKind >> 6] & " + "(1ULL << (jjmatchedKind & 077))) != 0L)");
writer.println(prefix + " {");
}
@@ -364,8 +362,8 @@ private void DumpGetNextToken(PrintWriter writer, LexerData data) {
writer.println(prefix + " {");
if (data.hasSpecial) {
- writer.println(
- prefix + " if ((jjtoSpecial[jjmatchedKind >> 6] & " + "(1L << (jjmatchedKind & 077))) != 0L)");
+ writer.println(prefix + " if ((jjtoSpecial[jjmatchedKind >> 6] & "
+ + "(1ULL << (jjmatchedKind & 077))) != 0L)");
writer.println(prefix + " {");
writer.println(prefix + " matchedToken = jjFillToken();");
@@ -855,17 +853,17 @@ private void DumpStartWithStates(PrintWriter writer, LexerStateData data) {
@Override
protected final void DumpHeadForCase(PrintWriter writer, int byteNum) {
if (byteNum == 0) {
- writer.println(" unsigned long long l = 1L << curChar;");
+ writer.println(" unsigned long long l = 1ULL << curChar;");
writer.println(" (void)l;");
} else if (byteNum == 1) {
- writer.println(" unsigned long long l = 1L << (curChar & 077);");
+ writer.println(" unsigned long long l = 1ULL << (curChar & 077);");
writer.println(" (void)l;");
} else {
writer.println(" int hiByte = (curChar >> 8);");
writer.println(" int i1 = hiByte >> 6;");
- writer.println(" unsigned long long l1 = 1L << (hiByte & 077);");
+ writer.println(" unsigned long long l1 = 1ULL << (hiByte & 077);");
writer.println(" int i2 = (curChar & 0xff) >> 6;");
- writer.println(" unsigned long long l2 = 1L << (curChar & 077);");
+ writer.println(" unsigned long long l2 = 1ULL << (curChar & 077);");
}
// writer.println(" MatchLoop: do");
@@ -877,7 +875,6 @@ protected final void DumpHeadForCase(PrintWriter writer, int byteNum) {
}
private final void DumpNonAsciiMoveMethod(LexerData data, NfaState state, PrintWriter writer) {
- int j;
writer.print("\nbool " + data.getParserName() + "TokenManager::jjCanMove_" + +state.nonAsciiMethod
+ "(int hiByte, int i1, int i2, unsigned long long l1, unsigned long long l2)");
@@ -886,32 +883,28 @@ private final void DumpNonAsciiMoveMethod(LexerData data, NfaState state, PrintW
writer.println(" switch(hiByte)");
writer.println(" {");
- if ((state.loByteVec != null) && (state.loByteVec.size() > 0)) {
- for (j = 0; j < state.loByteVec.size(); j += 2) {
- writer.println(" case " + state.loByteVec.get(j).intValue() + ":");
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.loByteVec.get(j + 1).intValue()))) {
- writer.println(
- " return ((jjbitVec" + state.loByteVec.get(j + 1).intValue() + "[i2" + "] & l2) != 0L);");
- } else {
- writer.println(" return true;");
- }
+ for (int j = 0; j < state.loByteVec.size(); j += 2) {
+ writer.println(" case " + state.loByteVec.get(j).intValue() + ":");
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.loByteVec.get(j + 1).intValue()))) {
+ writer
+ .println(" return ((jjbitVec" + state.loByteVec.get(j + 1).intValue() + "[i2" + "] & l2) != 0L);");
+ } else {
+ writer.println(" return true;");
}
}
writer.println(" default :");
- if ((state.nonAsciiMoveIndices != null) && ((j = state.nonAsciiMoveIndices.length) > 0)) {
- do {
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 2]))) {
- writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 2] + "[i1] & l1) != 0L)");
- }
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 1]))) {
- writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 1] + "[i2] & l2) == 0L)");
- writer.println(" return false;");
- writer.println(" else");
- }
- writer.println(" return true;");
- } while ((j -= 2) > 0);
+ for (int j = state.nonAsciiMoveIndices.length; j > 0; j -= 2) {
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 2]))) {
+ writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 2] + "[i1] & l1) != 0L)");
+ }
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 1]))) {
+ writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 1] + "[i2] & l2) == 0L)");
+ writer.println(" return false;");
+ writer.println(" else");
+ }
+ writer.println(" return true;");
}
writer.println(" return false;");
@@ -1017,7 +1010,7 @@ private final void DumpNfaStartStatesCode(PrintWriter writer, LexerStateData dat
condGenerated = true;
- writer.print("(active" + j + " & 0x" + Long.toHexString(actives[j]) + "L) != 0L");
+ writer.print("(active" + j + " & " + toHexString(actives[j]) + ") != 0L");
}
if (condGenerated) {
@@ -1379,7 +1372,7 @@ private final void DumpDfaCode(PrintWriter writer, LexerStateData data) {
int kindToPrint;
if (i != 0) {
- writer.println("((active" + j + " & 0x" + Long.toHexString(1L << k) + "L) != 0L)");
+ writer.println("((active" + j + " & " + toHexString(1L << k) + ") != 0L)");
}
if ((data.intermediateKinds != null) && (data.intermediateKinds[((j * 64) + k)] != null)
@@ -1440,7 +1433,7 @@ private final void DumpDfaCode(PrintWriter writer, LexerStateData data) {
atLeastOne = true;
}
- writer.print("0x" + Long.toHexString(info.validKinds[j]) + "L");
+ writer.print(toHexString(info.validKinds[j]));
}
}
@@ -1449,7 +1442,7 @@ private final void DumpDfaCode(PrintWriter writer, LexerStateData data) {
writer.print(", ");
}
- writer.print("0x" + Long.toHexString(info.validKinds[j]) + "L");
+ writer.print(toHexString(info.validKinds[j]));
}
writer.println(");");
} else {
@@ -1466,7 +1459,7 @@ private final void DumpDfaCode(PrintWriter writer, LexerStateData data) {
}
if (info.validKinds[j] != 0L) {
- writer.print("active" + j + ", 0x" + Long.toHexString(info.validKinds[j]) + "L");
+ writer.print("active" + j + ", " + toHexString(info.validKinds[j]));
} else {
writer.print("active" + j + ", 0L");
}
@@ -1478,7 +1471,7 @@ private final void DumpDfaCode(PrintWriter writer, LexerStateData data) {
writer.print(", ");
}
if (info.validKinds[j] != 0L) {
- writer.print("active" + j + ", 0x" + Long.toHexString(info.validKinds[j]) + "L");
+ writer.print("active" + j + ", " + toHexString(info.validKinds[j]));
} else {
writer.print("active" + j + ", 0L");
}
@@ -1817,15 +1810,4 @@ private static void getStateNames(PrintWriter writer, LexerData data) {
}
writer.println("};");
}
-
- // Assumes l != 0L
- private static char MaxChar(long l) {
- for (int i = 64; i-- > 0;) {
- if ((l & (1L << i)) != 0L) {
- return (char) i;
- }
- }
-
- return 0xffff;
- }
}
diff --git a/src/main/java/org/hivevm/cc/generator/cpp/CppParserGenerator.java b/src/main/java/org/hivevm/cc/generator/cpp/CppParserGenerator.java
index 40017451..41fbb6ba 100644
--- a/src/main/java/org/hivevm/cc/generator/cpp/CppParserGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/cpp/CppParserGenerator.java
@@ -3,11 +3,6 @@
package org.hivevm.cc.generator.cpp;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Iterator;
-import java.util.stream.Collectors;
-
import org.hivevm.cc.HiveCC;
import org.hivevm.cc.generator.ParserData;
import org.hivevm.cc.generator.ParserGenerator;
@@ -27,10 +22,14 @@
import org.hivevm.cc.parser.ZeroOrMore;
import org.hivevm.cc.parser.ZeroOrOne;
import org.hivevm.cc.semantic.Semanticize;
-import org.hivevm.cc.source.CppWriter;
-import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.Encoding;
import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.stream.Collectors;
/**
* Generate the parser.
@@ -40,6 +39,7 @@ public class CppParserGenerator extends ParserGenerator {
@Override
protected void generate(ParserData data) throws IOException {
TemplateOptions options = new TemplateOptions();
+ options.add("PARSER_NAME_UPPER", data.getParserName().toUpperCase());
options.set("IS_GENERATED", data.isGenerated());
options.set("LOOKAHEAD_NEEDED", data.isLookAheadNeeded());
@@ -61,12 +61,11 @@ protected void generate(ParserData data) throws IOException {
options.add("EXPANSIONS", data.getExpansions()).set("phase",
(e, p) -> generatePhase3Routine(data, e, data.getCount(e), p, data.options()));
- DigestOptions digest = new DigestOptions(data.options(), options);
- try (CppWriter writer = new CppWriter(data.getParserName(), CppTemplate.PARSER, digest)) {
- writer.writeTemplate();
- writer.switchToHeader();
- writer.writeTemplateHeader();
- }
+ TemplateProvider provider = CppTemplate.PARSER;
+ provider.render(data.options(), options,data.getParserName());
+
+ provider = CppTemplate.PARSER_H;
+ provider.render(data.options(), options,data.getParserName());
}
/**
diff --git a/src/main/java/org/hivevm/cc/generator/cpp/CppTemplate.java b/src/main/java/org/hivevm/cc/generator/cpp/CppTemplate.java
index 3eb743b2..613fb640 100644
--- a/src/main/java/org/hivevm/cc/generator/cpp/CppTemplate.java
+++ b/src/main/java/org/hivevm/cc/generator/cpp/CppTemplate.java
@@ -3,109 +3,88 @@
package org.hivevm.cc.generator.cpp;
-import java.io.File;
-import java.io.FileNotFoundException;
-
-import org.hivevm.cc.HiveCC;
import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.DigestWriter;
import org.hivevm.cc.utils.TemplateProvider;
+import java.io.File;
+import java.io.FileNotFoundException;
+
/**
* The {@link CppTemplate} class.
*/
public enum CppTemplate implements TemplateProvider {
- JAVACC("JavaCC", "h"),
-
- LEXER_H("Lexer", "h", "%s"),
- LEXER("Lexer", LEXER_H),
-
- PARSER_H("Parser", "h", "%s"),
- PARSER("Parser", PARSER_H),
+ JAVACC("JavaCC.h"),
- MULTINODE("MultiNode", "cc", "%s"),
- MULTINODE_H("MultiNode", "h"),
- NODE("Node"),
- NODE_H("Node", "h"),
+ LEXER_H("Lexer.h", "%sTokenManager.h"),
+ LEXER("Lexer.cc", "%sTokenManager.cc"),
- PARSEEXCEPTION("ParseException"),
- PARSEEXCEPTION_H("ParseException", "h"),
+ PARSER_H("Parser.h", "%s.h"),
+ PARSER("Parser.cc", "%s.cc"),
- PARSER_CONSTANTS("ParserConstants", "h", "%sConstants"),
+ PARSER_CONSTANTS("ParserConstants.h", "%sConstants.h"),
- PARSERHANDLER("ParserErrorHandler"),
- PARSERHANDLER_H("ParserErrorHandler", "h"),
+ PARSEEXCEPTION("ParseException.cc"),
+ PARSEEXCEPTION_H("ParseException.h"),
+ PARSERHANDLER("ParserErrorHandler.cc"),
+ PARSERHANDLER_H("ParserErrorHandler.h"),
- READER("Reader", "h"),
- STRINGREADER("StringReader"),
- STRINGREADER_H("StringReader", "h"),
+ TOKEN("Token.cc"),
+ TOKEN_H("Token.h"),
+ TOKENMANAGER("TokenManager.h"),
+ TOKENNANAGERERROR("TokenManagerError.cc"),
+ TOKENNANAGERERROR_H("TokenManagerError.h"),
+ TOKENNANAGERHANDLER("TokenManagerErrorHandler.cc"),
+ TOKENNANAGERHANDLER_H("TokenManagerErrorHandler.h"),
- TOKEN("Token"),
- TOKEN_H("Token", "h"),
- TOKENMANAGER("TokenManager", "h"),
- TOKENNANAGERERROR("TokenManagerError"),
- TOKENNANAGERERROR_H("TokenManagerError", "h"),
- TOKENNANAGERHANDLER("TokenManagerErrorHandler"),
- TOKENNANAGERHANDLER_H("TokenManagerErrorHandler", "h"),
+ READER("Reader.h"),
+ STRINGREADER("StringReader.cc"),
+ STRINGREADER_H("StringReader.h"),
+
+ NODE("Node.cc"),
+ NODE_H("Node.h"),
+ MULTINODE("MultiNode.cc", "%s.cc"),
+ MULTINODE_H("MultiNode.h"),
- TREE("Tree", "h"),
- TREESTATE("TreeState"),
- TREESTATE_H("TreeState", "h");
+ TREE("Tree.h"),
+ TREE_ONE("TreeOne.h", "%sTree.h"),
+ TREESTATE("TreeState.cc"),
+ TREESTATE_H("TreeState.h"),
+ TREE_CONSTANTS("TreeConstants.h", "%sTreeConstants.h"),
+ VISITOR("Visitor.h", "%sVisitor.h");
- private final String name;
- private final String type;
- private final String path;
- private final CppTemplate header;
+ private final String name;
+ private final String path;
private CppTemplate(String name) {
- this(name, "cc");
+ this(name, name);
}
- private CppTemplate(String name, String type) {
+ private CppTemplate(String name, String path) {
this.name = name;
- this.type = type;
- this.path = name;
- this.header = null;
- }
-
- private CppTemplate(String name, String type, String path) {
- this.name = name;
- this.type = type;
this.path = path;
- this.header = null;
- }
-
- private CppTemplate(String name, CppTemplate header) {
- this.name = name;
- this.type = "cc";
- this.path = header.path;
- this.header = header;
- }
-
- public CppTemplate getHeader() {
- return this.header;
}
@Override
public String getTemplate() {
- return String.format("cpp/%s.%s", this.name, this.type);
+ return String.format("cpp/%s", this.name);
}
@Override
public String getFilename(String name) {
- return String.format("%s.%s", (name == null) ? this.name : String.format(this.path, name), this.type);
+ return (name == null) ? this.path : String.format(this.path, name);
}
@Override
public final DigestWriter createDigestWriter(DigestOptions options) throws FileNotFoundException {
- return createDigestWriter(getFilename(null), options);
+ return createDigestWriter(options, getFilename(null));
}
@Override
- public final DigestWriter createDigestWriter(String name, DigestOptions options) throws FileNotFoundException {
+ public final DigestWriter createDigestWriter(DigestOptions options, String name) throws FileNotFoundException {
File file = new File(options.getOptions().getOutputDirectory(), getFilename(name));
- return DigestWriter.createCpp(file, HiveCC.VERSION, options);
+ return DigestWriter.create(file, options, true);
}
}
diff --git a/src/main/java/org/hivevm/cc/generator/cpp/CppTreeGenerator.java b/src/main/java/org/hivevm/cc/generator/cpp/CppTreeGenerator.java
index c277cbe3..86dc0cef 100644
--- a/src/main/java/org/hivevm/cc/generator/cpp/CppTreeGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/cpp/CppTreeGenerator.java
@@ -3,52 +3,31 @@
package org.hivevm.cc.generator.cpp;
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Set;
-
import org.hivevm.cc.HiveCC;
-import org.hivevm.cc.generator.JJTreeCodeGenerator;
+import org.hivevm.cc.generator.ASTCodeGenerator;
+import org.hivevm.cc.jjtree.ASTNode;
import org.hivevm.cc.jjtree.ASTNodeDescriptor;
import org.hivevm.cc.jjtree.ASTWriter;
import org.hivevm.cc.jjtree.JJTreeGlobals;
import org.hivevm.cc.jjtree.JJTreeOptions;
import org.hivevm.cc.jjtree.NodeScope;
import org.hivevm.cc.parser.Options;
-import org.hivevm.cc.utils.DigestOptions;
-import org.hivevm.cc.utils.DigestWriter;
-import org.hivevm.cc.utils.Template;
-
-public class CppTreeGenerator extends JJTreeCodeGenerator {
+import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
- @Override
- protected final String getPointer() {
- return "->";
- }
-
- @Override
- protected final String getBoolean() {
- return "bool";
- }
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
- @Override
- protected String getTryFinally() {
- return "";
- }
+public class CppTreeGenerator extends ASTCodeGenerator {
@Override
protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String indent, JJTreeOptions options) {
String type = ns.getNodeDescriptor().getNodeType();
- final String nodeClass;
- if ((options.getNodeClass().length() > 0) && !options.getMulti()) {
- nodeClass = options.getNodeClass();
- } else {
- nodeClass = type;
- }
+ boolean isType = options.getNodeClass().isEmpty() || options.getMulti();
+ String nodeClass = isType ? type : options.getNodeClass();
addType(type);
@@ -64,7 +43,7 @@ protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String inden
}
if (ns.usesCloseNodeVar()) {
- io.println(indent + getBoolean() + " " + ns.closedVar + " = true;");
+ io.println(indent + "bool " + ns.closedVar + " = true;");
}
io.println(indent + ns.getNodeDescriptor().openNode(ns.nodeVar));
if (options.getNodeScopeHook()) {
@@ -72,7 +51,26 @@ protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String inden
}
if (options.getTrackTokens()) {
- io.println(indent + ns.nodeVar + getPointer() + "jjtSetFirstToken(getToken(1));");
+ io.println(indent + ns.nodeVar + "->jjtSetFirstToken(getToken(1));");
+ }
+ }
+
+ @Override
+ protected final void insertCloseNodeCode(NodeScope ns, ASTWriter writer, String indent, boolean isFinal,
+ JJTreeOptions options) {
+ String closeNode = ns.getNodeDescriptor().closeNode(ns.nodeVar);
+ writer.println(indent + closeNode);
+ if (ns.usesCloseNodeVar() && !isFinal) {
+ writer.println(indent + ns.closedVar + " = false;");
+ }
+ if (options.getNodeScopeHook()) {
+ writer.println(indent + "if (jjtree.nodeCreated()) {");
+ writer.println(indent + " jjtreeCloseNodeScope(" + ns.nodeVar + ");");
+ writer.println(indent + "}");
+ }
+
+ if (options.getTrackTokens()) {
+ writer.println(indent + ns.nodeVar + "->jjtSetLastToken(getToken(0));");
}
}
@@ -90,6 +88,25 @@ protected final void insertCatchBlocks(NodeScope ns, ASTWriter io, Enumeration thrown_set = new Hashtable<>();
+ findThrown(ns, thrown_set, expansion_unit);
+ Enumeration thrown_names = thrown_set.elements();
+ insertCatchBlocks(ns, writer, thrown_names, indent);
+
+ writer.println(indent + "} {");
+ if (ns.usesCloseNodeVar()) {
+ writer.println(indent + " if (" + ns.closedVar + ") {");
+ insertCloseNodeCode(ns, writer, indent + " ", true, expansion_unit.jjtOptions());
+ writer.println(indent + " }");
+ }
+ writer.print(indent + "}");
+ writer.closeCodeBlock();
+ }
+
@Override
public final void generateJJTree(JJTreeOptions o) {
generateTreeConstants(o);
@@ -106,75 +123,51 @@ public final void generateJJTree(JJTreeOptions o) {
}
private void generateTreeState(JJTreeOptions o) {
- DigestOptions options = new DigestOptions(o);
+ TemplateOptions options = new TemplateOptions();
options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
- CppTemplate template = CppTemplate.TREESTATE_H;
- try (DigestWriter writer = template.createDigestWriter(options)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.TREESTATE_H;
+ template.render(o, options);
template = CppTemplate.TREESTATE;
- try (DigestWriter writer = template.createDigestWriter(options)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- e.printStackTrace();
- }
+ template.render(o, options);
}
- private static List headersForJJTreeH = new ArrayList<>();
-
-
private void generateNodeHeader(JJTreeOptions o) {
- DigestOptions optionMap = new DigestOptions(o);
+ TemplateOptions optionMap = new TemplateOptions();
optionMap.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_TYPE, CppTreeGenerator.getVisitorReturnType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_DATA_TYPE, CppTreeGenerator.getVisitorArgumentType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_VOID,
Boolean.valueOf(CppTreeGenerator.getVisitorReturnType(o).equals("void")));
- CppTemplate template = CppTemplate.NODE_H;
- try (DigestWriter writer = template.createDigestWriter(optionMap)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.NODE_H;
+ template.render(o, optionMap);
}
private void generateTreeInterface(JJTreeOptions o) {
- String node = "Tree";
- DigestOptions optionMap = new DigestOptions(o);
+ TemplateOptions optionMap = new TemplateOptions();
optionMap.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_TYPE, CppTreeGenerator.getVisitorReturnType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_DATA_TYPE, CppTreeGenerator.getVisitorArgumentType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_VOID,
Boolean.valueOf(CppTreeGenerator.getVisitorReturnType(o).equals("void")));
- optionMap.set(HiveCC.JJTREE_NODE_TYPE, node);
+ optionMap.set(HiveCC.JJTREE_NODE_TYPE, "Tree");
- CppTemplate template = CppTemplate.TREE;
- try (DigestWriter writer = template.createDigestWriter(optionMap)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.TREE;
+ template.render(o, optionMap);
}
private void generateNodeImpl(JJTreeOptions o) {
- DigestOptions optionMap = new DigestOptions(o);
+ TemplateOptions optionMap = new TemplateOptions();
optionMap.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_TYPE, CppTreeGenerator.getVisitorReturnType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_DATA_TYPE, CppTreeGenerator.getVisitorArgumentType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_VOID,
Boolean.valueOf(CppTreeGenerator.getVisitorReturnType(o).equals("void")));
- CppTemplate template = CppTemplate.NODE;
- try (DigestWriter writer = template.createDigestWriter(optionMap)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.NODE;
+ template.render(o, optionMap);
}
private void generateMultiTreeImpl(JJTreeOptions o) {
@@ -184,7 +177,7 @@ private void generateMultiTreeImpl(JJTreeOptions o) {
continue;
}
- DigestOptions optionMap = new DigestOptions(o);
+ TemplateOptions optionMap = new TemplateOptions();
optionMap.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_TYPE, CppTreeGenerator.getVisitorReturnType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_DATA_TYPE, CppTreeGenerator.getVisitorArgumentType(o));
@@ -192,93 +185,36 @@ private void generateMultiTreeImpl(JJTreeOptions o) {
Boolean.valueOf(CppTreeGenerator.getVisitorReturnType(o).equals("void")));
optionMap.set(HiveCC.JJTREE_NODE_TYPE, node);
- CppTemplate template = CppTemplate.MULTINODE;
- try (DigestWriter writer = template.createDigestWriter(optionMap)) {
- Template.of(template, writer.options()).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.MULTINODE;
+ template.render(o, optionMap);
}
}
private void generateOneTreeInterface(JJTreeOptions o) {
- DigestOptions optionMap = new DigestOptions(o);
+ TemplateOptions optionMap = new TemplateOptions();
optionMap.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_TYPE, CppTreeGenerator.getVisitorReturnType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_DATA_TYPE, CppTreeGenerator.getVisitorArgumentType(o));
optionMap.set(HiveCC.JJTREE_VISITOR_RETURN_VOID,
Boolean.valueOf(CppTreeGenerator.getVisitorReturnType(o).equals("void")));
+ optionMap.set("NODES", nodesToGenerate());
- File file = new File(o.getOutputDirectory(), JJTreeGlobals.parserName + "Tree.h");
- try (DigestWriter writer = DigestWriter.createCpp(file, HiveCC.VERSION, optionMap)) {
- // PrintWriter ostr = outputFile.getPrintWriter();
- file.getName().replace('.', '_').toUpperCase();
- writer.println("#ifndef JAVACC_ONE_TREE_H");
- writer.println("#define JAVACC_ONE_TREE_H");
- writer.println();
- writer.println("#include \"Node.h\"");
- for (String s : nodesToGenerate()) {
- writer.println("#include \"" + s + ".h\"");
- }
- writer.println("#endif");
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider template = CppTemplate.TREE_ONE;
+ template.render(o, optionMap, JJTreeGlobals.parserName);
}
private void generateTreeConstants(JJTreeOptions o) {
- String name = JJTreeGlobals.parserName + "TreeConstants";
- File file = new File(o.getOutputDirectory(), name + ".h");
- CppTreeGenerator.headersForJJTreeH.add(file.getName());
-
- try (DigestWriter ostr = DigestWriter.createCpp(file, HiveCC.VERSION, new DigestOptions(o))) {
- List nodeIds = ASTNodeDescriptor.getNodeIds();
- List nodeNames = ASTNodeDescriptor.getNodeNames();
-
- ostr.println("#ifndef JAVACC_" + file.getName().replace('.', '_').toUpperCase());
- ostr.println("#define JAVACC_" + file.getName().replace('.', '_').toUpperCase());
-
- ostr.println("\n#include \"JavaCC.h\"");
- boolean hasNamespace = ((String) ostr.options().get(HiveCC.JJPARSER_CPP_NAMESPACE)).length() > 0;
- if (hasNamespace) {
- ostr.println("namespace " + ostr.options().get(HiveCC.JJPARSER_CPP_NAMESPACE) + " {");
- }
- ostr.println("enum {");
- for (int i = 0; i < nodeIds.size(); ++i) {
- String n = nodeIds.get(i);
- ostr.println(" " + n + " = " + i + ",");
- }
-
- ostr.println("};");
- ostr.println();
-
- for (int i = 0; i < nodeNames.size(); ++i) {
- ostr.print("static JJChar jjtNodeName_arr_" + i + "[] = ");
- String n = nodeNames.get(i);
- ostr.print("{");
- CppFileGenerator.printCharArray(ostr, n);
- ostr.println("0};");
- }
- ostr.println("static JJString jjtNodeName[] = {");
- for (int i = 0; i < nodeNames.size(); i++) {
- ostr.println(" jjtNodeName_arr_" + i + ",");
- }
- ostr.println("};");
-
- if (hasNamespace) {
- ostr.println("}");
- }
-
-
- ostr.println("#endif");
- } catch (IOException e) {
- throw new Error(e.toString());
- }
- }
-
- private static String getVisitMethodName(String className) {
- return "visit";
+ TemplateOptions options = new TemplateOptions();
+ options.add("NODES", ASTNodeDescriptor.getNodeIds().size()).set("ordinal", i -> i).set("label",
+ i -> ASTNodeDescriptor.getNodeIds().get(i));
+ options.add("NODE_NAMES", ASTNodeDescriptor.getNodeNames().size()).set("ordinal", i -> i)
+ .set("label", i -> ASTNodeDescriptor.getNodeNames().get(i))
+ .set("chars", i -> CppFileGenerator.toCharArray(ASTNodeDescriptor.getNodeNames().get(i)));
+ options.set("NAME_UPPER", JJTreeGlobals.parserName.toUpperCase());
+
+ TemplateProvider template = CppTemplate.TREE_CONSTANTS;
+ template.render(o, options, JJTreeGlobals.parserName);
}
private static String getVisitorArgumentType(Options o) {
@@ -296,90 +232,26 @@ private void generateVisitors(JJTreeOptions o) {
return;
}
- File file = new File(o.getOutputDirectory(), JJTreeGlobals.parserName + "Visitor.h");
- try (DigestWriter ostr = DigestWriter.createCpp(file, HiveCC.VERSION, new DigestOptions(o))) {
- ostr.println("#ifndef " + file.getName().replace('.', '_').toUpperCase());
- ostr.println("#define " + file.getName().replace('.', '_').toUpperCase());
- ostr.println("\n#include \"JavaCC.h\"");
- ostr.println("#include \"" + JJTreeGlobals.parserName + "Tree.h" + "\"");
+ List nodeNames =
+ ASTNodeDescriptor.getNodeNames().stream().filter(n -> !n.equals("void")).collect(Collectors.toList());
- boolean hasNamespace = ((String) ostr.options().get(HiveCC.JJPARSER_CPP_NAMESPACE)).length() > 0;
- if (hasNamespace) {
- ostr.println("namespace " + ostr.options().get(HiveCC.JJPARSER_CPP_NAMESPACE) + " {");
- }
-
- generateVisitorInterface(ostr, o);
- generateDefaultVisitor(ostr, o);
-
- if (hasNamespace) {
- ostr.println("}");
- }
-
- ostr.println("#endif");
- } catch (IOException ioe) {
- throw new Error(ioe.toString());
- }
- }
-
- private void generateVisitorInterface(PrintWriter ostr, JJTreeOptions o) {
- String name = JJTreeGlobals.parserName + "Visitor";
- List nodeNames = ASTNodeDescriptor.getNodeNames();
-
- ostr.println("class " + name);
- ostr.println("{");
+ TemplateOptions options = new TemplateOptions();
+ options.add("NODES", nodeNames).set("type", n -> o.getNodePrefix() + n);
String argumentType = CppTreeGenerator.getVisitorArgumentType(o);
String returnType = CppTreeGenerator.getVisitorReturnType(o);
if (!o.getVisitorDataType().equals("")) {
argumentType = o.getVisitorDataType();
}
- ostr.println(" public:");
-
- ostr.println(" virtual " + returnType + " visit(const Node *node, " + argumentType + " data) = 0;");
- if (o.getMulti()) {
- for (String n : nodeNames) {
- if (n.equals("void")) {
- continue;
- }
- String nodeType = o.getNodePrefix() + n;
- ostr.println(" virtual " + returnType + " " + CppTreeGenerator.getVisitMethodName(nodeType) + "(const "
- + nodeType + " *node, " + argumentType + " data) = 0;");
- }
- }
-
- ostr.println(" virtual ~" + name + "() { }");
- ostr.println("};");
- }
-
- private void generateDefaultVisitor(PrintWriter ostr, JJTreeOptions o) {
- String className = JJTreeGlobals.parserName + "DefaultVisitor";
- List nodeNames = ASTNodeDescriptor.getNodeNames();
-
- ostr.println("class " + className + " : public " + JJTreeGlobals.parserName + "Visitor {");
- String argumentType = CppTreeGenerator.getVisitorArgumentType(o);
- String ret = CppTreeGenerator.getVisitorReturnType(o);
-
- ostr.println("public:");
- ostr.println(" virtual " + ret + " defaultVisit(const Node *node, " + argumentType + " data) = 0;");
-
- ostr.println(" virtual " + ret + " visit(const Node *node, " + argumentType + " data) {");
- ostr.println(" " + (ret.trim().equals("void") ? "" : "return ") + "defaultVisit(node, data);");
- ostr.println("}");
-
- if (o.getMulti()) {
- for (String n : nodeNames) {
- if (n.equals("void")) {
- continue;
- }
- String nodeType = o.getNodePrefix() + n;
- ostr.println(" virtual " + ret + " " + CppTreeGenerator.getVisitMethodName(nodeType) + "(const " + nodeType
- + " *node, " + argumentType + " data) {");
- ostr.println(" " + (ret.trim().equals("void") ? "" : "return ") + "defaultVisit(node, data);");
- ostr.println(" }");
- }
- }
- ostr.println(" ~" + className + "() { }");
- ostr.println("};");
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set("NAME_UPPER", JJTreeGlobals.parserName.toUpperCase());
+ options.set("ARGUMENT_TYPE", argumentType);
+ options.set("RETURN_TYPE", returnType);
+ options.set("RETURN", returnType.equals("void") ? "" : "return ");
+ options.set("IS_MULTI", o.getMulti());
+
+ TemplateProvider template = CppTemplate.VISITOR;
+ template.render(o, options, JJTreeGlobals.parserName);
}
}
diff --git a/src/main/java/org/hivevm/cc/generator/java/JavaFileGenerator.java b/src/main/java/org/hivevm/cc/generator/java/JavaFileGenerator.java
index 8c917318..0da65816 100644
--- a/src/main/java/org/hivevm/cc/generator/java/JavaFileGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/java/JavaFileGenerator.java
@@ -3,11 +3,7 @@
package org.hivevm.cc.generator.java;
-import java.util.ArrayList;
-import java.util.List;
-
import org.hivevm.cc.JavaCCRequest;
-import org.hivevm.cc.generator.AbstractFileGenerator;
import org.hivevm.cc.generator.FileGenerator;
import org.hivevm.cc.generator.LexerData;
import org.hivevm.cc.parser.JavaCCErrors;
@@ -16,14 +12,17 @@
import org.hivevm.cc.parser.RegExprSpec;
import org.hivevm.cc.parser.RegularExpression;
import org.hivevm.cc.parser.TokenProduction;
-import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.Encoding;
import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
+
+import java.util.ArrayList;
+import java.util.List;
/**
* Generates the Constants file.
*/
-public class JavaFileGenerator extends AbstractFileGenerator implements FileGenerator {
+public class JavaFileGenerator implements FileGenerator {
@Override
public final void handleRequest(JavaCCRequest request, LexerData context) throws ParseException {
@@ -57,15 +56,15 @@ public final void handleRequest(JavaCCRequest request, LexerData context) throws
return buffer.toString();
});
- generateFile(JavaTemplate.TOKEN, new DigestOptions(context.options()));
- generateFile(JavaTemplate.TOKEN_EXCEPTION, new DigestOptions(context.options()));
+ TemplateProvider.render(JavaTemplate.TOKEN, context.options());
+ TemplateProvider.render(JavaTemplate.TOKEN_EXCEPTION, context.options());
- generateFile(JavaTemplate.PROVIDER, new DigestOptions(context.options()));
- generateFile(JavaTemplate.STRING_PROVIDER, new DigestOptions(context.options()));
- generateFile(JavaTemplate.STREAM_PROVIDER, new DigestOptions(context.options()));
- generateFile(JavaTemplate.CHAR_STREAM, new DigestOptions(context.options()));
+ TemplateProvider.render(JavaTemplate.PROVIDER, context.options());
+ TemplateProvider.render(JavaTemplate.STRING_PROVIDER, context.options());
+ TemplateProvider.render(JavaTemplate.STREAM_PROVIDER, context.options());
+ TemplateProvider.render(JavaTemplate.CHAR_STREAM, context.options());
- generateFile(JavaTemplate.PARSER_EXCEPTION, new DigestOptions(context.options()));
- generateFile(JavaTemplate.PARSER_CONSTANTS, request.getParserName(), new DigestOptions(context.options(), options));
+ TemplateProvider.render(JavaTemplate.PARSER_EXCEPTION, context.options());
+ TemplateProvider.render(JavaTemplate.PARSER_CONSTANTS, context.options(), options, request.getParserName());
}
}
diff --git a/src/main/java/org/hivevm/cc/generator/java/JavaLexerGenerator.java b/src/main/java/org/hivevm/cc/generator/java/JavaLexerGenerator.java
index db9e3d73..5313f196 100644
--- a/src/main/java/org/hivevm/cc/generator/java/JavaLexerGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/java/JavaLexerGenerator.java
@@ -3,13 +3,7 @@
package org.hivevm.cc.generator.java;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Locale;
-
+import org.hivevm.cc.generator.Lexer;
import org.hivevm.cc.generator.LexerData;
import org.hivevm.cc.generator.LexerGenerator;
import org.hivevm.cc.generator.LexerStateData;
@@ -18,10 +12,16 @@
import org.hivevm.cc.parser.JavaCCErrors;
import org.hivevm.cc.parser.RStringLiteral.KindInfo;
import org.hivevm.cc.parser.Token;
-import org.hivevm.cc.source.SourceWriter;
-import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.Encoding;
import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Locale;
/**
* Generate lexer.
@@ -31,10 +31,10 @@ public class JavaLexerGenerator extends LexerGenerator {
@Override
protected final void dumpAll(LexerData data) throws IOException {
TemplateOptions options = new TemplateOptions();
- options.add("LOHI_BYTES", data.lohiByte.keySet()).set("bytes", i -> JavaLexerGenerator.getLohiBytes(data, i));
- options.add("STATES", data.stateNames).set("NfaAndDfa", (n, w) -> dumpNfaAndDfa(data.getStateData(n), w));
- options.add("NON_ASCII_TABLE", data.nonAsciiTableForMethod).set("AsciiMove",
- (s, w) -> DumpNonAsciiMoveMethod(s, data, w));
+ options.add(Lexer.LOHI_BYTES, data.lohiByte.keySet()).set("bytes", i -> getLohiBytes(data, i));
+ options.add(Lexer.STATES, data.stateNames).set("NfaAndDfa", (n, w) -> dumpNfaAndDfa(data.getStateData(n), w));
+ options.add(Lexer.NON_ASCII_TABLE, data.nonAsciiTableForMethod).set("NON_ASCII_METHOD", s -> s.nonAsciiMethod)
+ .set("ASCII_MOVE", (s, w) -> DumpNonAsciiMoveMethod(s, data, w));
options.set("LITERAL_IMAGES", () -> JavaLexerGenerator.getStrLiteralImages(data));
options.set("STATES_FOR_STATE", () -> getStatesForState(data));
@@ -66,18 +66,12 @@ protected final void dumpAll(LexerData data) throws IOException {
options.set("jjCheckNAddStatesDualNeeded", data.jjCheckNAddStatesDualNeeded);
options.set("jjCheckNAddStatesUnaryNeeded", data.jjCheckNAddStatesUnaryNeeded);
- try (SourceWriter writer = new SourceWriter(data.getParserName() + "TokenManager", JavaTemplate.LEXER,
- new DigestOptions(data.options(), options))) {
- dumpClassHeader(writer, data);
- writer.writeTemplate();
+ if ((data.options().getJavaLexer() != null) && !data.options().getJavaLexer().isEmpty()) {
+ options.set("LEXER", data.options().getJavaLexer());
}
- }
-
- private static String getLohiBytes(LexerData data, int i) {
- return "0x" + Long.toHexString(data.lohiByte.get(i)[0]) + "L, " + "0x" + Long.toHexString(data.lohiByte.get(i)[1])
- + "L, " + "0x" + Long.toHexString(data.lohiByte.get(i)[2]) + "L, " + "0x"
- + Long.toHexString(data.lohiByte.get(i)[3]) + "L";
+ TemplateProvider provider = JavaTemplate.LEXER;
+ provider.render(data.options(), options, data.getParserName());
}
private static void DumpStaticVarDeclarations(PrintWriter writer, LexerData data) {
@@ -249,11 +243,11 @@ private void DumpGetNextToken(PrintWriter writer, LexerData data) {
+ Long.toHexString(data.singlesToSkip[i].asciiMoves[1]) + "L & (1L << (curChar & 077))) != 0L)");
} else if (data.singlesToSkip[i].asciiMoves[1] == 0L) {
writer.println(
- prefix + " while (curChar <= " + (int) JavaLexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[0])
+ prefix + " while (curChar <= " + (int) LexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[0])
+ " && (0x" + Long.toHexString(data.singlesToSkip[i].asciiMoves[0]) + "L & (1L << curChar)) != 0L)");
} else if (data.singlesToSkip[i].asciiMoves[0] == 0L) {
writer.println(prefix + " while (curChar > 63 && curChar <= "
- + (JavaLexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[1]) + 64) + " && (0x"
+ + (LexerGenerator.MaxChar(data.singlesToSkip[i].asciiMoves[1]) + 64) + " && (0x"
+ Long.toHexString(data.singlesToSkip[i].asciiMoves[1]) + "L & (1L << (curChar & 077))) != 0L)");
}
@@ -495,7 +489,6 @@ private void DumpGetNextToken(PrintWriter writer, LexerData data) {
}
private void DumpSkipActions(PrintWriter writer, LexerData data) {
- Action act;
Outer:
for (int i = 0; i < data.maxOrdinal; i++) {
@@ -504,7 +497,8 @@ private void DumpSkipActions(PrintWriter writer, LexerData data) {
}
for (;;) {
- if ((((act = data.actions[i]) == null) || act.getActionTokens().isEmpty()) && !data.canLoop[data.getState(i)]) {
+ Action act = data.actions[i];
+ if (((act == null) || act.getActionTokens().isEmpty()) && !data.canLoop[data.getState(i)]) {
continue Outer;
}
@@ -814,60 +808,29 @@ protected final void DumpHeadForCase(PrintWriter writer, int byteNum) {
}
private void DumpNonAsciiMoveMethod(NfaState state, LexerData data, PrintWriter writer) {
- int j;
- writer.println("private static final boolean jjCanMove_" + state.nonAsciiMethod
- + "(int hiByte, int i1, int i2, long l1, long l2)");
- writer.println("{");
- writer.println(" switch(hiByte)");
- writer.println(" {");
-
- if ((state.loByteVec != null) && (state.loByteVec.size() > 0)) {
- for (j = 0; j < state.loByteVec.size(); j += 2) {
- writer.println(" case " + state.loByteVec.get(j).intValue() + ":");
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.loByteVec.get(j + 1).intValue()))) {
- writer.println(
- " return ((jjbitVec" + state.loByteVec.get(j + 1).intValue() + "[i2" + "] & l2) != 0L);");
- } else {
- writer.println(" return true;");
- }
+ for (int j = 0; j < state.loByteVec.size(); j += 2) {
+ writer.println(" case " + state.loByteVec.get(j).intValue() + ":");
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.loByteVec.get(j + 1).intValue()))) {
+ writer
+ .println(" return ((jjbitVec" + state.loByteVec.get(j + 1).intValue() + "[i2" + "] & l2) != 0L);");
+ } else {
+ writer.println(" return true;");
}
}
writer.println(" default :");
- if ((state.nonAsciiMoveIndices != null) && ((j = state.nonAsciiMoveIndices.length) > 0)) {
- do {
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 2]))) {
- writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 2] + "[i1] & l1) != 0L)");
- }
- if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 1]))) {
- writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 1] + "[i2] & l2) == 0L)");
- writer.println(" return false;");
- writer.println(" else");
- }
- writer.println(" return true;");
- } while ((j -= 2) > 0);
- }
-
- writer.println(" return false;");
- writer.println(" }");
- writer.println("}");
- }
-
- private final void dumpClassHeader(PrintWriter writer, LexerData data) {
- writer.print("package " + data.options().getJavaPackage());
- writer.println(";");
-
- writer.println("");
- writer.println("/** Token Manager. */");
-
- writer.print("class " + data.getParserName() + "TokenManager");
- if ((data.options().getJavaLexer() != null) && !data.options().getJavaLexer().isEmpty()) {
- writer.print(" extends " + data.options().getJavaLexer());
+ for (int j = state.nonAsciiMoveIndices.length; j > 0; j -= 2) {
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 2]))) {
+ writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 2] + "[i1] & l1) != 0L)");
+ }
+ if (!NfaState.AllBitsSet(data.allBitVectors.get(state.nonAsciiMoveIndices[j - 1]))) {
+ writer.println(" if ((jjbitVec" + state.nonAsciiMoveIndices[j - 1] + "[i2] & l2) == 0L)");
+ writer.println(" return false;");
+ writer.println(" else");
+ }
+ writer.println(" return true;");
}
- writer.print(" implements " + data.getParserName() + "Constants");
-
- writer.println(" {");
}
private String getStatesForState(LexerData data) {
@@ -1713,15 +1676,4 @@ private final void DumpMoveNfa(PrintWriter writer, LexerStateData data) {
}
writer.println("}");
}
-
- // Assumes l != 0L
- private static char MaxChar(long l) {
- for (int i = 64; i-- > 0;) {
- if ((l & (1L << i)) != 0L) {
- return (char) i;
- }
- }
-
- return 0xffff;
- }
}
diff --git a/src/main/java/org/hivevm/cc/generator/java/JavaParserGenerator.java b/src/main/java/org/hivevm/cc/generator/java/JavaParserGenerator.java
index 30bde0ad..05e8fd72 100644
--- a/src/main/java/org/hivevm/cc/generator/java/JavaParserGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/java/JavaParserGenerator.java
@@ -3,14 +3,6 @@
package org.hivevm.cc.generator.java;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.stream.Collectors;
-
import org.hivevm.cc.generator.ParserData;
import org.hivevm.cc.generator.ParserGenerator;
import org.hivevm.cc.parser.Action;
@@ -28,10 +20,17 @@
import org.hivevm.cc.parser.ZeroOrMore;
import org.hivevm.cc.parser.ZeroOrOne;
import org.hivevm.cc.semantic.Semanticize;
-import org.hivevm.cc.source.SourceWriter;
-import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.Encoding;
import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
/**
* Implements the {@link ParserGenerator} for the JAVA language.
@@ -64,10 +63,8 @@ protected void generate(ParserData data) throws IOException {
i -> data.maskVals().stream().map(v -> "0x" + Integer.toHexString(v[i])).collect(Collectors.joining(", ")))
.set("la1", i -> (i == 0) ? "" : (32 * i) + " + ");
- try (SourceWriter writer =
- new SourceWriter(data.getParserName(), JavaTemplate.PARSER, new DigestOptions(data.options(), options))) {
- writer.writeTemplate();
- }
+ TemplateProvider provider = JavaTemplate.PARSER;
+ provider.render(data.options(), options, data.getParserName());
}
/**
diff --git a/src/main/java/org/hivevm/cc/generator/java/JavaTemplate.java b/src/main/java/org/hivevm/cc/generator/java/JavaTemplate.java
index be5a117a..0b6cb3d7 100644
--- a/src/main/java/org/hivevm/cc/generator/java/JavaTemplate.java
+++ b/src/main/java/org/hivevm/cc/generator/java/JavaTemplate.java
@@ -3,21 +3,21 @@
package org.hivevm.cc.generator.java;
-import java.io.File;
-import java.io.FileNotFoundException;
-
-import org.hivevm.cc.HiveCC;
+import org.hivevm.cc.parser.Options;
import org.hivevm.cc.utils.DigestOptions;
import org.hivevm.cc.utils.DigestWriter;
import org.hivevm.cc.utils.TemplateProvider;
+import java.io.File;
+import java.io.FileNotFoundException;
+
/**
* The {@link JavaTemplate} class.
*/
public enum JavaTemplate implements TemplateProvider {
- LEXER("Lexer", "%s"),
+ LEXER("Lexer", "%sTokenManager"),
PARSER("Parser", "%s"),
PROVIDER("Provider"),
@@ -25,15 +25,20 @@ public enum JavaTemplate implements TemplateProvider {
STRING_PROVIDER("StringProvider"),
CHAR_STREAM("JavaCharStream"),
- MULTI_NODE("MultiNode"),
+ MULTI_NODE("MultiNode", "%s"),
NODE("Node"),
PARSER_EXCEPTION("ParseException"),
PARSER_CONSTANTS("ParserConstants", "%sConstants"),
TOKEN("Token"),
TOKEN_EXCEPTION("TokenException"),
+
TREE("Tree"),
- TREE_STATE("TreeState");
+ TREE_STATE("TreeState", "JJT%sState"),
+ TREE_CONSTANTS("TreeConstants", "%sTreeConstants"),
+
+ VISITOR("Visitor", "%sVisitor"),
+ DEFAULT_VISITOR("DefaultVisitor", "%sDefaultVisitor");
private final String name;
private final String path;
@@ -60,12 +65,25 @@ public String getFilename(String name) {
@Override
public final DigestWriter createDigestWriter(DigestOptions options) throws FileNotFoundException {
- return createDigestWriter(null, options);
+ return createDigestWriter(options, null);
}
@Override
- public final DigestWriter createDigestWriter(String name, DigestOptions options) throws FileNotFoundException {
- File file = new File(options.getOptions().getOutputDirectory(), getFilename(name));
- return DigestWriter.create(file, HiveCC.VERSION, options);
+ public final DigestWriter createDigestWriter(DigestOptions options, String name) throws FileNotFoundException {
+ File file = getFile(getFilename(name), options.getOptions());
+ return DigestWriter.create(file, options, false);
+ }
+
+ /**
+ * Get the Java file to generate.
+ *
+ * @param filename
+ * @param options
+ */
+ public static File getFile(String filename, Options options) {
+ String packagePath = options.getJavaPackage().replace('.', File.separatorChar);
+ File outputDir = new File(options.getOutputDirectory(), packagePath);
+ outputDir.mkdirs();
+ return new File(outputDir, filename);
}
}
diff --git a/src/main/java/org/hivevm/cc/generator/java/JavaTreeGenerator.java b/src/main/java/org/hivevm/cc/generator/java/JavaTreeGenerator.java
index a80c2026..fd09e3f6 100644
--- a/src/main/java/org/hivevm/cc/generator/java/JavaTreeGenerator.java
+++ b/src/main/java/org/hivevm/cc/generator/java/JavaTreeGenerator.java
@@ -3,50 +3,30 @@
package org.hivevm.cc.generator.java;
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Set;
-
import org.hivevm.cc.HiveCC;
-import org.hivevm.cc.generator.JJTreeCodeGenerator;
+import org.hivevm.cc.generator.ASTCodeGenerator;
+import org.hivevm.cc.jjtree.ASTNode;
import org.hivevm.cc.jjtree.ASTNodeDescriptor;
import org.hivevm.cc.jjtree.ASTWriter;
import org.hivevm.cc.jjtree.JJTreeGlobals;
import org.hivevm.cc.jjtree.JJTreeOptions;
import org.hivevm.cc.jjtree.NodeScope;
-import org.hivevm.cc.utils.DigestOptions;
-import org.hivevm.cc.utils.DigestWriter;
-import org.hivevm.cc.utils.Template;
+import org.hivevm.cc.utils.TemplateOptions;
+import org.hivevm.cc.utils.TemplateProvider;
-public class JavaTreeGenerator extends JJTreeCodeGenerator {
-
- @Override
- protected String getPointer() {
- return ".";
- }
-
- @Override
- protected String getBoolean() {
- return "boolean";
- }
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
- @Override
- protected final String getTryFinally() {
- return "finally ";
- }
+public class JavaTreeGenerator extends ASTCodeGenerator {
@Override
protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String indent, JJTreeOptions options) {
String type = ns.getNodeDescriptor().getNodeType();
- final String nodeClass;
- if ((options.getNodeClass().length() > 0) && !options.getMulti()) {
- nodeClass = options.getNodeClass();
- } else {
- nodeClass = type;
- }
+ boolean isType = options.getNodeClass().isEmpty() || options.getMulti();
+ String nodeClass = isType ? type : options.getNodeClass();
addType(type);
@@ -62,7 +42,7 @@ protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String inden
}
if (ns.usesCloseNodeVar()) {
- io.println(indent + getBoolean() + " " + ns.closedVar + " = true;");
+ io.println(indent + "boolean " + ns.closedVar + " = true;");
}
io.println(indent + ns.getNodeDescriptor().openNode(ns.nodeVar));
if (options.getNodeScopeHook()) {
@@ -70,7 +50,26 @@ protected final void insertOpenNodeCode(NodeScope ns, ASTWriter io, String inden
}
if (options.getTrackTokens()) {
- io.println(indent + ns.nodeVar + getPointer() + "jjtSetFirstToken(getToken(1));");
+ io.println(indent + ns.nodeVar + ".jjtSetFirstToken(getToken(1));");
+ }
+ }
+
+ @Override
+ protected final void insertCloseNodeCode(NodeScope ns, ASTWriter writer, String indent, boolean isFinal,
+ JJTreeOptions options) {
+ String closeNode = ns.getNodeDescriptor().closeNode(ns.nodeVar);
+ writer.println(indent + closeNode);
+ if (ns.usesCloseNodeVar() && !isFinal) {
+ writer.println(indent + ns.closedVar + " = false;");
+ }
+ if (options.getNodeScopeHook()) {
+ writer.println(indent + "if (jjtree.nodeCreated()) {");
+ writer.println(indent + " jjtreeCloseNodeScope(" + ns.nodeVar + ");");
+ writer.println(indent + "}");
+ }
+
+ if (options.getTrackTokens()) {
+ writer.println(indent + ns.nodeVar + ".jjtSetLastToken(getToken(0));");
}
}
@@ -101,12 +100,31 @@ protected final void insertCatchBlocks(NodeScope ns, ASTWriter io, Enumeration thrown_set = new Hashtable<>();
+ findThrown(ns, thrown_set, expansion_unit);
+ Enumeration thrown_names = thrown_set.elements();
+ insertCatchBlocks(ns, writer, thrown_names, indent);
+
+ writer.println(indent + "} finally {");
+ if (ns.usesCloseNodeVar()) {
+ writer.println(indent + " if (" + ns.closedVar + ") {");
+ insertCloseNodeCode(ns, writer, indent + " ", true, expansion_unit.jjtOptions());
+ writer.println(indent + " }");
+ }
+ writer.print(indent + "}");
+ writer.closeCodeBlock();
+ }
+
@Override
public final void generateJJTree(JJTreeOptions o) {
generateTreeConstants(o);
generateVisitors(o);
- generateTreeState(o);
generateDefaultVisitors(o);
+ generateTreeState(o);
// TreeClasses
generateNodeClass(o);
@@ -114,56 +132,17 @@ public final void generateJJTree(JJTreeOptions o) {
generateTreeClasses(o);
}
- private void generateTreeState(JJTreeOptions o) {
- DigestOptions options = new DigestOptions(o);
- options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
- options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
-
- String filePrefix = new File(o.getOutputDirectory(), "JJT" + JJTreeGlobals.parserName + "State").getAbsolutePath();
-
- File file = new File(filePrefix + ".java");
- try (DigestWriter ostr = DigestWriter.create(file, HiveCC.VERSION, options)) {
- Template.of(JavaTemplate.TREE_STATE, options).render(ostr);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
- }
-
- private static void generatePrologue(PrintWriter ostr, JJTreeOptions o) {
- ostr.println("package " + o.getJavaPackage() + ";");
- ostr.println();
- }
-
private void generateTreeConstants(JJTreeOptions o) {
- String name = JJTreeGlobals.parserName + "TreeConstants";
- File file = new File(o.getOutputDirectory(), name + ".java");
-
- try (PrintWriter ostr = DigestWriter.create(file, HiveCC.VERSION, new DigestOptions(o))) {
- List nodeIds = ASTNodeDescriptor.getNodeIds();
- List nodeNames = ASTNodeDescriptor.getNodeNames();
+ TemplateOptions options = new TemplateOptions();
+ options.add("NODE_NAMES", ASTNodeDescriptor.getNodeNames());
+ options.add("NODES", ASTNodeDescriptor.getNodeIds().size()).set("ordinal", i -> i).set("label",
+ i -> ASTNodeDescriptor.getNodeIds().get(i));
- JavaTreeGenerator.generatePrologue(ostr, o);
- ostr.println("public interface " + name);
- ostr.println("{");
-
- for (int i = 0; i < nodeIds.size(); ++i) {
- String n = nodeIds.get(i);
- ostr.println(" public final int " + n + " = " + i + ";");
- }
-
- ostr.println();
- ostr.println();
-
- ostr.println(" public static String[] jjtNodeName = {");
- for (String n : nodeNames) {
- ostr.println(" \"" + n + "\",");
- }
- ostr.println(" };");
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
- ostr.println("}");
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider provider = JavaTemplate.TREE_CONSTANTS;
+ provider.render(o, options, JJTreeGlobals.parserName);
}
private void generateVisitors(JJTreeOptions o) {
@@ -171,38 +150,19 @@ private void generateVisitors(JJTreeOptions o) {
return;
}
- String name = JJTreeGlobals.parserName + "Visitor";
- File file = new File(o.getOutputDirectory(), name + ".java");
-
- try (PrintWriter ostr = DigestWriter.create(file, HiveCC.VERSION, new DigestOptions(o))) {
- List nodeNames = ASTNodeDescriptor.getNodeNames();
-
- JavaTreeGenerator.generatePrologue(ostr, o);
- ostr.println("public interface " + name);
- ostr.println("{");
-
- String ve = JavaTreeGenerator.mergeVisitorException(o);
-
- String argumentType = "Object";
- if (!o.getVisitorDataType().equals("")) {
- argumentType = o.getVisitorDataType();
- }
-
- ostr.println(" public " + o.getVisitorReturnType() + " visit(Node node, " + argumentType + " data)" + ve + ";");
- if (o.getMulti()) {
- for (String n : nodeNames) {
- if (n.equals("void")) {
- continue;
- }
- String nodeType = o.getNodePrefix() + n;
- ostr.println(" public " + o.getVisitorReturnType() + " visit(" + nodeType + " node, " + argumentType
- + " data)" + ve + ";");
- }
- }
- ostr.println("}");
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ Stream nodes = ASTNodeDescriptor.getNodeNames().stream().filter(n -> !n.equals("void"));
+ TemplateOptions options = new TemplateOptions();
+ options.add("NODES", nodes.collect(Collectors.toList()));
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
+ options.set("RETURN_TYPE", o.getVisitorReturnType());
+ options.set("ARGUMENT_TYPE", o.getVisitorDataType().equals("") ? "Object" : o.getVisitorDataType());
+ options.set("EXCEPTION", JavaTreeGenerator.mergeVisitorException(o));
+ options.set("IS_MULTI", o.getMulti());
+ options.set("NODE_PREFIX", o.getNodePrefix());
+
+ TemplateProvider provider = JavaTemplate.VISITOR;
+ provider.render(o, options, JJTreeGlobals.parserName);
}
private void generateDefaultVisitors(JJTreeOptions o) {
@@ -210,132 +170,105 @@ private void generateDefaultVisitors(JJTreeOptions o) {
return;
}
- String className = JJTreeGlobals.parserName + "DefaultVisitor";
- File file = new File(o.getOutputDirectory(), className + ".java");
-
- try (PrintWriter ostr = DigestWriter.create(file, HiveCC.VERSION, new DigestOptions(o))) {
- List nodeNames = ASTNodeDescriptor.getNodeNames();
-
- JavaTreeGenerator.generatePrologue(ostr, o);
- ostr.println("public class " + className + " implements " + JJTreeGlobals.parserName + "Visitor{");
+ String argumentType = o.getVisitorDataType().equals("") ? "Object" : o.getVisitorDataType().trim();
+ String returnValue = returnValue(o.getVisitorReturnType(), argumentType);
+ boolean isVoidReturnType = "void".equals(o.getVisitorReturnType());
- final String ve = JavaTreeGenerator.mergeVisitorException(o);
-
- String argumentType = "Object";
- if (!o.getVisitorDataType().equals("")) {
- argumentType = o.getVisitorDataType().trim();
- }
+ Stream nodes = ASTNodeDescriptor.getNodeNames().stream().filter(n -> !n.equals("void"));
+ TemplateOptions options = new TemplateOptions();
+ options.add("NODES", nodes.collect(Collectors.toList()));
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
+ options.set("RETURN_TYPE", o.getVisitorReturnType());
+ options.set("RETURN_VALUE", returnValue);
+ options.set("RETURN", isVoidReturnType ? "" : "return ");
+ options.set("ARGUMENT_TYPE", argumentType);
+ options.set("EXCEPTION", JavaTreeGenerator.mergeVisitorException(o));
+ options.set("IS_MULTI", o.getMulti());
+ options.set("NODE_PREFIX", o.getNodePrefix());
+
+ TemplateProvider provider = JavaTemplate.DEFAULT_VISITOR;
+ provider.render(o, options, JJTreeGlobals.parserName);
+ }
- final String returnType = o.getVisitorReturnType().trim();
- final boolean isVoidReturnType = "void".equals(returnType);
-
- ostr.println(" public " + returnType + " defaultVisit(Node node, " + argumentType + " data)" + ve + "{");
- ostr.println(" node.childrenAccept(this, data);");
- ostr.print(" return");
- if (!isVoidReturnType) {
- if (returnType.equals(argumentType)) {
- ostr.print(" data");
- } else if ("boolean".equals(returnType)) {
- ostr.print(" false");
- } else if ("int".equals(returnType)) {
- ostr.print(" 0");
- } else if ("long".equals(returnType)) {
- ostr.print(" 0L");
- } else if ("double".equals(returnType)) {
- ostr.print(" 0.0d");
- } else if ("float".equals(returnType)) {
- ostr.print(" 0.0f");
- } else if ("short".equals(returnType)) {
- ostr.print(" 0");
- } else if ("byte".equals(returnType)) {
- ostr.print(" 0");
- } else if ("char".equals(returnType)) {
- ostr.print(" '\u0000'");
- } else {
- ostr.print(" null");
- }
- }
- ostr.println(";");
- ostr.println(" }");
-
- ostr.println(" public " + returnType + " visit(Node node, " + argumentType + " data)" + ve + "{");
- ostr.println(" " + (isVoidReturnType ? "" : "return ") + "defaultVisit(node, data);");
- ostr.println(" }");
-
- if (o.getMulti()) {
- for (String n : nodeNames) {
- if (n.equals("void")) {
- continue;
- }
- String nodeType = o.getNodePrefix() + n;
- ostr.println(
- " public " + returnType + " visit(" + nodeType + " node, " + argumentType + " data)" + ve + "{");
- ostr.println(" " + (isVoidReturnType ? "" : "return ") + "defaultVisit(node, data);");
- ostr.println(" }");
- }
- }
+ private void generateTreeState(JJTreeOptions o) {
+ TemplateOptions options = new TemplateOptions();
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
- ostr.println("}");
- } catch (final IOException e) {
- throw new Error(e.toString());
- }
+ TemplateProvider provider = JavaTemplate.TREE_STATE;
+ provider.render(o, options, JJTreeGlobals.parserName);
}
private void generateTreeClass(JJTreeOptions o) {
- String nodeType = "Tree";
- File file = new File(o.getOutputDirectory(), nodeType + ".java");
-
- DigestOptions options = new DigestOptions(o);
+ TemplateOptions options = new TemplateOptions();
options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
- try (DigestWriter writer = DigestWriter.create(file, HiveCC.VERSION, options)) {
- Template.of(JavaTemplate.TREE, options).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+
+ TemplateProvider provider = JavaTemplate.TREE;
+ provider.render(o, options);
}
private void generateNodeClass(JJTreeOptions o) {
- String nodeType = "Node";
- File file = new File(o.getOutputDirectory(), nodeType + ".java");
-
- DigestOptions options = new DigestOptions(o);
+ TemplateOptions options = new TemplateOptions();
options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
- try (DigestWriter writer = DigestWriter.create(file, HiveCC.VERSION, options)) {
- Template.of(JavaTemplate.NODE, options).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+
+ TemplateProvider provider = JavaTemplate.NODE;
+ provider.render(o, options);
}
private void generateTreeClasses(JJTreeOptions o) {
+ TemplateOptions options = new TemplateOptions();
+ options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
+ options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
+ options.set(HiveCC.JJTREE_VISITOR_RETURN_VOID, Boolean.valueOf(o.getVisitorReturnType().equals("void")));
+
Set excludes = o.getExcudeNodes();
for (String nodeType : nodesToGenerate()) {
if (!o.getBuildNodeFiles() || excludes.contains(nodeType)) {
continue;
}
- File file = new File(o.getOutputDirectory(), nodeType + ".java");
-
- DigestOptions options = new DigestOptions(o);
- options.set(HiveCC.PARSER_NAME, JJTreeGlobals.parserName);
- options.set(HiveCC.JJPARSER_JAVA_PACKAGE, o.getJavaPackage());
- try (DigestWriter writer = DigestWriter.create(file, HiveCC.VERSION, options)) {
- options.set(HiveCC.JJTREE_NODE_TYPE, nodeType);
- options.set(HiveCC.JJTREE_VISITOR_RETURN_VOID, Boolean.valueOf(o.getVisitorReturnType().equals("void")));
- Template.of(JavaTemplate.MULTI_NODE, options).render(writer);
- } catch (IOException e) {
- throw new Error(e.toString());
- }
+ options.set(HiveCC.JJTREE_NODE_TYPE, nodeType);
+
+ TemplateProvider provider = JavaTemplate.MULTI_NODE;
+ provider.render(o, options, nodeType);
}
}
private static String mergeVisitorException(JJTreeOptions o) {
String ve = o.getVisitorException();
- if (!"".equals(ve)) {
- ve = " throws " + ve;
+ return "".equals(ve) ? ve : " throws " + ve;
+ }
+
+ private static String returnValue(String returnType, String argumentType) {
+ boolean isVoidReturnType = "void".equals(returnType);
+ if (isVoidReturnType)
+ return "";
+
+ if (returnType.equals(argumentType))
+ return " data";
+
+ switch (returnType) {
+ case "boolean":
+ return " false";
+ case "int":
+ return " 0";
+ case "long":
+ return " 0L";
+ case "double":
+ return " 0.0d";
+ case "float":
+ return " 0.0f";
+ case "short":
+ return " 0";
+ case "byte":
+ return " 0";
+ case "char":
+ return " '\u0000'";
+ default:
+ return " null";
}
- return ve;
}
}
diff --git a/src/main/java/org/hivevm/cc/jjtree/ASTBNF.java b/src/main/java/org/hivevm/cc/jjtree/ASTBNF.java
index e7b9063f..659bd639 100644
--- a/src/main/java/org/hivevm/cc/jjtree/ASTBNF.java
+++ b/src/main/java/org/hivevm/cc/jjtree/ASTBNF.java
@@ -7,8 +7,8 @@ class ASTBNF extends ASTProduction {
public ASTBNF(JJTreeParser p, int id) {
super(p, id);
- this.throws_list.add("ParseException");
- this.throws_list.add("RuntimeException");
+ addThrow("ParseException");
+ addThrow("RuntimeException");
}
@Override
diff --git a/src/main/java/org/hivevm/cc/jjtree/ASTProduction.java b/src/main/java/org/hivevm/cc/jjtree/ASTProduction.java
index 31f33e8f..d0832ac5 100644
--- a/src/main/java/org/hivevm/cc/jjtree/ASTProduction.java
+++ b/src/main/java/org/hivevm/cc/jjtree/ASTProduction.java
@@ -3,15 +3,16 @@
package org.hivevm.cc.jjtree;
+import java.util.ArrayList;
import java.util.Hashtable;
-import java.util.Vector;
+import java.util.List;
public class ASTProduction extends ASTNode {
- String name;
- private int nextNodeScopeNumber;
- public Vector throws_list;
+ String name;
+ private int nextNodeScopeNumber;
+ private List throws_list;
private final Hashtable scopes;
@@ -19,8 +20,16 @@ public class ASTProduction extends ASTNode {
public ASTProduction(JJTreeParser p, int id) {
super(p, id);
this.nextNodeScopeNumber = 0;
- this.throws_list = new Vector<>();
this.scopes = new Hashtable<>();
+ this.throws_list = new ArrayList<>();
+ }
+
+ protected void addThrow(String throw_name) {
+ this.throws_list.add(throw_name);
+ }
+
+ public final Iterable throwElements() {
+ return this.throws_list;
}
diff --git a/src/main/java/org/hivevm/cc/jjtree/ASTWriter.java b/src/main/java/org/hivevm/cc/jjtree/ASTWriter.java
index a95cc7cf..b61255d5 100644
--- a/src/main/java/org/hivevm/cc/jjtree/ASTWriter.java
+++ b/src/main/java/org/hivevm/cc/jjtree/ASTWriter.java
@@ -3,14 +3,13 @@
package org.hivevm.cc.jjtree;
+import org.hivevm.cc.Language;
+import org.hivevm.cc.utils.Encoding;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
-import org.hivevm.cc.JJCodeBlock;
-import org.hivevm.cc.Language;
-import org.hivevm.cc.utils.Encoding;
-
/**
* The {@link ASTWriter} class.
@@ -27,6 +26,22 @@ public class ASTWriter extends PrintWriter {
// Indicates whether the token should be replaced by white space or replaced with the actual node
// variable.
private boolean whitingOut = false;
+
+ public enum CodeBlock {
+
+ CODE,
+ END;
+
+ public final String image;
+
+ private CodeBlock() {
+ this.image = "@" + name().toLowerCase();
+ }
+
+ public final String strip(String text) {
+ return text.substring(name().length() + 1);
+ }
+ }
/**
* Constructs an instance of {@link ASTWriter}.
@@ -51,7 +66,7 @@ public final Language getLanguage() {
*/
public final void openCodeBlock(String arg) {
append("\n");
- append(JJCodeBlock.Code.CODE);
+ append(CodeBlock.CODE.image);
if (arg != null) {
print(" // " + arg + "\n");
}
@@ -61,7 +76,7 @@ public final void openCodeBlock(String arg) {
* Closes a JJTree code block.
*/
public final void closeCodeBlock() {
- append("@end");
+ append(CodeBlock.END.image);
}
/**
@@ -91,9 +106,7 @@ public final void printToken(ASTNode node, Token token) {
NodeScope s = NodeScope.getEnclosingNodeScope(node);
if (s == null) {
- /*
- * Not within a node scope so we don't need to modify the source.
- */
+ // Not within a node scope so we don't need to modify the source.
print(Encoding.escapeUnicode(node.translateImage(token)));
return;
}
diff --git a/src/main/java/org/hivevm/cc/lexer/NfaState.java b/src/main/java/org/hivevm/cc/lexer/NfaState.java
index fb1a598f..feea8575 100644
--- a/src/main/java/org/hivevm/cc/lexer/NfaState.java
+++ b/src/main/java/org/hivevm/cc/lexer/NfaState.java
@@ -58,6 +58,8 @@ public NfaState(LexerStateData data) {
this.id = data.addAllState(this);
this.lexState = data.getStateIndex();
this.lookingFor = data.global.getCurrentKind();
+ this.loByteVec = new Vector<>();
+ this.nonAsciiMoveIndices = new int[0];
}
private NfaState CreateClone() {
diff --git a/src/main/java/org/hivevm/cc/semantic/MatchInfo.java b/src/main/java/org/hivevm/cc/semantic/MatchInfo.java
index b531649f..fa0392c1 100644
--- a/src/main/java/org/hivevm/cc/semantic/MatchInfo.java
+++ b/src/main/java/org/hivevm/cc/semantic/MatchInfo.java
@@ -6,7 +6,7 @@
/**
* Describes a match, within a given lookahead.
*/
-public class MatchInfo {
+class MatchInfo {
int[] match;
int firstFreeLoc;
diff --git a/src/main/java/org/hivevm/cc/source/CppWriter.java b/src/main/java/org/hivevm/cc/source/CppWriter.java
deleted file mode 100644
index 3ac9c866..00000000
--- a/src/main/java/org/hivevm/cc/source/CppWriter.java
+++ /dev/null
@@ -1,97 +0,0 @@
-// Copyright 2024 HiveVM.ORG. All rights reserved.
-// SPDX-License-Identifier: BSD-3-Clause
-
-package org.hivevm.cc.source;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-import org.hivevm.cc.generator.cpp.CppTemplate;
-import org.hivevm.cc.parser.JavaCCErrors;
-import org.hivevm.cc.utils.DigestOptions;
-import org.hivevm.cc.utils.DigestWriter;
-import org.hivevm.cc.utils.Template;
-import org.hivevm.cc.utils.TemplateProvider;
-
-/**
- * The {@link CppWriter} class.
- */
-public class CppWriter extends SourceWriter {
-
- private final TemplateProvider provider;
- private final StringWriter writer;
- private final StringWriter header = new StringWriter();
-
-
- /**
- * Constructs an instance of {@link CppWriter}.
- *
- * @param name
- * @param options
- */
- public CppWriter(String name, CppTemplate provider, DigestOptions options) {
- super(name, provider, options);
- this.provider = provider.getHeader();
- this.writer = (StringWriter) this.out;
- this.header.append("#ifndef JAVACC_" + name.replace('.', '_').toUpperCase() + "_H\n");
- this.header.append("#define JAVACC_" + name.replace('.', '_').toUpperCase() + "_H\n");
- }
-
- public final void switchToHeader() {
- this.out = this.header;
- }
-
- /**
- * Write the content using a template.
- *
- * @param path
- */
- public final void writeTemplateHeader() throws IOException {
- Template template = Template.of(this.provider, getOptions());
- template.render(new PrintWriter(this.out));
- }
-
- @Override
- public final void close() {
- // dump the statics into the main file with the code.
- StringBuffer buffer = new StringBuffer();
- buffer.append(this.writer.toString());
-
- saveOutput(this.provider, this.header.getBuffer(), getOptions());
- saveOutput(getProvider(), buffer, getOptions());
- }
-
- private void saveOutput(TemplateProvider provider, StringBuffer buffer, DigestOptions options) {
- CppWriter.fixupLongLiterals(buffer);
-
- try (DigestWriter writer = provider.createDigestWriter(getName(), options)) {
- writer.print(buffer.toString());
- } catch (IOException ioe) {
- JavaCCErrors.fatal("Could not create output file: " + provider.getFilename(getName()));
- }
- }
-
-
- private static boolean isHexDigit(char c) {
- return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
- }
-
- // HACK
- private static void fixupLongLiterals(StringBuffer buffer) {
- for (int i = 0; i < (buffer.length() - 1); i++) {
- char c1 = buffer.charAt(i);
- char c2 = buffer.charAt(i + 1);
- if (Character.isDigit(c1) || ((c1 == '0') && (c2 == 'x'))) {
- i += c1 == '0' ? 2 : 1;
- while (CppWriter.isHexDigit(buffer.charAt(i))) {
- i++;
- }
- if (buffer.charAt(i) == 'L') {
- buffer.insert(i, "UL");
- }
- i++;
- }
- }
- }
-}
diff --git a/src/main/java/org/hivevm/cc/source/SourceWriter.java b/src/main/java/org/hivevm/cc/source/SourceWriter.java
deleted file mode 100644
index 6843a6db..00000000
--- a/src/main/java/org/hivevm/cc/source/SourceWriter.java
+++ /dev/null
@@ -1,90 +0,0 @@
-// Copyright 2024 HiveVM.ORG. All rights reserved.
-// SPDX-License-Identifier: BSD-3-Clause
-
-package org.hivevm.cc.source;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-import org.hivevm.cc.parser.JavaCCErrors;
-import org.hivevm.cc.utils.DigestOptions;
-import org.hivevm.cc.utils.DigestWriter;
-import org.hivevm.cc.utils.Template;
-import org.hivevm.cc.utils.TemplateProvider;
-
-/**
- * The {@link SourceWriter} class.
- */
-public class SourceWriter extends PrintWriter {
-
- private final String name;
- private final TemplateProvider provider;
- private final DigestOptions options;
-
- /**
- * Constructs an instance of {@link SourceWriter}.
- *
- * @param name
- * @param provider
- * @param options
- */
- public SourceWriter(String name, TemplateProvider provider, DigestOptions options) {
- super(new StringWriter());
- this.name = name;
- this.provider = provider;
- this.options = options;
- }
-
- /**
- * Gets the name.
- */
- public final String getName() {
- return this.name;
- }
-
- /**
- * Gets the {@link TemplateProvider}.
- */
- public final TemplateProvider getProvider() {
- return this.provider;
- }
-
- /**
- * Gets the {@link DigestOptions}.
- */
- public final DigestOptions getOptions() {
- return this.options;
- }
-
- /**
- * Write the content using a template.
- *
- * @param path
- */
- public final void writeTemplate() throws IOException {
- Template template = Template.of(getProvider(), getOptions());
- template.render(new PrintWriter(this.out));
- }
-
- /**
- * Save {@link SourceWriter} to output path.
- *
- */
- @Override
- public void close() {
- try (DigestWriter writer = getProvider().createDigestWriter(getName(), this.options)) {
- writer.print(toString());
- } catch (IOException e) {
- JavaCCErrors.fatal("Could not create output file: " + getProvider().getFilename(name));
- }
- }
-
- /**
- * Converts the {@link StringWriter} to a {@link StringBuffer}.
- */
- @Override
- public final String toString() {
- return ((StringWriter) this.out).getBuffer().toString();
- }
-}
diff --git a/src/main/java/org/hivevm/cc/utils/DigestWriter.java b/src/main/java/org/hivevm/cc/utils/DigestWriter.java
index 4a2bdf95..23394c58 100644
--- a/src/main/java/org/hivevm/cc/utils/DigestWriter.java
+++ b/src/main/java/org/hivevm/cc/utils/DigestWriter.java
@@ -3,6 +3,8 @@
package org.hivevm.cc.utils;
+import org.hivevm.cc.HiveCC;
+
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
@@ -130,21 +132,9 @@ private static DigestWriter create(OutputStream stream, Version version, DigestO
* Constructs an instance of {@link DigestWriter}.
*
* @param file
- * @param version
- * @param options
- */
- public static DigestWriter create(File file, Version version, DigestOptions options) throws FileNotFoundException {
- return DigestWriter.create(new FileOutputStream(file), version, options, false);
- }
-
- /**
- * Constructs an instance of {@link DigestWriter}.
- *
- * @param file
- * @param version
* @param options
*/
- public static DigestWriter createCpp(File file, Version version, DigestOptions options) throws FileNotFoundException {
- return DigestWriter.create(new FileOutputStream(file), version, options, true);
+ public static DigestWriter create(File file, DigestOptions options, boolean isCpp) throws FileNotFoundException {
+ return DigestWriter.create(new FileOutputStream(file), HiveCC.VERSION, options, isCpp);
}
}
diff --git a/src/main/java/org/hivevm/cc/utils/TemplateProvider.java b/src/main/java/org/hivevm/cc/utils/TemplateProvider.java
index 79776b67..51d07052 100644
--- a/src/main/java/org/hivevm/cc/utils/TemplateProvider.java
+++ b/src/main/java/org/hivevm/cc/utils/TemplateProvider.java
@@ -3,7 +3,12 @@
package org.hivevm.cc.utils;
+import org.hivevm.cc.parser.JavaCCErrors;
+import org.hivevm.cc.parser.Options;
+
+import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
/**
* The {@link TemplateProvider} class.
@@ -16,5 +21,62 @@ public interface TemplateProvider {
DigestWriter createDigestWriter(DigestOptions options) throws FileNotFoundException;
- DigestWriter createDigestWriter(String filename, DigestOptions options) throws FileNotFoundException;
+ DigestWriter createDigestWriter(DigestOptions options, String filename) throws FileNotFoundException;
+
+ default void render(Options options, TemplateOptions options2) {
+ DigestOptions digest = new DigestOptions(options, options2);
+ try (DigestWriter writer = createDigestWriter(digest)) {
+ Template.of(this, digest).render(writer);
+ } catch (IOException e) {
+ throw new Error(e.toString());
+ }
+ }
+
+ default void render(Options options, TemplateOptions options2, String filename) {
+ DigestOptions digest = new DigestOptions(options, options2);
+ try (DigestWriter writer = createDigestWriter(digest, filename)) {
+ Template.of(this, digest).render(writer);
+ } catch (IOException e) {
+ throw new Error(e.toString());
+ }
+ }
+
+ /**
+ * Generates a {@link File} from a template.
+ *
+ * @param template
+ * @param options
+ */
+ static void render(TemplateProvider template, Options options) {
+ generate(template, null, new DigestOptions(options));
+ }
+
+ /**
+ * Generates a {@link File} from a template.
+ *
+ * @param template
+ * @param options
+ */
+ static void render(TemplateProvider template, Options options, TemplateOptions options2, String name) {
+ generate(template, name, new DigestOptions(options, options2));
+ }
+
+ /**
+ * Generates a {@link File} from a template.
+ *
+ * @param tpl
+ * @param name
+ * @param options
+ */
+ static void generate(TemplateProvider tpl, String name, DigestOptions options) {
+ try (DigestWriter writer = tpl.createDigestWriter(options, name)) {
+ Template template = Template.of(tpl, writer.options());
+ template.render(writer);
+ } catch (IOException e) {
+ String filename = tpl.getFilename(name);
+ System.err.println("Failed to create file: " + filename + " " + e);
+ JavaCCErrors.semantic_error("Could not open file: " + filename + " for writing.");
+ throw new Error();
+ }
+ }
}
diff --git a/src/main/resources/JavaCC.jj b/src/main/resources/JavaCC.jj
index 46e097d4..beff84b5 100644
--- a/src/main/resources/JavaCC.jj
+++ b/src/main/resources/JavaCC.jj
@@ -633,7 +633,7 @@ void regular_expression(Container c) :
@code
image = "";
@end
- < LANGLE: "<" >
+ < LT >
[
[ < HASH >
@code
@@ -642,7 +642,7 @@ void regular_expression(Container c) :
]
image=identifier() < COLON >
]
- complex_regular_expression_choices(c) < RANGLE: ">" >
+ complex_regular_expression_choices(c) < GT >
@code
RegularExpression re;
if (c.member instanceof RJustName) {
diff --git a/src/main/resources/templates/cpp/Lexer.h.template b/src/main/resources/templates/cpp/Lexer.h.template
index b9da5f8b..5cddbccc 100644
--- a/src/main/resources/templates/cpp/Lexer.h.template
+++ b/src/main/resources/templates/cpp/Lexer.h.template
@@ -1,3 +1,5 @@
+#ifndef JAVACC_{{PARSER_NAME_UPPER}}TOKENMANAGER_H
+#define JAVACC_{{PARSER_NAME_UPPER}}TOKENMANAGER_H
#include "JavaCC.h"
#include "Reader.h"
diff --git a/src/main/resources/templates/cpp/Parser.h.template b/src/main/resources/templates/cpp/Parser.h.template
index 6d2532b2..b1a4d495 100644
--- a/src/main/resources/templates/cpp/Parser.h.template
+++ b/src/main/resources/templates/cpp/Parser.h.template
@@ -1,3 +1,5 @@
+#ifndef JAVACC_{{PARSER_NAME_UPPER}}_H
+#define JAVACC_{{PARSER_NAME_UPPER}}_H
#include "JavaCC.h"
#include "Reader.h"
diff --git a/src/main/resources/templates/cpp/TreeConstants.h.template b/src/main/resources/templates/cpp/TreeConstants.h.template
new file mode 100644
index 00000000..26548683
--- /dev/null
+++ b/src/main/resources/templates/cpp/TreeConstants.h.template
@@ -0,0 +1,25 @@
+#ifndef JAVACC_{{NAME_UPPER}}TREECONSTANTS_H
+#define JAVACC_{{NAME_UPPER}}TREECONSTANTS_H
+
+#include "JavaCC.h"
+@if(NAMESPACE)
+namespace {{NAMESPACE}} {
+@fi
+enum {
+@foreach(node : NODES)
+ {{node.label}} = {{node.ordinal}},
+@end
+};
+
+@foreach(node : NODE_NAMES)
+static JJChar jjtNodeName_arr_{{node.ordinal}}[] = {{{node.chars}}0};
+@end
+static JJString jjtNodeName[] = {
+@foreach(node : NODE_NAMES)
+ jjtNodeName_arr_{{node.ordinal}},
+@end
+};
+@if(NAMESPACE)
+}
+@fi
+#endif
\ No newline at end of file
diff --git a/src/main/resources/templates/cpp/TreeOne.h.template b/src/main/resources/templates/cpp/TreeOne.h.template
new file mode 100644
index 00000000..569cd31b
--- /dev/null
+++ b/src/main/resources/templates/cpp/TreeOne.h.template
@@ -0,0 +1,8 @@
+#ifndef JAVACC_ONE_TREE_H
+#define JAVACC_ONE_TREE_H
+
+#include "Node.h"
+@foreach(node : NODES)
+#include {{node}}.h"
+@end
+#endif
\ No newline at end of file
diff --git a/src/main/resources/templates/cpp/Visitor.h.template b/src/main/resources/templates/cpp/Visitor.h.template
new file mode 100644
index 00000000..f8616819
--- /dev/null
+++ b/src/main/resources/templates/cpp/Visitor.h.template
@@ -0,0 +1,50 @@
+#ifndef {{NAME_UPPER}}VISITOR_H
+#define {{NAME_UPPER}}VISITOR_H
+
+#include "JavaCC.h"
+#include "{{PARSER_NAME}}Tree.h"
+
+@if(NAMESPACE)
+namespace {{NAMESPACE}} {
+@fi
+
+class {{PARSER_NAME}}Visitor
+{
+ public:
+
+ virtual {{RETURN_TYPE}} visit(const Node *node, {{ARGUMENT_TYPE}} data) = 0;
+@if(IS_MULTI)
+@foreach(node : NODES)
+ virtual {{RETURN_TYPE}} visit(const {{node.type} *node, {{ARGUMENT_TYPE}} data) = 0;
+@end
+@endif
+
+ virtual ~{{PARSER_NAME}}Visitor() { }
+};
+
+
+class {{PARSER_NAME}}DefaultVisitor() : public {{PARSER_NAME}}Visitor {
+
+public:
+ virtual {{RETURN_TYPE}} defaultVisit(const Node *node, {{ARGUMENT_TYPE}} data) = 0;
+
+ virtual {{RETURN_TYPE}} visit(const Node *node, {{ARGUMENT_TYPE}} data) {
+ {{RETURN}}defaultVisit(node, data);
+ }
+
+@if(IS_MULTI)
+@foreach(node : NODES)
+ virtual {{RETURN_TYPE}} visit(const {{node.type}} *node, {{ARGUMENT_TYPE}} data) {
+ {{RETURN}}defaultVisit(node, data);
+ }
+@end
+@endif
+
+ ~{{PARSER_NAME}}DefaultVisitor() { }
+};
+
+@if(NAMESPACE)
+}
+@fi
+
+#endif
\ No newline at end of file
diff --git a/src/main/resources/templates/java/DefaultVisitor.template b/src/main/resources/templates/java/DefaultVisitor.template
new file mode 100644
index 00000000..40b2bbe2
--- /dev/null
+++ b/src/main/resources/templates/java/DefaultVisitor.template
@@ -0,0 +1,19 @@
+package {{PACKAGE}};
+
+public class {{PARSER_NAME}}DefaultVisitor implements {{PARSER_NAME}}Visitor
+{
+ public {{RETURN_TYPE}} defaultVisit(Node node, {{ARGUMENT_TYPE}} data){{EXCEPTION}} {
+ node.childrenAccept(this, data);
+ return{{RETURN_VALUE}};
+ }
+ public {{RETURN_TYPE}} visit(Node node, {{ARGUMENT_TYPE}} data){{EXCEPTION}} {
+ {{RETURN}}defaultVisit(node, data);
+ }
+@if(IS_MULTI)
+@foreach(node : NODES)
+ public {{RETURN_TYPE}} visit({{NODE_PREFIX}}{{node}} node, {{ARGUMENT_TYPE}} data){{EXCEPTION}} {
+ {{RETURN}}defaultVisit(node, data);
+ }
+@end
+@fi
+}
\ No newline at end of file
diff --git a/src/main/resources/templates/java/Lexer.template b/src/main/resources/templates/java/Lexer.template
index 5e45b5af..1f78618d 100755
--- a/src/main/resources/templates/java/Lexer.template
+++ b/src/main/resources/templates/java/Lexer.template
@@ -1,3 +1,8 @@
+package {{PACKAGE}};
+
+/** Token Manager. */
+class {{PARSER_NAME}}TokenManager@if(LEXER) extends {{LEXER}}@fi implements {{PARSER_NAME}}Constants {
+
@foreach(index : LOHI_BYTES)
static final long[] jjbitVec{{index}} = { {{index.bytes}} };
@end
@@ -62,7 +67,15 @@ protected Token jjFillToken()
{{DumpStateSets}}
@foreach(nfa : NON_ASCII_TABLE)
-{{nfa.AsciiMove}}
+private static final boolean jjCanMove_{{nfa.NON_ASCII_METHOD}}(int hiByte, int i1, int i2, long l1, long l2)
+{
+ switch(hiByte)
+ {
+{{nfa.ASCII_MOVE}}
+ return false;
+ }
+}
+
@end
{{DumpGetNextToken}}
@if(DEBUG_TOKEN_MANAGER)
diff --git a/src/main/resources/templates/java/TreeConstants.template b/src/main/resources/templates/java/TreeConstants.template
new file mode 100644
index 00000000..53defef1
--- /dev/null
+++ b/src/main/resources/templates/java/TreeConstants.template
@@ -0,0 +1,15 @@
+package {{PACKAGE}};
+
+public interface {{PARSER_NAME}}TreeConstants
+{
+@foreach(node : NODES)
+ int {{node.label}} = {{node.ordinal}};
+@end
+
+
+ String[] jjtNodeName = {
+@foreach(node : NODE_NAMES)
+ "{{node}}",
+@end
+ };
+}
diff --git a/src/main/resources/templates/java/Visitor.template b/src/main/resources/templates/java/Visitor.template
new file mode 100644
index 00000000..d304fbd8
--- /dev/null
+++ b/src/main/resources/templates/java/Visitor.template
@@ -0,0 +1,11 @@
+package {{PACKAGE}};
+
+public interface {{PARSER_NAME}}Visitor
+{
+ {{RETURN_TYPE}} visit(Node node, {{ARGUMENT_TYPE}} data){{EXCEPTION}};
+@if(IS_MULTI)
+@foreach(node : NODES)
+ {{RETURN_TYPE}} visit({{NODE_PREFIX}}{{node}} node, {{ARGUMENT_TYPE}} data){{EXCEPTION}};
+@end
+@fi
+}
\ No newline at end of file