Skip to content

Commit

Permalink
Fix unicode bug and fix decimateTree
Browse files Browse the repository at this point in the history
- wanted to iterate byGrapheme, but that won't work in ctfe
- decimateTree should keep nodes with no matches when the root
parsetree has failed (there may be failedChild elements in there
useful for error reporting)
  • Loading branch information
skoppe committed Feb 5, 2020
1 parent 37469f6 commit 9add4b5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pegged/peg.d
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,16 @@ template literal(string s)
ParseTree literal(ParseTree p)
{
enum lit = "\"" ~ s ~ "\"";
import std.algorithm : commonPrefix;
auto prefix = p.input[p.end..$].commonPrefix(s);
if (prefix.length == s.length)
if (p.end+s.length <= p.input.length && p.input[p.end..p.end+s.length] == s)
return ParseTree(name, true, [s], p.input, p.end, p.end+s.length);
else
else {
import std.algorithm : commonPrefix;
import std.utf : byCodeUnit;
auto prefix = p.input[p.end..$].byCodeUnit.commonPrefix(s.byCodeUnit);
return ParseTree(name, false, [lit], p.input, p.end, p.end, null, p.end + prefix.length);
}
}
}
ParseTree literal(string input)
{
Expand Down Expand Up @@ -3596,14 +3598,16 @@ mixin template decimateTree()
{
if(p.children.length == 0) return p;
bool parseFailed = !p.successful;
ParseTree[] filterChildren(ParseTree pt)
{
ParseTree[] result;
foreach(child; pt.children)
{
import std.algorithm : startsWith;
if ( (isRule(child.name))
if ( (isRule(child.name) && (child.matches.length != 0 || parseFailed))
|| (!child.successful && child.children.length == 0)
|| (!child.successful && child.name.startsWith("or!") && child.children.length > 1)
|| (!pt.successful && child.successful && child.children.length == 0 && child.failedChild.length > 0))
Expand All @@ -3630,7 +3634,7 @@ mixin template decimateTree()
filterFailedChildren(child);
import std.algorithm : startsWith;
if ( (isRule(child.name))
if ( (isRule(child.name) && (child.matches.length != 0 || parseFailed))
|| (!child.successful && child.children.length == 0)
|| (!child.successful && child.name.startsWith("or!") && child.children.length > 1)
|| (!pt.successful && child.successful && child.children.length == 0 && child.failedChild.length > 0))
Expand Down

0 comments on commit 9add4b5

Please sign in to comment.