]> granicus.if.org Git - re2c/commitdiff
- Move Scanner state into base new struct ScannerState
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 24 Mar 2008 14:32:47 +0000 (14:32 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 24 Mar 2008 14:32:47 +0000 (14:32 +0000)
- Little bit of clean up
- Add ability to save & restore scanner state

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

index 38fee065791aeea8e74b6a9541f18dc89dcf6158..a04857ebd04f1ceb04f86caa5b2f54083c91bf76 100644 (file)
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.13.4.dev on Mon Mar 24 14:11:59 2008 */
+/* Generated by re2c 0.13.4.dev on Mon Mar 24 15:31:27 2008 */
 /* $Id$ */
 #include <stdlib.h>
 #include <string.h>
@@ -30,14 +30,16 @@ extern YYSTYPE yylval;
 namespace re2c
 {
 
-Scanner::Scanner(std::istream& i, std::ostream& o)
-       : in(i)
-       , out(o)
-       , bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL)
+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)
@@ -53,7 +55,7 @@ char *Scanner::fill(char *cursor, uint need)
                        if (cnt)
                        {
                                memmove(bot, tok, top - tok);
-                               tok = bot;
+                               tok  = bot;
                                ptr -= cnt;
                                cursor -= cnt;
                                pos -= cnt;
@@ -717,7 +719,7 @@ yy114:
        ++YYCURSOR;
 yy115:
        {
-                                       if(cursor == eof) RETURN(0);
+                                       if (cursor == eof) RETURN(0);
                                        pos = cursor;
                                        cline++;
                                        goto scan;
@@ -804,16 +806,9 @@ yy128:
        ++YYCURSOR;
        YYCURSOR = YYCTXMARKER;
        {
-                                       if (FFlag)
-                                       {
-                                               cur = ptr > tok ? ptr - 1 : cursor;
-                                               yylval.symbol = Symbol::find(token());
-                                               return FID;
-                                       } else {
-                                               cur = ptr > tok ? ptr - 1 : cursor;
-                                               yylval.symbol = Symbol::find(token());
-                                               return ID;
-                                       }
+                                       cur = ptr > tok ? ptr - 1 : cursor;
+                                       yylval.symbol = Symbol::find(token());
+                                       return FFlag ? FID : ID;
                                }
 yy130:
        ++YYCURSOR;
@@ -876,7 +871,7 @@ yy137:
 yy139:
        {
                                        cur = cursor;
-                                       tok+= 5; /* skip "re2c:" */
+                                       tok += 5; /* skip "re2c:" */
                                        iscfg = 1;
                                        yylval.str = new Str(token());
                                        return CONFIG;
@@ -1118,7 +1113,7 @@ yy178:
        if ((yych = *YYCURSOR) == '>') goto yy180;
        {
                                        cur = cursor;
-                                       tok+= 2; /* skip ":=" */
+                                       tok += 2; /* skip ":=" */
                                        depth = 0;
                                        goto code;
                                }
@@ -1443,7 +1438,7 @@ yy228:
        if ((yych = *YYCURSOR) == '/') goto yy236;
 yy229:
        {
-                                       if(cursor == eof)
+                                       if (cursor == eof)
                                        {
                                                RETURN(0);
                                        }
@@ -1456,7 +1451,7 @@ yy230:
 yy231:
        ++YYCURSOR;
        {
-                                       if(cursor == eof)
+                                       if (cursor == eof)
                                        {
                                                RETURN(0);
                                        }
@@ -1477,7 +1472,7 @@ yy234:
 yy236:
        ++YYCURSOR;
        {
-                                       if(--depth == 0)
+                                       if (--depth == 0)
                                        {
                                                goto scan;
                                        }
index 9882b24498e2267a16bbb70461b463b12a557575..c1a82a9c2c5dbf99b7d9fd02eebc9610598ef665 100644 (file)
@@ -2086,11 +2086,6 @@ std::ostream& operator << (std::ostream& o, const file_info& li)
        return o;
 }
 
-uint Scanner::get_line() const
-{
-       return cline;
-}
-
 void Scanner::config(const Str& cfg, int num)
 {
        if (cfg.to_string() == "indent:top")
index 208416b37e46e158feafdbb8d39b149679b33114..f2689d948a566b6c53cd8e86458dd1f039f71a74 100644 (file)
 namespace re2c
 {
 
+struct ScannerState
+{
+       ScannerState();
+
+       char    *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof, *ctx;
+       uint    tchar, tline, cline, iscfg, buf_size;
+       bool    in_parse;
+};
+
 class Scanner:
-       public line_number
+       public line_number, private ScannerState
 {
 private:
        std::istream&   in;
        std::ostream&   out;
-       char    *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof, *ctx;
-       uint    tchar, tline, cline, iscfg, buf_size;
-       bool    in_parse;
 
 private:
        char *fill(char*, uint);
@@ -39,6 +45,10 @@ public:
 
        ParseMode echo();
        int scan();
+       
+       size_t get_pos() const;
+       void save_state(ScannerState&) const;
+       void restore_state(const ScannerState&);
 
        uint get_cline() const;
        void set_in_parse(bool new_in_parse);
@@ -72,11 +82,35 @@ public:
        RegExp * mkDot() const;
 };
 
+inline size_t Scanner::get_pos() const
+{
+       return cur - bot;
+}
+
+inline uint Scanner::get_line() const
+{
+       return cline;
+}
+
 inline uint Scanner::get_cline() const
 {
        return cline;
 }
 
+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 c907ed2e1806c9f5f4df3494388168489f6830c5..b7e0afc404e0063288f03eb5ec62df657f28dada 100644 (file)
@@ -29,14 +29,16 @@ extern YYSTYPE yylval;
 namespace re2c
 {
 
-Scanner::Scanner(std::istream& i, std::ostream& o)
-       : in(i)
-       , out(o)
-       , bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL)
+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)
@@ -52,7 +54,7 @@ char *Scanner::fill(char *cursor, uint need)
                        if (cnt)
                        {
                                memmove(bot, tok, top - tok);
-                               tok = bot;
+                               tok  = bot;
                                ptr -= cnt;
                                cursor -= cnt;
                                pos -= cnt;
@@ -314,7 +316,7 @@ scan:
 
        ":="            {
                                        cur = cursor;
-                                       tok+= 2; /* skip ":=" */
+                                       tok += 2; /* skip ":=" */
                                        depth = 0;
                                        goto code;
                                }
@@ -435,23 +437,16 @@ scan:
 
        config          {
                                        cur = cursor;
-                                       tok+= 5; /* skip "re2c:" */
+                                       tok += 5; /* skip "re2c:" */
                                        iscfg = 1;
                                        yylval.str = new Str(token());
                                        return CONFIG;
                                }
 
        name / (space+ [^=>,])  {
-                                       if (FFlag)
-                                       {
-                                               cur = ptr > tok ? ptr - 1 : cursor;
-                                               yylval.symbol = Symbol::find(token());
-                                               return FID;
-                                       } else {
-                                               cur = ptr > tok ? ptr - 1 : cursor;
-                                               yylval.symbol = Symbol::find(token());
-                                               return ID;
-                                       }
+                                       cur = ptr > tok ? ptr - 1 : cursor;
+                                       yylval.symbol = Symbol::find(token());
+                                       return FFlag ? FID : ID;
                                }
 
        name / (space* [=>,])   {
@@ -491,7 +486,7 @@ scan:
                                }
 
        eol                     {
-                                       if(cursor == eof) RETURN(0);
+                                       if (cursor == eof) RETURN(0);
                                        pos = cursor;
                                        cline++;
                                        goto scan;
@@ -584,7 +579,7 @@ code:
 comment:
 /*!re2c
        "*/"            {
-                                       if(--depth == 0)
+                                       if (--depth == 0)
                                        {
                                                goto scan;
                                        }
@@ -599,7 +594,7 @@ comment:
                                        goto comment;
                                }
        "\n"            {
-                                       if(cursor == eof)
+                                       if (cursor == eof)
                                        {
                                                RETURN(0);
                                        }
@@ -608,7 +603,7 @@ comment:
                                        goto comment;
                                }
        any                     {
-                                       if(cursor == eof)
+                                       if (cursor == eof)
                                        {
                                                RETURN(0);
                                        }