-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (117 loc) · 4.04 KB
/
Copy pathmain.cpp
File metadata and controls
140 lines (117 loc) · 4.04 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
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
#include <metalyzer/frontend/LexerSpec.hpp>
#include <metalyzer/frontend/SpecParser.hpp>
#include <metalyzer/generator/Generator.hpp>
#include <metalyzer/lexer/LexerBuilder.hpp>
#include <metalyzer/lexer/TransTable.hpp>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
void printTransTable(const metalyzer::lexer::TransTable &tt) {
std::cout << "\n=== Compressed Transition Table ===\n";
std::cout << "Start State Index: " << tt.startStateId << "\n";
std::cout << "Rows (States): " << tt.table.size() << "\n";
std::cout << "Cols (Chars): " << (tt.table.empty() ? 0 : tt.table[0].size())
<< "\n";
std::cout << "-----------------------------------\n";
std::vector<int> usedChars;
if (!tt.table.empty()) {
for (size_t c = 0; c < tt.table[0].size(); ++c) {
bool used = false;
for (const auto &row : tt.table) {
if (row[c] != -1) {
used = true;
break;
}
}
if (used)
usedChars.push_back(c);
}
}
std::cout << " State | Rule |";
for (int c : usedChars) {
if (c >= 32 && c <= 126)
std::cout << " '" << (char)c << "' |";
else
std::cout << " x" << std::hex << c << std::dec << " |";
}
std::cout << "\n-------|------|";
for (size_t i = 0; i < usedChars.size(); ++i)
std::cout << "-----|";
std::cout << "\n";
for (size_t i = 0; i < tt.table.size(); ++i) {
std::cout << std::setw(6) << i << " | ";
int ruleId = tt.acceptRuleIds[i];
if (ruleId != -1)
std::cout << std::setw(4) << ruleId << " |";
else
std::cout << " - |";
for (int c : usedChars) {
int target = tt.table[i][c];
if (target == -1)
std::cout << " - |";
else
std::cout << std::setw(4) << target << " |";
}
std::cout << "\n";
}
std::cout << "-----------------------------------\n";
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: ./metalyzer_app <path_to_spec.mz>\n";
exit(1);
}
std::ifstream mzfilestream(argv[1]);
if (!mzfilestream.is_open()) {
std::cerr << "Filestream not open\n";
exit(1);
}
std::stringstream ss;
ss << mzfilestream.rdbuf();
std::string fileContent = ss.str();
// Extract the filename context dynamically (e.g., "../specs/json_core.mz" ->
// "json_core")
std::filesystem::path specPath(argv[1]);
std::string dynamicGrammarName = specPath.stem().string();
std::cout << "=== Metalyzer Parser & Builder Test ===\n";
std::cout << "Input File Content:\n" << fileContent << "\n\n";
std::cout << ">>> Parsing .mz content...\n";
metalyzer::frontend::SpecParser parser;
metalyzer::frontend::LexerSpec spec;
try {
spec = parser.parse(fileContent);
} catch (const std::exception &e) {
std::cerr << "Parser Error: " << e.what() << std::endl;
return 1;
}
std::cout << ">>> Parse Success!\n";
std::cout << "Header Code Size: " << spec.headerCode.size() << " chars\n";
std::cout << "User Code Size: " << spec.userCode.size() << " chars\n";
std::cout << "Found " << spec.rules.size() << " rules:\n";
for (const auto &rule : spec.rules) {
std::cout << " [" << rule.priority << "] Regex: '" << rule.regex
<< "' -> Action: '" << rule.actionCode << "'\n";
}
std::cout << "\n>>> Building Lexer Engine for grammar: " << dynamicGrammarName
<< "...\n";
metalyzer::lexer::LexerBuilder builder;
for (const auto &rule : spec.rules) {
builder.addRule(rule.regex, rule.priority);
}
// Pass your dynamically resolved grammar name directly into the build
// execution layer
metalyzer::lexer::TransTable table = builder.build(dynamicGrammarName);
printTransTable(table);
std::cout << "\n>>> Generating C++ Lexer Code...\n";
metalyzer::generator::Generator::Config config;
config.outputDir = "generated_lexer";
config.className = "MyLexer";
config.namespaceName = "user_code";
metalyzer::generator::Generator gen(config);
gen.generate(table, spec);
std::cout << "Done! Output in 'generated_lexer/'\n";
return 0;
}