Follow this link to skip to the main content

claraty::Parse_Tree Class Reference

#include <parse_tree.h>

Collaboration diagram for claraty::Parse_Tree:

Collaboration graph
[legend]
List of all members.

Public Member Functions

int size () const
bool is_map () const
bool is_array () const
bool is_atom () const
bool is_null () const
Parse_Tree get_value (bool *ok=NULL) const
 operator::std::string () const
Parse_Treeoperator= (const Parse_Tree &rhs)
void clear ()
void make_map () const
void make_array () const
void make_atom () const
void make_null () const
void set_value (const Parse_Tree &value)
void set_value (const std::string &value)
void set_value (const char *value)
bool check_for_field (const std::string &label) const
Parse_Tree get_field (const std::string &label, bool *found=NULL) const
template<class T>
void set_field (const std::string &label, const T &value)
bool get_map (std::map< std::string, Parse_Tree > &map) const
Parse_Tree get_element (int i, bool *found=NULL) const
template<class T>
void set_element (int index, const T &value)
bool io (FDM_Map map)
bool read_from_string (const std::string &str)
bool read_from_stream (std::istream &is)
std::string write_to_string () const
std::ostream & write_to_stream (std::ostream &os) const
bool read_map_from_stream (std::istream &is)
bool read_array_from_stream (std::istream &is)
std::string pretty_print (int width=79, int tabsize=2, int current_indent=0) const
std::string pretty_print_atom (int width, int tabsize, int current_indent) const
std::string pretty_print_array (int width, int tabsize, int current_indent) const
std::string pretty_print_map (int width, int tabsize, int current_indent) const

Protected Attributes

Parse_Tree_Data_data

Detailed Description

Definition at line 70 of file parse_tree.h.


Constructor & Destructor Documentation

claraty::Parse_Tree::Parse_Tree (  ) 

Definition at line 77 of file parse_tree.cc.

Referenced by get_field().

00077                        :
00078   _data(new Parse_Tree_Data())
00079 {
00080 }

claraty::Parse_Tree::Parse_Tree ( const std::string &  parseStr  ) 

Definition at line 82 of file parse_tree.cc.

References read_from_string().

00082                                                 :
00083   _data(new Parse_Tree_Data())
00084 {
00085   read_from_string(parseStr);
00086 }

Here is the call graph for this function:

claraty::Parse_Tree::Parse_Tree ( const Parse_Tree rhs  ) 

Definition at line 88 of file parse_tree.cc.

References _data, and claraty::Parse_Tree_Data::ref().

00088                                             {
00089   rhs._data->ref();
00090   _data= rhs._data;
00091 }

Here is the call graph for this function:

claraty::Parse_Tree::Parse_Tree ( const Parse_Tree rhs,
bool  dummy_deep_copy 
)

Definition at line 93 of file parse_tree.cc.

00094   : _data(new Parse_Tree_Data(*rhs._data, true))
00095 {
00096 }

claraty::Parse_Tree::~Parse_Tree (  ) 

Definition at line 98 of file parse_tree.cc.

References _data, and claraty::Parse_Tree_Data::unref().

00098                         {
00099   _data->unref();
00100   _data= NULL;
00101 }

Here is the call graph for this function:

claraty::Parse_Tree::Parse_Tree (  ) 

Definition at line 77 of file parse_tree.cc.

Referenced by get_field().

00077                        :
00078   _data(new Parse_Tree_Data())
00079 {
00080 }

claraty::Parse_Tree::Parse_Tree ( const std::string &  parseStr  ) 

Definition at line 82 of file parse_tree.cc.

References read_from_string().

00082                                                 :
00083   _data(new Parse_Tree_Data())
00084 {
00085   read_from_string(parseStr);
00086 }

Here is the call graph for this function:

claraty::Parse_Tree::Parse_Tree ( const Parse_Tree rhs  ) 

Definition at line 88 of file parse_tree.cc.

References _data, and claraty::Parse_Tree_Data::ref().

00088                                             {
00089   rhs._data->ref();
00090   _data= rhs._data;
00091 }

Here is the call graph for this function:

claraty::Parse_Tree::Parse_Tree ( const Parse_Tree rhs,
bool  dummy_deep_copy 
)

Definition at line 93 of file parse_tree.cc.

00094   : _data(new Parse_Tree_Data(*rhs._data, true))
00095 {
00096 }

claraty::Parse_Tree::~Parse_Tree (  ) 

Definition at line 98 of file parse_tree.cc.

References _data, and claraty::Parse_Tree_Data::unref().

00098                         {
00099   _data->unref();
00100   _data= NULL;
00101 }

Here is the call graph for this function:


Member Function Documentation

int claraty::Parse_Tree::size (  )  const [inline]

Definition at line 85 of file parse_tree.h.

References claraty::Parse_Tree_Data::_array, _data, claraty::Parse_Tree_Data::_map, and is_map().

Referenced by claraty::FDM_Parse_Tree::_array_resizable_length(), and pretty_print_array().

00085                    {
00086     if (is_map()) return _data->_map.size();
00087     return _data->_array.size();
00088   }

Here is the call graph for this function:

bool claraty::Parse_Tree::is_map (  )  const [inline]

Definition at line 89 of file parse_tree.h.

References _data, claraty::Parse_Tree_Data::_nodetype, and claraty::Parse_Tree_Data::PT_MAP.

Referenced by get_field(), pretty_print_map(), and size().

00089                         {
00090     return _data->_nodetype == Parse_Tree_Data::PT_MAP;
00091   }

bool claraty::Parse_Tree::is_array (  )  const [inline]

Definition at line 92 of file parse_tree.h.

References _data, claraty::Parse_Tree_Data::_nodetype, and claraty::Parse_Tree_Data::PT_ARRAY.

Referenced by claraty::FDM_Parse_Tree::_end(), and pretty_print_array().

00092                         {
00093     return _data->_nodetype == Parse_Tree_Data::PT_ARRAY;
00094   }

bool claraty::Parse_Tree::is_atom (  )  const [inline]

Definition at line 95 of file parse_tree.h.

References _data, claraty::Parse_Tree_Data::_nodetype, and claraty::Parse_Tree_Data::PT_ATOM.

Referenced by operator::std::string(), and pretty_print_atom().

00095                         {
00096     return _data->_nodetype == Parse_Tree_Data::PT_ATOM;
00097   }

bool claraty::Parse_Tree::is_null (  )  const [inline]

Definition at line 98 of file parse_tree.h.

References _data, claraty::Parse_Tree_Data::_nodetype, and claraty::Parse_Tree_Data::PT_NULL.

Referenced by claraty::File_FDM_Parse_Tree::flush(), and get_value().

00098                         {
00099     return _data->_nodetype == Parse_Tree_Data::PT_NULL;
00100   }

Parse_Tree claraty::Parse_Tree::get_value ( bool *  ok = NULL  )  const [inline]

Definition at line 102 of file parse_tree.h.

References is_null().

Referenced by claraty::FDM_Parse_Tree::_io_object(), and claraty::FDM_Parse_Tree::io_primitive().

00102                                             {
00103     if (ok) *ok= !is_null();
00104     return *this;
00105   }

Here is the call graph for this function:

claraty::Parse_Tree::operator::std::string (  )  const [inline]

Definition at line 108 of file parse_tree.h.

References claraty::Parse_Tree_Data::_atom, _data, is_atom(), operator::std::string(), and write_to_string().

Referenced by operator::std::string().

00108                             {
00109     if (is_atom()) {
00110       return _data->_atom;
00111     }
00112     else {
00113       return(write_to_string());
00114     }
00115   }

Here is the call graph for this function:

Parse_Tree& claraty::Parse_Tree::operator= ( const Parse_Tree rhs  )  [inline]

Definition at line 120 of file parse_tree.h.

References _data, claraty::Parse_Tree_Data::ref(), and claraty::Parse_Tree_Data::unref().

00120                                                {
00121     rhs._data->ref();
00122     Parse_Tree_Data *old_data= _data;
00123     _data= rhs._data;
00124     old_data->unref();
00125     return *this;
00126   }

Here is the call graph for this function:

void claraty::Parse_Tree::clear (  )  [inline]

Definition at line 128 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_NULL.

00128                {
00129     _data->set_nodetype(Parse_Tree_Data::PT_NULL);
00130   }

void claraty::Parse_Tree::make_map (  )  const [inline]

Definition at line 132 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_MAP.

Referenced by claraty::FDM_Parse_Tree::_begin_map().

00132                         {
00133     _data->set_nodetype(Parse_Tree_Data::PT_MAP);
00134   }

void claraty::Parse_Tree::make_array (  )  const [inline]

Definition at line 135 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_ARRAY.

Referenced by claraty::FDM_Parse_Tree::_begin_array().

00135                           {
00136     _data->set_nodetype(Parse_Tree_Data::PT_ARRAY);
00137   }

void claraty::Parse_Tree::make_atom (  )  const [inline]

Definition at line 138 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_ATOM.

00138                          {
00139     _data->set_nodetype(Parse_Tree_Data::PT_ATOM);
00140   }

void claraty::Parse_Tree::make_null (  )  const [inline]

Definition at line 141 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_NULL.

Referenced by claraty::read_item(), and claraty::read_string().

00141                          {
00142     _data->set_nodetype(Parse_Tree_Data::PT_NULL);
00143   }

void claraty::Parse_Tree::set_value ( const Parse_Tree value  )  [inline]

Definition at line 150 of file parse_tree.h.

Referenced by claraty::FDM_Parse_Tree::_io_object(), claraty::FDM_Parse_Tree::io_primitive(), claraty::read_item(), and claraty::read_string().

00150                                           {
00151     (*this)=value;
00152   }

void claraty::Parse_Tree::set_value ( const std::string &  value  )  [inline]

Definition at line 154 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_ATOM.

00154                                          {
00155     _data->set_nodetype(Parse_Tree_Data::PT_ATOM);
00156     _data->_atom= value;
00157   }

void claraty::Parse_Tree::set_value ( const char *  value  )  [inline]

Definition at line 159 of file parse_tree.h.

00159                                     {
00160     set_value(std::string(value));
00161   }

bool claraty::Parse_Tree::check_for_field ( const std::string &  label  )  const

Definition at line 106 of file parse_tree.cc.

References get_field().

00107 {
00108   bool found;
00109   get_field(label, &found);
00110   return found;
00111 }

Here is the call graph for this function:

Parse_Tree claraty::Parse_Tree::get_field ( const std::string &  label,
bool *  found = NULL 
) const

Definition at line 114 of file parse_tree.cc.

References _data, claraty::Parse_Tree_Data::_map, is_map(), and Parse_Tree().

Referenced by claraty::FDM_Parse_Tree::_map_field(), and check_for_field().

00115 {
00116   if (is_map()) {
00117     std::map<std::string, Parse_Tree>::const_iterator i=
00118       _data->_map.find(label);
00119       
00120     if (i != _data->_map.end()) {
00121       if (found) *found= true;
00122       return i->second;
00123     }
00124   }
00125   if (found) *found= false;
00126   return Parse_Tree();
00127 }

Here is the call graph for this function:

template<class T>
void claraty::Parse_Tree::set_field ( const std::string &  label,
const T &  value 
) [inline]

Definition at line 170 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_MAP.

Referenced by claraty::FDM_Parse_Tree::_map_field().

00170                                                          {
00171     _data->set_nodetype(Parse_Tree_Data::PT_MAP);
00172     if (_data->_map.find(label) == _data->_map.end()) {
00173       _data->_map_fields.push_back(label);
00174     }
00175     _data->_map[label].set_value(value);
00176   }

bool claraty::Parse_Tree::get_map ( std::map< std::string, Parse_Tree > &  map  )  const [inline]

Definition at line 178 of file parse_tree.h.

00179   {
00180     if(!is_map()) {
00181       return(false);
00182     }
00183     else {
00184       map = _data->_map;
00185       return(true);
00186     }
00187   }

Parse_Tree claraty::Parse_Tree::get_element ( int  i,
bool *  found = NULL 
) const [inline]

Definition at line 192 of file parse_tree.h.

Referenced by claraty::FDM_Parse_Tree::_array_element(), and pretty_print_array().

00192                                                         {
00193     if (is_array() && i >= 0 && i < size()) {
00194       if (found) *found= true;
00195       return _data->_array[i];
00196     }
00197     if (found) *found= false;
00198     return Parse_Tree();
00199   }

template<class T>
void claraty::Parse_Tree::set_element ( int  index,
const T &  value 
) [inline]

Definition at line 202 of file parse_tree.h.

References claraty::Parse_Tree_Data::PT_ARRAY.

Referenced by claraty::FDM_Parse_Tree::_array_element().

00202                                               {
00203     _data->set_nodetype(Parse_Tree_Data::PT_ARRAY);
00204     assert(index >= 0);
00205     if (index >= (int)_data->_array.size()) _data->_array.resize(index+1);
00206     _data->_array[index].set_value(value);
00207   }

bool claraty::Parse_Tree::io ( FDM_Map  map  ) 

Definition at line 133 of file parse_tree.cc.

References claraty::FDM_Map::field(), claraty::FDM_Map::is_read(), claraty::FDM_Map::is_write(), read_from_string(), and write_to_string().

00134 {
00135   string str;
00136   if(map.is_write()) {
00137     str = write_to_string();
00138   }
00139   bool ok = map.field("Parse_Tree", str);
00140   if(ok && map.is_read()) {
00141     return(read_from_string(str));
00142   }
00143   return(ok);
00144 }

Here is the call graph for this function:

bool claraty::Parse_Tree::read_from_string ( const std::string &  str  ) 

Definition at line 693 of file parse_tree.cc.

References read_from_stream().

Referenced by io(), Parse_Tree(), and claraty::FDM_Parse_Tree::string_to_object().

00694 {
00695   std::istringstream str_stream(str);
00696   return(read_from_stream(str_stream));
00697 }

Here is the call graph for this function:

bool claraty::Parse_Tree::read_from_stream ( std::istream &  is  ) 

std::string claraty::Parse_Tree::write_to_string (  )  const [inline]

Definition at line 220 of file parse_tree.h.

Referenced by io(), and operator::std::string().

00220                                     {
00221     // Use pretty_print even for atoms to take care of quoting
00222     return(pretty_print());
00223   }

std::ostream& claraty::Parse_Tree::write_to_stream ( std::ostream &  os  )  const

bool claraty::Parse_Tree::read_map_from_stream ( std::istream &  is  ) 

Referenced by claraty::read_item().

bool claraty::Parse_Tree::read_array_from_stream ( std::istream &  is  ) 

Referenced by claraty::read_item().

string claraty::Parse_Tree::pretty_print ( int  width = 79,
int  tabsize = 2,
int  current_indent = 0 
) const

Definition at line 159 of file parse_tree.cc.

References _data, claraty::Parse_Tree_Data::_nodetype, pretty_print_array(), pretty_print_atom(), pretty_print_map(), claraty::Parse_Tree_Data::PT_ARRAY, claraty::Parse_Tree_Data::PT_ATOM, claraty::Parse_Tree_Data::PT_MAP, and claraty::Parse_Tree_Data::PT_NULL.

Referenced by claraty::FDM_Parse_Tree::object_to_string(), and pretty_print_array().

00161 {
00162   switch (_data->_nodetype) {
00163   case Parse_Tree_Data::PT_ATOM:
00164     //cout << "pretty print atom\n";
00165     return pretty_print_atom(width, tabsize, current_indent);
00166   case Parse_Tree_Data::PT_ARRAY:
00167     //cout << "pretty print array\n";
00168     return pretty_print_array(width, tabsize, current_indent);
00169   case Parse_Tree_Data::PT_MAP:
00170     //cout << "pretty print map\n";
00171     return pretty_print_map(width, tabsize, current_indent);
00172   case Parse_Tree_Data::PT_NULL:
00173     return "NULL";
00174   default:
00175     assert(0);
00176     return "{ILLEGAL}";
00177   }
00178 }

Here is the call graph for this function:

string claraty::Parse_Tree::pretty_print_atom ( int  width,
int  tabsize,
int  current_indent 
) const

Definition at line 181 of file parse_tree.cc.

References claraty::Parse_Tree_Data::_atom, _data, and is_atom().

Referenced by pretty_print().

00183 {
00184   assert(is_atom());
00185   string ret=_data->_atom;
00186   
00187   // string_quote only quotes if there's whitespace, but it will
00188   // potentially break Parse_Tree parsing if there are semi-colons,
00189   // brackets, or braces.  TODO: Functionality should be added to
00190   // string_quote to take care of this...
00191   if(ret.find_first_of(";[]{} \t\n\"", 0)!=string::npos) {
00192     char quotechar = '"';
00193     ret = string("\"") + ret;   // Put first quote
00194     // Replace internal quotes with escaped quote
00195     size_t end=1;
00196     while((end = ret.find(quotechar, end))!=string::npos) {
00197       if(ret[end-1] != '\\') { 
00198         // It's an unescaped internal quote, escape it
00199         ret.insert(end, "\\");
00200         end++;
00201       }
00202       else {
00203         // It's an escaped internal quote, add two escapes, one 
00204         // to be escaped by the first escape, and one to escape the 
00205         // quote itself
00206         ret.insert(end, "\\\\");
00207         end+=2;
00208       }
00209       end++;
00210     }
00211     // Add the final quote
00212     ret += quotechar;
00213   } else if (ret == "NULL")  // specially quote NULL to avoid confusion with
00214     ret = "\"NULL\"";        // NULL nodes, which use the same representation
00215 
00216   return(ret);
00217 }

Here is the call graph for this function:

string claraty::Parse_Tree::pretty_print_array ( int  width,
int  tabsize,
int  current_indent 
) const

Definition at line 220 of file parse_tree.cc.

References get_element(), is_array(), pretty_print(), and size().

Referenced by pretty_print().

00222 {
00223   assert(is_array());
00224   // Output as [a b c
00225   //            d e f]
00226   string ret= "[";
00227   int element_indent= current_indent+1;
00228   int current_column= current_indent+1;
00229   bool prev_had_newline= false;
00230   for (int i= 0; i< size(); i++) {
00231     string elt= get_element(i).pretty_print(width, tabsize,
00232                                             current_indent+1);
00233     // Include space in elt_size if we might append
00234     int elt_size= elt.size() + (current_column != element_indent);
00235     // Do we need to start a new line because this field is too
00236     // wide?
00237     bool too_wide= (current_column > element_indent) &&
00238       (elt_size > width-current_column);
00239     // Do we need to start a new line because this field has multiple
00240     // lines?
00241     bool has_newline= elt.find('\n') != string::npos;
00242     if (too_wide || has_newline || prev_had_newline) {
00243       // Start new line
00244       ret.append(1, '\n'); ret.append(element_indent, ' ');
00245       current_column= element_indent;
00246     } else if (current_column > element_indent) {
00247       // Space between elements on line
00248       ret.append(1, ' ');
00249       current_column++;
00250     }
00251     ret.append(elt);
00252     // This won't be correct if elt has newlines, but in that case
00253     // the value of current_column does not matter
00254     current_column += elt.size(); 
00255     prev_had_newline= has_newline;
00256   }
00257   // Do we need to start a new line 
00258   bool too_wide= (current_column > element_indent) &&
00259     (1 > width-current_column);
00260   if (prev_had_newline || too_wide) {
00261     // Start new line for ']'
00262     ret.append(1, '\n'); ret.append(current_indent, ' ');
00263   }
00264   ret.append(1, ']');
00265   return ret;
00266 }

Here is the call graph for this function:

string claraty::Parse_Tree::pretty_print_map ( int  width,
int  tabsize,
int  current_indent 
) const

Definition at line 269 of file parse_tree.cc.

References _data, claraty::Parse_Tree_Data::_map, claraty::Parse_Tree_Data::_map_fields, and is_map().

Referenced by pretty_print().

00271 {
00272   assert(is_map());
00273   // Output as { field=value; field=value; field=value; }
00274   // or
00275   //           {
00276   //             field=value;
00277   //             field={value};
00278   //             field=
00279   //               {value};
00280   //             field={
00281   //               value;
00282   //             }
00283   //           }
00284   // 
00285 
00286   string ret= "{";
00287   int name_indent= current_indent + tabsize;
00288   int value_indent= name_indent + tabsize;
00289   int current_column= current_indent + 1;
00290   bool multi_line= false;
00291   vector<string> names;
00292   vector<string> vals;
00293   for (int i= 0; i< (int)_data->_map_fields.size(); i++) { // compiler warning
00294     const string &name= _data->_map_fields[i];
00295     const Parse_Tree &pt= _data->_map[name];
00296     string val= pt.pretty_print(width, tabsize,
00297                                 pt.is_map() ? name_indent : value_indent);
00298 
00299     if (!multi_line) {
00300       if (0 /*pp.find('\n') || pp.size()+1 > width-current_column*/) {
00301         multi_line= true;
00302         // Convert vector of past fields to multi-line representation
00303       } else {
00304         names.push_back(name);
00305         vals.push_back(val);
00306       }
00307     }
00308     if (multi_line) {
00309       // Add current field to string
00310     }
00311   }
00312   if (multi_line) {
00313     // foo
00314   } else {
00315     for (int i= 0; i< (int)names.size(); i++) {
00316       ret += " ";
00317       ret += names[i];
00318       ret += "=";
00319       ret += vals[i];
00320       ret += ";";
00321     }
00322     ret += " }";
00323   }
00324   return(ret);
00325 }

Here is the call graph for this function:


Member Data Documentation


The documentation for this class was generated from the following files: