2021-08-19 06:29:11 +00:00
|
|
|
|
#include <gtest/gtest.h>
|
2020-02-07 18:28:01 +00:00
|
|
|
|
#include <peglib.h>
|
|
|
|
|
|
|
|
|
|
using namespace peg;
|
|
|
|
|
|
|
|
|
|
#if !defined(PEGLIB_NO_UNICODE_CHARS)
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Simple_syntax_test_with_unicode) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(u8" ROOT ← _ "
|
|
|
|
|
" _ <- ' ' ");
|
|
|
|
|
|
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Simple_syntax_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- _
|
|
|
|
|
_ <- ' '
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Empty_syntax_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser("");
|
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Start_rule_with_ignore_operator_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-08-07 14:21:47 +00:00
|
|
|
|
~ROOT <- _
|
|
|
|
|
_ <- ' '
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(ret);
|
2020-08-07 14:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Invalid_UTF8_text_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::string s = "a <- '";
|
|
|
|
|
s += static_cast<char>(0xe8); // Make invalid utf8 text...
|
2020-08-07 17:35:48 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(s.data());
|
2020-08-07 17:35:48 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(ret);
|
2020-08-07 17:35:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Backslash_escape_sequence_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- _
|
|
|
|
|
_ <- '\\'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Invalid_escape_sequence_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- _
|
|
|
|
|
_ <- '\'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Action_taking_non_const_Semantic_Values_parameter) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- TEXT
|
|
|
|
|
TEXT <- [a-zA-Z]+
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["ROOT"] = [&](SemanticValues &vs) {
|
|
|
|
|
auto s = std::string(std::any_cast<std::string_view>(vs[0]));
|
|
|
|
|
s[0] = 'H'; // mutate
|
|
|
|
|
return s; // move
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["TEXT"] = [&](SemanticValues &vs) { return vs.token(); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::string val;
|
|
|
|
|
auto ret = parser.parse("hello", val);
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ("Hello", val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, String_capture_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- _ ('[' TAG_NAME ']' _)*
|
|
|
|
|
TAG_NAME <- (!']' .)+
|
|
|
|
|
_ <- [ \t]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::vector<std::string_view> tags;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["TAG_NAME"] = [&](const SemanticValues &vs) {
|
|
|
|
|
tags.push_back(vs.sv());
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(" [tag1] [tag:2] [tag-3] ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(3, tags.size());
|
|
|
|
|
EXPECT_EQ("tag1", tags[0]);
|
|
|
|
|
EXPECT_EQ("tag:2", tags[1]);
|
|
|
|
|
EXPECT_EQ("tag-3", tags[2]);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using namespace peg;
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, String_capture_test2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::vector<std::string_view> tags;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
Definition ROOT, TAG, TAG_NAME, WS;
|
|
|
|
|
ROOT <= seq(WS, zom(TAG));
|
|
|
|
|
TAG <= seq(chr('['), TAG_NAME, chr(']'), WS);
|
|
|
|
|
TAG_NAME <= oom(seq(npd(chr(']')), dot())),
|
|
|
|
|
[&](const SemanticValues &vs) { tags.push_back(vs.sv()); };
|
|
|
|
|
WS <= zom(cls(" \t"));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto r = ROOT.parse(" [tag1] [tag:2] [tag-3] ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(r.ret);
|
|
|
|
|
EXPECT_EQ(3, tags.size());
|
|
|
|
|
EXPECT_EQ("tag1", tags[0]);
|
|
|
|
|
EXPECT_EQ("tag:2", tags[1]);
|
|
|
|
|
EXPECT_EQ("tag-3", tags[2]);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, String_capture_test3) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser pg(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- _ TOKEN*
|
|
|
|
|
TOKEN <- '[' < (!']' .)+ > ']' _
|
|
|
|
|
_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::vector<std::string_view> tags;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
pg["TOKEN"] = [&](const SemanticValues &vs) { tags.push_back(vs.token()); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = pg.parse(" [tag1] [tag:2] [tag-3] ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(3, tags.size());
|
|
|
|
|
EXPECT_EQ("tag1", tags[0]);
|
|
|
|
|
EXPECT_EQ("tag:2", tags[1]);
|
|
|
|
|
EXPECT_EQ("tag-3", tags[2]);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Cyclic_grammer_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
Definition PARENT;
|
|
|
|
|
Definition CHILD;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
PARENT <= seq(CHILD);
|
|
|
|
|
CHILD <= seq(PARENT);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Visit_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
Definition ROOT, TAG, TAG_NAME, WS;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
ROOT <= seq(WS, zom(TAG));
|
|
|
|
|
TAG <= seq(chr('['), TAG_NAME, chr(']'), WS);
|
|
|
|
|
TAG_NAME <= oom(seq(npd(chr(']')), dot()));
|
|
|
|
|
WS <= zom(cls(" \t"));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
AssignIDToDefinition defIds;
|
|
|
|
|
ROOT.accept(defIds);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(4, defIds.ids.size());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Token_check_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
EXPRESSION <- _ TERM (TERM_OPERATOR TERM)*
|
|
|
|
|
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
|
|
|
|
FACTOR <- NUMBER / '(' _ EXPRESSION ')' _
|
|
|
|
|
TERM_OPERATOR <- < [-+] > _
|
|
|
|
|
FACTOR_OPERATOR <- < [/*] > _
|
|
|
|
|
NUMBER <- < [0-9]+ > _
|
|
|
|
|
_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(parser["EXPRESSION"].is_token());
|
|
|
|
|
EXPECT_FALSE(parser["FACTOR"].is_token());
|
|
|
|
|
EXPECT_TRUE(parser["FACTOR_OPERATOR"].is_token());
|
|
|
|
|
EXPECT_TRUE(parser["NUMBER"].is_token());
|
|
|
|
|
EXPECT_TRUE(parser["_"].is_token());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Lambda_action_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- (CHAR)*
|
|
|
|
|
CHAR <- .
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::string ss;
|
|
|
|
|
parser["CHAR"] = [&](const SemanticValues &vs) { ss += *vs.sv().data(); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser.parse("hello");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ("hello", ss);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, enter_leave_handlers_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- LTOKEN '=' RTOKEN
|
|
|
|
|
LTOKEN <- TOKEN
|
|
|
|
|
RTOKEN <- TOKEN
|
|
|
|
|
TOKEN <- [A-Za-z]+
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["LTOKEN"].enter = [&](const char *, size_t, std::any &dt) {
|
|
|
|
|
auto &require_upper_case = *std::any_cast<bool *>(dt);
|
|
|
|
|
require_upper_case = false;
|
|
|
|
|
};
|
|
|
|
|
parser["LTOKEN"].leave = [&](const char *, size_t, size_t, std::any &,
|
|
|
|
|
std::any &dt) {
|
|
|
|
|
auto &require_upper_case = *std::any_cast<bool *>(dt);
|
|
|
|
|
require_upper_case = true;
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto message = "should be upper case string...";
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2022-06-30 23:58:23 +00:00
|
|
|
|
parser["TOKEN"].predicate = [&](const SemanticValues &vs, const std::any &dt,
|
|
|
|
|
std::string &msg) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto &require_upper_case = *std::any_cast<bool *>(dt);
|
|
|
|
|
if (require_upper_case) {
|
|
|
|
|
const auto &s = vs.sv();
|
|
|
|
|
if (!std::all_of(s.begin(), s.end(), ::isupper)) {
|
2022-06-30 23:58:23 +00:00
|
|
|
|
msg = message;
|
|
|
|
|
return false;
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-30 23:58:23 +00:00
|
|
|
|
return true;
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool require_upper_case = false;
|
|
|
|
|
std::any dt = &require_upper_case;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(parser.parse("hello=world", dt));
|
|
|
|
|
EXPECT_FALSE(parser.parse("HELLO=world", dt));
|
|
|
|
|
EXPECT_TRUE(parser.parse("hello=WORLD", dt));
|
|
|
|
|
EXPECT_TRUE(parser.parse("HELLO=WORLD", dt));
|
2020-10-02 01:26:04 +00:00
|
|
|
|
|
|
|
|
|
parser.log = [&](size_t ln, size_t col, const std::string &msg) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(1, ln);
|
|
|
|
|
EXPECT_EQ(7, col);
|
|
|
|
|
EXPECT_EQ(message, msg);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
|
|
|
|
parser.parse("hello=world", dt);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, WHITESPACE_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
# Rules
|
|
|
|
|
ROOT <- ITEM (',' ITEM)*
|
|
|
|
|
ITEM <- WORD / PHRASE
|
|
|
|
|
|
|
|
|
|
# Tokens
|
|
|
|
|
WORD <- < [a-zA-Z0-9_]+ >
|
|
|
|
|
PHRASE <- < '"' (!'"' .)* '"' >
|
|
|
|
|
|
|
|
|
|
%whitespace <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(R"( one, "two, three", four )");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, WHITESPACE_test2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
# Rules
|
|
|
|
|
ROOT <- ITEM (',' ITEM)*
|
|
|
|
|
ITEM <- '[' < [a-zA-Z0-9_]+ > ']'
|
|
|
|
|
|
|
|
|
|
%whitespace <- (SPACE / TAB)*
|
|
|
|
|
SPACE <- ' '
|
|
|
|
|
TAB <- '\t'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::vector<std::string_view> items;
|
|
|
|
|
parser["ITEM"] = [&](const SemanticValues &vs) {
|
|
|
|
|
items.push_back(vs.token());
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(R"([one], [two] ,[three] )");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(3, items.size());
|
|
|
|
|
EXPECT_EQ("one", items[0]);
|
|
|
|
|
EXPECT_EQ("two", items[1]);
|
|
|
|
|
EXPECT_EQ("three", items[2]);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, WHITESPACE_test3) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
StrQuot <- < '"' < (StrEscape / StrChars)* > '"' >
|
|
|
|
|
StrEscape <- '\\' any
|
|
|
|
|
StrChars <- (!'"' !'\\' any)+
|
|
|
|
|
any <- .
|
|
|
|
|
%whitespace <- [ \t]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["StrQuot"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(R"( aaa \" bbb )", vs.token());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(R"( " aaa \" bbb " )");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, WHITESPACE_test4) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- HELLO OPE WORLD
|
|
|
|
|
HELLO <- 'hello'
|
|
|
|
|
OPE <- < [-+] >
|
|
|
|
|
WORLD <- 'world' / 'WORLD'
|
|
|
|
|
%whitespace <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["HELLO"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ("hello", vs.token());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2022-05-28 01:50:27 +00:00
|
|
|
|
parser["OPE"] = [](const SemanticValues &vs) { EXPECT_EQ("+", vs.token()); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["WORLD"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ("world", vs.token());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(" hello + world ");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Word_expression_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- 'hello' ','? 'world'
|
|
|
|
|
%whitespace <- [ \t\r\n]*
|
|
|
|
|
%word <- [a-z]+
|
|
|
|
|
)");
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(parser.parse("helloworld"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("hello world"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("hello,world"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("hello, world"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("hello , world"));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Skip_token_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(" ROOT <- _ ITEM (',' _ ITEM _)* "
|
|
|
|
|
" ITEM <- ([a-z0-9])+ "
|
|
|
|
|
" ~_ <- [ \t]* ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2022-05-28 01:50:27 +00:00
|
|
|
|
parser["ROOT"] = [&](const SemanticValues &vs) { EXPECT_EQ(2, vs.size()); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(" item1, item2 ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Skip_token_test2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- ITEM (',' ITEM)*
|
|
|
|
|
ITEM <- < ([a-z0-9])+ >
|
|
|
|
|
%whitespace <- [ \t]*
|
|
|
|
|
)");
|
|
|
|
|
|
2022-05-28 01:50:27 +00:00
|
|
|
|
parser["ROOT"] = [&](const SemanticValues &vs) { EXPECT_EQ(2, vs.size()); };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(" item1, item2 ");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Custom_AST_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
struct CustomType {};
|
|
|
|
|
using CustomAst = AstBase<CustomType>;
|
|
|
|
|
|
|
|
|
|
parser parser(R"(
|
2020-06-18 14:17:27 +00:00
|
|
|
|
ROOT <- _ TEXT*
|
|
|
|
|
TEXT <- [a-zA-Z]+ _
|
|
|
|
|
_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast<CustomAst>();
|
|
|
|
|
std::shared_ptr<CustomAst> ast;
|
|
|
|
|
bool ret = parser.parse("a b c", ast);
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(4, ast->nodes.size());
|
2020-06-18 14:17:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Backtracking_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- PAT1 / PAT2
|
|
|
|
|
PAT1 <- HELLO ' One'
|
|
|
|
|
PAT2 <- HELLO ' Two'
|
|
|
|
|
HELLO <- 'Hello'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
size_t count = 0;
|
|
|
|
|
parser["HELLO"] = [&](const SemanticValues & /*vs*/) { count++; };
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_packrat_parsing();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser.parse("Hello Two");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(1, count); // Skip second time
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Backtracking_with_AST) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
S <- A? B (A B)* A
|
|
|
|
|
A <- 'a'
|
|
|
|
|
B <- 'b'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
bool ret = parser.parse("ba", ast);
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(2, ast->nodes.size());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Octal_Hex_Unicode_value_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"( ROOT <- '\132\x7a\u30f3' )");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse("Zzン");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-25 20:05:23 +00:00
|
|
|
|
TEST(GeneralTest, Ignore_case_literal_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2022-06-25 20:05:23 +00:00
|
|
|
|
ROOT <- HELLO WORLD
|
|
|
|
|
HELLO <- 'hello'i
|
|
|
|
|
WORLD <- 'world'i
|
|
|
|
|
%whitespace <- [ \t\r\n]*
|
|
|
|
|
)");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["HELLO"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ("Hello", vs.token());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["WORLD"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ("World", vs.token());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto ret = parser.parse(" Hello World ");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-25 20:05:23 +00:00
|
|
|
|
TEST(GeneralTest, Ignore_case_character_class_test) {
|
|
|
|
|
parser parser(R"(ROOT <- [a-z]i+)");
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(parser.parse("abc"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("ABC"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("Abc"));
|
|
|
|
|
EXPECT_TRUE(parser.parse("Abc"));
|
|
|
|
|
EXPECT_FALSE(parser.parse("123"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(GeneralTest, Ignore_case_negate_character_class_test) {
|
|
|
|
|
parser parser(R"(ROOT <- [^a-z]i+)");
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(parser.parse("123"));
|
|
|
|
|
EXPECT_FALSE(parser.parse("ABC"));
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, mutable_lambda_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::vector<std::string_view> vec;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser pg("ROOT <- 'mutable lambda test'");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// This test makes sure if the following code can be compiled.
|
|
|
|
|
pg["TOKEN"] = [=](const SemanticValues &vs) mutable {
|
|
|
|
|
vec.push_back(vs.sv());
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Simple_calculator_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
Additive <- Multitive '+' Additive / Multitive
|
|
|
|
|
Multitive <- Primary '*' Multitive / Primary
|
|
|
|
|
Primary <- '(' Additive ')' / Number
|
|
|
|
|
Number <- [0-9]+
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["Additive"] = [](const SemanticValues &vs) {
|
|
|
|
|
switch (vs.choice()) {
|
|
|
|
|
case 0: return std::any_cast<int>(vs[0]) + std::any_cast<int>(vs[1]);
|
|
|
|
|
default: return std::any_cast<int>(vs[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["Multitive"] = [](const SemanticValues &vs) {
|
|
|
|
|
switch (vs.choice()) {
|
|
|
|
|
case 0: return std::any_cast<int>(vs[0]) * std::any_cast<int>(vs[1]);
|
|
|
|
|
default: return std::any_cast<int>(vs[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
parser["Number"] = [](const SemanticValues &vs) {
|
|
|
|
|
return vs.token_to_number<int>();
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
int val;
|
|
|
|
|
parser.parse("(1+2)*3", val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(9, val);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Calculator_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Construct grammer
|
|
|
|
|
Definition EXPRESSION, TERM, FACTOR, TERM_OPERATOR, FACTOR_OPERATOR, NUMBER;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
EXPRESSION <= seq(TERM, zom(seq(TERM_OPERATOR, TERM)));
|
|
|
|
|
TERM <= seq(FACTOR, zom(seq(FACTOR_OPERATOR, FACTOR)));
|
|
|
|
|
FACTOR <= cho(NUMBER, seq(chr('('), EXPRESSION, chr(')')));
|
|
|
|
|
TERM_OPERATOR <= cls("+-");
|
|
|
|
|
FACTOR_OPERATOR <= cls("*/");
|
|
|
|
|
NUMBER <= oom(cls("0-9"));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Setup actions
|
|
|
|
|
auto reduce = [](const SemanticValues &vs) -> long {
|
|
|
|
|
long ret = std::any_cast<long>(vs[0]);
|
|
|
|
|
for (auto i = 1u; i < vs.size(); i += 2) {
|
|
|
|
|
auto num = std::any_cast<long>(vs[i + 1]);
|
|
|
|
|
switch (std::any_cast<char>(vs[i])) {
|
|
|
|
|
case '+': ret += num; break;
|
|
|
|
|
case '-': ret -= num; break;
|
|
|
|
|
case '*': ret *= num; break;
|
|
|
|
|
case '/': ret /= num; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EXPRESSION = reduce;
|
|
|
|
|
TERM = reduce;
|
|
|
|
|
TERM_OPERATOR = [](const SemanticValues &vs) { return *vs.sv().data(); };
|
|
|
|
|
FACTOR_OPERATOR = [](const SemanticValues &vs) { return *vs.sv().data(); };
|
|
|
|
|
NUMBER = [](const SemanticValues &vs) { return vs.token_to_number<long>(); };
|
|
|
|
|
|
|
|
|
|
// Parse
|
|
|
|
|
long val;
|
|
|
|
|
auto r = EXPRESSION.parse_and_get_value("1+2*3*(4-5+6)/7-8", val);
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(r.ret);
|
|
|
|
|
EXPECT_EQ(-3, val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Calculator_test2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Parse syntax
|
|
|
|
|
auto syntax = R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
# Grammar for Calculator...
|
|
|
|
|
EXPRESSION <- TERM (TERM_OPERATOR TERM)*
|
|
|
|
|
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
|
|
|
|
FACTOR <- NUMBER / '(' EXPRESSION ')'
|
|
|
|
|
TERM_OPERATOR <- [-+]
|
|
|
|
|
FACTOR_OPERATOR <- [/*]
|
|
|
|
|
NUMBER <- [0-9]+
|
|
|
|
|
)";
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::string start;
|
2021-03-17 13:33:40 +00:00
|
|
|
|
bool enablePackratParsing = false;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
auto grammar = ParserGenerator::parse(syntax, strlen(syntax), start,
|
|
|
|
|
enablePackratParsing, nullptr);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto &g = *grammar;
|
|
|
|
|
|
|
|
|
|
// Setup actions
|
|
|
|
|
auto reduce = [](const SemanticValues &vs) -> long {
|
|
|
|
|
long ret = std::any_cast<long>(vs[0]);
|
|
|
|
|
for (auto i = 1u; i < vs.size(); i += 2) {
|
|
|
|
|
auto num = std::any_cast<long>(vs[i + 1]);
|
|
|
|
|
switch (std::any_cast<char>(vs[i])) {
|
|
|
|
|
case '+': ret += num; break;
|
|
|
|
|
case '-': ret -= num; break;
|
|
|
|
|
case '*': ret *= num; break;
|
|
|
|
|
case '/': ret /= num; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
g["EXPRESSION"] = reduce;
|
|
|
|
|
g["TERM"] = reduce;
|
|
|
|
|
g["TERM_OPERATOR"] = [](const SemanticValues &vs) { return *vs.sv().data(); };
|
2021-08-19 06:29:11 +00:00
|
|
|
|
g["FACTOR_OPERATOR"] = [](const SemanticValues &vs) {
|
|
|
|
|
return *vs.sv().data();
|
|
|
|
|
};
|
|
|
|
|
g["NUMBER"] = [](const SemanticValues &vs) {
|
|
|
|
|
return vs.token_to_number<long>();
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Parse
|
|
|
|
|
long val;
|
|
|
|
|
auto r = g[start].parse_and_get_value("1+2*3*(4-5+6)/7-8", val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(r.ret);
|
|
|
|
|
EXPECT_EQ(-3, val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Calculator_test3) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Parse syntax
|
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
# Grammar for Calculator...
|
|
|
|
|
EXPRESSION <- TERM (TERM_OPERATOR TERM)*
|
|
|
|
|
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
|
|
|
|
FACTOR <- NUMBER / '(' EXPRESSION ')'
|
|
|
|
|
TERM_OPERATOR <- [-+]
|
|
|
|
|
FACTOR_OPERATOR <- [/*]
|
|
|
|
|
NUMBER <- [0-9]+
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto reduce = [](const SemanticValues &vs) -> long {
|
|
|
|
|
long ret = std::any_cast<long>(vs[0]);
|
|
|
|
|
for (auto i = 1u; i < vs.size(); i += 2) {
|
|
|
|
|
auto num = std::any_cast<long>(vs[i + 1]);
|
|
|
|
|
switch (std::any_cast<char>(vs[i])) {
|
|
|
|
|
case '+': ret += num; break;
|
|
|
|
|
case '-': ret -= num; break;
|
|
|
|
|
case '*': ret *= num; break;
|
|
|
|
|
case '/': ret /= num; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Setup actions
|
|
|
|
|
parser["EXPRESSION"] = reduce;
|
|
|
|
|
parser["TERM"] = reduce;
|
|
|
|
|
parser["TERM_OPERATOR"] = [](const SemanticValues &vs) {
|
|
|
|
|
return static_cast<char>(*vs.sv().data());
|
|
|
|
|
};
|
|
|
|
|
parser["FACTOR_OPERATOR"] = [](const SemanticValues &vs) {
|
|
|
|
|
return static_cast<char>(*vs.sv().data());
|
|
|
|
|
};
|
2021-08-19 06:29:11 +00:00
|
|
|
|
parser["NUMBER"] = [](const SemanticValues &vs) {
|
|
|
|
|
return vs.token_to_number<long>();
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
// Parse
|
|
|
|
|
long val;
|
|
|
|
|
auto ret = parser.parse("1+2*3*(4-5+6)/7-8", val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(-3, val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Calculator_test_with_AST) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
EXPRESSION <- _ TERM (TERM_OPERATOR TERM)*
|
|
|
|
|
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
|
|
|
|
FACTOR <- NUMBER / '(' _ EXPRESSION ')' _
|
|
|
|
|
TERM_OPERATOR <- < [-+] > _
|
|
|
|
|
FACTOR_OPERATOR <- < [/*] > _
|
|
|
|
|
NUMBER <- < [0-9]+ > _
|
|
|
|
|
~_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
|
|
|
|
|
|
|
|
|
std::function<long(const Ast &)> eval = [&](const Ast &ast) {
|
|
|
|
|
if (ast.name == "NUMBER") {
|
|
|
|
|
return ast.token_to_number<long>();
|
|
|
|
|
} else {
|
|
|
|
|
const auto &nodes = ast.nodes;
|
|
|
|
|
auto result = eval(*nodes[0]);
|
|
|
|
|
for (auto i = 1u; i < nodes.size(); i += 2) {
|
|
|
|
|
auto num = eval(*nodes[i + 1]);
|
|
|
|
|
auto ope = nodes[i]->token[0];
|
|
|
|
|
switch (ope) {
|
|
|
|
|
case '+': result += num; break;
|
|
|
|
|
case '-': result -= num; break;
|
|
|
|
|
case '*': result *= num; break;
|
|
|
|
|
case '/': result /= num; break;
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse("1+2*3*(4-5+6)/7-8", ast);
|
2021-01-22 01:56:05 +00:00
|
|
|
|
ast = parser.optimize_ast(ast);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
auto val = eval(*ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(-3, val);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Calculator_test_with_combinators_and_AST) {
|
2020-06-08 00:27:19 +00:00
|
|
|
|
// Construct grammer
|
2020-10-02 01:26:04 +00:00
|
|
|
|
AST_DEFINITIONS(EXPRESSION, TERM, FACTOR, TERM_OPERATOR, FACTOR_OPERATOR,
|
|
|
|
|
NUMBER);
|
2020-06-08 00:27:19 +00:00
|
|
|
|
|
|
|
|
|
EXPRESSION <= seq(TERM, zom(seq(TERM_OPERATOR, TERM)));
|
|
|
|
|
TERM <= seq(FACTOR, zom(seq(FACTOR_OPERATOR, FACTOR)));
|
|
|
|
|
FACTOR <= cho(NUMBER, seq(chr('('), EXPRESSION, chr(')')));
|
|
|
|
|
TERM_OPERATOR <= cls("+-");
|
|
|
|
|
FACTOR_OPERATOR <= cls("*/");
|
|
|
|
|
NUMBER <= oom(cls("0-9"));
|
|
|
|
|
|
|
|
|
|
std::function<long(const Ast &)> eval = [&](const Ast &ast) {
|
|
|
|
|
if (ast.name == "NUMBER") {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
return ast.token_to_number<long>();
|
2020-06-08 00:27:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
const auto &nodes = ast.nodes;
|
|
|
|
|
auto result = eval(*nodes[0]);
|
|
|
|
|
for (auto i = 1u; i < nodes.size(); i += 2) {
|
|
|
|
|
auto num = eval(*nodes[i + 1]);
|
|
|
|
|
auto ope = nodes[i]->token[0];
|
|
|
|
|
switch (ope) {
|
|
|
|
|
case '+': result += num; break;
|
|
|
|
|
case '-': result -= num; break;
|
|
|
|
|
case '*': result *= num; break;
|
|
|
|
|
case '/': result /= num; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto r = EXPRESSION.parse_and_get_value("1+2*3*(4-5+6)/7-8", ast);
|
|
|
|
|
ast = AstOptimizer(true).optimize(ast);
|
|
|
|
|
auto val = eval(*ast);
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(r.ret);
|
|
|
|
|
EXPECT_EQ(-3, val);
|
2020-06-08 00:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Ignore_semantic_value_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- ~HELLO WORLD
|
|
|
|
|
HELLO <- 'Hello' _
|
|
|
|
|
WORLD <- 'World' _
|
|
|
|
|
_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse("Hello World", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(1, ast->nodes.size());
|
|
|
|
|
EXPECT_EQ("WORLD", ast->nodes[0]->name);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Ignore_semantic_value_of_or_predicate_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- _ !DUMMY HELLO_WORLD '.'
|
|
|
|
|
HELLO_WORLD <- HELLO 'World' _
|
|
|
|
|
HELLO <- 'Hello' _
|
|
|
|
|
DUMMY <- 'dummy' _
|
|
|
|
|
~_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse("Hello World.", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(1, ast->nodes.size());
|
|
|
|
|
EXPECT_EQ("HELLO_WORLD", ast->nodes[0]->name);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Ignore_semantic_value_of_and_predicate_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
START <- _ &HELLO HELLO_WORLD '.'
|
|
|
|
|
HELLO_WORLD <- HELLO 'World' _
|
|
|
|
|
HELLO <- 'Hello' _
|
|
|
|
|
~_ <- [ \t\r\n]*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse("Hello World.", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_EQ(1, ast->nodes.size());
|
|
|
|
|
EXPECT_EQ("HELLO_WORLD", ast->nodes[0]->name);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Literal_token_on_AST_test1) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
STRING_LITERAL <- '"' (('\\"' / '\\t' / '\\n') / (!["] .))* '"'
|
|
|
|
|
)");
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse(R"("a\tb")", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_TRUE(ast->is_token);
|
|
|
|
|
EXPECT_EQ(R"("a\tb")", ast->token);
|
|
|
|
|
EXPECT_TRUE(ast->nodes.empty());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Literal_token_on_AST_test2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
STRING_LITERAL <- '"' (ESC / CHAR)* '"'
|
|
|
|
|
ESC <- ('\\"' / '\\t' / '\\n')
|
|
|
|
|
CHAR <- (!["] .)
|
|
|
|
|
)");
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse(R"("a\tb")", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_FALSE(ast->is_token);
|
|
|
|
|
EXPECT_TRUE(ast->token.empty());
|
|
|
|
|
EXPECT_EQ(3, ast->nodes.size());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Literal_token_on_AST_test3) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
STRING_LITERAL <- < '"' (ESC / CHAR)* '"' >
|
|
|
|
|
ESC <- ('\\"' / '\\t' / '\\n')
|
|
|
|
|
CHAR <- (!["] .)
|
|
|
|
|
)");
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.enable_ast();
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse(R"("a\tb")", ast);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_TRUE(ast->is_token);
|
|
|
|
|
EXPECT_EQ(R"("a\tb")", ast->token);
|
|
|
|
|
EXPECT_TRUE(ast->nodes.empty());
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Missing_missing_definitions_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
A <- B C
|
|
|
|
|
)");
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(parser);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Definition_duplicates_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
A <- ''
|
|
|
|
|
A <- ''
|
|
|
|
|
)");
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_FALSE(parser);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Semantic_values_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
term <- ( a b c x )? a b c
|
|
|
|
|
a <- 'a'
|
|
|
|
|
b <- 'b'
|
|
|
|
|
c <- 'c'
|
|
|
|
|
x <- 'x'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
for (const auto &rule : parser.get_rule_names()) {
|
|
|
|
|
parser[rule.data()] = [rule](const SemanticValues &vs, std::any &) {
|
|
|
|
|
if (rule == "term") {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ("a at 0", std::any_cast<std::string>(vs[0]));
|
|
|
|
|
EXPECT_EQ("b at 1", std::any_cast<std::string>(vs[1]));
|
|
|
|
|
EXPECT_EQ("c at 2", std::any_cast<std::string>(vs[2]));
|
2020-10-02 01:26:04 +00:00
|
|
|
|
return std::string();
|
|
|
|
|
} else {
|
|
|
|
|
return rule + " at " + std::to_string(vs.sv().data() - vs.ss);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(parser.parse("abc"));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Ordered_choice_count) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
S <- 'a' / 'b'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["S"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(1, vs.choice());
|
|
|
|
|
EXPECT_EQ(2, vs.choice_count());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.parse("b");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Ordered_choice_count_2) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
S <- ('a' / 'b')*
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser["S"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(0, vs.choice());
|
|
|
|
|
EXPECT_EQ(0, vs.choice_count());
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser.parse("b");
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Semantic_value_tag) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
S <- A? B* C?
|
|
|
|
|
A <- 'a'
|
|
|
|
|
B <- 'b'
|
|
|
|
|
C <- 'c'
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
{
|
|
|
|
|
using namespace udl;
|
|
|
|
|
parser["S"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(1, vs.size());
|
|
|
|
|
EXPECT_EQ(1, vs.tags.size());
|
|
|
|
|
EXPECT_EQ("C"_, vs.tags[0]);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
|
|
|
|
auto ret = parser.parse("c");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
using namespace udl;
|
|
|
|
|
parser["S"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(2, vs.size());
|
|
|
|
|
EXPECT_EQ(2, vs.tags.size());
|
|
|
|
|
EXPECT_EQ("B"_, vs.tags[0]);
|
|
|
|
|
EXPECT_EQ("B"_, vs.tags[1]);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
|
|
|
|
auto ret = parser.parse("bb");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
using namespace udl;
|
|
|
|
|
parser["S"] = [](const SemanticValues &vs) {
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_EQ(2, vs.size());
|
|
|
|
|
EXPECT_EQ(2, vs.tags.size());
|
|
|
|
|
EXPECT_EQ("A"_, vs.tags[0]);
|
|
|
|
|
EXPECT_EQ("C"_, vs.tags[1]);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
};
|
|
|
|
|
auto ret = parser.parse("ac");
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-10-02 01:26:04 +00:00
|
|
|
|
}
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, Negated_Class_test) {
|
2020-10-02 01:26:04 +00:00
|
|
|
|
parser parser(R"(
|
2020-02-07 18:28:01 +00:00
|
|
|
|
ROOT <- [^a-z_]+
|
|
|
|
|
)");
|
|
|
|
|
|
2020-10-02 01:26:04 +00:00
|
|
|
|
bool ret = parser;
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2020-02-07 18:28:01 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(parser.parse("ABC123"));
|
|
|
|
|
EXPECT_FALSE(parser.parse("ABcZ"));
|
|
|
|
|
EXPECT_FALSE(parser.parse("ABCZ_"));
|
|
|
|
|
EXPECT_FALSE(parser.parse(""));
|
2020-02-07 18:28:01 +00:00
|
|
|
|
}
|
2021-01-20 21:29:28 +00:00
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
TEST(GeneralTest, token_to_number_float_test) {
|
2021-01-20 21:29:28 +00:00
|
|
|
|
parser parser(R"(
|
|
|
|
|
S <- '1.1'
|
|
|
|
|
)");
|
|
|
|
|
parser.enable_ast();
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Ast> ast;
|
|
|
|
|
auto ret = parser.parse("1.1", ast);
|
|
|
|
|
|
2021-08-19 06:29:11 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
EXPECT_TRUE(ast->is_token);
|
|
|
|
|
EXPECT_EQ("1.1", ast->token);
|
|
|
|
|
EXPECT_EQ(1.1f, ast->token_to_number<float>());
|
|
|
|
|
EXPECT_TRUE(ast->nodes.empty());
|
2021-01-20 21:29:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 16:23:41 +00:00
|
|
|
|
TEST(GeneralTest, ParentReferencesShouldNotBeExpired) {
|
2022-05-28 01:50:27 +00:00
|
|
|
|
auto parser = peg::parser(R"(
|
2022-01-03 16:23:41 +00:00
|
|
|
|
ROOT <- OPTIMIZES_AWAY
|
|
|
|
|
OPTIMIZES_AWAY <- ITEM+
|
|
|
|
|
ITEM <- 'a'
|
|
|
|
|
)");
|
2022-05-28 01:50:27 +00:00
|
|
|
|
parser.enable_ast<peg::Ast>();
|
2022-01-03 16:23:41 +00:00
|
|
|
|
|
2022-05-28 01:50:27 +00:00
|
|
|
|
std::shared_ptr<peg::Ast> ast;
|
|
|
|
|
parser.parse("aaa", ast);
|
|
|
|
|
ast = parser.optimize_ast(ast);
|
2022-01-03 16:23:41 +00:00
|
|
|
|
|
2022-05-28 01:50:27 +00:00
|
|
|
|
EXPECT_FALSE(ast->nodes[0]->parent.expired());
|
2022-01-03 16:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 14:25:07 +00:00
|
|
|
|
TEST(GeneralTest, EndOfInputTest) {
|
2022-06-08 15:10:59 +00:00
|
|
|
|
auto parser = peg::parser(R"(
|
|
|
|
|
S <- '[[' (!']]' .)* ']]' !.
|
|
|
|
|
)");
|
|
|
|
|
|
|
|
|
|
parser.disable_eoi_check();
|
|
|
|
|
|
|
|
|
|
auto ret = parser.parse("[[]]]");
|
|
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(GeneralTest, DefaultEndOfInputTest) {
|
2022-06-08 14:25:07 +00:00
|
|
|
|
auto parser = peg::parser(R"(
|
|
|
|
|
S <- '[[' (!']]' .)* ']]'
|
|
|
|
|
)");
|
|
|
|
|
|
|
|
|
|
auto ret = parser.parse("[[]]]");
|
|
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(GeneralTest, DisableEndOfInputCheckTest) {
|
|
|
|
|
auto parser = peg::parser(R"(
|
2022-06-08 15:10:59 +00:00
|
|
|
|
S <- '[[' (!']]' .)* ']]'
|
2022-06-08 14:25:07 +00:00
|
|
|
|
)");
|
|
|
|
|
|
2022-06-08 15:10:59 +00:00
|
|
|
|
parser.disable_eoi_check();
|
|
|
|
|
|
2022-06-08 14:25:07 +00:00
|
|
|
|
auto ret = parser.parse("[[]]]");
|
2022-06-08 15:10:59 +00:00
|
|
|
|
EXPECT_TRUE(ret);
|
2022-06-08 14:25:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-31 12:17:15 +00:00
|
|
|
|
TEST(GeneralTest, InvalidCutOperator) {
|
|
|
|
|
auto parser = peg::parser(R"(
|
|
|
|
|
S <- 'a' ↑ 'b'
|
|
|
|
|
)");
|
|
|
|
|
|
|
|
|
|
auto ret = parser.parse("ab");
|
|
|
|
|
EXPECT_TRUE(ret);
|
|
|
|
|
|
|
|
|
|
ret = parser.parse("ac");
|
|
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
|
|
|
|
|
|
ret = parser.parse("b");
|
|
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
|
}
|
2022-07-31 19:39:38 +00:00
|
|
|
|
|
|
|
|
|
TEST(GeneralTest, HeuristicErrorTokenTest) {
|
|
|
|
|
auto parser = peg::parser(R"(
|
|
|
|
|
program <- enum+
|
|
|
|
|
enum <- 'enum' enum_kind^untyped_enum
|
|
|
|
|
enum_kind <- 'sequence' / 'bitmask'
|
|
|
|
|
|
|
|
|
|
%whitespace <- [ \r\t\n]*
|
|
|
|
|
%word <- [a-zA-Z0-9_]
|
|
|
|
|
|
|
|
|
|
untyped_enum <- '' { message "invalid/missing enum type, expected one of 'sequence' or 'bitmask', got '%t'"}
|
|
|
|
|
)");
|
|
|
|
|
|
|
|
|
|
parser.log = [&](size_t ln, size_t col, const std::string &msg) {
|
|
|
|
|
EXPECT_EQ(1, ln);
|
|
|
|
|
EXPECT_EQ(6, col);
|
|
|
|
|
EXPECT_EQ("invalid/missing enum type, expected one of 'sequence' or "
|
|
|
|
|
"'bitmask', got 'sequencer'",
|
|
|
|
|
msg);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto ret = parser.parse("enum sequencer");
|
|
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
|
}
|