-
Notifications
You must be signed in to change notification settings - Fork 0
/
RpalScanner.cpp
143 lines (120 loc) · 3.15 KB
/
RpalScanner.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <fstream>
#include <string>
#include <algorithm>
#include <queue>
#include <ctype.h>
#include <boost/tokenizer.hpp>
#include "RpalScanner.h"
using namespace std;
const int RpalScanner::N_KEYWORDS = 20;
const string RpalScanner::KEYWORDS[] =
{"let", "in", "fn", "where", "aug", "or", "and", "not", "gr", "ls", "ge", "le", "eq", "ne", "rec", "within",
"nil", "true", "false", "dummy"};
RpalScanner::RpalScanner(char* filename, bool showSource)
{
string raw_source, line;
ifstream input_file;
input_file.open(filename);
if (input_file.is_open())
{
while ( input_file.good() )
{
getline (input_file, line);
//strip leading/trailing whitespace
line.erase(remove(line.begin(), line.end(), '\t'), line.end());
//strip comments
int commentStart = line.find("//");
if (commentStart != string::npos)
line.erase(line.find("//"), string::npos);
raw_source +=" " + line; //add space so newlines are separated
}
input_file.close();
if (showSource)
cout << "Raw source: \n" << raw_source << endl << endl;
boost::char_separator<char> sep("\n", " '()/*@&<>=+->|.,");
Tokenizer tokenizer(raw_source, sep);
process_tokens(tokenizer);
}
else
cout << "File could not be opened\n";
}
void RpalScanner::process_tokens(Tokenizer tokenizer)
{
Tokenizer::iterator iter = tokenizer.begin();
while (iter != tokenizer.end())
{
string nt = "";
//discard whitespace until a token or EOF is reached
while (iter != tokenizer.end() && (nt == "" || nt == " " || nt == "\t"))
{
nt = *(iter++);
}
if (nt == "\'") //string, keep reading until ' closes string
{
while (*iter != "\'")
{
if(iter == tokenizer.end())
cout << "Error: Encountered EOF without closing string";
nt += *(iter++);
}
nt += *(iter++);
}
//special handling for multi-character operators
else if (nt == "*" && *iter == "*")
nt += *(iter++); //nt = **
else if (nt == "-" && *iter == ">")
nt += *(iter++); //nt = ->
else if (nt == "<" && *iter == "=")
nt += *(iter++); //nt = <=
else if (nt == ">" && *iter == "=")
nt += *(iter++); //nt = <=
if (!(nt == " " || nt == "\t" || nt == ""))
_tokens.push(nt);
}
}
bool RpalScanner::Has_Next()
{
return !(_tokens.empty());
}
string RpalScanner::Next_Token()
{
string nt = _tokens.front();
_tokens.pop();
return nt;
}
//token type identification methods (static)
bool RpalScanner::IsID(string token)
{
if (!isalpha(token[0]))
return false;
for (int i = 0 ; i < N_KEYWORDS ; i++)
{
if (token == KEYWORDS[i])
return false;
}
int i = 1;
while (token[i])
{
if (!isalnum(token[i]) && token[i] != '_')
return false;
i++;
}
return true;
}
bool RpalScanner::IsString(string token)
{
return (token[0] == '\'' && *(token.rbegin()) == '\'');
}
bool RpalScanner::IsInt(string token)
{
if (!isdigit(token[0]))
return false;
int i = 1;
while (token[i])
{
if (!isdigit(token[i]))
return false;
i++;
}
return true;
}