-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.cpp
108 lines (98 loc) · 2.26 KB
/
tokenizer.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "stdafx.h"
#include "tokenizer.h"
void tokenizer::set_lookahead()
{
while (curridx < endidx && isspace(text[curridx]))
{
if (text[curridx] == '\n')
{
currcol = 1;
currline++;
}
else
{
currcol++;
}
curridx++;
}
lookahead.lineno = currline;
lookahead.colno = currcol;
if (curridx == endidx)
{
lookahead.type = tt_eof;
return;
}
char c = text[curridx];
if (c == '(' || c == ')' || c == '{' || c == '}' || c == ',')
{
lookahead.type = (c == '(') ? tt_lparen :
(c == ')') ? tt_rparen :
(c == '{') ? tt_lbrace :
(c == '}') ? tt_rbrace :
tt_comma;
curridx++;
currcol++;
return;
}
if (isalpha(c)) // ident
{
string result;
while (curridx < endidx && isalpha(text[curridx]))
{
result += text[curridx];
curridx++;
currcol++;
}
if (result == "repeat")
{
lookahead.type = tt_repeat;
return;
}
if (result == "if")
{
lookahead.type = tt_if;
return;
}
if (result == "def")
{
lookahead.type = tt_def;
return;
}
// check other keywords here
if (result == "true" || result == "false")
{
lookahead.type = tt_boolval;
lookahead.bool_value = result == "true";
return;
}
lookahead.type = tt_ident;
lookahead.string_value = result;
return;
}
if (isdigit(c)) // numeric
{
string result;
while (curridx < endidx && isalnum(text[curridx]))
{
result += text[curridx];
curridx++;
currcol++;
}
auto c_str = result.c_str();
char* last_char;
long converted = strtol(c_str, &last_char, 10);
if (*last_char == 0)
{
lookahead.type = tt_number;
lookahead.num_value = converted;
return;
}
if (*last_char == 's' && *(last_char + 1) == 0)
{
lookahead.type = tt_duration;
lookahead.num_value = converted;
return;
}
}
lookahead.type = tt_error;
}