Skip to content

Commit

Permalink
Detecting duplicated components
Browse files Browse the repository at this point in the history
Add check to detect duplicated components of a record
when parsing a TRLC file.

Example:
```
Something duplicated
{
	description = "This is fine!"
	description = "This is the duplicate."
}
```

The behavior of the `Record_Object` class is changed such that the `assign` method only
assigns components to the record if it has not been assigned already.
This is implemented by checking if the field type is different from `Implicit_Null`.

The `Parser` class asks the `Record_Object` if the component is `Implicit_Null`.
If no, then an error is sent to the message handler.
  • Loading branch information
phiwuu committed Dec 12, 2024
1 parent 629f9ec commit debad86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions trlc/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,9 @@ def to_python_dict(self):
"""
return {name: value.to_python_object()
for name, value in self.field.items()}

def is_component_implicit_null(self, component) -> bool:
return not isinstance(self.field[component.name], Implicit_Null)

def assign(self, component, value):
assert isinstance(component, Composite_Component)
Expand All @@ -3008,6 +3011,8 @@ def assign(self, component, value):
Implicit_Null,
Unary_Expression)), \
"value is %s" % value.__class__.__name__
if self.is_component_implicit_null(component):
raise KeyError(f"Component {component.name} already assigned to {self.n_typ.name} {self.name}!")
self.field[component.name] = value

def dump(self, indent=0): # pragma: no cover
Expand Down
5 changes: 5 additions & 0 deletions trlc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,11 @@ def parse_record_object_declaration(self):
comp = r_typ.components.lookup(self.mh,
self.ct,
ast.Composite_Component)
if obj.is_component_implicit_null(comp):
self.mh.error(self.ct.location,
"component '%s' already assigned at line %i" %
(comp.name,
obj.field[comp.name].location.line_no))
comp.set_ast_link(self.ct)
if r_typ.is_frozen(comp):
self.mh.error(self.ct.location,
Expand Down

0 comments on commit debad86

Please sign in to comment.