Back to index

texmacs  1.0.7.15
Functions | Variables
packrat_parser.cpp File Reference
#include "packrat_parser.hpp"
#include "analyze.hpp"
#include "drd_std.hpp"

Go to the source code of this file.

Functions

packrat_parser make_packrat_parser (string lan, tree in)
packrat_parser make_packrat_parser (string lan, tree in, path in_pos)
bool starts (tree t, string s)
static bool empty_line (tree t)
static bool consistent_portion (tree t, int begin, int end)
static void consistent_enlargement (tree t, int &begin, int &end)
path packrat_parse (string lan, string sym, tree in)
bool packrat_correct (string lan, string sym, tree in)
bool packrat_available_path (string lan, tree in, path in_p)
object packrat_context (string lan, string s, tree in, path in_pos)
bool packrat_select (string lan, string s, tree in, path in_pos, path &p1, path &p2, int mode)
void packrat_highlight_subtree (string lan, string s, tree in)
void packrat_highlight (string lan, string s, tree in)

Variables

tree the_et
bool packrat_invalid_colors = false

Function Documentation

static void consistent_enlargement ( tree  t,
int &  begin,
int &  end 
) [static]

Definition at line 708 of file packrat_parser.cpp.

                                                      {
  while (begin > 0 || end < N(t)) {
    while (begin > 0    && !empty_line (t[begin-1])) begin--;
    while (end   < N(t) && !empty_line (t[end    ])) end++;
    if (consistent_portion (t, begin, end)) return;
    //cout << "Inconsistent " << begin << " -- " << end << "\n";
    begin= max (0   , begin - max (end - begin, 1));
    end  = min (N(t), end   + max (end - begin, 1));
    //cout << "  Try " << begin << " -- " << end << "\n";
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool consistent_portion ( tree  t,
int  begin,
int  end 
) [static]

Definition at line 688 of file packrat_parser.cpp.

                                                {
  int level= 0;
  for (int i=begin; i<end; i++)
    if (is_atomic (t[i])) {
      string s= t[i]->label;
      for (int j=0; j<N(s); j++)
       switch (s[j]) {
       case '(': level++; break;
       case ')': if (level <= 0) return false; level--; break;
       case '[': level++; break;
       case ']': if (level <= 0) return false; level--; break;
       case '{': level++; break;
       case '}': if (level <= 0) return false; level--; break;
       default : break;
       }
    }
  return level == 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool empty_line ( tree  t) [static]

Definition at line 679 of file packrat_parser.cpp.

                    {
  if (!is_atomic (t)) return false;
  string s= t->label;
  for (int i=0; i<N(s); i++)
    if (s[i] != ' ') return false;
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 39 of file packrat_parser.cpp.

                                          {
  static string         last_lan   = "";
  static tree           last_in    = "";
  static packrat_parser last_par;
  if (lan != last_lan || in != last_in) {
    packrat_grammar gr= find_packrat_grammar (lan);
    last_lan   = lan;
    last_in    = copy (in);
    last_par   = packrat_parser (gr, in);
  }
  return last_par;
}

Here is the call graph for this function:

Here is the caller graph for this function:

packrat_parser make_packrat_parser ( string  lan,
tree  in,
path  in_pos 
)

Definition at line 53 of file packrat_parser.cpp.

                                                       {
  static string         last_lan   = "";
  static tree           last_in    = "";
  static path           last_in_pos= path ();
  static packrat_parser last_par;
  if (lan != last_lan || in != last_in || in_pos != last_in_pos) {
    packrat_grammar gr= find_packrat_grammar (lan);
    last_lan   = lan;
    last_in    = copy (in);
    last_in_pos= copy (last_in_pos);
    last_par   = packrat_parser (gr, in, in_pos);
  }
  return last_par;
}

Here is the call graph for this function:

bool packrat_available_path ( string  lan,
tree  in,
path  in_p 
)

Definition at line 739 of file packrat_parser.cpp.

                                                        {
  packrat_parser par= make_packrat_parser (lan, in);
  return par->current_start->contains (in_p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

object packrat_context ( string  lan,
string  s,
tree  in,
path  in_pos 
)

Definition at line 745 of file packrat_parser.cpp.

                                                             {
  //cout << "Context " << in << " at " << in_pos
  //     << " (" << lan << ", " << s << ")" << LF;
  packrat_parser par= make_packrat_parser (lan, in);
  C sym= encode_symbol (compound ("symbol", s));
  if (par->parse (sym, 0) != N(par->current_input))
    par= make_packrat_parser (lan, in, in_pos);
  C pos= par->encode_tree_position (in_pos);
  if (pos == PACKRAT_FAILED) return object (false);
  array<C> kind, begin, end;
  par->context (sym, 0, pos-1, pos+1, 0, kind, begin, end);
  par->compress (kind, begin, end);
  object ret= null_object ();
  for (int i=0; i<N(kind); i++) {
    object x1 (symbol_object (packrat_decode[kind[i]][0]->label));
    object x2 (par->decode_tree_position (begin[i]));
    object x3 (par->decode_tree_position (end[i]));
    ret= cons (list_object (x1, x2, x3), ret);
  }
  return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool packrat_correct ( string  lan,
string  sym,
tree  in 
)

Definition at line 732 of file packrat_parser.cpp.

                                                  {
  packrat_parser par= make_packrat_parser (lan, in);
  C pos= par->parse (encode_symbol (compound ("symbol", sym)), 0);
  return pos == N(par->current_input);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void packrat_highlight ( string  lan,
string  s,
tree  in 
)

Definition at line 824 of file packrat_parser.cpp.

                                                  {
  int hl_lan= packrat_abbreviation (lan, s);
  if (hl_lan == 0) return;
  //cout << "Highlight " << in << "\n";
  if (is_func (in, DOCUMENT)) {
    int i, begin, end;
    for (begin=0; begin<N(in); begin++)
      if (!has_highlight (in[begin], hl_lan))
       break;
    for (end=N(in)-1; end>begin; end--)
      if (!has_highlight (in[end-1], hl_lan))
       break;
    consistent_enlargement (in, begin, end);    
    for (i=begin; i<end; i++)
      detach_highlight (in[i], hl_lan);
    attach_highlight (in, hl_lan);
    packrat_highlight_subtree (lan, s, in (begin, end));
  }
  else {
    if (is_compound (in))
      for (int i=0; i<N(in); i++)
       detach_highlight (in[i], hl_lan);
    attach_highlight (in, hl_lan);
    packrat_highlight_subtree (lan, s, in);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void packrat_highlight_subtree ( string  lan,
string  s,
tree  in 
)

Definition at line 811 of file packrat_parser.cpp.

                                                          {
  //cout << "Highlight " << lan << ", " << s << " in " << in << "\n";
  int hl_lan= packrat_abbreviation (lan, s);
  if (hl_lan == 0) return;
  packrat_parser par= make_packrat_parser (lan, in);
  C sym = encode_symbol (compound ("symbol", s));
  if (par->parse (sym, 0) == N(par->current_input)) {
    par->current_hl_lan= hl_lan;
    par->highlight (sym, 0);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

path packrat_parse ( string  lan,
string  sym,
tree  in 
)

Definition at line 725 of file packrat_parser.cpp.

                                                {
  packrat_parser par= make_packrat_parser (lan, in);
  C pos= par->parse (encode_symbol (compound ("symbol", sym)), 0);
  return par->decode_tree_position (pos);
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool packrat_select ( string  lan,
string  s,
tree  in,
path  in_pos,
path p1,
path p2,
int  mode 
)

Definition at line 768 of file packrat_parser.cpp.

{
  // mode= 0: genuine semantic selection
  // mode= 1: strictly larger selection for select_enlarge
  // mode= 2: determine environment rectangles
  if (path_less (p2, p1))
    return packrat_select (lan, s, in, in_pos, p2, p1, mode);
  //cout << "Enlarge " << p1 << " -- " << p2 << " in " << in
  //<< " (" << lan << ", " << s << ")" << LF;
  packrat_parser par= make_packrat_parser (lan, in);
  C sym = encode_symbol (compound ("symbol", s));
  if (par->parse (sym, 0) != N(par->current_input))
    par= make_packrat_parser (lan, in, in_pos);
  C pos1= par->encode_tree_position (p1);
  C pos2= par->encode_tree_position (p2);
  //cout << "Encoded " << pos1 << " -- " << pos2
  //     << " in " << par->current_string << LF;
  if (par->parse (sym, 0) != N(par->current_input)) return false;
  if (pos1 == PACKRAT_FAILED || pos2 == PACKRAT_FAILED) return false;
  array<C> kind, begin, end;
  C pos0= pos1;
  if ((mode == 1 && pos1 == pos2) || mode == 2) pos0= max (pos1 - 1, 0);
  par->context (sym, 0, pos0, pos2, mode, kind, begin, end);
  //for (int i=0; i<N(kind); i++)
  //  cout << i << ":\t"
  //       << par->decode_tree_position (begin[i]) << "\t"
  //       << par->decode_tree_position (end[i]) << "\t"
  //       << packrat_decode[kind[i]] << LF;
  par->compress (kind, begin, end);
  int n= N(kind);
  if (n == 0) return false;
  if (mode == 1) {
    if (pos1 == begin[n-1] && pos2 == end[n-1]) n--;
    if (n == 0) return false;
  }
  p1= par->decode_tree_position (begin[n-1]);
  p2= par->decode_tree_position (end[n-1]);
  //cout << "Selected " << packrat_decode[kind[n-1]] << LF;
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool starts ( tree  t,
string  s 
)

Definition at line 175 of file packrat_parser.cpp.

                          {
  return is_atomic (t) && starts (t->label, s);
}

Here is the call graph for this function:


Variable Documentation

bool packrat_invalid_colors = false

Definition at line 17 of file packrat_parser.cpp.

Definition at line 18 of file new_document.cpp.