Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type names #697

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions ttcn3/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (k Kind) String() string {
// A Type represents a TTCN-3 type.
type Type interface {
String() string
descriptionString() string
}

// A PrimitiveType represents a non-composite type, such as integer, boolean,
Expand All @@ -146,6 +147,19 @@ type PrimitiveType struct {
}

func (t *PrimitiveType) String() string {
if _, ok := Predefined[t.Name]; ok {
return t.Name
}
if t.Name == "" {
return t.descriptionString()
}
return t.Name + " [" + t.descriptionString() + "]"
}

func (t *PrimitiveType) descriptionString() string {
if _, ok := Predefined[t.Name]; ok {
return t.Name
}
if t.ValueConstraints == nil {
return t.Kind.String()
}
Expand Down Expand Up @@ -179,23 +193,36 @@ type ListType struct {
}

func (t *ListType) String() string {
if _, ok := Predefined[t.Name]; ok {
return t.Name
}
if t.Name == "" {
return t.descriptionString()
}
return t.Name + " [" + t.descriptionString() + "]"
}

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"
Expand Down Expand Up @@ -242,6 +269,10 @@ func (t *StructuredType) String() string {
return ""
}

func (t *StructuredType) descriptionString() string {
return t.String()
}

// A Field represents a fields in structures types.
type Field struct {
Type
Expand Down Expand Up @@ -270,19 +301,27 @@ func (t *BehaviourType) String() string {
return ""
}

func (t *BehaviourType) descriptionString() 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 {
First, Second Type
}

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 ")
Expand Down
10 changes: 5 additions & 5 deletions ttcn3/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}},
},
},
Expand All @@ -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{
Expand All @@ -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",
Expand Down
Loading