util/dsymtable.hh
changeset 0 fb772792efd1
child 279 c0453b7f99df
equal deleted inserted replaced
-1:000000000000 0:fb772792efd1
       
     1 /*
       
     2  * (c) 2005 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * A generic symbol table that allows duplicate values.
       
    28  *
       
    29  * This is used to create a symbol table of previously defined
       
    30  * functions. Duplicate are allowed since the standard permits function\
       
    31  * overloading in the standard library.
       
    32  */
       
    33 
       
    34 
       
    35 
       
    36 #ifndef _DSYMTABLE_HH
       
    37 #define _DSYMTABLE_HH
       
    38 
       
    39 #include "../absyntax/absyntax.hh"
       
    40 
       
    41 #include <map>
       
    42 #include <string>
       
    43 
       
    44 
       
    45 
       
    46 
       
    47 template<typename value_type, value_type null_value> class dsymtable_c {
       
    48   /* Case insensitive string compare copied from
       
    49    * "The C++ Programming Language" - 3rd Edition
       
    50    * by Bjarne Stroustrup, ISBN 0201889544.
       
    51    */
       
    52   class nocase_c {
       
    53     public:
       
    54       bool operator() (const std::string& x, const std::string& y) const {
       
    55         std::string::const_iterator ix = x.begin();
       
    56         std::string::const_iterator iy = y.begin();
       
    57 
       
    58         for(; (ix != x.end()) && (iy != y.end()) && (toupper(*ix) == toupper(*iy)); ++ix, ++iy);
       
    59         if (ix == x.end()) return (iy != y.end());
       
    60         if (iy == y.end()) return false;
       
    61         return (toupper(*ix) < toupper(*iy));
       
    62       };
       
    63   };
       
    64 
       
    65   public:
       
    66     typedef value_type value_t;
       
    67 
       
    68   private:
       
    69     /* Comparison between identifiers must ignore case, therefore the use of nocase_c */
       
    70     typedef std::multimap<std::string, value_t, nocase_c> base_t;
       
    71     base_t _base;
       
    72 
       
    73   public:
       
    74   typedef typename base_t::iterator iterator;
       
    75   typedef typename base_t::const_iterator const_iterator;
       
    76   typedef typename base_t::reverse_iterator reverse_iterator;
       
    77   typedef typename base_t::const_reverse_iterator const_reverse_iterator;
       
    78 
       
    79   private:
       
    80     void reset(void); /* clear all entries... */
       
    81 
       
    82   public:
       
    83     dsymtable_c(void) {};
       
    84 
       
    85     void insert(const char *identifier_str, value_t value);
       
    86     void insert(const symbol_c *symbol, value_t value);
       
    87 
       
    88     /* Search for an entry. Will return end_value() if not found */
       
    89     value_t end_value(void) {return null_value;}
       
    90     value_t find_value(const char *identifier_str);
       
    91     value_t find_value(const symbol_c *symbol);
       
    92 
       
    93     iterator find(const char *identifier_str) {return _base.find(identifier_str);}
       
    94 
       
    95   /* iterators pointing to beg/end of map... */
       
    96     iterator begin() 			{return _base.begin();}
       
    97     const_iterator begin() const	{return _base.begin();}
       
    98     iterator end()			{return _base.end();}
       
    99     const_iterator end() const 		{return _base.end();}
       
   100     reverse_iterator rbegin()		{return _base.rbegin();}
       
   101     const_reverse_iterator rbegin() const {return _base.rbegin();}
       
   102     reverse_iterator rend() 		{return _base.rend();}
       
   103     const_reverse_iterator rend() const	{return _base.rend();}
       
   104 
       
   105     /* debuging function... */
       
   106     void print(void);
       
   107 };
       
   108 
       
   109 
       
   110 
       
   111 /* Templates must include the source into the code! */
       
   112 #include "dsymtable.cc"
       
   113 
       
   114 #endif /*  _DSYMTABLE_HH */