From: helly Date: Mon, 24 Mar 2008 14:32:47 +0000 (+0000) Subject: - Move Scanner state into base new struct ScannerState X-Git-Tag: 0.13.6~52 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=45160bb9905f89031fa5da4dca4348284f23e305;p=re2c - Move Scanner state into base new struct ScannerState - Little bit of clean up - Add ability to save & restore scanner state --- diff --git a/re2c/bootstrap/scanner.cc b/re2c/bootstrap/scanner.cc index 38fee065..a04857eb 100644 --- a/re2c/bootstrap/scanner.cc +++ b/re2c/bootstrap/scanner.cc @@ -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 #include @@ -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; } diff --git a/re2c/code.cc b/re2c/code.cc index 9882b244..c1a82a9c 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -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") diff --git a/re2c/scanner.h b/re2c/scanner.h index 208416b3..f2689d94 100644 --- a/re2c/scanner.h +++ b/re2c/scanner.h @@ -11,15 +11,21 @@ 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); diff --git a/re2c/scanner.re b/re2c/scanner.re index c907ed2e..b7e0afc4 100644 --- a/re2c/scanner.re +++ b/re2c/scanner.re @@ -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); }