diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-05-28 11:09:49 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-05-28 11:11:06 +0200 |
commit | 4dc413b059983a1b50bd781fd88e0999044f66c8 (patch) | |
tree | 8e488b1e9c01a4de0f9304fd8b3cabb62d00bc6d /src/Main.java | |
parent | 4de1659abf7fb73487632a8f3ee9d5febc5ee43e (diff) |
Format
Diffstat (limited to 'src/Main.java')
-rw-r--r-- | src/Main.java | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/src/Main.java b/src/Main.java index 7fc9494..02bc8f6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,60 +1,56 @@ -import java.io.InputStream; -import java.io.IOException; import java.io.BufferedReader; +import java.io.File; import java.io.FileReader; -import java.util.Objects; +import java.io.IOException; import java.util.Arrays; -import java.io.File; -import java.io.FileInputStream; +import java.util.Objects; +import javax.swing.*; +import org.antlr.v4.gui.TreeViewer; import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; -import org.antlr.v4.gui.TreeViewer; -import javax.swing.*; - public class Main { - public static void main(String[] args) { - for (File file: Objects.requireNonNull(new File("./progs/").listFiles())) { - try { - String fileStr = file.getPath(); - fileStr = "./progs/test.py"; - System.out.println(fileStr); + public static void main(String[] args) { + for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) { + try { + String fileStr = file.getPath(); + fileStr = "./progs/test.py"; + System.out.println(fileStr); - System.out.println(readFile(fileStr)); + System.out.println(readFile(fileStr)); - CharStream cs = CharStreams.fromFileName(fileStr); - Python3Lexer lexer = new Python3Lexer(cs); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - Python3Parser parser = new Python3Parser(tokenStream); - Python3Parser.RootContext tree = parser.root(); -// String treeStr = tree.toString(); -// System.out.println(treeStr); -// // Visualize the parse tree - JFrame frame = new JFrame("Parse Tree"); - JPanel panel = new JPanel(); - TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree); - viewer.setScale(1.5); // Zoom factor - panel.add(viewer); - frame.add(panel); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(800, 600); - frame.setVisible(true); - break; - } catch (Exception e) { - e.printStackTrace(); - } - } - } - 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(); - } + CharStream cs = CharStreams.fromFileName(fileStr); + Python3Lexer lexer = new Python3Lexer(cs); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + Python3Parser parser = new Python3Parser(tokenStream); + Python3Parser.RootContext tree = parser.root(); + // String treeStr = tree.toString(); + // System.out.println(treeStr); + // Visualize the parse tree + JFrame frame = new JFrame("Parse Tree"); + JPanel panel = new JPanel(); + TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree); + viewer.setScale(1.5); // Zoom factor + panel.add(viewer); + frame.add(panel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setVisible(true); + break; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + 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(); + } } - |