Back to index

texmacs  1.0.7.15
Defines | Functions | Variables
observer.cpp File Reference
#include "modification.hpp"
#include "analyze.hpp"
#include "hashmap.hpp"
#include "blackbox.hpp"

Go to the source code of this file.

Defines

#define DETACHED   (-5)

Functions

static void consistency_check (tree t, path ip)
void consistency_check ()
void stretched_print (tree t, bool ips, int indent)
tm_ostreamoperator<< (tm_ostream &out, observer o)
static void simplify (observer &obs)
static void detach (tree &ref, tree closest, bool right)
void raw_assign (tree &ref, tree t)
void raw_insert (tree &ref, int pos, tree t)
void raw_remove (tree &ref, int pos, int nr)
void raw_split (tree &ref, int pos, int at)
void raw_join (tree &ref, int pos)
void raw_assign_node (tree &ref, tree_label op)
void raw_insert_node (tree &ref, int pos, tree t)
void raw_remove_node (tree &ref, int pos)
void raw_set_cursor (tree &ref, int pos, tree data)
void raw_apply (tree &t, modification mod)
bool busy_path (path p)
bool busy_tree (tree &ref)
void apply (tree &ref, modification mod)
void assign (tree &ref, tree t)
void insert (tree &ref, int pos, tree t)
void remove (tree &ref, int pos, int nr)
void split (tree &ref, int pos, int at)
void join (tree &ref, int pos)
void assign_node (tree &ref, tree_label op)
void insert_node (tree &ref, int pos, tree t)
void remove_node (tree &ref, int pos)
void set_cursor (tree &ref, int pos, tree data)
void touch (tree &ref)
void assign (path p, tree t)
void insert (path p, tree ins)
void remove (path p, int nr)
void split (path p)
void join (path p)
void assign_node (path p, tree_label op)
void insert_node (path p, tree ins)
void remove_node (path p)
void set_cursor (path p, tree data)
void touch (path p)

Variables

observer nil_observer
tree the_et
bool packrat_invalid_colors
bool versioning_busy = false
static bool is_busy = false
static list< pathbusy_paths
static list< modificationupcoming

Define Documentation

#define DETACHED   (-5)

Definition at line 17 of file observer.cpp.


Function Documentation

void apply ( tree ref,
modification  mod 
)

Definition at line 389 of file observer.cpp.

                                    {
  if (!is_applicable (ref, mod)) {
    cout << "mod= " << mod << "\n";
    cout << "ref= " << ref << "\n";
    ASSERT (is_applicable (ref, mod), "invalid modification");
  }
  path ip= obtain_ip (ref);
  path rp= reverse (ip);
  path p = rp * root (mod);
  if (versioning_busy) raw_apply (ref, mod);
  else if (is_busy) {
    if (ip_attached (ip) && !busy_path (p)) {
      //cout << "Postpone " << (reverse (ip) * mod) << "\n";
      busy_paths= busy_paths * p;
      upcoming  = upcoming * (reverse (ip) * mod);
    }
  }
  else {
    if (!ip_attached (ip)) raw_apply (ref, mod);
    else {
      is_busy= true;
      busy_paths= list<path> (p);
      upcoming  = list<modification> (reverse (ip) * mod);
      while (!is_nil (upcoming)) {
       //cout << "Handle " << upcoming->item << "\n";
       raw_apply (the_et, upcoming->item);
       //cout << "Done " << upcoming->item << "\n";
       upcoming= upcoming->next;
      }
      busy_paths= list<path> ();
      is_busy= false;
      if (has_subtree (the_et, rp))
       ref= subtree (the_et, rp);
    }
  }
}

Here is the call graph for this function:

void assign ( tree ref,
tree  t 
)

Definition at line 427 of file observer.cpp.

                           {
  apply (ref, mod_assign (path (), t));
}

Here is the call graph for this function:

void assign ( path  p,
tree  t 
)

Definition at line 483 of file observer.cpp.

                        {
  assign (subtree (the_et, p), t);
}

Here is the call graph for this function:

void assign_node ( tree ref,
tree_label  op 
)

Definition at line 452 of file observer.cpp.

                                       {
  apply (ref, mod_assign_node (path (), op));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void assign_node ( path  p,
tree_label  op 
)

Definition at line 511 of file observer.cpp.

                                    {
  assign_node (subtree (the_et, p), op);
}

Here is the call graph for this function:

bool busy_path ( path  p)

Definition at line 375 of file observer.cpp.

                   {
  for (list<path> l= busy_paths; !is_nil (l); l= l->next)
    if (l->item <= p) return true;
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool busy_tree ( tree ref)

Definition at line 382 of file observer.cpp.

                      {
  path ip= obtain_ip (ref);
  if (ip_attached (ip)) return busy_path (reverse (ip));
  else return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void consistency_check ( tree  t,
path  ip 
) [static]

Definition at line 28 of file observer.cpp.

                                    {
  if (obtain_ip (t) != ip)
    cout << "Wrong ip] " << t << " " << obtain_ip (t)
        << " instead of " << ip << "\n";
  if (is_compound (t)) {
    int i, n= N(t);
    for (i=0; i<n; i++) {
      //if (!strong_equal (ip, obtain_ip (t[i])->next))
      if (obtain_ip (t) != obtain_ip (t[i])->next)
       cout << "Bad node] " << t << " " << obtain_ip (t) << " #" << i << "\n";
      consistency_check (t[i], path (i, ip));
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 44 of file observer.cpp.

Here is the call graph for this function:

static void detach ( tree ref,
tree  closest,
bool  right 
) [static]

Definition at line 102 of file observer.cpp.

                                             {
  if (!is_nil (ref->obs)) {
    ref->obs->notify_detach (ref, closest, right);
    simplify (ref->obs);
  }
  if (is_compound (ref)) {
    int i, n= N(ref);
    for (i=0; i<n; i++)
      detach (ref[i], closest, right);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void insert ( tree ref,
int  pos,
tree  t 
)

Definition at line 432 of file observer.cpp.

                                    {
  apply (ref, mod_insert (path (), pos, t));
}

Here is the call graph for this function:

void insert ( path  p,
tree  ins 
)

Definition at line 488 of file observer.cpp.

                          {
  insert (subtree (the_et, path_up (p)), last_item (p), ins);
}

Here is the call graph for this function:

void insert_node ( tree ref,
int  pos,
tree  t 
)

Definition at line 457 of file observer.cpp.

                                         {
  apply (ref, mod_insert_node (path (), pos, t));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void insert_node ( path  p,
tree  ins 
)

Definition at line 516 of file observer.cpp.

                               {
  insert_node (subtree (the_et, path_up (p)), last_item (p), ins);
}

Here is the call graph for this function:

void join ( tree ref,
int  pos 
)

Definition at line 447 of file observer.cpp.

                          {
  apply (ref, mod_join (path (), pos));
}

Here is the call graph for this function:

void join ( path  p)

Definition at line 506 of file observer.cpp.

              {
  join (subtree (the_et, path_up (p)), last_item (p));
}

Here is the call graph for this function:

tm_ostream& operator<< ( tm_ostream out,
observer  o 
)

Definition at line 68 of file observer.cpp.

                                          {
  out << "<observer";
  if (is_nil (o)) out << " null";
  else o->print (out);
  out << ">";
  return out;
}

Here is the call graph for this function:

void raw_apply ( tree t,
modification  mod 
)

Definition at line 331 of file observer.cpp.

                                      {
  ASSERT (is_applicable (t, mod), "invalid modification");
  switch (mod->k) {
  case MOD_ASSIGN:
    raw_assign (subtree (t, root (mod)), mod->t);
    break;
  case MOD_INSERT:
    raw_insert (subtree (t, root (mod)), index (mod), mod->t);
    break;
  case MOD_REMOVE:
    raw_remove (subtree (t, root (mod)), index (mod), argument (mod));
    break;
  case MOD_SPLIT:
    raw_split (subtree (t, root (mod)), index (mod), argument (mod));
    break;
  case MOD_JOIN:
    raw_join (subtree (t, root (mod)), index (mod));
    break;
  case MOD_ASSIGN_NODE:
    raw_assign_node (subtree (t, root (mod)), L (mod));
    break;
  case MOD_INSERT_NODE:
    raw_insert_node (subtree (t, root (mod)), argument (mod), mod->t);
    break;
  case MOD_REMOVE_NODE:
    raw_remove_node (subtree (t, root (mod)), index (mod));
    break;
  case MOD_SET_CURSOR:
    raw_set_cursor (subtree (t, root (mod)), index (mod), mod->t);
    break;
  }
  packrat_invalid_colors= true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_assign ( tree ref,
tree  t 
)

Definition at line 115 of file observer.cpp.

                               {
  // cout << "Assign " << ref << " := " << t << "\n";
  modification mod= mod_assign (path (), t);
  if (!is_nil (ref->obs)) {
    ref->obs->announce (ref, mod);
    ref->obs->notify_assign (ref, t);
    simplify (ref->obs);
  }
  if (is_compound (ref)) {
    int i, n= N(ref), mid= (n+1)>>1;
    for (i=0; i<n; i++)
      detach (ref[i], t, i >= mid);
  }
  ref= t;
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_assign_node ( tree ref,
tree_label  op 
)

Definition at line 264 of file observer.cpp.

                                           {
  // cout << "Assign node " << ref << " : " << tree (op) << "\n";
  modification mod= mod_assign_node (path (), op);
  if (!is_nil (ref->obs)) {
    ref->obs->announce (ref, mod);
    ref->obs->notify_assign_node (ref, op);
    simplify (ref->obs);
  }
  LR (ref)= op;
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_insert ( tree ref,
int  pos,
tree  t 
)

Definition at line 135 of file observer.cpp.

                                        {
  // cout << "Insert " << ref << " += " << t << " at " << pos << "\n";
  modification mod= mod_insert (path (), pos, t);
  if (!is_nil (ref->obs))
    ref->obs->announce (ref, mod);
  if (is_atomic (ref) && is_atomic (t))
    ref->label= ref->label (0, pos) *t->label* ref->label (pos, N(ref->label));
  else {
    int i, n= N(ref), nr= N(t);
    AR(ref)->resize (n+nr);
    for (i=n-1; i>=pos; i--)
      ref[i+nr]= ref[i];
    for (i=0; i<nr; i++)
      ref[pos+i]= t[i];
  }
  if (!is_nil (ref->obs)) {
    ref->obs->notify_insert (ref, pos, is_atomic (t)? N(t->label): N(t));
    simplify (ref->obs);
  }
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_insert_node ( tree ref,
int  pos,
tree  t 
)

Definition at line 279 of file observer.cpp.

                                             {
  // cout << "Insert node " << ref << " : " << t << " at " << pos << "\n";
  modification mod= mod_insert_node (path (), pos, t);
  if (!is_nil (ref->obs)) ref->obs->announce (ref, mod);
  int i, n= N(t);
  tree r (t, n+1);
  for (i=0; i<pos; i++) r[i]= t[i];
  r[pos]= ref;
  for (i=pos; i<n; i++) r[i+1]= t[i];
  ref= r;
  if (!is_nil (ref[pos]->obs)) {
    ref[pos]->obs->notify_insert_node (ref, pos);
    simplify (ref[pos]->obs);
  }
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_join ( tree ref,
int  pos 
)

Definition at line 228 of file observer.cpp.

                              {
  // cout << "Join " << ref << " at " << pos << "\n";
  // the following code is added for security
  if (is_atomic (ref[pos]) && (!is_atomic (ref[pos+1])))
    insert_node (ref[pos], 0, tree (L(ref[pos+1])));
  if (is_atomic (ref[pos+1]) && (!is_atomic (ref[pos])))
    insert_node (ref[pos+1], 0, tree (L(ref[pos])));
  // end security code

  modification mod= mod_join (path (), pos);
  if (!is_nil (ref->obs)) ref->obs->announce (ref, mod);
  tree t1= ref[pos], t2= ref[pos+1], t;
  int offset= is_atomic (ref)? N(t1->label): N(t1);
  if (is_atomic (t1) && is_atomic (t2)) t= t1->label * t2->label;
  else t= t1 * t2;
  if (!is_nil (ref->obs)) ref->obs->notify_join (ref, pos, t);
  if (!is_nil (t1->obs)) {
    t1->obs->notify_var_join (t1, t, 0);
    simplify (t1->obs);
  }
  if (!is_nil (t2->obs)) {
    t2->obs->notify_var_join (t2, t, offset);
    simplify (t2->obs);
  }
  ref[pos]= t;

  int i, n= N(ref)-1;
  for (i=pos+1; i<n; i++)
    ref[i]= ref[i+1];
  AR(ref)->resize (n);
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_remove ( tree ref,
int  pos,
int  nr 
)

Definition at line 160 of file observer.cpp.

                                        {
  // cout << "Remove " << ref << " -= " << nr << " at " << pos << "\n";
  modification mod= mod_remove (path (), pos, nr);
  if (nr == 0) return;
  if (!is_nil (ref->obs)) {
    ref->obs->announce (ref, mod);
    ref->obs->notify_remove (ref, pos, nr);
    simplify (ref->obs);
  }
  if (is_compound (ref)) {
    int i, n= N(ref), end= pos+nr, mid= (pos+end+1) >> 1;
    for (i=pos; i<mid; i++)
      if (pos == 0) detach (ref[i], ref, false);
      else detach (ref[i], ref[pos-1], true);
    for (; i<end; i++)
      if (end == n) detach (ref[i], ref, true);
      else detach (ref[i], ref[pos+nr], false);
  }

  if (is_atomic (ref))
    ref->label= ref->label (0, pos) * ref->label (pos+nr, N(ref->label));
  else {
    int i, n= N(ref)-nr;
    for (i=pos; i<n; i++)
      ref[i]= ref[i+nr];
    AR(ref)->resize (n);
  }
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_remove_node ( tree ref,
int  pos 
)

Definition at line 299 of file observer.cpp.

                                     {
  // cout << "Remove node " << ref << " : " << pos << "\n";
  modification mod= mod_remove_node (path (), pos);
  if (!is_nil (ref->obs)) {
    ref->obs->announce (ref, mod);
    ref->obs->notify_remove_node (ref, pos);
    simplify (ref->obs);
  }
  for (int i=0; i<N(ref); i++)
    if (i < pos) detach (ref[i], ref[pos], false);
    else if (i > pos) detach (ref[i], ref[pos], true);
  ref= ref[pos];
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_set_cursor ( tree ref,
int  pos,
tree  data 
)

Definition at line 317 of file observer.cpp.

                                               {
  // cout << "Set cursor " << ref << " : " << pos << ", " << data << "\n";
  modification mod= mod_set_cursor (path (), pos, data);
  if (!is_nil (ref->obs)) {
    ref->obs->announce (ref, mod);
    ref->obs->notify_set_cursor (ref, pos, data);
    simplify (ref->obs);
  }
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void raw_split ( tree ref,
int  pos,
int  at 
)

Definition at line 193 of file observer.cpp.

                                       {
  // cout << "Split " << ref << " at " << pos << ", " << at << "\n";
  modification mod= mod_split (path (), pos, at);
  if (!is_nil (ref->obs))
    ref->obs->announce (ref, mod);
  tree t= ref[pos], t1, t2;
  if (is_atomic (ref[pos])) {    
    t1= ref[pos]->label (0, at);
    t2= ref[pos]->label (at, N(ref[pos]->label));
  }
  else {
    t1= ref[pos] (0, at);
    t2= ref[pos] (at, N(ref[pos]));
  }
  int i, n= N(ref);
  AR(ref)->resize (n+1);
  for (i=n; i>(pos+1); i--)
    ref[i]= ref[i-1];
  ref[pos  ]= t1;
  ref[pos+1]= t2;

  if (!is_nil (ref->obs)) {
    ref->obs->notify_split (ref, pos, t);
    simplify (ref->obs);
  }
  if (!is_nil (t->obs)) {
    t->obs->notify_var_split (t, t1, t2);
    simplify (t->obs);
  }
  if (!is_nil (ref->obs)) ref->obs->done (ref, mod);
  // stretched_print (ref, true, 1);
  // consistency_check ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void remove ( tree ref,
int  pos,
int  nr 
)

Definition at line 437 of file observer.cpp.

                                    {
  apply (ref, mod_remove (path (), pos, nr));
}

Here is the call graph for this function:

void remove ( path  p,
int  nr 
)

Definition at line 493 of file observer.cpp.

                        {
  remove (subtree (the_et, path_up (p)), last_item (p), nr);
}

Here is the call graph for this function:

void remove_node ( tree ref,
int  pos 
)

Definition at line 462 of file observer.cpp.

                                 {
  apply (ref, mod_remove_node (path (), pos));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void remove_node ( path  p)

Definition at line 521 of file observer.cpp.

Here is the call graph for this function:

void set_cursor ( tree ref,
int  pos,
tree  data 
)

Definition at line 467 of file observer.cpp.

                                           {
  apply (ref, mod_set_cursor (path (), pos, data));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void set_cursor ( path  p,
tree  data 
)

Definition at line 526 of file observer.cpp.

                               {
  if (is_inside (the_et, p))
    set_cursor (subtree (the_et, path_up (p)), last_item (p), data);
  else
    cout << "TeXmacs] warning: invalid cursor position " << p << "\n";
}

Here is the call graph for this function:

static void simplify ( observer obs) [static]

Definition at line 90 of file observer.cpp.

                         {
  if (is_nil (obs)) return;
  observer& o1= obs->get_child (0);
  observer& o2= obs->get_child (1);  
  if (!is_nil (o1) || !is_nil (o2)) {
    simplify (o1);
    simplify (o2);
    obs= list_observer (o1, o2);
  }
}

Here is the call graph for this function:

void split ( tree ref,
int  pos,
int  at 
)

Definition at line 442 of file observer.cpp.

                                   {
  apply (ref, mod_split (path (), pos, at));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void split ( path  p)

Definition at line 498 of file observer.cpp.

               {
  tree& st= subtree (the_et, path_up (path_up (p)));
  int   l1= last_item (path_up (p));
  int   l2= last_item (p);
  split (st, l1, l2);  
}

Here is the call graph for this function:

void stretched_print ( tree  t,
bool  ips,
int  indent 
)

Definition at line 50 of file observer.cpp.

                                               {
  int i;
  for (i=0; i<indent; i++) cout << "  ";
  if (is_atomic (t)) {
    cout << raw_quote (t->label);
    if (ips) cout << " -- " << obtain_ip (t);
    cout << "\n";
  }
  else {
    cout << as_string (L(t));
    if (ips) cout << " -- " << obtain_ip (t);
    cout << "\n";    
    for (i=0; i<N(t); i++)
      stretched_print (t[i], ips, indent+1);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void touch ( tree ref)

Definition at line 472 of file observer.cpp.

                  {
  //cout << "Touch " << ref << "\n";
  if (!is_nil (ref->obs))
    ref->obs->touched (ref, path ());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void touch ( path  p)

Definition at line 534 of file observer.cpp.

               {
  touch (subtree (the_et, p));
}

Here is the call graph for this function:


Variable Documentation

list<path> busy_paths [static]

Definition at line 371 of file observer.cpp.

bool is_busy = false [static]

Definition at line 370 of file observer.cpp.

Definition at line 19 of file observer.cpp.

Definition at line 17 of file packrat_parser.cpp.

Definition at line 18 of file new_document.cpp.

Definition at line 372 of file observer.cpp.

bool versioning_busy = false

Definition at line 369 of file observer.cpp.