From 432c5cea77fbb025232a27a4ad9e14fc03dafa81 Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Mon, 24 Jul 2023 15:40:52 +0200 Subject: [PATCH 1/2] Add Type names --- ttcn3/types/types.go | 29 ++++++++++++++++++++++++++++- ttcn3/types/types_test.go | 10 +++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 748315ba..2aa35d8f 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -134,6 +134,7 @@ func (k Kind) String() string { // A Type represents a TTCN-3 type. type Type interface { String() string + string() string } // A PrimitiveType represents a non-composite type, such as integer, boolean, @@ -146,6 +147,13 @@ type PrimitiveType struct { } func (t *PrimitiveType) String() string { + if t.Name != "" && t.Name != t.Kind.String() { + return t.Name + " [" + t.string() + "]" + } + return t.string() +} + +func (t *PrimitiveType) string() string { if t.ValueConstraints == nil { return t.Kind.String() } @@ -179,9 +187,16 @@ type ListType struct { } func (t *ListType) String() string { + if t.Name != "" && t.Name != t.Kind.String() { + return t.Name + " [" + t.string() + "]" + } + return t.string() +} + +func (t *ListType) string() string { elem := "any" if t.ElementType != nil && !isString(t.Kind) { - elem = t.ElementType.String() + elem = t.ElementType.string() } switch t.Kind { case RecordOf, Any: @@ -242,6 +257,10 @@ func (t *StructuredType) String() string { return "" } +func (t *StructuredType) string() string { + return t.String() +} + // A Field represents a fields in structures types. type Field struct { Type @@ -270,6 +289,10 @@ func (t *BehaviourType) String() string { return "" } +func (t *BehaviourType) string() string { + return t.String() +} + // A PairType represents a pair. Pairs are not specified by TTCN-3 standard explicitly. It is for modeling map types as // a set of key-value-pairs. type PairType struct { @@ -288,6 +311,10 @@ func (t *PairType) String() string { return strings.Join(res, " to ") } +func (t *PairType) string() string { + return t.String() +} + // A Value represents a single value constraint, such as '1' or '10..20'. type Value struct { syntax.Expr diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 12f4c549..b388f34c 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -238,7 +238,7 @@ func TestTypeStrings(t *testing.T) { Kind: types.Array, ElementType: &types.ListType{ Kind: types.SetOf, - ElementType: types.Float, + ElementType: types.Predefined["float"], }}, }, }, @@ -247,13 +247,13 @@ func TestTypeStrings(t *testing.T) { // Named types - {skip: true, Output: "foo [any]", + {Output: "foo [any]", Type: &types.PrimitiveType{Name: "foo"}}, - {skip: true, Output: "foo [record of any]", + {Output: "foo [record of any]", Type: &types.ListType{Name: "foo"}}, - {skip: true, Output: "foo [record of integer]", + {Output: "foo [record of integer]", Type: &types.ListType{ Name: "foo", ElementType: &types.PrimitiveType{ @@ -262,7 +262,7 @@ func TestTypeStrings(t *testing.T) { }, }}, - {skip: true, Output: "record of integer", + {Output: "record of integer", Type: &types.ListType{ ElementType: &types.PrimitiveType{ Name: "foo", From 8e20761c2bc3d2018c262e64d495197291435565 Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:21:43 +0200 Subject: [PATCH 2/2] Handle TypeNames of Predefined Types --- ttcn3/types/types.go | 60 ++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 2aa35d8f..0db274e6 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -134,7 +134,7 @@ func (k Kind) String() string { // A Type represents a TTCN-3 type. type Type interface { String() string - string() string + descriptionString() string } // A PrimitiveType represents a non-composite type, such as integer, boolean, @@ -147,13 +147,19 @@ type PrimitiveType struct { } func (t *PrimitiveType) String() string { - if t.Name != "" && t.Name != t.Kind.String() { - return t.Name + " [" + t.string() + "]" + if _, ok := Predefined[t.Name]; ok { + return t.Name } - return t.string() + if t.Name == "" { + return t.descriptionString() + } + return t.Name + " [" + t.descriptionString() + "]" } -func (t *PrimitiveType) string() string { +func (t *PrimitiveType) descriptionString() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } if t.ValueConstraints == nil { return t.Kind.String() } @@ -187,30 +193,36 @@ type ListType struct { } func (t *ListType) String() string { - if t.Name != "" && t.Name != t.Kind.String() { - return t.Name + " [" + t.string() + "]" + if _, ok := Predefined[t.Name]; ok { + return t.Name + } + if t.Name == "" { + return t.descriptionString() } - return t.string() + return t.Name + " [" + t.descriptionString() + "]" } -func (t *ListType) string() string { +func (t *ListType) descriptionString() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } elem := "any" if t.ElementType != nil && !isString(t.Kind) { - elem = t.ElementType.string() + elem = t.ElementType.descriptionString() } switch t.Kind { case RecordOf, Any: - var lengthConstraint string = " " + var lengthConstraint string = "" if t.LengthConstraint.Expr != nil { - lengthConstraint = " length(" + t.LengthConstraint.String() + ") " + lengthConstraint = "length(" + t.LengthConstraint.String() + ") " } - return "record" + lengthConstraint + "of " + elem + return "record " + lengthConstraint + "of " + elem case SetOf: - var lengthConstraint string = " " + var lengthConstraint string = "" if t.LengthConstraint.Expr != nil { - lengthConstraint = " length(" + t.LengthConstraint.String() + ") " + lengthConstraint = "length(" + t.LengthConstraint.String() + ") " } - return "set" + lengthConstraint + "of " + elem + return "set " + lengthConstraint + "of " + elem case Map: if _, ok := t.ElementType.(*PairType); !ok { return "map from " + elem + " to any" @@ -257,7 +269,7 @@ func (t *StructuredType) String() string { return "" } -func (t *StructuredType) string() string { +func (t *StructuredType) descriptionString() string { return t.String() } @@ -289,7 +301,7 @@ func (t *BehaviourType) String() string { return "" } -func (t *BehaviourType) string() string { +func (t *BehaviourType) descriptionString() string { return t.String() } @@ -300,21 +312,21 @@ type PairType struct { } func (t *PairType) String() string { + return t.descriptionString() +} + +func (t *PairType) descriptionString() string { res := []string{"any", "any"} if t.First != nil { - res[0] = t.First.String() + res[0] = t.First.descriptionString() } if t.Second != nil { - res[1] = t.Second.String() + res[1] = t.Second.descriptionString() } return strings.Join(res, " to ") } -func (t *PairType) string() string { - return t.String() -} - // A Value represents a single value constraint, such as '1' or '10..20'. type Value struct { syntax.Expr