Support value-range and single-value constraints for REAL
authorKim Grasman <kim.grasman@gmail.com>
Sat, 26 Jul 2014 13:57:50 +0000 (15:57 +0200)
committerKim Grasman <kim.grasman@gmail.com>
Sat, 26 Jul 2014 13:57:50 +0000 (15:57 +0200)
asn1ate/parser.py
testdata/single_value_constraint.asn
testdata/valuerange_constraint.asn

index a9f18db..36ab32b 100644 (file)
@@ -186,6 +186,10 @@ def _build_asn1_grammar():
     exponent = (Literal('e') | Literal('E')) + signed_number
     real_value = Combine(signed_number + Optional(Literal('.') + Optional(number)) + Optional(exponent))
 
+    # In value range constraints, decimal points must be followed by number, or
+    # the grammar becomes ambiguous: ([1.].100) vs ([1]..[100])
+    constraint_real_value = Combine(signed_number + Optional(Literal('.') + number) + Optional(exponent))
+
     builtin_value = boolean_value | bitstring_value | real_value | integer_value | null_value | cstring_value
     external_value_reference = module_reference + Suppress('.') + valuereference
     defined_value = external_value_reference | valuereference  # todo: more options from 13.1
@@ -234,8 +238,8 @@ def _build_asn1_grammar():
 
     # constraints
     # todo: consider the full subtype and general constraint syntax described in 45.*
-    lower_bound = (signed_number | referenced_value | MIN)
-    upper_bound = (signed_number | referenced_value | MAX)
+    lower_bound = (constraint_real_value | signed_number | referenced_value | MIN)
+    upper_bound = (constraint_real_value | signed_number | referenced_value | MAX)
     single_value_constraint = Suppress('(') + value + Suppress(')')
     value_range_constraint = Suppress('(') + lower_bound + Suppress('..') + upper_bound + Suppress(')')
     # TODO: Include contained subtype constraint here if we ever implement it.
@@ -282,7 +286,7 @@ def _build_asn1_grammar():
     useful_type = GeneralizedTime | UTCTime | ObjectDescriptor
 
     # todo: consider other builtins from 16.2
-    simple_type = (boolean_type | null_type | octetstring_type | characterstring_type | real_type | plain_integer_type | object_identifier_type | useful_type) + Optional(value_range_constraint)
+    simple_type = (boolean_type | null_type | octetstring_type | characterstring_type | real_type | plain_integer_type | object_identifier_type | useful_type) + Optional(value_range_constraint | single_value_constraint)
     constructed_type = choice_type | sequence_type | set_type
     value_list_type = restricted_integer_type | enumerated_type
     builtin_type = value_list_type | tagged_type | simple_type | constructed_type | sequenceof_type | setof_type | bitstring_type
index ebad396..e12cfe5 100644 (file)
@@ -9,5 +9,10 @@ BEGIN
 \r
   ConstrainedBitString1 ::= BIT STRING { one(1), two(2) } (1)\r
 \r
+  realValue REAL ::= 3.14\r
+  ConstrainedReal1 ::= REAL (2.73)\r
+  ConstrainedReal2 ::= REAL (realValue)\r
+  ConstrainedReal3 ::= REAL (4E9)\r
+\r
   -- TODO: Add tests for the rest of the types as we need them.\r
 END\r
index e63ac5e..83929c6 100644 (file)
@@ -1,12 +1,14 @@
-TEST DEFINITIONS ::=\r
-BEGIN\r
-  -- Value range constraints apply to the following types.\r
-  min INTEGER ::= 128\r
-  max INTEGER ::= 256\r
-  ConstraintedInteger1 ::= INTEGER(1..100)\r
-  ConstraintedInteger2 ::= INTEGER(min..max)\r
-  -- This is not yet translated in codegen\r
-  -- ConstraintedInteger3 ::= INTEGER(MIN..MAX)\r
-\r
-  -- TODO: Add tests for REAL types once we have them in place.\r
-END\r
+TEST DEFINITIONS ::=
+BEGIN
+  -- Value range constraints apply to the following types.
+  min INTEGER ::= 128
+  max INTEGER ::= 256
+  ConstraintedInteger1 ::= INTEGER(1..100)
+  ConstraintedInteger2 ::= INTEGER(min..max)
+  -- This is not yet translated in codegen
+  -- ConstraintedInteger3 ::= INTEGER(MIN..MAX)
+
+  realValue REAL ::= 3.14
+  ConstrainedReal1 ::= REAL (0.12 .. 6.28)
+  ConstrainedReal2 ::= REAL (realValue .. 9.21)
+END