etisserant@0: /* Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3 etisserant@0: * Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant etisserant@0: * Edouard@279: * This program is free software: you can redistribute it and/or modify Edouard@279: * it under the terms of the GNU General Public License as published by Edouard@279: * the Free Software Foundation, either version 3 of the License, or Edouard@279: * (at your option) any later version. Edouard@279: * Edouard@279: * This program is distributed in the hope that it will be useful, Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Edouard@279: * GNU General Public License for more details. Edouard@279: * Edouard@279: * You should have received a copy of the GNU General Public License Edouard@279: * along with this program. If not, see . Edouard@279: * etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * A generic symbol table that allows duplicate values. etisserant@0: * etisserant@0: * This is used to create a symbol table of previously defined etisserant@0: * functions. Duplicate are allowed since the standard permits function\ etisserant@0: * overloading in the standard library. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: #ifndef _DSYMTABLE_HH etisserant@0: #define _DSYMTABLE_HH etisserant@0: etisserant@0: #include "../absyntax/absyntax.hh" etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: template class dsymtable_c { etisserant@0: /* Case insensitive string compare copied from etisserant@0: * "The C++ Programming Language" - 3rd Edition etisserant@0: * by Bjarne Stroustrup, ISBN 0201889544. etisserant@0: */ etisserant@0: class nocase_c { etisserant@0: public: etisserant@0: bool operator() (const std::string& x, const std::string& y) const { etisserant@0: std::string::const_iterator ix = x.begin(); etisserant@0: std::string::const_iterator iy = y.begin(); etisserant@0: etisserant@0: for(; (ix != x.end()) && (iy != y.end()) && (toupper(*ix) == toupper(*iy)); ++ix, ++iy); etisserant@0: if (ix == x.end()) return (iy != y.end()); etisserant@0: if (iy == y.end()) return false; etisserant@0: return (toupper(*ix) < toupper(*iy)); etisserant@0: }; etisserant@0: }; etisserant@0: etisserant@0: public: etisserant@0: typedef value_type value_t; etisserant@0: etisserant@0: private: etisserant@0: /* Comparison between identifiers must ignore case, therefore the use of nocase_c */ etisserant@0: typedef std::multimap base_t; etisserant@0: base_t _base; etisserant@0: etisserant@0: public: etisserant@0: typedef typename base_t::iterator iterator; etisserant@0: typedef typename base_t::const_iterator const_iterator; etisserant@0: typedef typename base_t::reverse_iterator reverse_iterator; etisserant@0: typedef typename base_t::const_reverse_iterator const_reverse_iterator; etisserant@0: etisserant@0: private: etisserant@0: void reset(void); /* clear all entries... */ msousa@350: const char *symbol_to_string(const symbol_c *symbol); etisserant@0: etisserant@0: public: etisserant@0: dsymtable_c(void) {}; etisserant@0: etisserant@0: void insert(const char *identifier_str, value_t value); etisserant@0: void insert(const symbol_c *symbol, value_t value); etisserant@0: msousa@350: /* Determine how many entries are associated to key identifier_str */ msousa@350: /* returns: msousa@350: * 0: if no entry is found msousa@350: * 1: if 1 entry is found msousa@350: * 2: if more than 1 entry is found msousa@350: */ msousa@350: int multiplicity(const char *identifier_str); msousa@350: int multiplicity(const symbol_c *symbol) {return multiplicity(symbol_to_string(symbol));} msousa@350: etisserant@0: /* Search for an entry. Will return end_value() if not found */ etisserant@0: value_t end_value(void) {return null_value;} etisserant@0: value_t find_value(const char *identifier_str); msousa@350: value_t find_value(const symbol_c *symbol) {return find_value(symbol_to_string(symbol));} etisserant@0: msousa@350: /* Search for an entry associated with identifier_str. Will return end() if not found */ msousa@350: iterator find(const char *identifier_str) {return _base.find(identifier_str);} msousa@350: iterator find(const symbol_c *symbol) {return find(symbol_to_string(symbol));} msousa@350: msousa@350: /* Search for the first entry associated with (i.e. with key ==) identifier_str. Will return end() if not found */ msousa@350: /* Basically, the same as find() */ msousa@350: iterator lower_bound(const char *identifier_str) {return _base.lower_bound(identifier_str);} msousa@350: iterator lower_bound(const symbol_c *symbol) {return lower_bound(symbol_to_string(symbol));} msousa@350: msousa@350: /* Search for the first entry with key greater than identifier_str. Will return end() if not found */ msousa@350: iterator upper_bound(const char *identifier_str) {return _base.upper_bound(identifier_str);} msousa@350: iterator upper_bound(const symbol_c *symbol) {return upper_bound(symbol_to_string(symbol));} msousa@350: msousa@350: /* get the value to which an iterator is pointing to... */ msousa@350: value_t get_value(const iterator i) {return i->second;} etisserant@0: etisserant@0: /* iterators pointing to beg/end of map... */ etisserant@0: iterator begin() {return _base.begin();} etisserant@0: const_iterator begin() const {return _base.begin();} etisserant@0: iterator end() {return _base.end();} etisserant@0: const_iterator end() const {return _base.end();} etisserant@0: reverse_iterator rbegin() {return _base.rbegin();} etisserant@0: const_reverse_iterator rbegin() const {return _base.rbegin();} etisserant@0: reverse_iterator rend() {return _base.rend();} etisserant@0: const_reverse_iterator rend() const {return _base.rend();} etisserant@0: etisserant@0: /* debuging function... */ etisserant@0: void print(void); etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* Templates must include the source into the code! */ etisserant@0: #include "dsymtable.cc" etisserant@0: etisserant@0: #endif /* _DSYMTABLE_HH */