-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.cpp
77 lines (56 loc) · 1.5 KB
/
Scanner.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
#include "Scanner.h"
#include <fstream>
#include <sstream>
Metal::Scanner * g_scanner = nullptr;
namespace
{
const std::string extractPath(const std::string & filename,const std::string & separator) {
std::string path;
auto sepPos = filename.find_last_of(separator);
if(sepPos != std::string::npos) {
path = filename.substr(0, sepPos);
}
return path;
}
}
Metal::Scanner::Scanner(const std::string & filename) : yyFlexLexer() {
g_scanner = this;
pushFile(filename);
_path = extractPath(filename, getSeparator());
}
Metal::Scanner::~Scanner () {
g_scanner = nullptr;
}
Metal::Scanner * Metal::Scanner::getInstance() {
return g_scanner;
}
void Metal::Scanner::pushFile(const std::string & filename) {
const std::string fullPath = !_path.empty() ? _path + getSeparator() + filename : filename;
std::ifstream * stream = new std::ifstream(fullPath); // TODO: leaks
if(!stream->good()) {
std::cerr << "Could not find file: " << fullPath << std::endl;
delete stream;
return;
}
yypush_buffer_state(yy_create_buffer(stream,2 << 16));
}
void Metal::Scanner::popFile() {
yypop_buffer_state();
}
const std::string Metal::Scanner::getSeparator() const {
return "/";
}
int Metal::Scanner::comment(int lines) {
while(true) {
const char c0 = yyinput();
if (c0 == '\n')
lines++;
if(c0!='*' && c0!=0)
continue;
const char c1 = yyinput();
if(c1!='/' && c1!=0)
continue;
break;
}
return lines;
}