Skip to content

Commit

Permalink
Fix #128
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Nov 9, 2020
1 parent 11ed83e commit b92da07
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/native.js

Large diffs are not rendered by default.

Binary file modified docs/native.wasm
Binary file not shown.
24 changes: 15 additions & 9 deletions peglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ class Context {
std::vector<Definition *> rule_stack;
std::vector<std::vector<std::shared_ptr<Ope>>> args_stack;

bool in_token = false;
size_t in_token_boundary_count = 0;

std::shared_ptr<Ope> whitespaceOpe;
bool in_whitespace = false;
Expand Down Expand Up @@ -2392,7 +2392,7 @@ inline size_t parse_literal(const char *s, size_t n, SemanticValues &sv,
}

// Skip whiltespace
if (!c.in_token) {
if (!c.in_token_boundary_count) {
if (c.whitespaceOpe) {
auto len = c.whitespaceOpe->parse(s + i, n - i, sv, c, dt);
if (fail(len)) { return static_cast<size_t>(-1); }
Expand Down Expand Up @@ -2457,16 +2457,22 @@ inline size_t LiteralString::parse_core(const char *s, size_t n,
inline size_t TokenBoundary::parse_core(const char *s, size_t n,
SemanticValues &sv, Context &c,
any &dt) const {
c.in_token = true;
auto se = make_scope_exit([&]() { c.in_token = false; });
auto len = ope_->parse(s, n, sv, c, dt);
size_t len;
{
c.in_token_boundary_count++;
auto se = make_scope_exit([&]() { c.in_token_boundary_count--; });
len = ope_->parse(s, n, sv, c, dt);
}

if (success(len)) {
sv.tokens.emplace_back(std::make_pair(s, len));

if (c.whitespaceOpe) {
auto l = c.whitespaceOpe->parse(s + len, n - len, sv, c, dt);
if (fail(l)) { return static_cast<size_t>(-1); }
len += l;
if (!c.in_token_boundary_count) {
if (c.whitespaceOpe) {
auto l = c.whitespaceOpe->parse(s + len, n - len, sv, c, dt);
if (fail(l)) { return static_cast<size_t>(-1); }
len += l;
}
}
}
return len;
Expand Down
87 changes: 87 additions & 0 deletions test/test2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,93 @@

using namespace peg;

TEST_CASE("Token boundary 1", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- 'a' 'b' 'c'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(pg.parse(" a b c "));
}

TEST_CASE("Token boundary 2", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- < 'a' 'b' 'c' >
%whitespace <- [ \t\r\n]*
)");

REQUIRE(!pg.parse(" a b c "));
}

TEST_CASE("Token boundary 3", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- < 'a' B 'c' >
B <- 'b'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(!pg.parse(" a b c "));
}

TEST_CASE("Token boundary 4", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- < A 'b' 'c' >
A <- 'a'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(!pg.parse(" a b c "));
}

TEST_CASE("Token boundary 5", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- A < 'b' C >
A <- 'a'
C <- 'c'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(!pg.parse(" a b c "));
}

TEST_CASE("Token boundary 6", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- < A > B C
A <- 'a'
B <- 'b'
C <- 'c'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(pg.parse(" a b c "));
}

TEST_CASE("Token boundary 7", "[token boundary]")
{
parser pg(R"(
ROOT <- TOP
TOP <- < A B C >
A <- 'a'
B <- 'b'
C <- 'c'
%whitespace <- [ \t\r\n]*
)");

REQUIRE(!pg.parse(" a b c "));
}

TEST_CASE("Infinite loop 1", "[infinite loop]")
{
parser pg(R"(
Expand Down

0 comments on commit b92da07

Please sign in to comment.