From f9cbdb31b7981cc21b2f240c4d9c96dab7288a40 Mon Sep 17 00:00:00 2001 From: Marko Ristin Date: Sat, 31 Aug 2024 10:54:11 +0200 Subject: [PATCH] Update aas-core-codegen to 6df5c9e8 (#345) We update to [aas-core-codegen 6df5c9e8] for better type inference related to optional value types and nullness checks. [aas-core-codegen 6df5c9e8]: https://github.com/aas-core-works/aas-core-codegen/commit/6df5c9e8 --- htmlgen/for_metamodel.py | 2 +- htmlgen/transpilation.py | 102 +++++++++++++++++++++------------------ setup.py | 2 +- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/htmlgen/for_metamodel.py b/htmlgen/for_metamodel.py index eeff437..3c79c71 100644 --- a/htmlgen/for_metamodel.py +++ b/htmlgen/for_metamodel.py @@ -1301,7 +1301,7 @@ def _generate_page_for_verification_function( code_div, error = htmlgen.transpilation.transpile_body_of_verification( verification=verification, symbol_table=symbol_table, - environment=environment, + base_environment=environment, ) if error is not None: return None, error diff --git a/htmlgen/transpilation.py b/htmlgen/transpilation.py index 903a5a9..8497f04 100644 --- a/htmlgen/transpilation.py +++ b/htmlgen/transpilation.py @@ -10,6 +10,7 @@ Union, Set, Sequence, + cast, ) from aas_core_codegen import intermediate @@ -1074,10 +1075,7 @@ def __init__( ], environment: intermediate_type_inference.Environment, symbol_table: intermediate.SymbolTable, - verification: Union[ - intermediate.TranspilableVerification, - intermediate.PatternVerification, - ], + verification: intermediate.TranspilableVerification, ) -> None: """Initialize with the given values.""" htmlgen.transpilation._Transpiler.__init__( @@ -1132,7 +1130,7 @@ def transpile_body_of_verification( intermediate.ImplementationSpecificVerification, ], symbol_table: intermediate.SymbolTable, - environment: intermediate_type_inference.Environment, + base_environment: intermediate_type_inference.Environment, ) -> Tuple[Optional[Stripped], Optional[Error]]: """Transpile a verification function to HTML.""" parsed_body = None # type: Optional[Sequence[parse_tree.Node]] @@ -1152,50 +1150,62 @@ def transpile_body_of_verification( ), None, ) - else: assert_never(verification) assert parsed_body is not None assert not isinstance(verification, intermediate.ImplementationSpecificVerification) - canonicalizer = intermediate_type_inference.Canonicalizer() - for node in parsed_body: - _ = canonicalizer.transform(node) - - environment_with_args = intermediate_type_inference.MutableEnvironment( - parent=environment + environment = intermediate_type_inference.MutableEnvironment( + parent=base_environment ) - for arg in verification.arguments: - environment_with_args.set( - identifier=arg.name, - type_annotation=intermediate_type_inference.convert_type_annotation( - arg.type_annotation + if isinstance(verification, intermediate.PatternVerification): + # NOTE (mristin): + # This type is wrong for the ``re.match`` function. However, we just populate + # it here so that we do not have to re-implement the code highlighting for + # pattern verification. + environment.set( + Identifier("match"), + intermediate_type_inference.BuiltinFunctionTypeAnnotation( + intermediate_type_inference.BuiltinFunction( + name=Identifier("match"), + returns=intermediate_type_inference.OptionalTypeAnnotation( + value=intermediate_type_inference.PrimitiveTypeAnnotation( + a_type=intermediate_type_inference.PrimitiveType.BOOL + ) + ), + ) ), ) - type_inferrer = intermediate_type_inference.Inferrer( - symbol_table=symbol_table, - environment=environment_with_args, - representation_map=canonicalizer.representation_map, + transpilable_verification = cast( + intermediate.TranspilableVerification, verification + ) + elif isinstance(verification, intermediate.TranspilableVerification): + transpilable_verification = verification + else: + assert_never(verification) + raise AssertionError("Unexpected execution path") + + # fmt: off + type_inference, error = ( + intermediate_type_inference.infer_for_verification( + verification=transpilable_verification, + base_environment=environment + ) ) + # fmt: on - for node in verification.parsed.body: - _ = type_inferrer.transform(node) + if error is not None: + return None, error - if len(type_inferrer.errors): - return None, Error( - verification.parsed.node, - f"Failed to infer the types " - f"in the verification function {verification.name!r}", - type_inferrer.errors, - ) + assert type_inference is not None transpiler = _TranspilableVerificationTranspiler( - type_map=type_inferrer.type_map, - environment=environment_with_args, + type_map=type_inference.type_map, + environment=type_inference.environment_with_args, symbol_table=symbol_table, - verification=verification, + verification=transpilable_verification, ) body = [] # type: List[Stripped] @@ -1283,26 +1293,22 @@ def transpile_invariant( environment: intermediate_type_inference.Environment, ) -> Tuple[Optional[Stripped], Optional[Error]]: """Translate the invariant from the meta-model into HTML.""" - canonicalizer = intermediate_type_inference.Canonicalizer() - _ = canonicalizer.transform(invariant.body) - - type_inferrer = intermediate_type_inference.Inferrer( - symbol_table=symbol_table, - environment=environment, - representation_map=canonicalizer.representation_map, + # fmt: off + type_map, inference_error = ( + intermediate_type_inference.infer_for_invariant( + invariant=invariant, + environment=environment + ) ) + # fmt: on - _ = type_inferrer.transform(invariant.body) + if inference_error is not None: + return None, inference_error - if len(type_inferrer.errors): - return None, Error( - invariant.parsed.node, - "Failed to infer the types in the invariant", - type_inferrer.errors, - ) + assert type_map is not None transpiler = _InvariantTranspiler( - type_map=type_inferrer.type_map, + type_map=type_map, environment=environment, symbol_table=symbol_table, ) diff --git a/setup.py b/setup.py index 56c4f78..ea0be5f 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ "mypy==0.910", "pylint==2.17.0", "asttokens>=2.0.8,<3", - "aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@bd310fc2#egg=aas-core-codegen", + "aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@6df5c9e8#egg=aas-core-codegen", "astpretty==3.0.0", "pygments>=2,<3" ],