src
scanner.cc
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <iostream>
6 #include <new>
7 
8 #include "src/codegen/label.h"
9 #include "src/codegen/output.h"
10 #include "src/conf/opt.h"
11 #include "src/globals.h"
12 #include "src/parse/scanner.h"
13 #include "src/util/counter.h"
14 
15 // used by Scanner::fatal_at and Scanner::fatalf
16 #if defined(_MSC_VER) && !defined(vsnprintf)
17 # define vsnprintf _vsnprintf
18 #endif
19 
20 namespace re2c {
21 
22 const uint32_t Scanner::BSIZE = 8192;
23 
25  : tok (NULL)
26  , ptr (NULL)
27  , cur (NULL)
28  , pos (NULL)
29  , ctx (NULL)
30  , bot (NULL)
31  , lim (NULL)
32  , top (NULL)
33  , eof (NULL)
34  , tchar (0)
35  , tline (0)
36  , cline (1)
37  , in_parse (false)
38  , lexer_state (LEX_NORMAL)
39 {}
40 
42  : tok (s.tok)
43  , ptr (s.ptr)
44  , cur (s.cur)
45  , pos (s.pos)
46  , ctx (s.ctx)
47  , bot (s.bot)
48  , lim (s.lim)
49  , top (s.top)
50  , eof (s.eof)
51  , tchar (s.tchar)
52  , tline (s.tline)
53  , cline (s.cline)
54  , in_parse (s.in_parse)
55  , lexer_state (s.lexer_state)
56 {}
57 
59 {
60  this->~ScannerState ();
61  new (this) ScannerState (s);
62  return * this;
63 }
64 
66  : ScannerState ()
67  , in (i)
68  , out (o)
69 {}
70 
71 void Scanner::fill (uint32_t need)
72 {
73  if(!eof)
74  {
75  /* Do not get rid of anything when rFlag is active. Otherwise
76  * get rid of everything that was already handedout. */
77  if (!opts->rFlag)
78  {
79  const ptrdiff_t diff = tok - bot;
80  if (diff > 0)
81  {
82  const size_t move = static_cast<size_t> (top - tok);
83  memmove (bot, tok, move);
84  tok -= diff;
85  ptr -= diff;
86  cur -= diff;
87  pos -= diff;
88  lim -= diff;
89  ctx -= diff;
90  }
91  }
92  /* In crease buffer size. */
93  if (BSIZE > need)
94  {
95  need = BSIZE;
96  }
97  if (static_cast<uint32_t> (top - lim) < need)
98  {
99  const size_t copy = static_cast<size_t> (lim - bot);
100  char * buf = new char[copy + need];
101  if (!buf)
102  {
103  fatal("Out of memory");
104  }
105  memcpy (buf, bot, copy);
106  tok = &buf[tok - bot];
107  ptr = &buf[ptr - bot];
108  cur = &buf[cur - bot];
109  pos = &buf[pos - bot];
110  lim = &buf[lim - bot];
111  top = &lim[need];
112  ctx = &buf[ctx - bot];
113  delete [] bot;
114  bot = buf;
115  }
116  /* Append to buffer. */
117  const size_t have = fread (lim, 1, need, in.file);
118  if (have != need)
119  {
120  eof = &lim[have];
121  *eof++ = '\0';
122  }
123  lim += have;
124  }
125 }
126 
127 void Scanner::set_in_parse(bool new_in_parse)
128 {
129  in_parse = new_in_parse;
130 }
131 
132 void Scanner::fatal_at(uint32_t line, ptrdiff_t ofs, const char *msg) const
133 {
134  std::cerr << "re2c: error: "
135  << "line " << line << ", column " << (tchar + ofs + 1) << ": "
136  << msg << std::endl;
137  exit(1);
138 }
139 
140 void Scanner::fatal(ptrdiff_t ofs, const char *msg) const
141 {
142  fatal_at(in_parse ? tline : cline, ofs, msg);
143 }
144 
145 void Scanner::fatalf_at(uint32_t line, const char* fmt, ...) const
146 {
147  char szBuf[4096];
148 
149  va_list args;
150 
151  va_start(args, fmt);
152  vsnprintf(szBuf, sizeof(szBuf), fmt, args);
153  va_end(args);
154 
155  szBuf[sizeof(szBuf)-1] = '0';
156 
157  fatal_at(line, 0, szBuf);
158 }
159 
160 void Scanner::fatalf(const char *fmt, ...) const
161 {
162  char szBuf[4096];
163 
164  va_list args;
165 
166  va_start(args, fmt);
167  vsnprintf(szBuf, sizeof(szBuf), fmt, args);
168  va_end(args);
169 
170  szBuf[sizeof(szBuf)-1] = '0';
171 
172  fatal(szBuf);
173 }
174 
176 {
177  delete [] bot;
178 }
179 
181 {
182  out.label_counter.reset ();
183  last_fill_index = 0;
184  bWroteGetState = false;
185  bWroteCondCheck = false;
187 }
188 
190 {
191  ptrdiff_t diff = bot - state.bot;
192  char *old_bot = bot;
193  char *old_lim = lim;
194  char *old_top = top;
195  char *old_eof = eof;
196  *(ScannerState*)this = state;
197  if (diff)
198  {
199  tok -= diff;
200  ptr -= diff;
201  cur -= diff;
202  pos -= diff;
203  ctx -= diff;
204  bot = old_bot;
205  lim = old_lim;
206  top = old_top;
207  eof = old_eof;
208  }
209 }
210 
211 } // namespace re2c
void restore_state(const ScannerState &)
Definition: scanner.cc:189
bool bWroteGetState
Definition: main.cc:17
bool rFlag
Definition: opt.h:118
static void need(OutputFile &o, uint32_t ind, bool &readCh, size_t n, bool bSetMarker)
Definition: emit_action.cc:299
void fatalf_at(uint32_t line, const char *,...) const RE2C_GXX_ATTRIBUTE((format(printf
Definition: scanner.cc:145
void reuse()
Definition: scanner.cc:180
void void fatalf(const char *,...) const RE2C_GXX_ATTRIBUTE((format(printf
Definition: scanner.cc:160
void reset_mapCodeName()
Definition: opt.cc:299
void void void fatal(const char *) const
Definition: scanner.h:140
bool bWroteCondCheck
Definition: main.cc:18
void set_in_parse(bool new_in_parse)
Definition: scanner.cc:127
Scanner(Input &, OutputFile &)
Definition: scanner.cc:65
uint32_t cline
Definition: scanner.h:42
uint32_t tline
Definition: scanner.h:41
Opt opts
Definition: opt.cc:7
ScannerState & operator=(const ScannerState &)
Definition: scanner.cc:58
uint32_t last_fill_index
Definition: main.cc:19
static int32_t diff(const re2c::Range *r1, const re2c::Range *r2, const re2c::Range *op1, const re2c::Range *op2, const char *op)
Definition: test.cc:43
void fatal_at(uint32_t line, ptrdiff_t ofs, const char *msg) const
Definition: scanner.cc:132
ptrdiff_t tchar
Definition: scanner.h:40
FILE * file
Definition: input.h:13
counter_t< label_t > label_counter
Definition: output.h:65
Definition: bitmap.cc:10
OutputFile & out
Definition: scanner.h:58