Files
UNI-PROG3-CW2-DYOPL/Q2/Artefacts/fspowVisitorUser.py
T
2026-04-28 20:06:29 +01:00

88 lines
2.9 KiB
Python

from antlr4 import *
from fspowParser import fspowParser
from fspowVisitor import fspowVisitor
from repertoire.FileCollection import FileCollection
from repertoire.Selector import Selector
class fspowVisitorUser(fspowVisitor):
def __init__(self):
# Dictionary to track assigned variables during the AST walk
self.variablesTable = {}
def visitMessage(self, ctx):
# Quotes are already stripped by the inline lexer action
print(ctx.STRING().getText())
return self.visitChildren(ctx)
def visitFcCreation(self, ctx):
root_name = self.visit(ctx.rootSpecifier())
return FileCollection(root_name)
def visitRootSpecifier(self, ctx):
return ctx.STRING().getText()
def visitAssignment(self, ctx):
# Maps string identifiers to their evaluated backend objects
var_id = ctx.ID().getText()
self.variablesTable[var_id] = self.visit(ctx.expression())
return True
def visitFcList(self, ctx):
var_id = ctx.getChild(0).getText()
if var_id in self.variablesTable:
print(self.variablesTable[var_id].list())
else:
print(f"File collection {var_id} not found")
def visitFcApplySelector(self, ctx):
fc_id = ctx.ID().getText()
if fc_id in self.variablesTable:
sel_id = ctx.expression().getText()
if sel_id in self.variablesTable:
self.variablesTable[fc_id].apply(self.variablesTable[sel_id])
else:
print(f"Selector {sel_id} not found")
else:
print(f"File collection {fc_id} not found")
def visitSelCreation(self, ctx):
# Triggers the AST walk to process the complex filter expression
return self.visit(ctx.testexpression())
def visitExprID(self, ctx):
var_id = ctx.ID().getText()
return self.variablesTable.get(var_id)
# --- Filter List ---
def visitTestexpression(self, ctx):
# Dynamically builds the recursive list, encoding 'intersect' as 1
expr_list = []
for i in range(ctx.getChildCount()):
child = ctx.getChild(i)
if child.getText() == 'intersect':
expr_list.append(1)
else:
expr_list.append(self.visit(child))
return expr_list
def visitTestunitnottestexpression(self, ctx):
# Appends -1 to mathematically encode the 'not' inversion
return [-1, self.visit(ctx.testexpression())]
def visitTestunittestexpression(self, ctx):
return self.visit(ctx.testexpression())
def visitTestunitname(self, ctx):
return {"name": ctx.STRING().getText()}
def visitTestunitsize(self, ctx):
return {"size": ctx.STRING().getText()}
def visitTestunittype(self, ctx):
return {"type": ctx.STRING().getText()}
def visitTestunitdate(self, ctx):
return {"date": ctx.STRING().getText()}