Code cleanup

This commit is contained in:
yhirose 2019-08-27 08:28:03 -04:00
parent d75ced2408
commit b7da359bac

View File

@ -174,9 +174,10 @@ TEST_CASE("Token check test", "[general]")
TEST_CASE("Lambda action test", "[general]") TEST_CASE("Lambda action test", "[general]")
{ {
parser parser( parser parser(R"(
" START <- (CHAR)* " START <- (CHAR)*
" CHAR <- . "); CHAR <- .
)");
string ss; string ss;
parser["CHAR"] = [&](const SemanticValues& sv) { parser["CHAR"] = [&](const SemanticValues& sv) {
@ -370,12 +371,12 @@ TEST_CASE("Skip token test2", "[general]")
TEST_CASE("Backtracking test", "[general]") TEST_CASE("Backtracking test", "[general]")
{ {
parser parser( peg::parser parser(R"(
" START <- PAT1 / PAT2 " START <- PAT1 / PAT2
" PAT1 <- HELLO ' One' " PAT1 <- HELLO ' One'
" PAT2 <- HELLO ' Two' " PAT2 <- HELLO ' Two'
" HELLO <- 'Hello' " HELLO <- 'Hello'
); )");
size_t count = 0; size_t count = 0;
parser["HELLO"] = [&](const SemanticValues& /*sv*/) { parser["HELLO"] = [&](const SemanticValues& /*sv*/) {
@ -429,11 +430,12 @@ TEST_CASE("mutable lambda test", "[general]")
TEST_CASE("Simple calculator test", "[general]") TEST_CASE("Simple calculator test", "[general]")
{ {
auto syntax = auto syntax = R"(
" Additive <- Multitive '+' Additive / Multitive " Additive <- Multitive '+' Additive / Multitive
" Multitive <- Primary '*' Multitive / Primary " Multitive <- Primary '*' Multitive / Primary
" Primary <- '(' Additive ')' / Number " Primary <- '(' Additive ')' / Number
" Number <- [0-9]+ "; Number <- [0-9]+
)";
parser parser(syntax); parser parser(syntax);
@ -509,15 +511,15 @@ TEST_CASE("Calculator test", "[general]")
TEST_CASE("Calculator test2", "[general]") TEST_CASE("Calculator test2", "[general]")
{ {
// Parse syntax // Parse syntax
auto syntax = auto syntax = R"(
" # Grammar for Calculator...\n " # Grammar for Calculator...
" EXPRESSION <- TERM (TERM_OPERATOR TERM)* " EXPRESSION <- TERM (TERM_OPERATOR TERM)*
" TERM <- FACTOR (FACTOR_OPERATOR FACTOR)* " TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
" FACTOR <- NUMBER / '(' EXPRESSION ')' " FACTOR <- NUMBER / '(' EXPRESSION ')'
" TERM_OPERATOR <- [-+] " TERM_OPERATOR <- [-+]
" FACTOR_OPERATOR <- [/*] " FACTOR_OPERATOR <- [/*]
" NUMBER <- [0-9]+ " NUMBER <- [0-9]+
; )";
string start; string start;
auto grammar = ParserGenerator::parse(syntax, strlen(syntax), start, nullptr); auto grammar = ParserGenerator::parse(syntax, strlen(syntax), start, nullptr);
@ -555,15 +557,15 @@ TEST_CASE("Calculator test2", "[general]")
TEST_CASE("Calculator test3", "[general]") TEST_CASE("Calculator test3", "[general]")
{ {
// Parse syntax // Parse syntax
parser parser( parser parser(R"(
" # Grammar for Calculator...\n " # Grammar for Calculator...
" EXPRESSION <- TERM (TERM_OPERATOR TERM)* " EXPRESSION <- TERM (TERM_OPERATOR TERM)*
" TERM <- FACTOR (FACTOR_OPERATOR FACTOR)* " TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
" FACTOR <- NUMBER / '(' EXPRESSION ')' " FACTOR <- NUMBER / '(' EXPRESSION ')'
" TERM_OPERATOR <- [-+] " TERM_OPERATOR <- [-+]
" FACTOR_OPERATOR <- [/*] " FACTOR_OPERATOR <- [/*]
" NUMBER <- [0-9]+ " NUMBER <- [0-9]+
); )");
auto reduce = [](const SemanticValues& sv) -> long { auto reduce = [](const SemanticValues& sv) -> long {
long ret = sv[0].get<long>(); long ret = sv[0].get<long>();
@ -596,15 +598,15 @@ TEST_CASE("Calculator test3", "[general]")
TEST_CASE("Calculator test with AST", "[general]") TEST_CASE("Calculator test with AST", "[general]")
{ {
parser parser( parser parser(R"(
" EXPRESSION <- _ TERM (TERM_OPERATOR TERM)* " EXPRESSION <- _ TERM (TERM_OPERATOR TERM)*
" TERM <- FACTOR (FACTOR_OPERATOR FACTOR)* " TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
" FACTOR <- NUMBER / '(' _ EXPRESSION ')' _ " FACTOR <- NUMBER / '(' _ EXPRESSION ')' _
" TERM_OPERATOR <- < [-+] > _ " TERM_OPERATOR <- < [-+] > _
" FACTOR_OPERATOR <- < [/*] > _ " FACTOR_OPERATOR <- < [/*] > _
" NUMBER <- < [0-9]+ > _ " NUMBER <- < [0-9]+ > _
" ~_ <- [ \t\r\n]* " ~_ <- [ \t\r\n]*
); )");
parser.enable_ast(); parser.enable_ast();
@ -639,12 +641,12 @@ TEST_CASE("Calculator test with AST", "[general]")
TEST_CASE("Ignore semantic value test", "[general]") TEST_CASE("Ignore semantic value test", "[general]")
{ {
parser parser( parser parser(R"(
" START <- ~HELLO WORLD " START <- ~HELLO WORLD
" HELLO <- 'Hello' _ " HELLO <- 'Hello' _
" WORLD <- 'World' _ " WORLD <- 'World' _
" _ <- [ \t\r\n]* " _ <- [ \t\r\n]*
); )");
parser.enable_ast(); parser.enable_ast();
@ -658,13 +660,13 @@ TEST_CASE("Ignore semantic value test", "[general]")
TEST_CASE("Ignore semantic value of 'or' predicate test", "[general]") TEST_CASE("Ignore semantic value of 'or' predicate test", "[general]")
{ {
parser parser( parser parser(R"(
" START <- _ !DUMMY HELLO_WORLD '.' " START <- _ !DUMMY HELLO_WORLD '.'
" HELLO_WORLD <- HELLO 'World' _ " HELLO_WORLD <- HELLO 'World' _
" HELLO <- 'Hello' _ " HELLO <- 'Hello' _
" DUMMY <- 'dummy' _ " DUMMY <- 'dummy' _
" ~_ <- [ \t\r\n]* " ~_ <- [ \t\r\n]*
); )");
parser.enable_ast(); parser.enable_ast();
@ -678,12 +680,12 @@ TEST_CASE("Ignore semantic value of 'or' predicate test", "[general]")
TEST_CASE("Ignore semantic value of 'and' predicate test", "[general]") TEST_CASE("Ignore semantic value of 'and' predicate test", "[general]")
{ {
parser parser( parser parser(R"(
" START <- _ &HELLO HELLO_WORLD '.' " START <- _ &HELLO HELLO_WORLD '.'
" HELLO_WORLD <- HELLO 'World' _ " HELLO_WORLD <- HELLO 'World' _
" HELLO <- 'Hello' _ " HELLO <- 'Hello' _
" ~_ <- [ \t\r\n]* " ~_ <- [ \t\r\n]*
); )");
parser.enable_ast(); parser.enable_ast();
@ -749,19 +751,19 @@ TEST_CASE("Literal token on AST test3", "[general]")
TEST_CASE("Missing missing definitions test", "[general]") TEST_CASE("Missing missing definitions test", "[general]")
{ {
parser parser( parser parser(R"(
" A <- B C " A <- B C
); )");
REQUIRE(!parser); REQUIRE(!parser);
} }
TEST_CASE("Definition duplicates test", "[general]") TEST_CASE("Definition duplicates test", "[general]")
{ {
parser parser( parser parser(R"(
" A <- ''" A <- ''
" A <- ''" A <- ''
); )");
REQUIRE(!parser); REQUIRE(!parser);
} }
@ -1030,29 +1032,29 @@ TEST_CASE("Backreference with Option test", "[backreference]")
TEST_CASE("Left recursive test", "[left recursive]") TEST_CASE("Left recursive test", "[left recursive]")
{ {
parser parser( parser parser(R"(
" A <- A 'a'" A <- A 'a'
" B <- A 'a'" B <- A 'a'
); )");
REQUIRE(!parser); REQUIRE(!parser);
} }
TEST_CASE("Left recursive with option test", "[left recursive]") TEST_CASE("Left recursive with option test", "[left recursive]")
{ {
parser parser( parser parser(R"(
" A <- 'a' / 'b'? B 'c' " A <- 'a' / 'b'? B 'c'
" B <- A " B <- A
); )");
REQUIRE(!parser); REQUIRE(!parser);
} }
TEST_CASE("Left recursive with zom test", "[left recursive]") TEST_CASE("Left recursive with zom test", "[left recursive]")
{ {
parser parser( parser parser(R"(
" A <- 'a'* A* " A <- 'a'* A*
); )");
REQUIRE(!parser); REQUIRE(!parser);
} }