]> granicus.if.org Git - re2c/commitdiff
- Implement SymbolTable using map instead of linked list as inspired by
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Tue, 25 Oct 2005 22:02:43 +0000 (22:02 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Tue, 25 Oct 2005 22:02:43 +0000 (22:02 +0000)
  #1335305 symbol table reimplementation

CHANGELOG
actions.cc
parser.h
substr.h

index 730bee047c7c3eba9e026d72c7a48d6c0472ee9b..51f9522362e4acc31c67af7503c072855156d973 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 Version 0.9.11 (????-??-??)
 ---------------------------
+- Implemented #1335305 symbol table reimplementation, just slightly modifed.
 
 Version 0.9.10 (2005-09-04)
 ---------------------------
index 3f32d279932fe41e65a4c15b5281c28bfbf2ddd7..afc394b1c8179062513fc7c6a1cf2d444d88d9da 100644 (file)
 namespace re2c
 {
 
-Symbol *Symbol::first = NULL;
-
-Symbol::Symbol(const SubStr &str) : next(first), name(str), re(NULL)
+void Symbol::ClearTable()
 {
-       first = this;
+       symbol_table.clear();
 }
 
+Symbol::SymbolTable Symbol::symbol_table;
+
 Symbol *Symbol::find(const SubStr &str)
 {
-       for (Symbol *sym = first; sym; sym = sym->next)
-               if (sym->name == str)
-                       return sym;
+       const std::string ss(str.to_string());
+       SymbolTable::const_iterator it = symbol_table.find(ss);
 
-       return new Symbol(str);
+       if (it == symbol_table.end())
+       {
+               return (*symbol_table.insert(SymbolTable::value_type(ss, new Symbol(str))).first).second;
+       }
+       
+       return (*it).second;
 }
 
 void showIns(std::ostream &o, const Ins &i, const Ins &base)
index ceb9465699cc754ccbed0f785ae5ef386df55274..58435f26871baa9fbaecd62653382d9a13e2b494 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -5,22 +5,40 @@
 #include "scanner.h"
 #include "re.h"
 #include <iosfwd>
+#include <map>
 
 namespace re2c
 {
 
 class Symbol
 {
-
 public:
-       static Symbol   *first;
-       Symbol  *next;
-       Str     name;
-       RegExp  *re;
 
-public:
-       Symbol(const SubStr&);
+       RegExp*   re;
+
        static Symbol *find(const SubStr&);
+       static void ClearTable();
+
+       ~Symbol()
+       {
+               /** \todo should we delete 're'? */
+       }
+
+protected:
+
+       Symbol(const SubStr& str)
+               : name(str)
+               , re(NULL)
+       {
+       }
+
+private:
+
+       typedef std::map<std::string, Symbol*> SymbolTable;
+
+       static SymbolTable symbol_table;
+
+       Str     name;
 };
 
 void line_source(unsigned int, std::ostream&);
index cb046f486e0d6f48f92ac9712e3b84c26ed43807..255f6ec62876bd1e29a0ee7e2cca9efcb9426563 100644 (file)
--- a/substr.h
+++ b/substr.h
@@ -3,6 +3,7 @@
 #define _substr_h
 
 #include <iostream>
+#include <string>
 #include "basics.h"
 
 namespace re2c
@@ -22,6 +23,10 @@ public:
        SubStr(char*);
        SubStr(const SubStr&);
        void out(std::ostream&) const;
+       std::string to_string() const
+       {
+               return std::string(str, len);
+       }
 };
 
 class Str: public SubStr