Add the node summary to the Tree interface.
[tatoo.git] / src / naive_tree.ml
index 89b9e86..a7194be 100644 (file)
@@ -16,7 +16,7 @@
 type node = {
   tag : QName.t;
   preorder : int;
-  mutable kind : Tree.NodeKind.t;
+  mutable summary : Tree.NodeSummary.t;
   mutable data : string;
   mutable first_child : node;
   mutable next_sibling : node;
@@ -27,7 +27,7 @@ type node = {
 
 let rec nil = {
   tag = QName.nil;
-  kind = Tree.NodeKind.Element;
+  summary = Tree.NodeSummary.dummy;
   preorder = -1;
   data = "";
   first_child = nil;
@@ -38,7 +38,7 @@ let rec nil = {
 let dummy_tag = QName.of_string "#dummy"
 let rec dummy = {
   tag = dummy_tag;
-  kind = Tree.NodeKind.Element;
+  summary = Tree.NodeSummary.dummy;
   preorder = -1;
   data = "";
   first_child = dummy;
@@ -116,7 +116,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
     do_text parser_ ctx;
     let parent = top ctx in
     let n = { tag = QName.of_string tag;
-              kind = Tree.NodeKind.Element;
+              summary = Tree.NodeSummary.make false false false false Tree.NodeKind.Element;
               preorder = next ctx;
               data = "";
               first_child = dummy;
@@ -133,7 +133,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
     start_element_handler parser_ ctx att [];
     let n = top ctx in
     n.data <- value;
-    n.kind <- Tree.NodeKind.Attribute;
+    n.summary <- Tree.NodeSummary.make false false false false Tree.NodeKind.Attribute;
     end_element_handler parser_ ctx att
 
   and consume_closing ctx n =
@@ -156,7 +156,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
       start_element_handler parser_ ctx text_string [];
       let node = top ctx in
       node.data <- s;
-      node.kind <- Tree.NodeKind.Text;
+      node.summary <- Tree.NodeSummary.make false false false false Tree.NodeKind.Text;
       end_element_handler parser_ ctx text_string
 
   and comment_handler parser_ ctx s =
@@ -164,7 +164,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
     start_element_handler parser_ ctx comment_string [];
     let node = top ctx in
     node.data <- s;
-    node.kind <- Tree.NodeKind.Comment;
+    node.summary <- Tree.NodeSummary.make false false false false Tree.NodeKind.Comment;
     end_element_handler parser_ ctx comment_string
 
   and processing_instruction_handler parser_ ctx tag data =
@@ -172,7 +172,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
     start_element_handler parser_ ctx tag [];
     let node = top ctx in
     node.data <- data;
-    node.kind <- Tree.NodeKind.ProcessingInstruction;
+    node.summary <- Tree.NodeSummary.make false false false false Tree.NodeKind.ProcessingInstruction;
     end_element_handler parser_ ctx tag
 
 
@@ -193,7 +193,7 @@ first_child=%a; next_sibling=%a; parent=%a }"
       (processing_instruction_handler psr ctx);
     push { tag = QName.document;
            preorder = next ctx;
-           kind = Tree.NodeKind.Document;
+           summary = Tree.NodeSummary.make false false false false Tree.NodeKind.Document;
            data = "";
            first_child = dummy;
            next_sibling = dummy;
@@ -211,6 +211,13 @@ first_child=%a; next_sibling=%a; parent=%a }"
        let rec loop n =
          if n != nil then
            begin
+             n.summary <-
+               Tree.NodeSummary.make
+                 (n == n.parent.first_child)
+                 (n == n.parent.next_sibling)
+                 (n.first_child != nil)
+                 (n.next_sibling != nil)
+                 (Tree.NodeSummary.kind n.summary);
              a.(n.preorder) <- n;
              loop n.first_child;
              loop n.next_sibling;
@@ -269,9 +276,11 @@ let output_escape_string out s =
     | c -> output_char out c
   done
 
+let kind _ n = Tree.NodeSummary.kind n.summary
+let summary _ n = n.summary
 
 let rec print_attributes ?(sep=true) out tree_ node =
-  if (node.kind == Tree.NodeKind.Attribute) then
+  if (kind tree_ node == Tree.NodeKind.Attribute) then
     let tag = QName.to_string node.tag in
     if sep then output_char out ' ';
     output_string out tag;
@@ -286,7 +295,7 @@ let rec print_xml out tree_ node =
   if node != nil then
   let () =
     let open Tree.NodeKind in
-    match node.kind with
+    match kind tree_ node with
     | Node -> ()
     | Text -> output_escape_string out node.data
     | Element | Document ->
@@ -326,7 +335,6 @@ let next_sibling _ n = n.next_sibling
 let parent _ n = n.parent
 let tag _ n = n.tag
 let data _ n = n.data
-let kind _ n = n.kind
 let preorder _ n = n.preorder
 let by_preorder t i =
  if i >= 0 && i < t.size then Array.unsafe_get t.by_preorder i