From 3dade998ba6e7cb5c7c78cb1303d3c499ecb6e45 Mon Sep 17 00:00:00 2001 From: Sandy Brownlee Date: Thu, 15 Sep 2022 17:42:16 +0100 Subject: [PATCH] possible fix for windows --- .../syntacticAnalyzer/SourceFile.java | 20 ++++++++++++++----- .../syntacticAnalyser/TestScanner.java | 8 +++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/SourceFile.java b/Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/SourceFile.java index 74826f3..3651455 100644 --- a/Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/SourceFile.java +++ b/Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/SourceFile.java @@ -14,26 +14,36 @@ package triangle.syntacticAnalyzer; +import java.net.URL; + public class SourceFile { public static final char EOL = '\n'; public static final char EOT = '\u0000'; java.io.File sourceFile; - java.io.FileInputStream source; + java.io.InputStream source; int currentLine; public static SourceFile ofPath(String pathname) { try { - return new SourceFile(pathname); + SourceFile sf = new SourceFile(); + sf.sourceFile = new java.io.File(pathname); + sf.source = new java.io.FileInputStream(sf.sourceFile); + return sf; } catch (java.io.IOException s) { return null; } } + + public static SourceFile fromResource(String handle) { + SourceFile sf = new SourceFile(); + //sf.sourceFile = new java.io.File(pathname); + sf.source = sf.getClass().getResourceAsStream(handle); + return sf; + } - private SourceFile(String pathname) throws java.io.FileNotFoundException { - sourceFile = new java.io.File(pathname); - source = new java.io.FileInputStream(sourceFile); + private SourceFile() { currentLine = 1; } diff --git a/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java b/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java index ae4aaa2..ab10811 100644 --- a/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java +++ b/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java @@ -49,8 +49,9 @@ public class TestScanner { // build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs") // which adds the programs directory to the list of places Java can easily find files // getResource() below searches for a file, which is in /programs - SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString()); - + //SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString()); + SourceFile source = SourceFile.fromResource(filename); + Scanner scanner = new Scanner(source); ErrorReporter reporter = new ErrorReporter(true); Parser parser = new Parser(scanner, reporter); @@ -63,7 +64,8 @@ public class TestScanner { } private void compileExpectFailure(String filename) { - SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString()); + //SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString()); + SourceFile source = SourceFile.fromResource(filename); Scanner scanner = new Scanner(source); ErrorReporter reporter = new ErrorReporter(true); Parser parser = new Parser(scanner, reporter);