-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdump_tokens.cpp
More file actions
47 lines (38 loc) · 1.33 KB
/
Copy pathdump_tokens.cpp
File metadata and controls
47 lines (38 loc) · 1.33 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
#include "lexer.h"
#include <iostream>
#include <fstream>
#include <sstream>
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <source_file>" << std::endl;
return 1;
}
std::string filename = argv[1];
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Cannot open source file: " << filename << std::endl;
return 1;
}
std::stringstream buffer;
buffer << file.rdbuf();
std::string source = buffer.str();
Lexer lexer(source);
std::vector<Token> tokens = lexer.tokenize();
std::cout << "Token dump for " << filename << ":" << std::endl;
std::cout << "----------------------------" << std::endl;
// Show tokens around position 196
int target = 196;
int start = std::max(0, target - 10);
int end = std::min(static_cast<int>(tokens.size()), target + 10);
for (int i = start; i < end; i++) {
const Token& token = tokens[i];
std::cout << i << ": Type=" << static_cast<int>(token.type)
<< ", Value='" << token.value << "'"
<< ", Line=" << token.line << ", Column=" << token.column;
if (i == target) {
std::cout << " <-- STUCK HERE";
}
std::cout << std::endl;
}
return 0;
}