Index: ruby/trunk/adl_parser/lib/parser.y
===================================================================
--- ruby/trunk/adl_parser/lib/parser.y	(revision 21)
+++ ruby/trunk/adl_parser/lib/parser.y	(revision 22)
@@ -35,7 +35,12 @@
 archetype: arch_identification arch_specialisation arch_concept arch_language arch_description arch_definition arch_invariant arch_ontology
   { 
+    assert_at(__FILE__,__LINE__) do
+      val[4].instance_of?(OpenEHR::AM::Archetype::Archetype_Description::ARCHETYPE_DESCRIPTION) and val[5].instance_of?(OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT) and val[7].instance_of?(OpenEHR::AM::Archetype::Ontology::ARCHETYPE_ONTOLOGY) 
+    end
+    
     result = OpenEHR::AM::Archetype::ARCHETYPE.new(
                                                    :description => val[4],
-                                                   :definition => val[5]
+                                                   :definition => val[5],
+                                                   :ontology => val[7]
                                                    )
   }
@@ -100,4 +105,6 @@
 cadl_section: c_complex_object
   { 
+    assert_at(__FILE__,__LINE__){val[0].instance_of?(OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT)}
+                      
     result = val[0]
   }
@@ -112,5 +119,8 @@
     | c_complex_object_head SYM_MATCHES SYM_START_CBLOCK c_complex_object_body SYM_END_CBLOCK
   { 
-    result = OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT.new
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT.new(
+                                                                            :assumed_value => val[0],
+                                                                            :attributes => val[2]
+                                                                            )
   }
 #    | c_complex_object_head error SYM_END_CBLOCK
@@ -121,5 +131,5 @@
 c_complex_object_id: type_identifier
   {
-    result = {:c_complex_object_id => {:type_identifier => val[0]}}
+    result = val[0]
   }
   | type_identifier V_LOCAL_TERM_CODE_REF
@@ -130,4 +140,7 @@
 c_complex_object_body: c_any #-- used to indicate that any value of a type is ok
   | c_attributes
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT.new(:attributes => val[0])
+  }
 
 
@@ -135,11 +148,35 @@
 
 c_object: v_c_domain_type
+  { 
+    result = val[0]
+  }
   | c_complex_object
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_COMPLEX_OBJECT.new
+  }
   | archetype_internal_ref
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::ARCHETYPE_INTERNAL_REF.new
+  }
   | archetype_slot
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::ARCHETYPE_SLOT.new
+  }
   | constraint_ref
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::CONSTRAINT_REF.new
+  }
   | c_code_phrase
+  { 
+    result = val[0]
+  }
   | c_ordinal
+  { 
+    result = val[0]
+  }
   | c_primitive_object
+  { 
+    result = val[0]
+  }
 #  | v_c_domain_type
 #  | V_C_DOMAIN_TYPE
@@ -170,17 +207,50 @@
 # 'c_primitive_object' is an node representing a constraint on a primitive object type.
 c_primitive_object: c_primitive
+  {
+    assert_at(__FILE__,__LINE__){val[0].kind_of?(OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_PRIMITIVE)}
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_PRIMITIVE_OBJECT.new(:item => val[0])
+  }
 
 c_primitive: c_integer
   {
-    @@log.info("#{__FILE__}:#{__LINE__}: c_integer = #{val[0]} at #{@filename}")
-    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_Integer.new
+    @@log.info("#{__FILE__}:#{__LINE__}: c_integer = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_INTEGER.new
   }
   | c_real
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_real = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_REAL.new
+  }
   | c_date
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_date = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_DATE.new
+  }
   | c_time
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_time = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_TIME.new
+  }
   | c_date_time
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_date_time = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_DATE_TIME.new
+  }
   | c_duration
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_duration = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_DURATION.new
+  }
   | c_string
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: c_string = #{val[0]} at #{@filename}:#{@lineno}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_STRING.new
+  }
   | c_boolean
+  {
+    assert_at(__FILE__,__LINE__){val[0].instance_of?(OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_BOOLEAN)}
+    @@log.info("#{__FILE__}:#{__LINE__}: c_boolean = #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
+  }
 
 c_any: Star_code
@@ -190,10 +260,30 @@
 
 c_attributes: c_attribute
+  { 
+    result = [val[0]]
+  }
   | c_attributes c_attribute
+  { 
+    result = (val[0] << val[1])
+  }
 
 # 'c_attribute' is a node representing a constraint on an attribute in an object model.
 c_attribute: c_attr_head SYM_MATCHES SYM_START_CBLOCK c_attr_values SYM_END_CBLOCK
+  { 
+    assert_at(__FILE__,__LINE__){ val[0].kind_of?(OpenEHR::AM::Archetype::Constraint_Model::C_ATTRIBUTE)}
+    c_attribute = val[0]
+    c_attribute.children = val[3]
+    result = c_attribute
+  }
   | c_attr_head SYM_MATCHES START_REGEXP_BLOCK REGEXP_BODY END_REGEXP_BLOCK # added by akimichi
+  { 
+    assert_at(__FILE__,__LINE__){ val[0].kind_of?(OpenEHR::AM::Archetype::Constraint_Model::C_ATTRIBUTE)}
+    result = val[0]
+  }
   | c_attr_head SYM_MATCHES SYM_START_CBLOCK error SYM_END_CBLOCK
+  { 
+    assert_at(__FILE__,__LINE__){ val[0].kind_of?(OpenEHR::AM::Archetype::Constraint_Model::C_ATTRIBUTE)}
+    result = val[0]
+  }
 
 
@@ -201,13 +291,33 @@
   {
     @@log.info("#{__FILE__}:#{__LINE__}: V_ATTRIBUTE_IDENTIFIER = #{val[0]}, c_existence = #{val[1]} at #{@filename}")
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_SINGLE_ATTRIBUTE.new(
+                                                                              :rm_attribute_name => val[0],
+                                                                              :existence => val[1]
+                                                                              )
+
   }
   | V_ATTRIBUTE_IDENTIFIER c_existence c_cardinality
   {
+    assert_at(__FILE__,__LINE__){ val[2].instance_of?(OpenEHR::AM::Archetype::Constraint_Model::CARDINALITY) }
     @@log.info("#{__FILE__}:#{__LINE__}: V_ATTRIBUTE_IDENTIFIER: #{val[0]}, c_existence = #{val[1]}, c_cardinality = #{val[2]} at #{@filename}") 
+    result = OpenEHR::AM::Archetype::Constraint_Model::C_MULTIPLE_ATTRIBUTE.new(
+                                                                                :rm_attribute_name => val[0],
+                                                                                :existence => val[1],
+                                                                                :cardinality => val[2]
+                                                                                )
   }
 
 c_attr_values: c_object
+  { 
+    result = Array[val[0]]
+  }
   | c_attr_values c_object
+  { 
+    result = (val[0] << val[1])
+  }
   | c_any #	-- to allow a property to have any value
+  { 
+    result = Array[val[0]]
+  }
 
 ### c_includes: #-- Empty
@@ -237,4 +347,7 @@
 
 arch_ontology: SYM_ONTOLOGY dadl_section
+  { 
+    result = OpenEHR::AM::Archetype::Ontology::ARCHETYPE_ONTOLOGY.new
+  }
   | SYM_ONTOLOGY error
 
@@ -260,21 +373,44 @@
   {
     @@log.info("#{__FILE__}:#{__LINE__}: V_ATTRIBUTE_IDENTIFIER = #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | V_ATTRIBUTE_IDENTIFIER error
 
 object_block: complex_object_block
+  { 
+    result = val[0]
+  }
   | primitive_object_block
+  { 
+    result = val[0]
+  }
 
 complex_object_block: single_attr_object_block
+  { 
+    result = val[0]
+  }
   | multiple_attr_object_block
+  { 
+    result = val[0]
+  }
 
 multiple_attr_object_block: untyped_multiple_attr_object_block
+  { 
+    result = val[0]
+  }
   | type_identifier untyped_multiple_attr_object_block
+  { 
+    result = val[0]
+  }
 
 untyped_multiple_attr_object_block: multiple_attr_object_block_head keyed_objects SYM_END_DBLOCK
+  { 
+    result = val[0]
+  }
 
 multiple_attr_object_block_head: SYM_START_DBLOCK
-  {
+  { 
     @@log.info("SYM_START_DBLOCK: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
 
@@ -290,4 +426,5 @@
   {
     @@log.info("object_key: [#{val[1]}] at #{@filename}:#{@lineno}")
+    result = val[1]
   }
 
@@ -295,57 +432,100 @@
   | type_identifier untyped_single_attr_object_block
 untyped_single_attr_object_block: single_attr_object_complex_head SYM_END_DBLOCK # <>
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: single_attr_object_complex_head = #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
+  }
   | single_attr_object_complex_head attr_vals SYM_END_DBLOCK
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: single_attr_object_complex_head = #{val[0]}, attr_vals = #{val[1]} at #{@filename}:#{@lineno}")
+    result = val[0]
+  }
 single_attr_object_complex_head: SYM_START_DBLOCK
 primitive_object_block: untyped_primitive_object_block
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: untyped_primitive_object_block = #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
+  }
   | type_identifier untyped_primitive_object_block
+  {
+    @@log.info("#{__FILE__}:#{__LINE__}: type_identifier = #{val[0]}, untyped_primitive_object_block = #{val[1]} at #{@filename}:#{@lineno}")
+    result = val[0]
+  }
 untyped_primitive_object_block: SYM_START_DBLOCK primitive_object_value SYM_END_DBLOCK
   {
-    @@log.info("#{__FILE__}:#{__LINE__}: primitive_object_block = <#{val[2]}> at #{@filename}:#{@lineno}")
+    @@log.info("#{__FILE__}:#{__LINE__}: primitive_object_block = <#{val[1]}> at #{@filename}:#{@lineno}")
+    result = val[1]
   }
 primitive_object_value: simple_value
+  {
+    result = val[0]
+  }
   | simple_list_value
+  {
+    result = val[0]
+  }
   | simple_interval_value
+  {
+    result = val[0]
+  }
   | term_code
+  {
+    result = val[0]
+  }
   | term_code_list_value
+  {
+    result = val[0]
+  }
 simple_value: string_value
   {
     @@log.info("string_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | integer_value
   {
     @@log.info("integer_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | real_value
   {
     @@log.info("real_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | boolean_value
   {
     @@log.info("boolean_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | character_value
   {
     @@log.info("character_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | date_value
   {
     @@log.info("date_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | time_value
   {
     @@log.info("time_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | date_time_value
   {
     @@log.info("date_time_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | duration_value
   {
     @@log.info("duration_value: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | uri_value
   {
     @@log.info("uri_value: #{val[0]} at #{@filename}:#{@lineno}")
-  }
+    result = val[0]
+  }
+
 simple_list_value: string_list_value
   | integer_list_value
@@ -368,8 +548,10 @@
   {
     @@log.info("V_TYPE_IDENTIFIER: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
   | V_GENERIC_TYPE_IDENTIFIER
   {
     @@log.info("V_GENERIC_TYPE_IDENTIFIER: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
 
@@ -377,4 +559,5 @@
   {
     @@log.info("V_STRING: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
 
@@ -384,6 +567,30 @@
 
 integer_value: V_INTEGER
+  { 
+    begin
+      integer = Integer(val[0])
+    rescue
+      raise
+    end
+    result = integer
+  }
   | Plus_code V_INTEGER
+  {
+    begin
+      integer = Integer(val[0])
+    rescue
+      raise
+    end
+    result = integer
+  }
   | Minus_code V_INTEGER
+  {
+    begin
+      integer = Integer(val[0])
+    rescue
+      raise
+    end
+    result = - integer
+  }
 ###   | '+' V_INTEGER
 ###   | '-' V_INTEGER
@@ -488,4 +695,5 @@
   {
     @@log.info("V_ISO8601_DURATION: #{val[0]} at #{@filename}:#{@lineno}")
+    result = val[0]
   }
 
@@ -598,10 +806,36 @@
 
 c_existence: #-- default to 1..1
-    | SYM_EXISTENCE SYM_MATCHES SYM_START_CBLOCK existence_spec SYM_END_CBLOCK
+    { 
+    result = Range.new(1,1)
+  }
+  | SYM_EXISTENCE SYM_MATCHES SYM_START_CBLOCK existence_spec SYM_END_CBLOCK
+  { 
+    result = val[3]
+  }
 
 existence_spec:  V_INTEGER #-- can only be 0 or 1
+  { 
+    begin
+      integer = Integer(val[0])
+    rescue
+      raise
+    end
+    result = integer
+  }
   | V_INTEGER SYM_ELLIPSIS V_INTEGER #-- can only be 0..0, 0..1, 1..1
+  { 
+    begin
+      from_integer = Integer(val[0])
+      to_integer = Integer(val[2])
+    rescue
+      raise
+    end
+    result = Range.new(from_integer,to_integer)
+  }
 
 c_cardinality: SYM_CARDINALITY SYM_MATCHES SYM_START_CBLOCK cardinality_spec SYM_END_CBLOCK
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::CARDINALITY.new
+  }
 
 cardinality_spec: occurrence_spec
@@ -687,21 +921,39 @@
 
 c_boolean_spec: SYM_TRUE
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_BOOLEAN.new(:true_valid => true)
+  }
   | SYM_FALSE
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_BOOLEAN.new(:true_valid => false)
+  }
   | SYM_TRUE Comma_code SYM_FALSE
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_BOOLEAN.new(:true_valid => true,:false_valid => false)
+  }
   | SYM_FALSE Comma_code SYM_TRUE
+  { 
+    result = OpenEHR::AM::Archetype::Constraint_Model::Primitive::C_BOOLEAN.new(:true_valid => false,:false_valid => true)
+  }
 
 c_boolean: c_boolean_spec
+  { 
+    result = val[0]
+  }
   | c_boolean_spec Semicolon_code boolean_value
+  {
+    raise 'Not implemented yet'
+  }
   | c_boolean_spec Semicolon_code error
-#  | c_boolean_spec ';' error
+  {
+    raise 'Not implemented yet'
+  }
 
 c_ordinal: c_ordinal_spec
   | c_ordinal_spec Semicolon_code integer_value
   | c_ordinal_spec Semicolon_code error
-#  | c_ordinal_spec ';' error
 
 c_ordinal_spec: ordinal
   | c_ordinal_spec Comma_code ordinal
-#  | c_ordinal_spec ',' ordinal
 
 ordinal: integer_value SYM_INTERVAL_DELIM V_QUALIFIED_TERM_CODE_REF
@@ -756,4 +1008,10 @@
 
 ---- inner
+
+def assert_at(file,line, message = "")
+  unless yield
+    raise "Assertion failed !: #{file}, #{line}: #{message}"
+  end
+end
 
 @@log = Logger.new('log/parser.log','daily')
