diff options
Diffstat (limited to 'src/ParseAll.java')
-rw-r--r-- | src/ParseAll.java | 90 |
1 files changed, 23 insertions, 67 deletions
diff --git a/src/ParseAll.java b/src/ParseAll.java index 3e5d868..7def6f4 100644 --- a/src/ParseAll.java +++ b/src/ParseAll.java @@ -1,16 +1,19 @@ -import java.io.BufferedReader; + import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Objects; -import javax.swing.*; -import org.antlr.v4.gui.TreeViewer; import org.antlr.v4.runtime.*; -import parser.*; +import ast.Python3VisitorImpl; +import ast.nodes.*; +import parser.Python3Lexer; +import parser.Python3Parser; +import semanticanalysis.SemanticError; +import semanticanalysis.SymbolTable; +import semanticanalysis.Share; public class ParseAll { + @SuppressWarnings("unused") public static void main(String[] args) { new File("./trees/").mkdirs(); @@ -19,15 +22,11 @@ public class ParseAll { // fileStr = "./progs/wrong.py"; try { - if (!file.isFile() || !getExtension(file.getName()).equals("py")) { + if (!file.isFile() || !Share.getExtension(file.getName()).equals("py")) { System.err.println("Wont parse: " + fileStr); continue; - } else { - System.out.println(fileStr); } - // System.out.println(readFile(fileStr)); - CharStream cs = CharStreams.fromFileName(fileStr); Python3Lexer lexer = new Python3Lexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(lexer); @@ -37,11 +36,18 @@ public class ParseAll { String treeStr = tree.toStringTree(); // System.out.println(treeStr); - TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree); - viewer.setScale(1.5); - saveTree(viewer, "./trees/" + removeExtension(file.getName()) + ".png"); - if (parser.getNumberOfSyntaxErrors() != 0) { - System.err.println("Parse errors: " + fileStr); + Python3VisitorImpl visitor = new Python3VisitorImpl(); + SymbolTable ST = new SymbolTable(); + Node ast = visitor.visit(tree); + ArrayList<SemanticError> errorsWithDup = ast.checkSemantics(ST, 0); + ArrayList<SemanticError> errors = Share.removeDuplicates(errorsWithDup); + if (!errors.isEmpty()) { + System.out.println(); + System.out.println(fileStr); + System.out.println("You had " + errors.size() + " errors:"); + for (SemanticError e : errors) { + System.out.println("\t" + e); + } } } catch (Exception e) { @@ -50,54 +56,4 @@ public class ParseAll { } } } - - @SuppressWarnings("unused") - private static String readFile(String filePath) throws IOException { - StringBuilder content = new StringBuilder(); - try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { - String line; - while ((line = reader.readLine()) != null) { - content.append(line).append("\n"); - } - } - return content.toString(); - } - - @SuppressWarnings("unused") - private static void showTree(TreeViewer viewer) { - JFrame frame = new JFrame("Parse Tree"); - JPanel panel = new JPanel(); - panel.add(viewer); - frame.add(panel); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(800, 600); - frame.setVisible(true); - } - - private static void saveTree(TreeViewer viewer, String name) { - try { - viewer.save(name); - } catch (Exception e) { - System.err.println(name); - e.printStackTrace(); - } - } - - public static String removeExtension(String fileName) { - int extensionIndex = fileName.lastIndexOf('.'); - if (extensionIndex == -1) { - return fileName; - } else { - return fileName.substring(0, extensionIndex); - } - } - - public static String getExtension(String fileName) { - int extensionIndex = fileName.lastIndexOf('.'); - if (extensionIndex == -1) { - return fileName; - } else { - return fileName.substring(extensionIndex + 1); - } - } } |