Index: /ruby/trunk/lib/adl_parser/lib/adl_scanner.rb
===================================================================
--- /ruby/trunk/lib/adl_parser/lib/adl_scanner.rb	(revision 313)
+++ /ruby/trunk/lib/adl_parser/lib/adl_scanner.rb	(revision 314)
@@ -3,4 +3,6 @@
 require 'adl_parser.rb'
 require 'open_ehr'
+#require 'am.rb'
+#require 'rm.rb'
 require 'util.rb'
 
@@ -9,5 +11,6 @@
   module ADL
     module Scanner
-      LOGGER = Logger.new('log/scanner.log')
+#      LOGGER = Logger.new('log/scanner.log')
+      LOGGER = Logger.new('log/parser.log')
       LOGGER.level = Logger::DEBUG
 
@@ -25,24 +28,9 @@
       end
 
-      class RootScanner < Base
-        def initialize(filename, lineno = 1)
-          @current_scanner = ADLScanner.new
-        end
-
-        def scan(data)
-          until data.nil?  do
-            case @current_scanner
-            when ADLScanner
-              @current_scanner.scan(data)
-            end
-          end
-        end          
-      end
-
       #
       # ADLScanner
       # 
       class ADLScanner < Base
-        attr_accessor :adl_type, :lineno
+        attr_accessor :adl_type, :lineno, :cadl_scanner, :dadl_scanner, :regex_scanner, :term_constraint_scanner
 
         @@logger = OpenEhr::ADL::Scanner::LOGGER #Logger.new('log/scanner.log')
@@ -69,5 +57,4 @@
           super(adl_type, filename, lineno)
           @in_interval  = false
-          @adl_type  = nil
         end
 
@@ -78,5 +65,6 @@
           @@logger.debug("#{__FILE__}:#{__LINE__}: Entering  ADLScanner#scan at #{@filename}:#{@lineno}: data = #{data.inspect}")
           until data.nil?  do
-            if @adl_type.nil?
+            case @adl_type.last
+            when :adl
               case data
               when /\A\n/ # carriage return
@@ -98,4 +86,5 @@
               when /\A(\w+)-(\w+)-(\w+)\.(\w+)((?:-\w+)*)\.(v\w+)/   #V_ARCHETYPE_ID
                 object_id, rm_originator, rm_name, rm_entity, concept_name, specialisation, version_id = $&, $1, $2, $3, $4, $5, $6
+                #archetype_id = OpenEhr::RM::Support::Identification::ArchetypeID.new(object_id, concept_name, rm_name, rm_entity, rm_originator, specialisation, version_id)
                 archetype_id = OpenEhr::RM::Support::Identification::ArchetypeID.new(:concept_name => concept_name, :rm_name => rm_name, :rm_entity => rm_entity, :rm_originator => :rm_originator, :specialisation => specialisation, :version_id => version_id)
                 yield :V_ARCHETYPE_ID, archetype_id
@@ -119,5 +108,5 @@
                   yield :SYM_LT, :SYM_LT
                 else
-                  @adl_type = OpenEhr::ADL::Scanner::DADLScanner.new(@filename, @lineno)
+                  @adl_type.push(:dadl)
                   yield :SYM_START_DBLOCK,  $&
                 end
@@ -126,5 +115,4 @@
                   yield :SYM_GT, :SYM_GT
                 else
-                  raise
                   adl_type = @adl_type.pop
                   assert_at(__FILE__,__LINE__){adl_type == :dadl}
@@ -132,8 +120,8 @@
                 end
               when /\A\{/   # {
-                @adl_type = OpenEhr::ADL::Scanner::CADLScanner.new(@filename, @lineno)
+                @adl_type.push(:cadl)
+                @@logger.debug("ADLScanner#scan: SYM_START_CBLOCK")
                 yield :SYM_START_CBLOCK, :SYM_START_CBLOCK
               when /\A\}/   # }
-                raise
                 adl_type = @adl_type.pop
                 assert_at(__FILE__,__LINE__){adl_type == :cadl}
@@ -221,8 +209,26 @@
               end
               data = $' # variable $' receives the string after the match
+            when :dadl
+              dadl_scanner = OpenEhr::ADL::Scanner::DADLScanner.new(@adl_type, @filename, @lineno)
+              data = dadl_scanner.scan(data) do |sym, val|
+                yield sym, val
+              end
+            when :cadl
+              cadl_scanner = OpenEhr::ADL::Scanner::CADLScanner.new(@adl_type, @filename, @lineno)
+              data = cadl_scanner.scan(data) do |sym, val|
+                yield sym, val
+              end
+            when :regexp
+              regex_scanner = OpenEhr::ADL::Scanner::RegexScanner.new(@adl_type, @filename, @lineno)
+              data = regex_scanner.scan(data) do |sym, val|
+                yield sym, val
+              end
+            when :term_constraint
+              term_constraint_scanner = OpenEhr::ADL::Scanner::TermConstraintScanner.new(@adl_type, @filename, @lineno)
+              data = term_constraint_scanner.scan(data) do |sym, val|
+                yield sym, val
+              end
             else
-              data = @adl_type.scan(data) do |sym, val|
-                yield sym, val
-              end
+              raise
             end
           end
@@ -234,5 +240,5 @@
       # 
       class DADLScanner < Base
-        attr_accessor :in_interval, :in_c_domain_type, :in_dblock
+        attr_accessor :in_interval, :in_c_domain_type, :dblock_depth
         @@logger = OpenEhr::ADL::Scanner::LOGGER #Logger.new('log/scanner.log')
         RESERVED = {
@@ -242,10 +248,9 @@
         }
 
-        def initialize(filename, lineno = 1)
+        def initialize(adl_type, filename, lineno = 1)
           super(adl_type, filename, lineno)
           @in_interval = false
           @in_c_domain_type = false
-          @in_dblock = true
-          @adl_type = nil
+          @dblock_depth = 0
         end
 
@@ -254,6 +259,10 @@
         #
         def scan(data)
+          @@logger.debug("Entering DADLScanner#scan at #{@filename}:#{@lineno}: @adl_type = #{@adl_type.inspect}, data = #{data.inspect}")
           until data.nil?  do
-            if @adl_type.nil?
+            @@logger.debug("#{@filename}:#{@lineno}: DADLScanner#scan:loop data = #{data.inspect}")
+            @@logger.debug("#{@filename}:#{@lineno}: DADLScanner#scan:loop self = \n#{self.to_yaml}")
+            case @adl_type.last
+            when :dadl
               case data
               when /\A\n/ # carriage return
@@ -287,6 +296,9 @@
                   yield :SYM_LT, :SYM_LT
                 else
-                  raise
-                  @adl_type.push(:dadl)
+                  unless @in_c_domain_type
+                    @adl_type.push(:dadl)
+                  else
+                    @dblock_depth += 1
+                  end
                   yield :SYM_START_DBLOCK, :SYM_START_DBLOCK
                 end
@@ -294,20 +306,27 @@
                 if @in_interval
                   yield :SYM_GT, :SYM_GT
-                elsif @in_c_domain_type == true
-                  assert_at(__FILE__,__LINE__){@adl_type.last == :dadl}
-                  adl_type = @adl_type.pop
-                  if adl_type == :dadl
-                    @in_c_domain_type = false
-                    yield :END_V_C_DOMAIN_TYPE_BLOCK, :END_V_C_DOMAIN_TYPE_BLOCK
-#                    yield :SYM_END_DBLOCK, :SYM_END_DBLOCK
+                else
+                  if @in_c_domain_type
+                    assert_at(__FILE__,__LINE__){@adl_type.last == :dadl}
+                    @dblock_depth -= 1
+                    if @dblock_depth < 0
+                      @adl_type.pop
+                      @in_c_domain_type = false
+                      yield :END_V_C_DOMAIN_TYPE_BLOCK, :END_V_C_DOMAIN_TYPE_BLOCK
+                    else
+                      yield :SYM_END_DBLOCK, :SYM_END_DBLOCK
+                    end
+#                     adl_type = @adl_type.pop
+#                     if adl_type == :dadl
+#                       yield :SYM_END_DBLOCK, :SYM_END_DBLOCK
+#                     else
+#                       @in_c_domain_type = false
+#                       yield :END_V_C_DOMAIN_TYPE_BLOCK, :END_V_C_DOMAIN_TYPE_BLOCK
+#                     end
                   else
-                    yield :SYM_END_DBLOCK, :SYM_END_DBLOCK
+                    adl_type = @adl_type.pop
+                    assert_at(__FILE__,__LINE__){adl_type == :dadl}
+                    yield :SYM_END_DBLOCK, $&
                   end
-                elsif @in_c_domain_type == false
-                  adl_type = @adl_type.pop
-                  assert_at(__FILE__,__LINE__){adl_type == :dadl}
-                  yield :SYM_END_DBLOCK, $&
-                else
-                  raise
                 end
               when /\A\-/   # -
@@ -389,10 +408,4 @@
               end
               data = $' # variable $' receives the string after the match
-
-
-
-          until data.nil?  do
-            case @adl_type.last
-            when :dadl
             when :adl
               adl_scanner = OpenEhr::ADL::Scanner::ADLScanner.new(@adl_type, @filename, @lineno)
Index: /ruby/trunk/lib/adl_parser/test/parser_test.rb
===================================================================
--- /ruby/trunk/lib/adl_parser/test/parser_test.rb	(revision 313)
+++ /ruby/trunk/lib/adl_parser/test/parser_test.rb	(revision 314)
@@ -183,50 +183,50 @@
 
 # C_DV_QUANTITY parse error
-#   must "openEHR-EHR-CLUSTER.dimensions-circumference.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.dimensions-circumference.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.dimensions-circumference.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
-
-#   must "openEHR-EHR-CLUSTER.dimensions.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.dimensions.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.dimensions.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
-
-#   must "openEHR-EHR-CLUSTER.exam-fetus.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.exam-fetus.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.exam-fetus.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
-
-#   must "openEHR-EHR-CLUSTER.exam-uterine_cervix.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.exam-uterine_cervix.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.exam-uterine_cervix.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
-
-#   must "openEHR-EHR-EVALUATION.adverse.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-EVALUATION.adverse.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-EVALUATION.adverse.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
-#   must "openEHR-EHR-OBSERVATION.body_mass_index.v1.adl be properly parsed" do
-#     file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-OBSERVATION.body_mass_index.v1.adl")
-#     assert_nothing_raised do
-#       ast = @parser.parse(file, 'openEHR-EHR-OBSERVATION.body_mass_index.v1')
-#       assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
-#     end
-#   end
+  must "openEHR-EHR-CLUSTER.dimensions-circumference.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.dimensions-circumference.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.dimensions-circumference.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
+
+  must "openEHR-EHR-CLUSTER.dimensions.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.dimensions.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.dimensions.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
+
+  must "openEHR-EHR-CLUSTER.exam-fetus.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.exam-fetus.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.exam-fetus.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
+
+  must "openEHR-EHR-CLUSTER.exam-uterine_cervix.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-CLUSTER.exam-uterine_cervix.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-CLUSTER.exam-uterine_cervix.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
+
+  must "openEHR-EHR-EVALUATION.adverse.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-EVALUATION.adverse.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-EVALUATION.adverse.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
+  must "openEHR-EHR-OBSERVATION.body_mass_index.v1.adl be properly parsed" do
+    file =  File.read("#{TEST_ROOT_DIR}/adl/openEHR-EHR-OBSERVATION.body_mass_index.v1.adl")
+    assert_nothing_raised do
+      ast = @parser.parse(file, 'openEHR-EHR-OBSERVATION.body_mass_index.v1')
+      assert_instance_of OpenEhr::RM::Support::Identification::ArchetypeID, ast.archetype_id
+    end
+  end
 
 
