Skip to content

Commit

Permalink
Remove extend method in RecordFluxError
Browse files Browse the repository at this point in the history
Ref. #748
  • Loading branch information
Alexander Senier committed Aug 25, 2021
1 parent 5e56237 commit 173e37e
Show file tree
Hide file tree
Showing 15 changed files with 1,273 additions and 873 deletions.
2 changes: 1 addition & 1 deletion rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def parse(files: Sequence[Path], skip_verification: bool = False) -> Model:

for f in files:
if not f.is_file():
error.append(f'file not found: "{f}"', Subsystem.CLI, Severity.ERROR)
error.extend([(f'file not found: "{f}"', Subsystem.CLI, Severity.ERROR, None)])
continue

present_files.append(Path(f))
Expand Down
28 changes: 16 additions & 12 deletions rflx/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,22 @@ def check_type(
if ID(r.field) == self.expression.selector and r.sdu.is_compatible(declaration_type):
break
else:
error.append(
f'invalid renaming to "{self.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
error.append(
f'refinement for message "{self.expression.prefix.type_.identifier}"'
" would make operation legal",
Subsystem.MODEL,
Severity.INFO,
self.location,
error.extend(
[
(
f'invalid renaming to "{self.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
self.location,
),
(
f'refinement for message "{self.expression.prefix.type_.identifier}"'
" would make operation legal",
Subsystem.MODEL,
Severity.INFO,
self.location,
),
],
)
return error + self.expression.check_type(rty.OPAQUE)

Expand Down
18 changes: 2 additions & 16 deletions rflx/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,6 @@ def __iadd__(self: Self, other: object) -> Self:
def errors(self) -> Deque["BaseError.Entry"]:
return self.__errors

def append(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.append(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def appendleft(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.appendleft(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def extend(
self,
entries: Union[List[Tuple[str, Subsystem, Severity, Optional[Location]]], "BaseError"],
Expand Down Expand Up @@ -220,7 +206,7 @@ def _fail(
severity: Severity = Severity.ERROR,
location: Location = None,
) -> NoReturn:
error.append(message, subsystem, severity, location)
error.extend([(message, subsystem, severity, location)])
error.propagate()
assert False

Expand All @@ -232,5 +218,5 @@ def warn(
location: Location = None,
) -> None:
e = RecordFluxError()
e.append(message, subsystem, severity, location)
e.extend([(message, subsystem, severity, location)])
print(e)
198 changes: 122 additions & 76 deletions rflx/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,15 @@ def _check_type_subexpr(self) -> RecordFluxError:
error = self.prefix.prefix.check_type_instance(rty.Message)
else:
error = RecordFluxError()
error.append(
"invalid prefix for attribute Present",
Subsystem.MODEL,
Severity.ERROR,
self.location,
error.extend(
[
(
"invalid prefix for attribute Present",
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
]
)
return error

Expand All @@ -1218,11 +1222,15 @@ def __init__(self, prefix: Union[StrID, Expr], negative: bool = False) -> None:
def _check_type_subexpr(self) -> RecordFluxError:
error = self.prefix.check_type_instance(rty.Composite)
if not isinstance(self.prefix, (Variable, Selected)):
error.append(
"prefix of attribute Head must be a name",
Subsystem.MODEL,
Severity.ERROR,
self.prefix.location,
error.extend(
[
(
"prefix of attribute Head must be a name",
Subsystem.MODEL,
Severity.ERROR,
self.prefix.location,
)
],
)
return error

Expand Down Expand Up @@ -1331,14 +1339,18 @@ def _check_type_subexpr(self) -> RecordFluxError:
if self.selector in self.prefix.type_.fields:
self.type_ = self.prefix.type_.field_types[self.selector]
else:
error.append(
f'invalid field "{self.selector}" for {self.prefix.type_}',
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
error.extend(
_similar_field_names(self.selector, self.prefix.type_.fields, self.location)
[
(
f'invalid field "{self.selector}" for {self.prefix.type_}',
Subsystem.MODEL,
Severity.ERROR,
self.location,
),
*_similar_field_names(
self.selector, self.prefix.type_.fields, self.location
),
]
)
self.type_ = rty.Any()
else:
Expand Down Expand Up @@ -1405,19 +1417,27 @@ def _check_type_subexpr(self) -> RecordFluxError:

if self.type_ != rty.Undefined():
if len(self.args) < len(self.argument_types):
error.append(
"missing function arguments",
Subsystem.MODEL,
Severity.ERROR,
self.location,
error.extend(
[
(
"missing function arguments",
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
],
)

if len(self.args) > len(self.argument_types):
error.append(
"too many function arguments",
Subsystem.MODEL,
Severity.ERROR,
self.location,
error.extend(
[
(
"too many function arguments",
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
],
)

return error
Expand Down Expand Up @@ -2035,26 +2055,38 @@ def _check_type_subexpr(self) -> RecordFluxError:
if self.argument_types:
error += self.argument.prefix.check_type(tuple(self.argument_types))
else:
error.append(
f'invalid conversion to "{self.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
self.location,
error.extend(
[
(
f'invalid conversion to "{self.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
],
)
if isinstance(self.argument.prefix.type_, rty.Message):
error.append(
f'refinement for message "{self.argument.prefix.type_.identifier}"'
" would make operation legal",
Subsystem.MODEL,
Severity.INFO,
self.location,
error.extend(
[
(
f'refinement for message "{self.argument.prefix.type_.identifier}"'
" would make operation legal",
Subsystem.MODEL,
Severity.INFO,
self.location,
)
],
)
else:
error.append(
"invalid argument for conversion, expected message field",
Subsystem.MODEL,
Severity.ERROR,
self.argument.location,
error.extend(
[
(
"invalid argument for conversion, expected message field",
Subsystem.MODEL,
Severity.ERROR,
self.argument.location,
)
],
)

return error
Expand Down Expand Up @@ -2224,13 +2256,17 @@ def _check_type_subexpr(self) -> RecordFluxError:

for i, (field, expr) in enumerate(self.field_values.items()):
if field not in self.type_.fields:
error.append(
f'invalid field "{field}" for {self.type_}',
Subsystem.MODEL,
Severity.ERROR,
field.location,
error.extend(
[
(
f'invalid field "{field}" for {self.type_}',
Subsystem.MODEL,
Severity.ERROR,
field.location,
),
*_similar_field_names(field, self.type_.fields, field.location),
]
)
error.extend(_similar_field_names(field, self.type_.fields, field.location))
continue

field_type = self.type_.field_types[field]
Expand All @@ -2249,27 +2285,37 @@ def _check_type_subexpr(self) -> RecordFluxError:
}

if not field_combinations:
error.append(
f'invalid position for field "{field}" of {self.type_}',
Subsystem.MODEL,
Severity.ERROR,
field.location,
error.extend(
[
(
f'invalid position for field "{field}" of {self.type_}',
Subsystem.MODEL,
Severity.ERROR,
field.location,
)
],
)
break

if field_combinations and all(len(c) > len(self.field_values) for c in field_combinations):
error.append(
f"missing fields for {self.type_}",
Subsystem.MODEL,
Severity.ERROR,
self.location,
)
error.append(
"possible next fields: "
+ ", ".join(unique(c[len(self.field_values)] for c in sorted(field_combinations))),
Subsystem.MODEL,
Severity.INFO,
self.location,
error.extend(
[
(
f"missing fields for {self.type_}",
Subsystem.MODEL,
Severity.ERROR,
self.location,
),
(
"possible next fields: "
+ ", ".join(
unique(c[len(self.field_values)] for c in sorted(field_combinations))
),
Subsystem.MODEL,
Severity.INFO,
self.location,
),
],
)

return error
Expand Down Expand Up @@ -2435,20 +2481,20 @@ def _entity_name(expr: Expr) -> str:

def _similar_field_names(
field: ID, fields: Iterable[ID], location: Optional[Location]
) -> RecordFluxError:
) -> List[Tuple[str, Subsystem, Severity, Optional[Location]]]:
field_similarity = sorted(
((f, difflib.SequenceMatcher(None, str(f), str(field)).ratio()) for f in sorted(fields)),
key=lambda x: x[1],
reverse=True,
)
similar_fields = [f for f, s in field_similarity if s >= 0.5]

error = RecordFluxError()
if similar_fields:
error.append(
"similar field names: " + ", ".join(str(f) for f in similar_fields),
Subsystem.MODEL,
Severity.INFO,
location,
)
return error
return [
(
"similar field names: " + ", ".join(str(f) for f in similar_fields),
Subsystem.MODEL,
Severity.INFO,
location,
)
]
return []
20 changes: 12 additions & 8 deletions rflx/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ def __init__(

error = RecordFluxError()
if not self._parts:
error.append("empty identifier", Subsystem.ID, Severity.ERROR, location)
error.extend([("empty identifier", Subsystem.ID, Severity.ERROR, location)])
elif "" in self._parts:
error.append(
f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location
error.extend(
[(f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location)]
)
else:
for c in [" ", ".", ":"]:
if any(c in part for part in self._parts):
error.append(
f'"{c}" in identifier parts of "{self}"',
Subsystem.ID,
Severity.ERROR,
location,
error.extend(
[
(
f'"{c}" in identifier parts of "{self}"',
Subsystem.ID,
Severity.ERROR,
location,
)
],
)
error.propagate()

Expand Down
Loading

0 comments on commit 173e37e

Please sign in to comment.