forked from csaroff/MiniJava-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassNamer.java
More file actions
72 lines (66 loc) · 3.01 KB
/
Copy pathClassNamer.java
File metadata and controls
72 lines (66 loc) · 3.01 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeProperty;
import java.util.*;
/**
* A listener mechanism for creating and naming the klasses in the symbol
* table. When the ParseTreeWalker visits a node in the parse tree,
* it calls the enterRule method associated with that node.
* It then does the same thing to all children of that node.
* When the ParseTreeWalker exits a node, it calls the exitRule
* method associated with that node.
*
* This class is listening for the ParseTreeWalker to visit each node.
* When the ParseTreeWalker enters a class declaration, the
* enterClassDeclaration method is called. All children are visited
* in the same manner and then the exitClassDeclaration method is called.
*
* This results in an in-order traversal of the parse tree.
*/
public class ClassNamer extends MinijavaBaseListener {
//The symbol-table collection of classes
private Map<String, Klass> klasses;
//The parser that generated the parse tree that this listener
//is attached to. Useful for reporting the line number
//and column number of the offending token.
private MinijavaParser parser;
/**
* [ClassNamer description]
* @param klasses The symbol-table collection of classes
* @param parser The parser that generated the parse tree
* that this listener is attached to.
*/
public ClassNamer(Map<String, Klass> klasses, MinijavaParser parser){
this.klasses = klasses;
this.parser = parser;
}
/**
* Creates a klass in the symbol-table with the name derived from the
* context. Prints a duplicateClassError if the symbol-table already
* contains a klass with that name.
*/
@Override public void enterClassDeclaration(@NotNull MinijavaParser.ClassDeclarationContext ctx) {
Klass currentKlass = new Klass(ctx.Identifier(0).getText());
if(klasses.put(currentKlass.getScopeName(), currentKlass)!=null){
ErrorPrinter.printDuplicateClassError(parser, ctx.Identifier(0).getSymbol(), currentKlass.getScopeName());
}
}
/**
* Adds the main class along with the primitive types to the symbol-table
*/
@Override public void enterMainClass(@NotNull MinijavaParser.MainClassContext ctx) {
Klass currentKlass;
currentKlass = new Klass("int[]");
klasses.put(currentKlass.getScopeName(),currentKlass);
currentKlass = new Klass("int");
klasses.put(currentKlass.getScopeName(),currentKlass);
//System.out.println("Klasses.get(\"int\" = " + klasses.get("int"));
currentKlass = new Klass("boolean");
klasses.put(currentKlass.getScopeName(),currentKlass);
currentKlass = new Klass(ctx.Identifier(0).getText());
klasses.put(currentKlass.getScopeName(), currentKlass);
}
}