]> granicus.if.org Git - re2c/commitdiff
- Move Scanner functions to code.cc
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 24 Mar 2008 18:46:49 +0000 (18:46 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 24 Mar 2008 18:46:49 +0000 (18:46 +0000)
- Allow to expand buffer while in reuse

re2c/bootstrap/scanner.cc
re2c/code.cc
re2c/globals.h
re2c/scanner.h
re2c/scanner.re

index 8366b8652ea5c927f99b7741b82c7d96c1d4914d..aff31064caf47a70c6836914b97504d6392c1151 100644 (file)
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.13.4.dev on Mon Mar 24 16:50:42 2008 */
+/* Generated by re2c 0.13.4.dev on Mon Mar 24 18:41:16 2008 */
 /* $Id$ */
 #include <stdlib.h>
 #include <string.h>
@@ -30,71 +30,6 @@ extern YYSTYPE yylval;
 namespace re2c
 {
 
-ScannerState::ScannerState()
-       : bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL)
-       , top(NULL), eof(NULL), ctx(NULL), tchar(0), tline(0), cline(1), iscfg(0)
-       , in_parse(false)
-{
-}
-
-Scanner::Scanner(std::istream& i, std::ostream& o)
-       : in(i), out(o)
-{
-}
-
-char *Scanner::fill(char *cursor, uint need)
-{
-       if(!eof)
-       {
-               uint cnt;
-               /* Do not get rid of anything when rFlag is active. Otherwise
-                * get rid of everything that was already handedout. */
-               if (!rFlag)
-               {
-                       cnt = tok - bot;
-                       if (cnt)
-                       {
-                               memmove(bot, tok, top - tok);
-                               tok  = bot;
-                               ptr -= cnt;
-                               cursor -= cnt;
-                               pos -= cnt;
-                               lim -= cnt;
-                       }
-               }
-               /* In crease buffer size. */
-               need = MAX(need, BSIZE);
-               if (static_cast<uint>(top - lim) < need)
-               {
-                       char *buf = new char[(lim - bot) + need];
-                       if (!buf)
-                       {
-                               fatal("Out of memory");
-                       }
-                       memcpy(buf, tok, lim - tok);
-                       tok = buf;
-                       ptr = &buf[ptr - bot];
-                       cursor = &buf[cursor - bot];
-                       pos = &buf[pos - bot];
-                       lim = &buf[lim - bot];
-                       top = &lim[need];
-                       delete [] bot;
-                       bot = buf;
-               }
-               /* Append to buffer. */
-               in.read(lim, need);
-               if ((cnt = in.gcount()) != need)
-               {
-                       eof = &lim[cnt];
-                       *eof++ = '\0';
-               }
-               lim += cnt;
-       }
-       return cursor;
-}
-
-
-
 Scanner::ParseMode Scanner::echo()
 {
        char *cursor = cur;
@@ -1801,83 +1736,4 @@ yy273:
 
 }
 
-void Scanner::set_in_parse(bool new_in_parse)
-{
-       in_parse = new_in_parse;
-}
-
-void Scanner::fatal_at(uint line, uint ofs, const char *msg) const
-{
-       out.flush();
-       std::cerr << "re2c: error: "
-               << "line " << line << ", column " << (tchar + ofs + 1) << ": "
-               << msg << std::endl;
-       exit(1);
-}
-
-void Scanner::fatal(uint ofs, const char *msg) const
-{
-       fatal_at(in_parse ? tline : cline, ofs, msg);
-}
-
-void Scanner::fatalf_at(uint line, const char* fmt, ...) const
-{
-       char szBuf[4096];
-
-       va_list args;
-       
-       va_start(args, fmt);
-       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
-       va_end(args);
-       
-       szBuf[sizeof(szBuf)-1] = '0';
-       
-       fatal_at(line, 0, szBuf);
-}
-
-void Scanner::fatalf(const char *fmt, ...) const
-{
-       char szBuf[4096];
-
-       va_list args;
-       
-       va_start(args, fmt);
-       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
-       va_end(args);
-       
-       szBuf[sizeof(szBuf)-1] = '0';
-       
-       fatal(szBuf);
-}
-
-Scanner::~Scanner()
-{
-       if (bot)
-       {
-               delete [] bot;
-       }
-}
-
-void Scanner::check_token_length(char *pos, uint len) const
-{
-       if (pos < bot || pos + len >= top)
-       {
-               fatal("Token exceeds limit");
-       }
-}
-
-SubStr Scanner::raw_token(std::string enclosure) const
-{
-       return SubStr(std::string(enclosure + token().to_string() + enclosure).c_str());
-}
-
-void Scanner::reuse()
-{
-       next_label = 0;
-       next_fill_index = 0;
-       bWroteGetState = false;
-       bWroteCondCheck = false;
-       mapCodeName.clear();
-}
-
 } // end namespace re2c
index c1a82a9c2c5dbf99b7d9fd02eebc9610598ef665..33d8a7adbc0404f390a824f677fc43a3f020e304 100644 (file)
@@ -1,4 +1,5 @@
 /* $Id$ */
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -2315,4 +2316,174 @@ void Scanner::config(const Str& cfg, const Str& val)
        }
 }
 
+ScannerState::ScannerState()
+       : tok(NULL), ptr(NULL), cur(NULL), pos(NULL), ctx(NULL)
+       , bot(NULL), lim(NULL), top(NULL), eof(NULL)
+       , tchar(0), tline(0), cline(1), iscfg(0)
+       , in_parse(false)
+{
+}
+
+Scanner::Scanner(std::istream& i, std::ostream& o)
+       : ScannerState(), in(i), out(o)
+{
+}
+
+char *Scanner::fill(char *cursor, uint need)
+{
+       if(!eof)
+       {
+               uint cnt;
+               /* Do not get rid of anything when rFlag is active. Otherwise
+                * get rid of everything that was already handedout. */
+               if (!rFlag)
+               {
+                       cnt = tok - bot;
+                       if (cnt)
+                       {
+                               memmove(bot, tok, top - tok);
+                               tok  = bot;
+                               ptr -= cnt;
+                               cursor -= cnt;
+                               pos -= cnt;
+                               lim -= cnt;
+                       }
+               }
+               /* In crease buffer size. */
+               if (BSIZE > need)
+               {
+                       need = BSIZE;
+               }
+               if (static_cast<uint>(top - lim) < need)
+               {
+                       char *buf = new char[(lim - bot) + need];
+                       if (!buf)
+                       {
+                               fatal("Out of memory");
+                       }
+                       memcpy(buf, bot, lim - bot);
+                       tok = &buf[tok - bot];
+                       ptr = &buf[ptr - bot];
+                       cur = &buf[cur - bot];
+                       pos = &buf[pos - bot];
+                       lim = &buf[lim - bot];
+                       top = &lim[need];
+                       ctx = &buf[ctx - bot];
+                       cursor = &buf[cursor - bot];
+                       delete [] bot;
+                       bot = buf;
+               }
+               /* Append to buffer. */
+               in.read(lim, need);
+               if ((cnt = in.gcount()) != need)
+               {
+                       eof = &lim[cnt];
+                       *eof++ = '\0';
+               }
+               lim += cnt;
+       }
+       return cursor;
+}
+
+void Scanner::set_in_parse(bool new_in_parse)
+{
+       in_parse = new_in_parse;
+}
+
+void Scanner::fatal_at(uint line, uint ofs, const char *msg) const
+{
+       out.flush();
+       std::cerr << "re2c: error: "
+               << "line " << line << ", column " << (tchar + ofs + 1) << ": "
+               << msg << std::endl;
+       exit(1);
+}
+
+void Scanner::fatal(uint ofs, const char *msg) const
+{
+       fatal_at(in_parse ? tline : cline, ofs, msg);
+}
+
+void Scanner::fatalf_at(uint line, const char* fmt, ...) const
+{
+       char szBuf[4096];
+
+       va_list args;
+       
+       va_start(args, fmt);
+       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
+       va_end(args);
+       
+       szBuf[sizeof(szBuf)-1] = '0';
+       
+       fatal_at(line, 0, szBuf);
+}
+
+void Scanner::fatalf(const char *fmt, ...) const
+{
+       char szBuf[4096];
+
+       va_list args;
+       
+       va_start(args, fmt);
+       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
+       va_end(args);
+       
+       szBuf[sizeof(szBuf)-1] = '0';
+       
+       fatal(szBuf);
+}
+
+Scanner::~Scanner()
+{
+       if (bot)
+       {
+               delete [] bot;
+       }
+}
+
+void Scanner::check_token_length(char *pos, uint len) const
+{
+       if (pos < bot || pos + len >= top)
+       {
+               fatal("Token exceeds limit");
+       }
+}
+
+SubStr Scanner::raw_token(std::string enclosure) const
+{
+       return SubStr(std::string(enclosure + token().to_string() + enclosure).c_str());
+}
+
+void Scanner::reuse()
+{
+       next_label = 0;
+       next_fill_index = 0;
+       bWroteGetState = false;
+       bWroteCondCheck = false;
+       mapCodeName.clear();
+}
+
+void Scanner::restore_state(const ScannerState& state)
+{
+       int diff = bot - state.bot;
+       char *old_bot = bot;
+       char *old_lim = lim;
+       char *old_top = top;
+       char *old_eof = eof;
+       *(ScannerState*)this = state;
+       if (diff)
+       {
+               tok -= diff;
+               ptr -= diff;
+               cur -= diff;
+               pos -= diff;
+               ctx -= diff;            
+               bot = old_bot;
+               lim = old_lim;
+               top = old_top;
+               eof = old_eof;
+       }
+}
+
 } // end namespace re2c
index d94ec071100ef22829d57eb6ac1c788492ca7292..05cdd310a24887ed61c2274de0c658ffd2bee79f 100644 (file)
@@ -12,6 +12,8 @@
 namespace re2c
 {
 
+enum { BSIZE = 8192};
+
 extern file_info sourceFileInfo;
 extern file_info outputFileInfo;
 extern file_info headerFileInfo;
index bc9fd37cd64769e10f4ee4ba70f1a71d2a478549..67035c0b5636dbdf9ab6fe7c8506f6b9010250bc 100644 (file)
@@ -15,7 +15,8 @@ struct ScannerState
 {
        ScannerState();
 
-       char    *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof, *ctx;
+       char    *tok, *ptr, *cur, *pos, *ctx;  // positioning
+       char    *bot, *lim, *top, *eof;        // buffer
        uint    tchar, tline, cline, iscfg, buf_size;
        bool    in_parse;
 };
@@ -103,15 +104,6 @@ inline void Scanner::save_state(ScannerState& state) const
        state = *this;
 }
 
-inline void Scanner::restore_state(const ScannerState& state)
-{
-       if (bot != state.bot)
-       {
-               fatal("illegal internal restore operation");
-       }
-       *(ScannerState*)this = state;
-}
-
 inline void Scanner::fatal(const char *msg) const
 {
        fatal(0, msg);
index 55655af194552d749a79a6be6b2ade028d84ed12..59944fe3dc29b7bb02e1dfdb75273b0e589ac2b0 100644 (file)
@@ -3,7 +3,6 @@
 #include <string.h>
 #include <iostream>
 #include <sstream>
-#include <stdarg.h>
 #include "scanner.h"
 #include "parser.h"
 #include "y.tab.h"
@@ -16,7 +15,6 @@ extern YYSTYPE yylval;
 #define MAX(a,b) (((a)>(b))?(a):(b))
 #endif
 
-#define        BSIZE           8192
 #define        YYCTYPE         unsigned char
 #define        YYCURSOR        cursor
 #define        YYLIMIT         lim
@@ -29,69 +27,6 @@ extern YYSTYPE yylval;
 namespace re2c
 {
 
-ScannerState::ScannerState()
-       : bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL)
-       , top(NULL), eof(NULL), ctx(NULL), tchar(0), tline(0), cline(1), iscfg(0)
-       , in_parse(false)
-{
-}
-
-Scanner::Scanner(std::istream& i, std::ostream& o)
-       : in(i), out(o)
-{
-}
-
-char *Scanner::fill(char *cursor, uint need)
-{
-       if(!eof)
-       {
-               uint cnt;
-               /* Do not get rid of anything when rFlag is active. Otherwise
-                * get rid of everything that was already handedout. */
-               if (!rFlag)
-               {
-                       cnt = tok - bot;
-                       if (cnt)
-                       {
-                               memmove(bot, tok, top - tok);
-                               tok  = bot;
-                               ptr -= cnt;
-                               cursor -= cnt;
-                               pos -= cnt;
-                               lim -= cnt;
-                       }
-               }
-               /* In crease buffer size. */
-               need = MAX(need, BSIZE);
-               if (static_cast<uint>(top - lim) < need)
-               {
-                       char *buf = new char[(lim - bot) + need];
-                       if (!buf)
-                       {
-                               fatal("Out of memory");
-                       }
-                       memcpy(buf, tok, lim - tok);
-                       tok = buf;
-                       ptr = &buf[ptr - bot];
-                       cursor = &buf[cursor - bot];
-                       pos = &buf[pos - bot];
-                       lim = &buf[lim - bot];
-                       top = &lim[need];
-                       delete [] bot;
-                       bot = buf;
-               }
-               /* Append to buffer. */
-               in.read(lim, need);
-               if ((cnt = in.gcount()) != need)
-               {
-                       eof = &lim[cnt];
-                       *eof++ = '\0';
-               }
-               lim += cnt;
-       }
-       return cursor;
-}
-
 /*!re2c
 zero    = "\000";
 any     = [\000-\377];
@@ -639,83 +574,4 @@ value:
 */
 }
 
-void Scanner::set_in_parse(bool new_in_parse)
-{
-       in_parse = new_in_parse;
-}
-
-void Scanner::fatal_at(uint line, uint ofs, const char *msg) const
-{
-       out.flush();
-       std::cerr << "re2c: error: "
-               << "line " << line << ", column " << (tchar + ofs + 1) << ": "
-               << msg << std::endl;
-       exit(1);
-}
-
-void Scanner::fatal(uint ofs, const char *msg) const
-{
-       fatal_at(in_parse ? tline : cline, ofs, msg);
-}
-
-void Scanner::fatalf_at(uint line, const char* fmt, ...) const
-{
-       char szBuf[4096];
-
-       va_list args;
-       
-       va_start(args, fmt);
-       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
-       va_end(args);
-       
-       szBuf[sizeof(szBuf)-1] = '0';
-       
-       fatal_at(line, 0, szBuf);
-}
-
-void Scanner::fatalf(const char *fmt, ...) const
-{
-       char szBuf[4096];
-
-       va_list args;
-       
-       va_start(args, fmt);
-       vsnprintf(szBuf, sizeof(szBuf), fmt, args);
-       va_end(args);
-       
-       szBuf[sizeof(szBuf)-1] = '0';
-       
-       fatal(szBuf);
-}
-
-Scanner::~Scanner()
-{
-       if (bot)
-       {
-               delete [] bot;
-       }
-}
-
-void Scanner::check_token_length(char *pos, uint len) const
-{
-       if (pos < bot || pos + len >= top)
-       {
-               fatal("Token exceeds limit");
-       }
-}
-
-SubStr Scanner::raw_token(std::string enclosure) const
-{
-       return SubStr(std::string(enclosure + token().to_string() + enclosure).c_str());
-}
-
-void Scanner::reuse()
-{
-       next_label = 0;
-       next_fill_index = 0;
-       bWroteGetState = false;
-       bWroteCondCheck = false;
-       mapCodeName.clear();
-}
-
 } // end namespace re2c