Skip to content

Commit e466722

Browse files
committed
modify InputSource [skip ci]
1 parent b77a471 commit e466722

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

gcc/rust/lex/rust-lex.h

+55-12
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,47 @@ class Lexer
234234
// Input source wrapper thing.
235235
class InputSource
236236
{
237+
private:
238+
unsigned int pos;
239+
std::vector<int> chars;
240+
241+
// Overload operator () to return next char from input stream.
242+
virtual int next_byte () = 0;
243+
244+
int next ()
245+
{
246+
if (pos >= chars.size ())
247+
return EOF;
248+
else
249+
{
250+
int c = chars[pos];
251+
pos++;
252+
return c;
253+
}
254+
}
255+
237256
public:
257+
InputSource () : pos (0), chars ({}) {}
258+
238259
virtual ~InputSource () {}
239260

240-
// Overload operator () to return next char from input stream.
241-
virtual int next () = 0;
261+
// Check if the input source is valid as utf-8 and buffer all characters to
262+
// `chars`.
263+
void init ()
264+
{
265+
// TODO remove
266+
std::cout << "Checking if input is valid as utf-8." << std::endl;
267+
268+
// TODO skip UTF BOM
269+
270+
int c = next_byte ();
271+
while (c != EOF)
272+
{
273+
// TODO validate utf-8 encoding and push one codepoint to `chars`
274+
chars.push_back (c);
275+
c = next_byte ();
276+
}
277+
}
242278
};
243279

244280
class FileInputSource : public InputSource
@@ -247,11 +283,15 @@ class Lexer
247283
// Input source file.
248284
FILE *input;
249285

286+
int next_byte () override { return fgetc (input); }
287+
250288
public:
251289
// Create new input source from file.
252-
FileInputSource (FILE *input) : input (input) {}
253-
254-
int next () override { return fgetc (input); }
290+
FileInputSource (FILE *input) : InputSource (), input (input)
291+
{
292+
// TODO make this better
293+
init ();
294+
}
255295
};
256296

257297
class BufferInputSource : public InputSource
@@ -260,19 +300,22 @@ class Lexer
260300
const std::string &buffer;
261301
size_t offs;
262302

263-
public:
264-
// Create new input source from file.
265-
BufferInputSource (const std::string &b, size_t offset)
266-
: buffer (b), offs (offset)
267-
{}
268-
269-
int next () override
303+
int next_byte () override
270304
{
271305
if (offs >= buffer.size ())
272306
return EOF;
273307

274308
return buffer.at (offs++);
275309
}
310+
311+
public:
312+
// Create new input source from file.
313+
BufferInputSource (const std::string &b, size_t offset)
314+
: InputSource (), buffer (b), offs (offset)
315+
{
316+
// TODO make this better
317+
init ();
318+
}
276319
};
277320

278321
// The input source for the lexer.

0 commit comments

Comments
 (0)