Skip to content

Commit

Permalink
Parsing for CHOICE extensions
Browse files Browse the repository at this point in the history
Adds proper handling for CHOICE extensions during parsing.
Code generation still needs to be updated.

Bug: #2
  • Loading branch information
chemikadze committed Oct 12, 2022
1 parent bf9fbb2 commit 805efdd
Show file tree
Hide file tree
Showing 3 changed files with 424 additions and 336 deletions.
1 change: 1 addition & 0 deletions asn1.y
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ ChoiceType : CHOICE OPEN_CURLY AlternativeTypeLists CLOSE_CURLY { $$ = $3 }

AlternativeTypeLists : AlternativeTypeList COMMA ExtensionAndException ExtensionAdditionAlternatives OptionalExtensionMarker { $$ = ChoiceType{$1,$4} }
| AlternativeTypeList { $$ = ChoiceType{AlternativeTypeList: $1} }
| ExtensionAndException ExtensionAdditionAlternatives OptionalExtensionMarker { $$ = ChoiceType{nil, $2} }
;

// defined in grammar, but screws up ExtensionAndException parsing
Expand Down
78 changes: 77 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,83 @@ func TestSequenceSyntax(t *testing.T) {
t.Skip(tc.skipReason)
}
r := testNotFails(t, tc.content)
if diff := cmp.Diff(r.ModuleBody.AssignmentList, tc.expected); diff != "" {
if diff := cmp.Diff(tc.expected, r.ModuleBody.AssignmentList); diff != "" {
t.Errorf("Module did not match expected, diff (-want, +got):\n%v", diff)
}
})
}
}

func TestChoiceSyntax(t *testing.T) {
testCases := []struct {
name string
content string
expected AssignmentList
skipReason string
}{
{
name: "choice with elements",
content: `
TestSpec DEFINITIONS ::= BEGIN
Choice ::= CHOICE {
alt1 BOOLEAN,
alt2 INTEGER
}
END
`,
expected: AssignmentList{
TypeAssignment{TypeReference: "Choice", Type: ChoiceType{AlternativeTypeList: []NamedType{
{Identifier: "alt1", Type: BooleanType{}},
{Identifier: "alt2", Type: IntegerType{}},
}}},
},
},
{
name: "choice extensions",
content: `
TestSpec DEFINITIONS ::= BEGIN
Choice ::= CHOICE { ... }
Choice2 ::= CHOICE {
alt1 BOOLEAN,
alt2 BOOLEAN,
...
}
Choice3 ::= CHOICE {
alt1 BOOLEAN,
...,
ext2 BOOLEAN,
ext3 BOOLEAN
}
END
`,
expected: AssignmentList{
TypeAssignment{TypeReference: "Choice", Type: ChoiceType{ExtensionTypes: []ChoiceExtension{}}},
TypeAssignment{TypeReference: "Choice2", Type: ChoiceType{
AlternativeTypeList: []NamedType{
{Identifier: "alt1", Type: BooleanType{}},
{Identifier: "alt2", Type: BooleanType{}},
},
ExtensionTypes: []ChoiceExtension{}},
},
TypeAssignment{TypeReference: "Choice3", Type: ChoiceType{
AlternativeTypeList: []NamedType{
{Identifier: "alt1", Type: BooleanType{}},
},
ExtensionTypes: []ChoiceExtension{
NamedType{Identifier: "ext2", Type: BooleanType{}},
NamedType{Identifier: "ext3", Type: BooleanType{}},
}},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipReason != "" {
t.Skip(tc.skipReason)
}
r := testNotFails(t, tc.content)
if diff := cmp.Diff(tc.expected, r.ModuleBody.AssignmentList); diff != "" {
t.Errorf("Module did not match expected, diff (-want, +got):\n%v", diff)
}
})
Expand Down
Loading

0 comments on commit 805efdd

Please sign in to comment.