Commit 4aeaa78f authored by Maxime Perrotin's avatar Maxime Perrotin

Add iterators for enumerated types

parent 7797eab0
......@@ -5,7 +5,9 @@ all:
@echo A testcase can be build from source with "make test"
install: uninstall
mkdir -p obj && sed 's,$$PREFIX,$(PREFIX),g' src/taste-asn1-iterators > obj/taste-asn1-iterators && chmod +x obj/taste-asn1-iterators
mkdir -p obj && rm -f obj/taste-asn1-iterators && \
sed 's,$$PREFIX,$(PREFIX),g' src/taste-asn1-iterators > obj/taste-asn1-iterators && \
chmod +x obj/taste-asn1-iterators
gprbuild -p asn1_iterators.gpr
gprinstall -p -XLIBRARY_TYPE=dynamic --build-name=shared --prefix=$(PREFIX) asn1_iterators.gpr
@echo Do not forget to export ADA_PROJECT_PATH=$(PREFIX)/share/gpr
......
package body ASN1_Iterators.Generic_Enumerated is
function Elem_Init return Sort is
begin
Is_Last := (Sort'First = Sort'Last);
return Sort'First;
end Elem_Init;
function Has_Elem (Value: Sort) return Boolean is
Result : constant Boolean := (not Is_Last and Value <= Sort'Last);
begin
if Value = Sort'Last then
Is_Last := True;
end if;
return Result;
end Has_Elem;
end;
with ASN1_Iterators.SimpleTypes,
ASN1_Iterators.Generic_Basic;
with Interfaces;
use Interfaces;
generic
type Sort is (<>); -- a discrete type, such as enumerated
package ASN1_Iterators.Generic_Enumerated is
Is_Last : Boolean := False;
-- function Elem_Init return Sort is (Sort'First);
function Elem_Init return Sort;
function Has_Elem (Value: Sort) return Boolean;
-- function Has_Elem (Value: Sort) return Boolean is (Value <= Sort'Last);
function Elem_First return Sort is (Sort'First);
function Elem_Next (Value: Sort) return Sort is
(if Is_Last then Sort'Last else Sort'Succ (Value));
package Discrete_Type is new SimpleTypes (Element => Sort,
Elem_Init => Elem_Init,
Has_Elem => Has_Elem,
Elem_First => Elem_First,
Elem_Next => Elem_Next);
package It is new Generic_Basic (P => Discrete_Type);
subtype Instance is It.Basic_ASN1_Iterator;
end;
......@@ -67,11 +67,11 @@ $sSubType$
sName may be Generic_Integer or Generic_Real
*/
MinMaxType(sName, sMin, sMax, bFixedSize, bIsUnsigned, bIsRealType) ::= <<
function Image(Elm: ASN1_Type) return String is (Elm'Img);
function Image (Elm : ASN1_Type) return String is (Elm'Img);
procedure To_ASN1(from: Interfaces.$if(bIsUnsigned)$Unsigned_64$else$$if(bIsRealType)$Long_Float$else$Integer_64$endif$$endif$; to: out ASN1_Type) is
procedure To_ASN1 (From : Interfaces.$if(bIsUnsigned)$Unsigned_64$else$$if(bIsRealType)$Long_Float$else$Integer_64$endif$$endif$; To : out ASN1_Type) is
begin
to := from;
To := From;
>>
......@@ -98,7 +98,6 @@ IA5StringType () ::= "IA5StringType"
NumericStringType () ::= "NumericStringType"
AssigOpNormalType () ::= "="
AssigOpSpecialType () ::= "::"
......@@ -115,17 +114,12 @@ AssigOpSpecialType () ::= "::"
nVal is the integer value of the enumerated, use it if needed
*/
EnumItem (sName, sCName, nVal, nLine, nPos, sCID) ::= <<
"$sName$": type("$sCID$", (object,), {
"IntValue": $nVal$, "Line": $nLine$, "CharPositionInLine": $nPos$, "EnumID": "$sCID$"
})
>>
/* Enumerated type: arrsItems is an array of EnumItem */
EnumType(arrsItems) ::= <<
"kind": "EnumeratedType", "Extensible": "False", "ValuesAutoCalculated": "False", "EnumValues": {
$arrsItems;separator=",\n"$
}
function Image (Elm : ASN1_Type) return String is (Elm'Img);
>>
......@@ -139,43 +133,32 @@ EnumType(arrsItems) ::= <<
sNamePresent is an unambiguous name to access the field
*/
ChoiceChild(sName, sCName, nLine, nPos, sChildContent, sNamePresent ) ::= <<
"$sName$": type("$sNamePresent$", (object,), {
"Line": $nLine$, "CharPositionInLine": $nPos$, "EnumID": "$sNamePresent$", "type": type("$sNamePresent$_type", (object,), {
$sChildContent$
})
})
>>
ChoiceType(arrsChildren) ::= <<
"kind": "ChoiceType", "Children": {
$arrsChildren;separator=",\n"$
-- $arrsChildren;separator=",\n"$
}
>>
/* Sequence (record) field. May be optional and may have a default value */
SequenceChild(sName, sCName, bOptional, sDefVal, nLine, nPos, sChildContent ) ::= <<
"$sName$": type("$sName$", (object,), {
"Optional": "$bOptional$"$if(sDefVal)$, "DefaultValue": "$sDefVal$"$endif$, "Line": $nLine$, "CharPositionInLine": $nPos$, "type": type("$sName$_type", (object,), {
$sChildContent$
})
})
>>
/* Full sequence (record) type */
SequenceType(arrsChildren) ::= <<
$arrsChildren;separator=",\n"$
-- $arrsChildren;separator=",\n"$
}
>>
/* Sequence of (array) */
SequenceOfType(sMin, sMax, sChild, bFixedSize) ::= <<
procedure To_ASN1(from: Inner.MySeqOf; to: out ASN1_Type) is
procedure To_ASN1(From: Inner.MySeqOf; to: out ASN1_Type) is
begin
for idx in 1..from.length loop
to.data(idx) := from.data(idx);
for Idx in 1 .. From.Length loop
To.Data (Idx) := From.Data (Idx);
end loop;
>>
......@@ -204,36 +187,37 @@ end;
// SeqOF may have fixed size, in that case there is no Length field
ContractExprSize(sPattern, sMin, sMax, bFixedSize) ::= <<
$if(!bFixedSize)$
to.length := from.length;
To.Length := From.Length;
$endif$
end;
function Image(Elm: ASN1_Type) return String is
function Image (Elm : ASN1_Type) return String is
$if(bFixedSize)$
Res : Unbounded_String;
begin
if Elm.Data'Length >= 1 then
Res := To_Unbounded_String(Image(Elm.Data(1)));
Res := To_Unbounded_String (Image (Elm.Data (1)));
end if;
if Elm.Data'Length > 1 then
for idx in 2..Elm.Data'Length loop
Res := Res & "," & Image(Elm.Data(Idx));
for Idx in 2 .. Elm.Data'Length loop
Res := Res & "," & Image (Elm.Data (Idx));
end loop;
end if;
return To_String("{" & Res & " }");
return To_String ("{" & Res & " }");
end;
$else$
function Image_rec(Elm: ASN1_Type) return String is
function Image_Rec (Elm : ASN1_Type) return String is
(if Elm.Length > 0 then
(Image(Elm.Data(1)) & (if Elm.Length > 1 then "," &
Image_Rec(ASN1_Type'(Length => Elm.Length-1,
Data => Elm.Data(2..Elm.Length) &
Elm.Data(1..Elm.Data'Length-Elm.Length+1)))
(Image (Elm.Data (1))
& (if Elm.Length > 1 then ","
& Image_Rec (ASN1_Type'(Length => Elm.Length-1,
Data => Elm.Data(2 .. Elm.Length) &
Elm.Data(1 .. Elm.Data'Length - Elm.Length + 1)))
else ""))
else "");
begin
return "{" & Image_rec(Elm) & " }";
return "{" & Image_Rec (Elm) & " }";
end;
$endif$
>>
......
......@@ -21,6 +21,7 @@ RootXml(arrsFiles) ::= <<
with ASN1_Ada_Iterators,
ASN1_Iterators.Generic_Integer,
ASN1_Iterators.Generic_Unsigned_Integer,
ASN1_Iterators.Generic_Enumerated,
ASN1_Iterators.Generic_SeqOf,
Interfaces,
Ada.Strings.Unbounded;
......@@ -71,7 +72,7 @@ package $sCName$_pkg is
subtype ASN1_Type is asn1Scc$sCName$;
package Inner is new $sType$
subtype Instance is Inner.Instance;
function Image(Elm: ASN1_Type) return String;
function Image (Elm : ASN1_Type) return String;
end;
>>
......@@ -92,14 +93,14 @@ $sSubType$
MinMaxType(sName, sMin, sMax, bFixedSize, bIsUnsigned, bIsRealType) ::= <<
Generic_$if(bIsUnsigned)$Unsigned_$endif$$sName$ (Min => $sMin$, Max => $sMax$);
package It renames Inner.It;
procedure To_ASN1(from: Interfaces.$if(bIsUnsigned)$Unsigned_64$else$$if(bIsRealType)$Long_Float$else$Integer_64$endif$$endif$; to: out ASN1_Type);
procedure To_ASN1(From : Interfaces.$if(bIsUnsigned)$Unsigned_64$else$$if(bIsRealType)$Long_Float$else$Integer_64$endif$$endif$; To : out ASN1_Type);
>>
/* String types with size constraint: Bitstring, Octetstring, Ia5String,
NumericString */
MinMaxType2(sName, sMin, sMax, bFixedSize) ::= <<
"kind": "$sName$", "Min": "$sMin$", "Max": "$sMax$"
-- "kind": "$sName$", "Min": "$sMin$", "Max": "$sMax$"
>>
BooleanType () ::= "Generic_Boolean"
......@@ -136,17 +137,12 @@ AssigOpSpecialType () ::= "::"
nVal is the integer value of the enumerated, use it if needed
*/
EnumItem (sName, sCName, nVal, nLine, nPos, sCID) ::= <<
"$sName$": type("$sCID$", (object,), {
"IntValue": $nVal$, "Line": $nLine$, "CharPositionInLine": $nPos$, "EnumID": "$sCID$"
})
>>
/* Enumerated type: arrsItems is an array of EnumItem */
EnumType(arrsItems) ::= <<
"kind": "EnumeratedType", "Extensible": "False", "ValuesAutoCalculated": "False", "EnumValues": {
$arrsItems;separator=",\n"$
}
Generic_Enumerated (Sort => ASN1_Type);
>>
......@@ -160,33 +156,20 @@ EnumType(arrsItems) ::= <<
sNamePresent is an unambiguous name to access the field
*/
ChoiceChild(sName, sCName, nLine, nPos, sChildContent, sNamePresent ) ::= <<
"$sName$": type("$sNamePresent$", (object,), {
"Line": $nLine$, "CharPositionInLine": $nPos$, "EnumID": "$sNamePresent$", "type": type("$sNamePresent$_type", (object,), {
$sChildContent$
})
})
>>
ChoiceType(arrsChildren) ::= <<
"kind": "ChoiceType", "Children": {
$arrsChildren;separator=",\n"$
}
>>
/* Sequence (record) field. May be optional and may have a default value */
SequenceChild(sName, sCName, bOptional, sDefVal, nLine, nPos, sChildContent ) ::= <<
"$sName$": type("$sName$", (object,), {
"Optional": "$bOptional$"$if(sDefVal)$, "DefaultValue": "$sDefVal$"$endif$, "Line": $nLine$, "CharPositionInLine": $nPos$, "type": type("$sName$_type", (object,), {
$sChildContent$
})
})
>>
/* Full sequence (record) type */
SequenceType(arrsChildren) ::= <<
$arrsChildren;separator=",\n"$
-- $arrsChildren;separator=",\n"$
}
>>
......@@ -195,7 +178,7 @@ SequenceType(arrsChildren) ::= <<
SequenceOfType(sMin, sMax, sChild, bFixedSize) ::= <<
Generic_SeqOf (Min => $sMin$, Max => $sMax$, Basic => $sChild$_Pkg.It);
use $sChild$_Pkg;
procedure To_ASN1(from: Inner.MySeqOf; to: out ASN1_Type);
procedure To_ASN1 (From : Inner.MySeqOf; To : out ASN1_Type);
>>
......
......@@ -7,6 +7,8 @@ MySeqOf ::= SEQUENCE (SIZE (3)) OF MyInteger
MyVarSeqOf ::= SEQUENCE (SIZE (1..4)) OF MyInteger
MyEnum ::= ENUMERATED { hello, world, how-are-you }
--MyChoice ::= CHOICE {
-- choice-a MyInteger,
-- choice-b MySeqOf
......
with ASN1_Ada_Iterators.iterators; -- Generated by asn1scc using custom stg file
use ASN1_Ada_Iterators.iterators;
with TASTE_DataView;
use TASTE_DataView;
......@@ -25,15 +26,17 @@ procedure test_generic is
use MyInteger_Pkg;
use MySeqOf_Pkg;
use MyVarSeqOf_Pkg;
use MyEnum_Pkg;
-- Test cases
MyIt : MyInteger_Pkg.Instance;
Fixed_SeqOf : MySeqOf_Pkg.Instance;
Var_SeqOf : MyVarSeqOf_Pkg.Instance;
Enum : MyEnum_Pkg.Instance;
SeqOfItm : asn1SccMySeqOf;
VarSeqOfItm : asn1SccMyVarSeqOf;
i: natural := 1;
i : Natural := 1;
begin
-- test: compute all possible values of the integer type
......@@ -49,7 +52,7 @@ begin
Put(Image(VarSeqOfItm));
i := (if i mod 10 = 0 then 1 else i + 1);
if i = 1 then
new_line;
New_Line;
end if;
end loop;
-- test: compute all combination of values for the variable-size array
......@@ -61,10 +64,14 @@ begin
Put(Image(SeqOfItm));
i := (if i mod 10 = 0 then 1 else i + 1);
if i = 1 then
new_line;
New_Line;
end if;
end loop;
New_Line;
Put_Line ("Enumerated type");
for Each of Enum loop
Put_Line (Image (Each));
end loop;
end;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment