From 974f5aa2d999a8d221b2efec4d1984afa2f76fd8 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Tue, 28 Apr 2020 14:49:53 +0200 Subject: [PATCH] Convert patchfiles into a proper separate runtime library Previously the patchfiles were copied into the source of each generated parser, even though they are identical for all parsers and always use a hardcoded package name. This means that all generated parsers had conflicting classes, although in practice this was not noticeable, because the classes were always identical. These common files are now a proper separate library that all generated parsers will depend on. This will fix the class name conflicts when using more than one SableCC parser in a single project. --- build.gradle | 10 +- sablecc-runtime/build.gradle | 64 +++++++++++++ .../de/hhu/stups/sablecc/patch/IParser.java | 0 .../de/hhu/stups/sablecc/patch/IToken.java | 0 .../sablecc/patch/ITokenListContainer.java | 0 .../stups/sablecc/patch/PositionedNode.java | 0 .../stups/sablecc/patch/SourcePosition.java | 0 .../stups/sablecc/patch/SourcePositions.java | 0 .../stups/sablecc/patch/SourcecodeRange.java | 0 settings.gradle | 1 + .../java/org/sablecc/sablecc/SableCC.java | 95 ------------------- 11 files changed, 70 insertions(+), 100 deletions(-) create mode 100644 sablecc-runtime/build.gradle rename src/main/resources/patchfiles/IParser.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IParser.java (100%) rename src/main/resources/patchfiles/IToken.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IToken.java (100%) rename src/main/resources/patchfiles/ITokenListContainer.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/ITokenListContainer.java (100%) rename src/main/resources/patchfiles/PositionedNode.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/PositionedNode.java (100%) rename src/main/resources/patchfiles/SourcePosition.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePosition.java (100%) rename src/main/resources/patchfiles/SourcePositions.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePositions.java (100%) rename src/main/resources/patchfiles/SourcecodeRange.txt => sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcecodeRange.java (100%) diff --git a/build.gradle b/build.gradle index 2ac7215..1a5153b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,17 +4,17 @@ apply plugin: 'eclipse' apply plugin: 'maven-publish' apply plugin: 'signing' - -project.version = '3.2.15-SNAPSHOT' -project.group = 'de.hhu.stups' +allprojects { + project.group = 'de.hhu.stups' + project.version = '3.3.0-SNAPSHOT' + project.ext.isSnapshot = project.version.endsWith("-SNAPSHOT") +} wrapper { gradleVersion = "6.3" distributionType = Wrapper.DistributionType.ALL } -final isSnapshot = project.version.endsWith("-SNAPSHOT") - sourceCompatibility = 7 targetCompatibility = 7 diff --git a/sablecc-runtime/build.gradle b/sablecc-runtime/build.gradle new file mode 100644 index 0000000..2534e9b --- /dev/null +++ b/sablecc-runtime/build.gradle @@ -0,0 +1,64 @@ +plugins { + id "java-library" + id "maven-publish" + id "signing" +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_7 + withSourcesJar() + withJavadocJar() +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + + pom { + name = 'SableCC - Stups fork (runtime library)' + description = 'This is the runtime support library for the Stups fork of SableCC. It provides common classes and interfaces used by the generated parsers at runtime.' + url = 'https://gitlab.cs.uni-duesseldorf.de/general/stups/sablecc-stups' + + licenses { + license { + name = 'GNU Lesser General Public License, Version 2.1' + url = 'http://www.gnu.org/licenses/lgpl-2.1.html' + } + } + + scm { + connection = 'scm:git:https://gitlab.cs.uni-duesseldorf.de/general/stups/sablecc-stups.git' + developerConnection = 'scm:git:git@gitlab.cs.uni-duesseldorf.de:general/stups/sablecc-stups.git' + url = 'https://gitlab.cs.uni-duesseldorf.de/general/stups/sablecc-stups' + } + + developers { + developer { + id = 'bendisposto' + name = 'Jens Bendisposto' + email = 'jens@bendisposto.de' + } + } + } + } + } + + repositories { + maven { + final releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2" + final snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots" + url isSnapshot ? snapshotsRepoUrl : releasesRepoUrl + if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { + credentials { + username project.ossrhUsername + password project.ossrhPassword + } + } + } + } +} + +signing { + sign publishing.publications.mavenJava +} diff --git a/src/main/resources/patchfiles/IParser.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IParser.java similarity index 100% rename from src/main/resources/patchfiles/IParser.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IParser.java diff --git a/src/main/resources/patchfiles/IToken.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IToken.java similarity index 100% rename from src/main/resources/patchfiles/IToken.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/IToken.java diff --git a/src/main/resources/patchfiles/ITokenListContainer.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/ITokenListContainer.java similarity index 100% rename from src/main/resources/patchfiles/ITokenListContainer.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/ITokenListContainer.java diff --git a/src/main/resources/patchfiles/PositionedNode.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/PositionedNode.java similarity index 100% rename from src/main/resources/patchfiles/PositionedNode.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/PositionedNode.java diff --git a/src/main/resources/patchfiles/SourcePosition.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePosition.java similarity index 100% rename from src/main/resources/patchfiles/SourcePosition.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePosition.java diff --git a/src/main/resources/patchfiles/SourcePositions.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePositions.java similarity index 100% rename from src/main/resources/patchfiles/SourcePositions.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcePositions.java diff --git a/src/main/resources/patchfiles/SourcecodeRange.txt b/sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcecodeRange.java similarity index 100% rename from src/main/resources/patchfiles/SourcecodeRange.txt rename to sablecc-runtime/src/main/java/de/hhu/stups/sablecc/patch/SourcecodeRange.java diff --git a/settings.gradle b/settings.gradle index f8e1a30..00be2ad 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ rootProject.name = "sablecc" +include "sablecc-runtime" diff --git a/src/main/java/org/sablecc/sablecc/SableCC.java b/src/main/java/org/sablecc/sablecc/SableCC.java index 235a92e..3c80cb6 100644 --- a/src/main/java/org/sablecc/sablecc/SableCC.java +++ b/src/main/java/org/sablecc/sablecc/SableCC.java @@ -113,33 +113,6 @@ public class SableCC { } } - /* - * extract additional stups-classes from .jar-file - */ - String generated = "."; - boolean found = false; - for (String p : arguments) { - if (found) { - generated = p; - break; - } - - if ("-d".equals(p)) { - found = true; - } - } - - String patchFolder = generated + "/de/hhu/stups/sablecc/patch"; - String[] patchFiles = { "IParser", "IToken", "ITokenListContainer", - "PositionedNode", "SourcePosition", "SourcePositions", - "SourcecodeRange" }; - - new File(patchFolder).mkdirs(); - - for (String f : patchFiles) { - extractPatchFile(patchFolder, f); - } - try { for (int i = 0; i < filename.size(); i++) { processGrammar((String) filename.elementAt(i), d_option); @@ -151,74 +124,6 @@ public class SableCC { System.exit(0); } - private static void extractPatchFile(String patchFolder, String patchFile) { - - ClassLoader loader = SableCC.class.getClassLoader(); - InputStream input = loader.getResourceAsStream("patchfiles/" - + patchFile + ".txt"); - String output = ""; - - String outputFile = patchFolder + "/" + patchFile + ".java"; - try { - output = convertStreamToString(input); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } - - Writer writer = null; - try { - File file = new File(outputFile); - writer = new BufferedWriter(new FileWriter(file)); - writer.write(output); - } catch (FileNotFoundException e) { - e.printStackTrace(); - System.exit(1); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } finally { - try { - if (writer != null) { - writer.close(); - } - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } - } - } - - private static String convertStreamToString(InputStream is) - throws IOException { - /* - * To convert the InputStream to String we use the Reader.read(char[] - * buffer) method. We iterate until the Reader return -1 which means - * there's no more data to read. We use the StringWriter class to - * produce the string. - */ - - if (is != null) { - Writer writer = new StringWriter(); - - char[] buffer = new char[1024]; - try { - Reader reader = new BufferedReader(new InputStreamReader(is, - "UTF-8")); - int n; - while ((n = reader.read(buffer)) != -1) { - writer.write(buffer, 0, n); - } - } finally { - is.close(); - } - return writer.toString(); - } else { - return ""; - } - - } - /** * The main method for processing grammar file and generating the * parser/lexer. -- GitLab