Added lexer from previous project

This commit is contained in:
Pasha Bibko
2025-04-16 19:32:26 +01:00
parent 753c72c653
commit 4ac5061f03
14 changed files with 506 additions and 23 deletions

View File

@@ -2,7 +2,9 @@
#include <iostream>
#include <fstream>
#include <string>
#include <memory>
#include <Lexer.h>
#include <Util.h>
namespace LX
@@ -12,6 +14,7 @@ namespace LX
struct IncorrectCommandLineArgs {};
struct InvalidInputFilePath {};
struct InvalidOutputFilePath {};
struct InvalidLogFilePath {};
}
int main(int argc, char** argv)
@@ -19,7 +22,7 @@ int main(int argc, char** argv)
try
{
// Checks there is the correct ammount of arguments
LX::ThrowIf<LX::IncorrectCommandLineArgs>(argc != 3);
LX::ThrowIf<LX::IncorrectCommandLineArgs>((argc == 3 || argc == 4) == false);
// Turns the file paths into the C++ type for handling them
std::filesystem::path inpPath = argv[1];
@@ -28,18 +31,32 @@ int main(int argc, char** argv)
// Prints the full paths to the console to let the user know compiling is being done
std::cout << std::filesystem::absolute(inpPath) << " -> " << std::filesystem::absolute(outPath) << std::endl;
// Checks the input file exists
// Checks the input file exists and opens it
LX::ThrowIf<LX::InvalidInputFilePath>(std::filesystem::exists(inpPath) == false);
std::ifstream inpFile(inpPath, std::ios::binary | std::ios::ate); // Opens in binary at the end for microptimisation
LX::ThrowIf<LX::InvalidInputFilePath>(inpFile.is_open() == false);
// Opens / Creates the output file and checks if it is open
// Opens / Creates the output file
std::ofstream outFile(outPath);
LX::ThrowIf<LX::InvalidOutputFilePath>(outFile.is_open() == false);
// Opens / Creates the log file
std::unique_ptr<std::ofstream> log = nullptr;
if (argc == 4)
{
log = std::make_unique<std::ofstream>(argv[3]);
LX::ThrowIf<LX::InvalidLogFilePath>(log->is_open() == false);
}
// Create tokens out of the input file
std::vector<LX::Token>tokens = LX::LexicalAnalyze(inpFile, log.get());
}
catch (LX::IncorrectCommandLineArgs)
{
// Displays to the console of how to use the program
std::cout << "\nUsage: [source file] [output file]\n";
std::cout << "\nUsage: [source file] [output file] (optional)[log file]\n";
return 1;
}
@@ -47,7 +64,7 @@ int main(int argc, char** argv)
catch (LX::InvalidInputFilePath)
{
// Tells user the input file could not be found
std::cout << "\nFile path: {" << argv[1] << "} could not be found\n";
std::cout << "\nFile path: {" << argv[1] << "} could not be opened\n";
return 2;
}
@@ -55,7 +72,25 @@ int main(int argc, char** argv)
catch (LX::InvalidOutputFilePath)
{
// Tells the user the output file could not be opened
std::cout << "\nCould not open/create {" << argv[2] << "}";
std::cout << "\nCould not open/create {" << argv[2] << "}\n";
}
catch (LX::InvalidCharInSource& e)
{
//
std::cout << "\nInvalid character found in source file: {" << e.invalid << "} at index: " << e.index << "\n";
}
catch (std::exception& e)
{
// Prints the std exception to the console
std::cout << "\nAn error occured:\n" << e.what() << std::endl;
}
catch (...)
{
// Tells the user if an error has happened
std::cout << "\nAn Error occured\n";
}
return 0;