Skip to content

Commit

Permalink
Create sub-symbol tables on declaration statements
Browse files Browse the repository at this point in the history
The entry and exit of a class, constructor, or method
declarations should have a new symbol table.

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb committed Apr 10, 2024
1 parent 1f9cadd commit 0bf3056
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions precli/parsers/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def visit_import_declaration(self, nodes: list[Node]):
symbol = package.split(".")[-1]
self.current_symtab.put(symbol, tokens.IMPORT, package)

def visit_class_declaration(self, nodes: list[Node]):
class_id = self.child_by_type(self.context["node"], tokens.IDENTIFIER)
cls_name = class_id.text.decode()
self.current_symtab = SymbolTable(cls_name, parent=self.current_symtab)
self.visit(nodes)
self.current_symtab = self.current_symtab.parent()

def visit_constructor_declaration(self, nodes: list[Node]):
const_id = self.child_by_type(self.context["node"], tokens.IDENTIFIER)
cst_name = const_id.text.decode()
self.current_symtab = SymbolTable(cst_name, parent=self.current_symtab)
self.visit(nodes)
self.current_symtab = self.current_symtab.parent()

def visit_method_declaration(self, nodes: list[Node]):
method_id = self.child_by_type(self.context["node"], tokens.IDENTIFIER)
mth_name = method_id.text.decode()
self.current_symtab = SymbolTable(mth_name, parent=self.current_symtab)
self.visit(nodes)
self.current_symtab = self.current_symtab.parent()

def _get_var_node(self, node: Node) -> Node:
if (
len(node.named_children) >= 2
Expand Down

0 comments on commit 0bf3056

Please sign in to comment.