summaryrefslogtreecommitdiff
path: root/src/ParseAll.java
blob: 7def6f4522c24cfa58e849a14989e6af6e682ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.File;
import java.util.ArrayList;
import java.util.Objects;
import org.antlr.v4.runtime.*;

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();
        for (File file : Objects.requireNonNull(new File("./progs").listFiles())) {
            String fileStr = file.getPath();
            // fileStr = "./progs/wrong.py";
            try {

                if (!file.isFile() || !Share.getExtension(file.getName()).equals("py")) {
                    System.err.println("Wont parse: " + fileStr);
                    continue;
                }

                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.toStringTree();
                // System.out.println(treeStr);

                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) {
                e.printStackTrace();
                System.err.println(fileStr);
            }
        }
    }
}