Skip to content

Commit

Permalink
Improvements on generated parsers v.1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Brigl committed Nov 17, 2024
1 parent 1a42b6b commit 619cfb8
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 133 deletions.
Binary file modified bootstrap/javacc.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<property name="jjtree-generated-src" value="${generated-src}/org/hivevm/cc/jjtree" />

<property name="bootstrap-jar" value="bootstrap/javacc.jar" />
<property name="jjtree-class-name" value="org.hivevm.cc.JJTree" />
<property name="javacc-class-name" value="org.hivevm.cc.JJParser" />
<property name="jjtree-class-name" value="org.hivevm.cc.HiveCCTree" />
<property name="javacc-class-name" value="org.hivevm.cc.HiveCCParser" />

<property name="target-folder" value="target" />

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/javacc.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
public interface javacc {

static void main(String[] args) throws Exception {
org.hivevm.cc.JJParser.main(args);
org.hivevm.cc.HiveCCParser.main(args);
}
}
2 changes: 1 addition & 1 deletion src/main/java/jjtree.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
public interface jjtree {

static void main(String[] args) {
org.hivevm.cc.JJTree.main(args);
org.hivevm.cc.HiveCCTree.main(args);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/hivevm/cc/HiveCCBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public final void build() {
}
arguments.add(this.jjt.getAbsolutePath());

JJTree.main(arguments.toArray(new String[arguments.size()]));
HiveCCTree.main(arguments.toArray(new String[arguments.size()]));

String path = this.jjt.getAbsolutePath();
int offset = path.lastIndexOf("/");
Expand All @@ -103,7 +103,7 @@ public final void build() {
arguments.add(this.jj.getAbsolutePath());
}

JJParser.main(arguments.toArray(new String[arguments.size()]));
HiveCCParser.main(arguments.toArray(new String[arguments.size()]));
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
/**
* Entry point.
*/
public abstract class JJParser {
public abstract class HiveCCParser {

private JJParser() {}
private HiveCCParser() {}

private static void printHelp() {
System.out.println("Usage:");
Expand Down Expand Up @@ -56,15 +56,15 @@ public static void main(String args[]) throws Exception {
System.exit(0);
}

JJMain.bannerLine("Parser Generator", "");
HiveCCTools.bannerLine("Parser Generator", "");

JavaCCErrors.reInit();
Options options = new Options();

JavaCCParser parser = null;
if (args.length == 0) {
System.out.println("");
JJParser.printHelp();
HiveCCParser.printHelp();
System.exit(1);
} else {
System.out.println("(type \"javacc\" with no arguments for help)");
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void main(String args[]) throws Exception {
try {
String jjFile = args[args.length - 1];
System.out.println("Reading from file " + jjFile + " . . .");
boolean jjtreeGenerated = JJMain.isGeneratedBy("JJTree", jjFile);
boolean jjtreeGenerated = HiveCCTree.isGeneratedByTree(jjFile);
JavaCCData request = new JavaCCData(jjtreeGenerated, options);

parser.initialize(request);
Expand All @@ -116,7 +116,7 @@ public static void main(String args[]) throws Exception {
// Initialize the parser data
ParserEngine engine = ParserEngine.create(Options.getOutputLanguage());

JJMain.createOutputDir(options.getOutputDirectory());
HiveCCTools.createOutputDir(options.getOutputDirectory());

Semanticize.semanticize(request, options);

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/hivevm/cc/HiveCCTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 HiveVM.ORG. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package org.hivevm.cc;

import java.io.File;

import org.hivevm.cc.parser.JavaCCErrors;

/**
* The {@link HiveCCTools} class.
*/
public abstract class HiveCCTools {

/**
* Constructs an instance of {@link HiveCCTools}.
*/
private HiveCCTools() {}

/**
* This prints the banner line when the various tools are invoked. This takes as argument the
* tool's full name and its version.
*/
public static void bannerLine(String fullName, String ver) {
System.out.print("Java Compiler Compiler Version " + HiveCC.VERSION.toString() + " (" + fullName);
if (!ver.equals("")) {
System.out.print(" Version " + ver);
}
System.out.println(")");
}

static void createOutputDir(File outputDir) {
if (!outputDir.exists()) {
JavaCCErrors.warning("Output directory \"" + outputDir + "\" does not exist. Creating the directory.");

if (!outputDir.mkdirs()) {
JavaCCErrors.semantic_error("Cannot create the output directory : " + outputDir);
return;
}
}

if (!outputDir.isDirectory()) {
JavaCCErrors.semantic_error("\"" + outputDir + " is not a valid output directory.");
return;
}

if (!outputDir.canWrite()) {
JavaCCErrors.semantic_error("Cannot write to the output output directory : \"" + outputDir + "\"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

package org.hivevm.cc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hivevm.cc.generator.ParserEngine;
import org.hivevm.cc.jjtree.ASTGrammar;
import org.hivevm.cc.jjtree.ASTWriter;
Expand All @@ -12,14 +27,9 @@
import org.hivevm.cc.parser.JavaCCErrors;
import org.hivevm.cc.parser.Options;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.ParseException;
public class HiveCCTree {

public class JJTree {
private static final Pattern GENERATED = Pattern.compile("@generated\\(([^\\)]+)\\)");

private static void help_message() {
System.out.println("Usage:");
Expand Down Expand Up @@ -76,19 +86,58 @@ private static void help_message() {
System.out.println("");
}

/**
* Writes the generated string.
*
* @param writer
*/
private static void writeGenerated(PrintWriter writer) {
writer.println("/* @generated(JJTree) */");
}

/**
* Parses the tool list from the generated string.
*
* @param text
*/
private static List<String> readToolNameList(String text) {
Matcher matcher = HiveCCTree.GENERATED.matcher(text);
while (matcher.find()) {
return Arrays.asList(matcher.group(1).split(","));
}
return Collections.emptyList();
}

/**
* Returns true if tool name passed is one of the tool names returned by getToolNames(fileName).
*
* @param fileName
*/
static boolean isGeneratedByTree(String fileName) {
try (InputStream stream = new FileInputStream(fileName)) {
String data = new String(stream.readAllBytes());
for (String element : HiveCCTree.readToolNameList(data)) {
if ("JJTree".equals(element)) {
return true;
}
}
} catch (IOException e) {}
return false;
}

/**
* A main program that exercises the parser.
*/
public static void main(String args[]) {
JJMain.bannerLine("Tree Builder", "");
HiveCCTools.bannerLine("Tree Builder", "");

JavaCCErrors.reInit();
JJTreeOptions options = new JJTreeOptions();
JJTreeGlobals.initialize();

if (args.length == 0) {
System.out.println("");
JJTree.help_message();
HiveCCTree.help_message();
System.exit(1);
} else {
System.out.println("(type \"jjtree\" with no arguments for help)");
Expand All @@ -109,11 +158,11 @@ public static void main(String args[]) {

options.validate();

JJMain.createOutputDir(options.getOutputDirectory());
File file = new File(options.getOutputDirectory(), JJTree.create_output_file_name(fn, options));
HiveCCTools.createOutputDir(options.getOutputDirectory());
File file = new File(options.getOutputDirectory(), HiveCCTree.create_output_file_name(fn, options));

try {
if (JJMain.isGeneratedBy("JJTree", fn)) {
if (HiveCCTree.isGeneratedByTree(fn)) {
throw new IOException(fn + " was generated by jjtree. Cannot run jjtree again.");
}

Expand All @@ -131,7 +180,7 @@ public static void main(String args[]) {
ParserEngine engine = ParserEngine.create(Options.getOutputLanguage());

try (ASTWriter writer = new ASTWriter(file, Options.getOutputLanguage())) {
JJMain.writeGenerated(writer);
HiveCCTree.writeGenerated(writer);
engine.generateJJTree(root, writer, options);
} catch (IOException ioe) {
System.out.println("Error setting input: " + ioe.getMessage());
Expand Down
94 changes: 0 additions & 94 deletions src/main/java/org/hivevm/cc/JJMain.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/hivevm/cc/doc/JJDocMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package org.hivevm.cc.doc;

import org.hivevm.cc.JJMain;
import org.hivevm.cc.HiveCCTools;
import org.hivevm.cc.parser.JavaCCData;
import org.hivevm.cc.parser.JavaCCErrors;
import org.hivevm.cc.parser.JavaCCParser;
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void main(String args[]) throws Exception {
JavaCCErrors.reInit();
Options options = new JJDocOptions();

JJMain.bannerLine("Documentation Generator", "0.1.4");
HiveCCTools.bannerLine("Documentation Generator", "0.1.4");

JavaCCParser parser = null;
if (args.length == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/hivevm/cc/generator/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CodeGenerator {

private int crow, ccol;

protected void genTokenSetup(Token t) {
protected final void genTokenSetup(Token t) {
Token tt = t;

while (tt.specialToken != null) {
Expand Down Expand Up @@ -64,7 +64,7 @@ protected final String getStringToPrint(Token t) {
return retval + getStringForTokenOnly(t);
}

protected String getStringForTokenOnly(Token t) {
protected final String getStringForTokenOnly(Token t) {
String retval = "";
for (; this.crow < t.beginLine; this.crow++) {
retval += "\n";
Expand Down
Loading

0 comments on commit 619cfb8

Please sign in to comment.