Skip to content

Commit da6d25f

Browse files
usr3-1415hakanurhan
authored andcommitted
issue esa#196
1 parent 0c58588 commit da6d25f

17 files changed

+176
-34
lines changed

Antlr/asn1.g

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ bitStringType
343343
;
344344

345345
bitStringItem
346-
: identifier a=L_PAREN (INT|valuereference) R_PAREN -> ^(NUMBER_LST_ITEM[$a] identifier INT? valuereference?)
346+
: identifier a=L_PAREN (INT|definedValue) R_PAREN -> ^(NUMBER_LST_ITEM[$a] identifier INT? definedValue?)
347347
;
348348

349349
booleanType

BackendAst/DAstTypeDefinition.fs

+15-2
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,24 @@ let createOctetString (r:Asn1AcnAst.AstRoot) (l:ProgrammingLanguage) (t:Asn1Acn
191191

192192
let createBitString (r:Asn1AcnAst.AstRoot) (l:ProgrammingLanguage) (t:Asn1AcnAst.Asn1Type) (o:Asn1AcnAst.BitString) (us:State) =
193193
let td = o.typeDef.[l]
194-
let define_new_bit_string = match l with C -> header_c.Define_new_bit_string | Ada -> header_a.Define_new_bit_string
194+
let define_new_bit_string = match l with C -> header_c.Define_new_bit_string | Ada -> header_a.Define_new_bit_string
195+
let define_named_bit = match l with C -> header_c.Define_new_bit_string_named_bit | Ada -> header_a.Define_new_bit_string_named_bit
196+
195197
let define_subType_bit_string = match l with C -> header_c.Define_subType_bit_string | Ada -> header_a.Define_subType_bit_string
196198
match td.kind with
197199
| NonPrimitiveNewTypeDefinition ->
198-
let completeDefintion = define_new_bit_string td (o.minSize.uper) (o.maxSize.uper) (o.minSize.uper = o.maxSize.uper) (BigInteger o.MaxOctets)
200+
let nblist =
201+
o.namedBitList |>
202+
List.filter(fun nb -> nb.resolvedValue < 64I) |>
203+
List.map(fun nb ->
204+
let hexValue =
205+
let aa = int nb.resolvedValue
206+
let hexVal = ((uint64 1) <<< aa)
207+
hexVal.ToString("X")
208+
let sComment = sprintf "(1 << %A)" nb.resolvedValue
209+
define_named_bit td (ToC (nb.Name.Value.ToUpper())) hexValue sComment
210+
)
211+
let completeDefintion = define_new_bit_string td (o.minSize.uper) (o.maxSize.uper) (o.minSize.uper = o.maxSize.uper) (BigInteger o.MaxOctets) nblist
199212
Some completeDefintion
200213
| NonPrimitiveNewSubTypeDefinition subDef ->
201214
let otherProgramUnit = if td.programUnit = subDef.programUnit then None else (Some subDef.programUnit)

BackendAst/PrintAsn1.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let rec PrintType (t:Asn1Type) (m:Asn1Module) (bPrintInSignleModule:bool) =
7676
|Integer -> stg_asn1.Print_Integer "" cons
7777
|Real -> stg_asn1.Print_Real cons
7878
|Boolean -> stg_asn1.Print_Boolean cons
79-
|BitString -> stg_asn1.Print_BitString cons
79+
|BitString _-> stg_asn1.Print_BitString cons
8080
|OctetString-> stg_asn1.Print_OctetString cons
8181
|NullType -> stg_asn1.Print_NullType cons
8282
|IA5String -> stg_asn1.Print_IA5String cons

CommonTypes/CommonTypes.fs

+18
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,21 @@ type AntlrParserResult = {
747747
type ContainedInOctOrBitString =
748748
| ContainedInOctString
749749
| ContainedInBitString
750+
751+
752+
type IntegerOrDefinedValue =
753+
| IDV_IntegerValue of IntLoc //integer literal i.e. 5
754+
| IDV_DefinedValue of (StringLoc*StringLoc) // reference to an integer value assignment defined in another module
755+
756+
type NamedBit0 = {
757+
Name:StringLoc
758+
_value : IntegerOrDefinedValue
759+
Comments: string array
760+
}
761+
762+
type NamedBit1 = {
763+
Name:StringLoc
764+
resolvedValue : BigInteger
765+
_value : IntegerOrDefinedValue
766+
Comments: string array
767+
}

FrontEndAst/AcnCreateFromAntlr.fs

+12-4
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,15 @@ let private mergeOctetStringType (asn1:Asn1Ast.AstRoot) (loc:SrcLoc) (acnErrLoc:
502502
let typeDef, us1 = getSizeableTypeDifition tdarg us
503503
{OctetString.acnProperties = acnProperties; cons = cons; withcons = withcons; minSize=minSize; maxSize =maxSize; uperMaxSizeInBits = uperMaxSizeInBits; uperMinSizeInBits=uperMinSizeInBits; acnEncodingClass = acnEncodingClass; acnMinSizeInBits=acnMinSizeInBits; acnMaxSizeInBits = acnMaxSizeInBits; typeDef=typeDef}, us1
504504

505-
let private mergeBitStringType (asn1:Asn1Ast.AstRoot) (loc:SrcLoc) (acnErrLoc: SrcLoc option) (props:GenericAcnProperty list) cons withcons (tdarg:GetTypeDifition_arg) (us:Asn1AcnMergeState) =
505+
let private mergeBitStringType (asn1:Asn1Ast.AstRoot) (namedBitList: NamedBit0 list) (loc:SrcLoc) (acnErrLoc: SrcLoc option) (props:GenericAcnProperty list) cons withcons (tdarg:GetTypeDifition_arg) (us:Asn1AcnMergeState) =
506+
let newNamedBitList =
507+
namedBitList |> List.map(fun nb ->
508+
let resolvedValue =
509+
match nb._value with
510+
| IDV_IntegerValue intVal -> intVal.Value
511+
| IDV_DefinedValue (mdVal, refVal) -> Asn1Ast.GetValueAsInt (Asn1Ast.GetBaseValue mdVal refVal asn1) asn1
512+
{NamedBit1.Name = nb.Name; _value = nb._value; resolvedValue = resolvedValue; Comments = nb.Comments})
513+
506514
let sizeUperRange = uPER.getBitStringUperRange cons loc
507515
let sizeUperAcnRange = uPER.getBitStringUperRange (cons@withcons) loc
508516
//let minSize, maxSize = uPER.getSizeMinAndMaxValue loc sizeUperRange
@@ -525,7 +533,7 @@ let private mergeBitStringType (asn1:Asn1Ast.AstRoot) (loc:SrcLoc) (acnErrLoc: S
525533
let acnEncodingClass, acnMinSizeInBits, acnMaxSizeInBits= AcnEncodingClasses.GetBitStringEncodingClass aligment loc acnProperties acnUperMinSizeInBits uperMaxSizeInBits minSize.acn maxSize.acn
526534

527535
let typeDef, us1 = getSizeableTypeDifition tdarg us
528-
{BitString.acnProperties = acnProperties; cons = cons; withcons = withcons; minSize=minSize; maxSize =maxSize; uperMaxSizeInBits = uperMaxSizeInBits; uperMinSizeInBits=uperMinSizeInBits; acnEncodingClass = acnEncodingClass; acnMinSizeInBits=acnMinSizeInBits; acnMaxSizeInBits = acnMaxSizeInBits; typeDef=typeDef}, us1
536+
{BitString.acnProperties = acnProperties; cons = cons; withcons = withcons; minSize=minSize; maxSize =maxSize; uperMaxSizeInBits = uperMaxSizeInBits; uperMinSizeInBits=uperMinSizeInBits; acnEncodingClass = acnEncodingClass; acnMinSizeInBits=acnMinSizeInBits; acnMaxSizeInBits = acnMaxSizeInBits; typeDef=typeDef; namedBitList = newNamedBitList}, us1
529537

530538
let private mergeNullType (acnErrLoc: SrcLoc option) (props:GenericAcnProperty list) (tdarg:GetTypeDifition_arg) (us:Asn1AcnMergeState) =
531539
let getRtlTypeName l = match l with C -> "", header_c.Declare_NullType (), "NULL" | Ada -> "adaasn1rtl", header_a.Declare_NULLNoRTL (), "NULL"
@@ -891,10 +899,10 @@ let rec private mergeType (asn1:Asn1Ast.AstRoot) (acn:AcnAst) (m:Asn1Ast.Asn1Mo
891899
let wcons = withCons |> List.collect fixConstraint |> List.map (ConstraintsMapping.getOctetStringConstraint asn1 t)
892900
let o, us1 = mergeOctetStringType asn1 t.Location acnErrLoc combinedProperties cons wcons tfdArg us
893901
OctetString o, us1
894-
| Asn1Ast.BitString ->
902+
| Asn1Ast.BitString namedBitList ->
895903
let cons = t.Constraints@refTypeCons |> List.collect fixConstraint |> List.map (ConstraintsMapping.getBitStringConstraint asn1 t)
896904
let wcons = withCons |> List.collect fixConstraint |> List.map (ConstraintsMapping.getBitStringConstraint asn1 t)
897-
let o, us1 = mergeBitStringType asn1 t.Location acnErrLoc combinedProperties cons wcons tfdArg us
905+
let o, us1 = mergeBitStringType asn1 namedBitList t.Location acnErrLoc combinedProperties cons wcons tfdArg us
898906
BitString o, us1
899907
| Asn1Ast.NullType ->
900908
let constraints = []

FrontEndAst/Asn1AcnAst.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ type BitString = {
355355
acnMinSizeInBits : BigInteger
356356
acnEncodingClass : SizeableAcnEncodingClass
357357
typeDef : Map<ProgrammingLanguage, FE_SizeableTypeDefinition>
358-
358+
namedBitList : NamedBit1 list
359359
}
360360

361361
type TimeType = {

FrontEndAst/Asn1Ast.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ and Asn1TypeKind =
134134
| OctetString
135135
| NullType
136136
| TimeType of TimeTypeClass
137-
| BitString
137+
| BitString of list<NamedBit0>
138138
| Boolean
139139
| ObjectIdentifier
140140
| RelativeObjectIdentifier
@@ -351,7 +351,7 @@ let rec getASN1Name (r:AstRoot) (t:Asn1Type) =
351351
| NumericString -> "NumericString"
352352
| OctetString -> "OCTET STRING"
353353
| NullType -> "NULL"
354-
| BitString -> "BIT STRING"
354+
| BitString _ -> "BIT STRING"
355355
| Boolean -> "BOOLEAN"
356356
| Enumerated _ -> "ENUMERATED"
357357
| SequenceOf _ -> "SEQUENCE OF"

FrontEndAst/CheckAsn1.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ let rec CheckIfVariableViolatesTypeConstraints (t:Asn1Type) (v:Asn1Value) ast =
285285
let bitOrOctSrt =
286286
match (Asn1Ast.GetActualType t ast).Kind with
287287
| OctetString -> Some TP_OCT_STR
288-
| BitString -> Some TP_BIT_STR
288+
| BitString _ -> Some TP_BIT_STR
289289
| _ -> None
290290
let ret = t.Constraints |> Seq.forall(fun c -> IsValueAllowed c v false bitOrOctSrt ast )
291291
match v.Kind, t.Kind with
@@ -548,7 +548,7 @@ let rec CheckType(t:Asn1Type) (m:Asn1Module) ast =
548548
| Some itm -> ()
549549
| None -> raise(SemanticError(t.Location, "The constraints defined for this type do not allow any value"))
550550

551-
| BitString -> ()
551+
| BitString _ -> ()
552552
| Integer -> ()
553553
| TimeType _ -> ()
554554
(* ++++

FrontEndAst/ConstraintsMapping.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ let rec getAnyConstraint (r:Asn1Ast.AstRoot) (t:Asn1Ast.Asn1Type) (c:Asn1Ast.Asn
334334
|Asn1Ast.OctetString -> OctetStringConstraint (getOctetStringConstraint r t c)
335335
|Asn1Ast.NullType -> NullConstraint
336336
|Asn1Ast.TimeType _ -> TimeConstraint (getTimeConstraint r t c)
337-
|Asn1Ast.BitString -> BitStringConstraint (getBitStringConstraint r t c)
337+
|Asn1Ast.BitString _ -> BitStringConstraint (getBitStringConstraint r t c)
338338
|Asn1Ast.Boolean -> BoolConstraint (getBoolConstraint r t c)
339339
|Asn1Ast.ObjectIdentifier -> ObjectIdConstraint(getObjectIdConstraint r t c)
340340
|Asn1Ast.RelativeObjectIdentifier -> ObjectIdConstraint(getObjectIdConstraint r t c)

FrontEndAst/CreateAsn1AstFromAntlrTree.fs

+35-12
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ let rec CreateType (tasParameters : TemplateParameter list) (acnTypeEncodingSpec
196196
| asn1Parser.SEQUENCE_TYPE -> Sequence(CreateSequenceChild tasParameters acnTypeEncodingSpec astRoot typeNode fileTokens alreadyTakenComments )
197197
| asn1Parser.SET_TYPE -> Sequence(CreateSequenceChild tasParameters acnTypeEncodingSpec astRoot typeNode fileTokens alreadyTakenComments )
198198
| asn1Parser.ENUMERATED_TYPE -> Enumerated(CreateNamedItems astRoot typeNode fileTokens alreadyTakenComments)
199-
| asn1Parser.BIT_STRING_TYPE -> BitString
199+
| asn1Parser.BIT_STRING_TYPE -> BitString(CreateNamedBitList astRoot typeNode fileTokens alreadyTakenComments)
200200
| asn1Parser.OCTECT_STING -> OctetString
201201
| asn1Parser.IA5String -> IA5String
202202
| asn1Parser.NumericString -> NumericString
@@ -276,18 +276,19 @@ and CreateChoiceChild (tasParameters : TemplateParameter list) (chAcnTypeEncodin
276276
| _ -> raise (BugErrorException("Bug in CreateChoiceChild"))
277277
)
278278

279+
and singleReference2DoubleReference (tree:ITree) =
280+
let strVal = tree.GetChild(0).TextL
281+
let modl = tree.GetAncestor(asn1Parser.MODULE_DEF)
282+
let modName = modl.GetChild(0).TextL
283+
let imports = modl.GetChildrenByType(asn1Parser.IMPORTS_FROM_MODULE)
284+
let importedFromModule = imports |> List.tryFind(fun imp-> imp.GetChildrenByType(asn1Parser.LID) |> Seq.exists(fun impTypeName -> impTypeName.Text = strVal.Value ))
285+
let valToReturn =
286+
match importedFromModule with
287+
|Some(imp) -> ( imp.GetChild(0).TextL, strVal)
288+
|None -> ( modName, strVal)
289+
valToReturn
290+
279291
and CreateValue (astRoot:list<ITree>) (tree:ITree ) : Asn1Value=
280-
let singleReference2DoubleReference (tree:ITree) =
281-
let strVal = tree.GetChild(0).TextL
282-
let modl = tree.GetAncestor(asn1Parser.MODULE_DEF)
283-
let modName = modl.GetChild(0).TextL
284-
let imports = modl.GetChildrenByType(asn1Parser.IMPORTS_FROM_MODULE)
285-
let importedFromModule = imports |> List.tryFind(fun imp-> imp.GetChildrenByType(asn1Parser.LID) |> Seq.exists(fun impTypeName -> impTypeName.Text = strVal.Value ))
286-
let valToReturn =
287-
match importedFromModule with
288-
|Some(imp) -> ( imp.GetChild(0).TextL, strVal)
289-
|None -> ( modName, strVal)
290-
valToReturn
291292

292293

293294
let GetActualString (str:string) =
@@ -463,6 +464,28 @@ and CreateNamedItems (astRoot:list<ITree>) (tree:ITree) (fileTokens:array<IToken
463464
let enumItes = getChildrenByType(tree, asn1Parser.NUMBER_LST_ITEM)
464465
enumItes |> List.map CreateItem
465466

467+
and CreateNamedBitList (astRoot:list<ITree>) (tree:ITree) (fileTokens:array<IToken>) (alreadyTakenComments:System.Collections.Generic.List<IToken>)=
468+
let CreateNamedBit(itemItree:ITree) =
469+
let itemChildren = getTreeChildren(itemItree)
470+
match itemChildren with
471+
| name::vlue::_ ->
472+
let value =
473+
match vlue.Type with
474+
| asn1Parser.INT ->
475+
match vlue.BigIntL.Value >= 0I with
476+
| true -> IDV_IntegerValue(vlue.BigIntL)
477+
| false -> raise (SemanticError(vlue.Location, "Negative values are not permitted"))
478+
| asn1Parser.DEFINED_VALUE ->
479+
match vlue.ChildCount with
480+
| 2 -> IDV_DefinedValue(vlue.GetChild(0).TextL, vlue.GetChild(1).TextL)
481+
| 1 -> IDV_DefinedValue(singleReference2DoubleReference vlue)
482+
| _ -> raise (BugErrorException("Bug in CreateValue CreateNamedBit 1"))
483+
| _ -> raise (BugErrorException("Bug in CreateValue CreateNamedBit 2"))
484+
{NamedBit0.Name=name.TextL; _value=value; Comments = Antlr.Comment.GetComments(fileTokens, alreadyTakenComments, fileTokens.[itemItree.TokenStopIndex].Line, itemItree.TokenStartIndex - 1, itemItree.TokenStopIndex + 2)}
485+
| _ -> raise (BugErrorException("Bug in CreateNamedBitList.CreateItem"))
486+
let namedBits = getChildrenByType(tree, asn1Parser.NUMBER_LST_ITEM)
487+
namedBits |> List.map CreateNamedBit
488+
466489
and CreateTimeClass (astRoot:list<ITree>) (tree:ITree) (fileTokens:array<IToken>) (alreadyTakenComments:System.Collections.Generic.List<IToken>)=
467490
let rec removeSpaceArountEqual (str:string) =
468491
let rs = [" =";"\t=";"\r\n=";"\r=";"\n=";"= ";"=\t";"=\r\n";"=\r";"=\n"]

FrontEndAst/FE_TypeDefinition.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ let getRefereceTypeDefinition (asn1:Asn1Ast.AstRoot) (t:Asn1Ast.Asn1Type) (arg:G
491491
| Asn1Ast.Boolean -> getPrimitiveTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_PrimitiveTypeDefinition d)) |> Map.ofList,b)
492492
| Asn1Ast.Enumerated _ -> getEnumeratedTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_EnumeratedTypeDefinition d)) |> Map.ofList,b)
493493
| Asn1Ast.OctetString -> getSizeableTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_SizeableTypeDefinition d)) |> Map.ofList,b)
494-
| Asn1Ast.BitString -> getSizeableTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_SizeableTypeDefinition d)) |> Map.ofList,b)
494+
| Asn1Ast.BitString _ -> getSizeableTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_SizeableTypeDefinition d)) |> Map.ofList,b)
495495
| Asn1Ast.SequenceOf _ -> getSizeableTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_SizeableTypeDefinition d)) |> Map.ofList,b)
496496
| Asn1Ast.NumericString -> getStringTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_StringTypeDefinition d)) |> Map.ofList,b)
497497
| Asn1Ast.IA5String -> getStringTypeDifition arg us |> (fun (a,b) -> a |> Map.toList |> List.map (fun (l, d) -> (l, FE_StringTypeDefinition d)) |> Map.ofList,b)

FrontEndAst/MapParamAstToNonParamAst.fs

+21-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ let rec MapAsn1Value (r:ParameterizedAsn1Ast.AstRoot) (kind: ParameterizedAsn1As
150150
|ParameterizedAsn1Ast.SeqOfValue(vals) ->
151151
match actKind with
152152
| ParameterizedAsn1Ast.SequenceOf(ch) -> Asn1Ast.SeqOfValue(vals |> List.mapi (fun idx v -> MapAsn1Value r ch.Kind typeScope (visitSeqOfValue variableScope idx) v))
153+
| ParameterizedAsn1Ast.BitString namedBits ->
154+
let bitPos =
155+
vals |>
156+
List.map(fun chV ->
157+
match chV.Kind with
158+
| ParameterizedAsn1Ast.RefValue (_,refVal) ->
159+
match namedBits |> Seq.tryFind(fun z -> z.Name.Value = refVal.Value) with
160+
| None -> raise (SemanticError(v.Location, (sprintf "Expecting a BIT STRING value. '%s' is not defined as a named bit" refVal.Value)))
161+
| Some nb ->
162+
match nb._value with
163+
| CommonTypes.IDV_IntegerValue intVal -> intVal.Value
164+
| CommonTypes.IDV_DefinedValue (mdVal, refVal) -> ParameterizedAsn1Ast.GetValueAsInt (ParameterizedAsn1Ast.GetBaseValue mdVal refVal r) r
165+
166+
| _ -> raise (SemanticError(v.Location, (sprintf "Expecting a BIT STRING value but found a SEQUENCE OF value" )))
167+
) |> Set.ofList
168+
let maxValue = bitPos.MaximumElement
169+
170+
let bitStrVal =
171+
[0I .. maxValue] |> List.map(fun bi -> if bitPos.Contains bi then '1' else '0') |> Seq.StrJoin ""
172+
Asn1Ast.BitStringValue ({StringLoc.Value = bitStrVal; Location = v.Location})
153173
| _ -> raise(SemanticError(v.Location, "Expecting a SEQUENCE OF value"))
154174
|ParameterizedAsn1Ast.SeqValue(vals) ->
155175
match actKind with
@@ -309,7 +329,7 @@ and MapAsn1Type (r:ParameterizedAsn1Ast.AstRoot) typeScope (t:ParameterizedAsn1A
309329
| ParameterizedAsn1Ast.OctetString -> aux Asn1Ast.OctetString
310330
| ParameterizedAsn1Ast.TimeType cl -> aux (Asn1Ast.TimeType cl)
311331
| ParameterizedAsn1Ast.NullType -> aux Asn1Ast.NullType
312-
| ParameterizedAsn1Ast.BitString -> aux Asn1Ast.BitString
332+
| ParameterizedAsn1Ast.BitString nBits -> aux (Asn1Ast.BitString nBits)
313333
| ParameterizedAsn1Ast.Boolean -> aux Asn1Ast.Boolean
314334
| ParameterizedAsn1Ast.ObjectIdentifier -> aux Asn1Ast.ObjectIdentifier
315335
| ParameterizedAsn1Ast.RelativeObjectIdentifier -> aux Asn1Ast.RelativeObjectIdentifier

0 commit comments

Comments
 (0)