219 lines
6.0 KiB
C++
219 lines
6.0 KiB
C++
#include <catch2/catch.hpp>
|
|
#include "../src/Lexer.hpp"
|
|
#include "../src/Parser.hpp"
|
|
|
|
using namespace sn;
|
|
|
|
class ParserTest
|
|
{
|
|
public:
|
|
explicit ParserTest() {}
|
|
virtual ~ParserTest() {}
|
|
|
|
void test_parse(std::string const& oracle,
|
|
std::string const& source)
|
|
{
|
|
Lexer lexer;
|
|
lexer.scan(source);
|
|
|
|
Parser parser;
|
|
auto node = parser.parse(lexer.all());
|
|
|
|
REQUIRE(oracle == node->string());
|
|
}
|
|
|
|
protected:
|
|
};
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_rule")
|
|
{
|
|
std::stringstream ss;
|
|
ss << " hello.elf -> hello.cpp {" << std::endl;
|
|
ss << " g++ hello.cpp -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC(RULE(TARGET("
|
|
"IDENT[hello.elf]"
|
|
"),DEPS("
|
|
"IDENT[hello.cpp]"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],IDENT[hello.cpp],IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_var_decl_and_use")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = world.hpp" << std::endl;
|
|
ss << " $BIM -> hello.cpp {" << std::endl;
|
|
ss << " g++ $BIM -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],IDENT[world.hpp]),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"IDENT[hello.cpp]"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],VAR[BIM],IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_var_array")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = (world.hpp world.cpp)" << std::endl;
|
|
ss << " $BIM -> hello.cpp {" << std::endl;
|
|
ss << " g++ $BIM -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],ARRAY(IDENT[world.hpp],IDENT[world.cpp])),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"IDENT[hello.cpp]"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],VAR[BIM],IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_array")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = (world.hpp world.cpp)" << std::endl;
|
|
ss << " $BIM -> (hello.cpp world.cpp) {" << std::endl;
|
|
ss << " g++ $BIM -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],ARRAY(IDENT[world.hpp],IDENT[world.cpp])),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"ARRAY(IDENT[hello.cpp],IDENT[world.cpp])"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],VAR[BIM],IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_array_index")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = (world.hpp world.cpp)" << std::endl;
|
|
ss << " $BIM -> (hello.cpp world.cpp) {" << std::endl;
|
|
ss << " g++ $BIM[4] -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],ARRAY(IDENT[world.hpp],IDENT[world.cpp])),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"ARRAY(IDENT[hello.cpp],IDENT[world.cpp])"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],INDEX(VAR[BIM],IDENT[4]),IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_array_index_literal")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = (world.hpp world.cpp)[14]" << std::endl;
|
|
ss << " $BIM -> (hello.cpp world.cpp) {" << std::endl;
|
|
ss << " g++ $BIM[4] -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],"
|
|
"INDEX(ARRAY(IDENT[world.hpp],IDENT[world.cpp]),IDENT[14])),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"ARRAY(IDENT[hello.cpp],IDENT[world.cpp])"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],INDEX(VAR[BIM],IDENT[4]),IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_replace_ext_index")
|
|
{
|
|
std::stringstream ss;
|
|
ss << "$BIM = hello.cpp" << std::endl;
|
|
ss << "$BAM = (world.hpp world.cpp)[14]" << std::endl;
|
|
ss << " $BIM -> (hello.cpp world.cpp) {" << std::endl;
|
|
ss << " g++ $BIM[.c=.o] -o hello.elf, " << std::endl;
|
|
ss << " ls " << std::endl;
|
|
ss << " }" << std::endl;
|
|
|
|
test_parse("DOC("
|
|
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
|
|
"VAR_DECL(VAR[BAM],"
|
|
"INDEX(ARRAY(IDENT[world.hpp],IDENT[world.cpp]),IDENT[14])),"
|
|
"RULE("
|
|
"TARGET("
|
|
"VAR[BIM]"
|
|
"),DEPS("
|
|
"ARRAY(IDENT[hello.cpp],IDENT[world.cpp])"
|
|
"),BLOCK("
|
|
"CMD("
|
|
"IDENT[g++],INDEX(VAR[BIM],IDENT[.c],IDENT[.o]),IDENT[-o],IDENT[hello.elf]"
|
|
"),CMD("
|
|
"IDENT[ls]"
|
|
")"
|
|
")))",
|
|
ss.str());
|
|
}
|