-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preprocessor to convert cppx to cpp code
- Loading branch information
1 parent
3c8f0b4
commit ebef05b
Showing
4 changed files
with
466 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <cctype> | ||
#include <iostream> | ||
#include <regex> | ||
#include <sstream> | ||
#include <stack> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
class Preprocessor { | ||
public: | ||
static std::string Process(const std::string& input); | ||
|
||
private: | ||
struct DOMNode { | ||
std::string type; | ||
std::string tagName; | ||
std::unordered_map<std::string, std::string> attributes; | ||
std::vector<DOMNode> children; | ||
std::string textContent; | ||
}; | ||
|
||
static std::string AddHeader(const std::string& script); | ||
static std::vector<std::string> ExtractValidHtmlBlocks(const std::string& script); | ||
static DOMNode ParseHTML(const std::string& html, size_t& pos); | ||
static std::string GenerateJSON(const DOMNode& node, int indent = 0); | ||
static std::string CorrectIndentation(const std::string& code); | ||
static std::string Trim(const std::string& str); | ||
static std::unordered_map<std::string, std::string> ParseAttributes(const std::string& attrString); | ||
static const std::unordered_set<std::string> htmlTags; | ||
static const std::string header; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,42 @@ | ||
#include <chrono> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <random> | ||
#include <string> | ||
#include <thread> | ||
#include <sstream> | ||
|
||
#include "cppx/json.hpp" | ||
#include "cppx/preprocessor.hpp" | ||
|
||
int main() { | ||
std::mt19937 rng(std::random_device{}()); | ||
std::uniform_int_distribution<int> dist(1000, 9999); | ||
std::string ReadFile(const std::string& filePath) { | ||
std::ifstream inFile(filePath); | ||
if (!inFile) { | ||
std::cerr << "Error: Unable to open input file: " << filePath << std::endl; | ||
return ""; | ||
} | ||
std::stringstream buffer; | ||
buffer << inFile.rdbuf(); | ||
return buffer.str(); | ||
} | ||
|
||
while (true) { | ||
JSON json = { | ||
"html", { | ||
"children", { | ||
"head", { | ||
"children", { | ||
"title", nullptr | ||
} | ||
}, | ||
"body", { | ||
"children", { | ||
"h1", { | ||
"children", "Welcome to the landing page!" | ||
}, | ||
"p", { | ||
"children", "Your random number is: " + std::to_string(dist(rng)) | ||
}, | ||
"", "Hello, ", | ||
"strong", { | ||
"children", "world" | ||
}, | ||
"", "!", | ||
"footer", { | ||
"children", "© 2024 CPPX 🚀" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
void WriteFile(const std::string& filePath, const std::string& content) { | ||
std::ofstream outFile(filePath); | ||
if (!outFile) { | ||
std::cerr << "Error: Unable to open output file: " << filePath << std::endl; | ||
return; | ||
} | ||
outFile << content; | ||
} | ||
|
||
json["mutations"]["boolean"] = true; | ||
json["mutations"]["integral"] = 420; | ||
json["mutations"]["floating"] = 3.14; | ||
json["mutations"]["string"] = "Hello, world!"; | ||
json["mutations"]["array"] = std::vector<JSON>{nullptr, true, 420, 3.14, "Hello, world!"}; | ||
json["mutations"]["object"] = JSON{ | ||
"first", 1, | ||
"second", 2, | ||
"third", 3 | ||
}; | ||
int main(int argc, char* argv[]) { | ||
if (argc != 3) { | ||
std::cerr << "Usage: cppx <input_file> <output_file>" << std::endl; | ||
return 1; | ||
} | ||
|
||
std::cout << "\033[2J\033[1;1H" << json << std::endl; | ||
std::string inputFilePath = argv[1]; | ||
std::string outputFilePath = argv[2]; | ||
|
||
try { | ||
JSON parsed_json = JSON::parse(json.stringify()); | ||
} catch (const std::exception& e) { | ||
std::cerr << "Parse error: " << e.what() << std::endl; | ||
} | ||
std::string inputScript = ReadFile(inputFilePath); | ||
std::string transformedScript = Preprocessor::Process(inputScript); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
WriteFile(outputFilePath, transformedScript); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.