parser prints line numbers AND gives an error if you use anonymous objects/links in LHS of a rule

This commit is contained in:
Joeri Exelmans 2024-12-11 20:32:55 +01:00
parent c7288635f8
commit 6314506ac0
2 changed files with 102 additions and 46 deletions

View file

@ -5,13 +5,14 @@ from concrete_syntax.common import indent
from transformation.rule import Rule
# parse model and check conformance
def parse_and_check(state, m_cs, mm, descr: str, check_conformance=True, type_transform=lambda type_name: type_name):
def parse_and_check(state, m_cs, mm, descr: str, check_conformance=True, type_transform=lambda type_name: type_name, name_generator=parser.DefaultNameGenerator()):
try:
m = parser.parse_od(
state,
m_text=m_cs,
mm=mm,
type_transform=type_transform,
name_generator=name_generator,
)
except Exception as e:
e.add_note("While parsing model " + descr)
@ -35,6 +36,11 @@ def read_file(filename):
KINDS = ["nac", "lhs", "rhs"]
# Phony name generator that raises an error if you try to use it :)
class LHSNameGenerator:
def __call__(self, type_name):
raise Exception(f"Error: Object or link of type '{type_name}' does not have a name.\nAnonymous objects/links are not allowed in the LHS of a rule, because they can have unintended consequences. Please give all of the elements in the LHS explicit names.")
# load model transformation rules
def load_rules(state, get_filename, rt_mm_ramified, rule_names, check_conformance=True):
rules = {}
@ -62,9 +68,12 @@ def load_rules(state, get_filename, rt_mm_ramified, rule_names, check_conformanc
if suffix == "":
print(f"Warning: rule {rule_name} has no NAC ({filename} not found)")
return nacs
elif kind == "lhs" or kind == "rhs":
else:
try:
m = parse_and_check(state, read_file(filename), rt_mm_ramified, descr, check_conformance)
if kind == "lhs":
m = parse_and_check(state, read_file(filename), rt_mm_ramified, descr, check_conformance, name_generator=LHSNameGenerator())
elif kind == "rhs":
m = parse_and_check(state, read_file(filename), rt_mm_ramified, descr, check_conformance)
files_read.append(filename)
return m
except FileNotFoundError as e: