Back to index

texmacs  1.0.7.15
Functions | Variables
packrat_grammar.cpp File Reference
#include "packrat_grammar.hpp"
#include "analyze.hpp"
#include "iterator.hpp"

Go to the source code of this file.

Functions

 RESOURCE_CODE (packrat_grammar)
C new_token (string s)
C encode_token (string s)
array< Cencode_tokens (string s)
C new_symbol (tree t)
C encode_symbol (tree t)
void initialize_color_encodings ()
void initialize_color_decodings ()
int encode_color (string s)
string decode_color (int c)
bool left_recursive (string s, tree t)
tree left_head (string s, tree t)
tree left_tail (string s, tree t)
array< Csingleton (C c)
packrat_grammar make_packrat_grammar (string s)
packrat_grammar find_packrat_grammar (string s)
void packrat_define (string lan, string s, tree t)
void packrat_property (string lan, string s, string var, string val)
void packrat_inherit (string lan, string from)
int packrat_abbreviation (string lan, string s)

Variables

tree packrat_uninit (UNINIT)
int packrat_nr_tokens = 256
int packrat_nr_symbols = 0
hashmap< string, Cpackrat_tokens
hashmap< tree, Cpackrat_symbols
hashmap< C, treepackrat_decode (packrat_uninit)
hashmap< string, int > color_encoding
hashmap< int, stringcolor_decoding

Function Documentation

string decode_color ( int  c)

Definition at line 155 of file packrat_grammar.cpp.

                     {
  if (N(color_decoding) == 0) initialize_color_decodings ();
  if (color_decoding->contains (c)) return color_decoding[c];
  else return "";
}

Here is the call graph for this function:

Here is the caller graph for this function:

int encode_color ( string  s)

Definition at line 148 of file packrat_grammar.cpp.

                        {
  if (N(color_encoding) == 0) initialize_color_encodings ();
  if (color_encoding->contains (s)) return color_encoding[s];
  else return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 79 of file packrat_grammar.cpp.

                       {
  if (!packrat_symbols->contains (t)) {
    C sym= new_symbol (t);
    packrat_symbols (t)= sym;
    packrat_decode  (sym)= t;
  }
  return packrat_symbols[t];
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 43 of file packrat_grammar.cpp.

                        {
  if (!packrat_tokens->contains (s)) {
    int pos= 0;
    tm_char_forwards (s, pos);
    if (pos == 0 || pos != N(s)) return -1;
    C sym= new_token (s);
    packrat_tokens (s)= sym;
    packrat_decode (sym)= s;
  }
  return packrat_tokens[s];
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 56 of file packrat_grammar.cpp.

                         {
  int pos= 0;
  array<C> ret;
  while (pos < N(s)) {
    int old= pos;
    tm_char_forwards (s, pos);
    ret << encode_token (s (old, pos));
  }
  return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

packrat_grammar find_packrat_grammar ( string  s)

Definition at line 266 of file packrat_grammar.cpp.

                                {
  if (packrat_grammar::instances -> contains (s)) return packrat_grammar (s);
  eval ("(lazy-language-force " * s * ")");
  return make_packrat_grammar (s);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 120 of file packrat_grammar.cpp.

                              {
  color_decoding (-1)= "red";
  color_decoding (1)= "brown";
  color_decoding (2)= "dark green";
  color_decoding (3)= "dark red";
  color_decoding (10)= "#4040c0";
  color_decoding (11)= "#4040c0";
  color_decoding (12)= "#4040c0";
  color_decoding (13)= "#4040c0";
  color_decoding (14)= "#4040c0";
  color_decoding (15)= "#4040c0";
  color_decoding (16)= "#4040c0";
  color_decoding (17)= "#4040c0";
  color_decoding (20)= "#606060";
  color_decoding (21)= "#606060";
  color_decoding (22)= "#606060";
  color_decoding (23)= "#00c000";
  color_decoding (24)= "#00c000";
  color_decoding (25)= "#00c000";
  color_decoding (30)= "#0000c0";
  color_decoding (31)= "#0000c0";
  color_decoding (32)= "#0000c0";
  color_decoding (33)= "#0000c0";
  color_decoding (34)= "#0000c0";
  color_decoding (35)= "#0000c0";
}

Here is the caller graph for this function:

Definition at line 93 of file packrat_grammar.cpp.

                              {
  color_encoding ("comment")= 1;
  color_encoding ("keyword")= 2;
  color_encoding ("error")= 3;
  color_encoding ("constant")= 10;
  color_encoding ("constant_identifier")= 11;
  color_encoding ("constant_function")= 12;
  color_encoding ("constant_type")= 13;
  color_encoding ("constant_category")= 14;
  color_encoding ("constant_module")= 15;
  color_encoding ("constant_number")= 16;
  color_encoding ("constant_string")= 17;
  color_encoding ("variable")= 20;
  color_encoding ("variable_identifier")= 21;
  color_encoding ("variable_function")= 22;
  color_encoding ("variable_type")= 23;
  color_encoding ("variable_category")= 24;
  color_encoding ("variable_module")= 25;
  color_encoding ("declare")= 30;
  color_encoding ("declare_identifier")= 31;
  color_encoding ("declare_function")= 32;
  color_encoding ("declare_type")= 33;
  color_encoding ("declare_category")= 34;
  color_encoding ("declare_module")= 35;
}

Here is the caller graph for this function:

tree left_head ( string  s,
tree  t 
)

Definition at line 183 of file packrat_grammar.cpp.

                             {
  if (is_atomic (t)) return t;
  else if (is_compound (t, "symbol", 1) && is_atomic (t[0])) {
    if (t[0]->label == s) return compound ("or");
    else return t;
  }
  else if (is_compound (t, "or")) {
    tree r= compound ("or");
    for (int i=0; i<N(t); i++) {
      tree h= left_head (s, t[i]);
      if (is_compound (h, "or")) r << A(h);
      else r << h;
    }
    return r;
  }
  else if (is_compound (t, "concat")) {
    if (N(t) == 0 || !left_recursive (s, t)) return t;
    else return left_head (s, t[0]);
  }
  else return t;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool left_recursive ( string  s,
tree  t 
)

Definition at line 166 of file packrat_grammar.cpp.

                                  {
  if (is_atomic (t))
    return false;
  else if (is_compound (t, "symbol", 1) && is_atomic (t[0]))
    return t[0]->label == s;
  else if (is_compound (t, "or")) {
    for (int i=0; i<N(t); i++)
      if (left_recursive (s, t[i]))
       return true;
    return false;
  }
  else if (is_compound (t, "concat"))
    return N(t) > 0 && left_recursive (s, t[0]);
  else return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree left_tail ( string  s,
tree  t 
)

Definition at line 206 of file packrat_grammar.cpp.

                             {
  if (is_atomic (t)) return t;
  else if (is_compound (t, "symbol", 1) && is_atomic (t[0])) {
    if (t[0]->label == s) return compound ("concat");
    else return compound ("or");
  }
  else if (is_compound (t, "or")) {
    tree r= compound ("or");
    for (int i=0; i<N(t); i++) {
      tree u= left_tail (s, t[i]);
      if (is_compound (u, "or")) r << A(u);
      else r << u;
    }
    return r;
  }
  else if (is_compound (t, "concat")) {
    if (N(t) == 0 || !left_recursive (s, t)) return compound ("or");
    else {
      tree r= compound ("concat");
      tree u= left_tail (s, t[0]);
      if (u == compound ("or")) return u;
      else if (is_compound (u, "concat")) r << A(u);
      else r << u;
      r << A (t (1, N(t)));
      return r;
    }
  }
  else compound ("or");
}

Here is the call graph for this function:

Here is the caller graph for this function:

packrat_grammar make_packrat_grammar ( string  s)

Definition at line 260 of file packrat_grammar.cpp.

                                {
  if (packrat_grammar::instances -> contains (s)) return packrat_grammar (s);
  return make (packrat_grammar, s, tm_new<packrat_grammar_rep> (s));
}

Here is the call graph for this function:

Here is the caller graph for this function:

C new_symbol ( tree  t)

Definition at line 68 of file packrat_grammar.cpp.

                    {
  if (is_atomic (t)) {
    C ret= encode_token (t->label);
    if (ret != -1) return ret;
  }
  C ret= (packrat_nr_symbols++) + PACKRAT_SYMBOLS;
  //cout << "Encode " << t << " -> " << ret << LF;
  return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

C new_token ( string  s)

Definition at line 32 of file packrat_grammar.cpp.

                     {
  if (N(s) == 1)
    return (C) (unsigned char) s[0];
  else {
    C ret= packrat_nr_tokens++;
    //cout << "Encode " << s << " -> " << ret << LF;
    return ret;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int packrat_abbreviation ( string  lan,
string  s 
)

Definition at line 486 of file packrat_grammar.cpp.

                                            {
  static int nr= 1;
  static hashmap<string,int> abbrs (-1);
  string key= lan * ":" * s;
  int r= abbrs[key];
  if (r >= 0) return r;
  packrat_grammar gr= find_packrat_grammar (lan);
  C sym= encode_symbol (compound ("symbol", s));
  if (gr->grammar->contains (sym)) r= nr++;
  else r= 0;
  abbrs (key)= r;
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void packrat_define ( string  lan,
string  s,
tree  t 
)

Definition at line 454 of file packrat_grammar.cpp.

                                              {
  packrat_grammar gr= find_packrat_grammar (lan);
  gr->define (s, t);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void packrat_inherit ( string  lan,
string  from 
)

Definition at line 466 of file packrat_grammar.cpp.

                                          {
  packrat_grammar gr = find_packrat_grammar (lan);
  packrat_grammar inh= find_packrat_grammar (from);
  iterator<C>     it = iterate (inh->grammar);
  while (it->busy ()) {
    C sym= it->next ();
    //cout << "Inherit " << sym << " -> " << inh->grammar (sym) << LF;
    gr->grammar (sym)= inh->grammar (sym);
    gr->productions (sym)= inh->productions (sym);
  }

  iterator<D> it2 = iterate (inh->properties);
  while (it2->busy ()) {
    D p= it2->next ();
    //cout << "Inherit " << p << " -> " << inh->properties (p) << LF;
    gr->properties (p)= inh->properties (p);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void packrat_property ( string  lan,
string  s,
string  var,
string  val 
)

Definition at line 460 of file packrat_grammar.cpp.

                                                                {
  packrat_grammar gr= find_packrat_grammar (lan);
  gr->set_property (s, var, val);
}

Here is the call graph for this function:

Here is the caller graph for this function:

RESOURCE_CODE ( packrat_grammar  )
array<C> singleton ( C  c)

Definition at line 241 of file packrat_grammar.cpp.

                {
  array<C> ret;
  ret << c;
  return ret;
}

Variable Documentation

Definition at line 23 of file packrat_grammar.cpp.

Definition at line 22 of file packrat_grammar.cpp.

Definition at line 18 of file packrat_grammar.cpp.

int packrat_nr_tokens = 256

Definition at line 17 of file packrat_grammar.cpp.

Definition at line 20 of file packrat_grammar.cpp.

Definition at line 19 of file packrat_grammar.cpp.