-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.hxx
More file actions
91 lines (83 loc) · 2.12 KB
/
token.hxx
File metadata and controls
91 lines (83 loc) · 2.12 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef HXX__ncc__token__
#define HXX__ncc__token__
#include <string>
#include <iostream>
#include "exceptions.hxx"
namespace ncc {
static const char TOKEN_EOF = 0;
static const char TOKEN_INT_VALUE = 1;
static const char TOKEN_FLOAT_VALUE = 2;
static const char TOKEN_STRING = 3;
static const char TOKEN_IDENT = 4;
static const char TOKEN_IF = 5;
static const char TOKEN_ELSE = 6;
static const char TOKEN_WHILE = 7;
static const char TOKEN_FOR = 8;
static const char TOKEN_DO = 9;
static const char TOKEN_INT = 10;
static const char TOKEN_FLOAT = 11;
static const char TOKEN_PTR = 12;
static const char TOKEN_RETURN = 13;
static const char TOKEN_EQUAL = 14;
static const char TOKEN_NOT_EQUAL = 15;
static const char TOKEN_GT_EQUAL = 16;
static const char TOKEN_LT_EQUAL = 17;
static const char TOKEN_SC_AND = 18;
static const char TOKEN_SC_OR = 19;
class Tokenizer {
protected:
std::istream& stream;
char token;
std::string text;
int int_value;
double float_value;
int line;
int column;
// TODO: keep track of file postion
void parse_number();
void skip_line_comment();
void skip_comment();
char read_char_escape();
void parse_string();
int read_char(bool eof_ok);
void unread_char();
public:
Tokenizer(std::istream& stream):
stream(stream), line(1), column(0) {};
char current_token(){
return token;
}
const std::string& get_string_value(){
return text;
}
int get_int_value(){
return int_value;
}
double get_float_value() {
return float_value;
}
const std::string& get_text(){
return text;
}
void next_token();
void eat_token(char expected){
if (current_token() != expected){
throw new ExpectedToken(expected, current_token());
}
next_token();
}
void next_token_expect(char expected){
next_token();
if (current_token() != expected){
throw new ExpectedToken(expected, current_token());
}
}
int get_line(){
return line;
}
int get_column(){
return column;
}
};
}
#endif