-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.h
66 lines (53 loc) · 985 Bytes
/
tokenizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef TOKENIZER_H
#define TOKENIZER_H
#include <string>
using namespace std; // never do this
enum token_type
{
// keywords
tt_repeat,
tt_if,
tt_def,
tt_ident,
tt_number,
tt_duration,
tt_boolval,
// punctuation
tt_lparen,
tt_rparen,
tt_lbrace,
tt_rbrace,
tt_comma,
// end of file
tt_eof,
// parse error
tt_error
};
struct token
{
token_type type;
string string_value;
long num_value;
bool bool_value;
int lineno;
int colno;
};
class tokenizer
{
const string text;
int curridx;
int endidx;
int currline, currcol;
token lookahead;
void set_lookahead();
public:
tokenizer(string text) : text(text), curridx(0), endidx(text.length()), currline(1), currcol(1)
{
lookahead.num_value = 0;
lookahead.string_value = "";
set_lookahead();
}
token peek_next() { return lookahead; }
void move_ahead() { set_lookahead(); }
};
#endif