Skip to content

Commit

Permalink
Merge pull request #221 from veelo/tracer
Browse files Browse the repository at this point in the history
Improvements to the tracer.
  • Loading branch information
PhilippeSigaud authored Feb 26, 2017
2 parents 8139a50 + 3caaf02 commit 25a8dd8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
7 changes: 4 additions & 3 deletions pegged/examples/extended_pascal/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ int main(string[] args)
import std.experimental.logger;
import std.algorithm : startsWith;
sharedLog = new TraceLogger("TraceLog.txt");
bool cond (string ruleName)
bool cond (string ruleName, const ref ParseTree p)
{
static startTrace = false;
if (ruleName.startsWith("EP.FunctionDeclaration"))
//if (ruleName.startsWith("EP.FunctionDeclaration"))
if (p.begin > 4000)
startTrace = true;
return startTrace && ruleName.startsWith("EP");
}
// Various ways of turning logging on and of:
//setTraceConditionFunction(&cond);
setTraceConditionFunction(ruleName => ruleName.startsWith("EP"));
setTraceConditionFunction(function(string ruleName, const ref ParseTree p) {return ruleName.startsWith("EP");});
//traceAll;
}

Expand Down
52 changes: 26 additions & 26 deletions pegged/peg.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ version (tracer)
import std.algorithm.comparison : min;

// Function pointers.
private static bool function(string ruleName) traceConditionFunction;
private static bool delegate(string ruleName) traceConditionDelegate;
private static bool function(string ruleName, const ref ParseTree p) traceConditionFunction;
private static bool delegate(string ruleName, const ref ParseTree p) traceConditionDelegate;
private static int traceLevel;
private static bool traceBlocked;
private static bool logTraceLevel = false;
Expand All @@ -55,14 +55,14 @@ version (tracer)
traceLevel--;
}

private bool shouldTrace(string ruleName)
private bool shouldTrace(string ruleName, const ref ParseTree p)
{
if (__ctfe || traceBlocked)
return false;
if (traceConditionDelegate != null)
return traceConditionDelegate(ruleName);
return traceConditionDelegate(ruleName, p);
if (traceConditionFunction != null)
return traceConditionFunction(ruleName);
return traceConditionFunction(ruleName, p);
return false;
}

Expand All @@ -81,14 +81,14 @@ version (tracer)
+ setTraceConditionFunction(ruleName => ruleName.startsWith("MyGrammar"));
+ ---
+/
void setTraceConditionFunction(bool delegate(string ruleName) condition)
void setTraceConditionFunction(bool delegate(string ruleName, const ref ParseTree p) condition)
{
traceConditionDelegate = condition;
traceConditionFunction = null;
}

/// ditto
void setTraceConditionFunction(bool function(string ruleName) condition)
void setTraceConditionFunction(bool function(string ruleName, const ref ParseTree p) condition)
{
traceConditionFunction = condition;
traceConditionDelegate = null;
Expand All @@ -100,7 +100,7 @@ version (tracer)
*/
void traceAll()
{
setTraceConditionFunction(string => true);
setTraceConditionFunction(function(string ruleName, const ref ParseTree p) {return true;});
}

/** Do not trace any rules. */
Expand All @@ -118,7 +118,7 @@ version (tracer)
string result;
for (auto i = 1; i <= traceLevel; i++)
result ~= format("%d|", i);
result ~= format(" (l:%d, c:%d)\t", pos.line, pos.col) ~
result ~= format(" (l:%d, c:%d, i:%d)\t", pos.line + 1, pos.col + 1, pos.index) ~
expression.stringified ~ " considering rule " ~ name.stringified ~ " on " ~
p.input[p.end .. min(p.input.length, p.end + inputLength)].stringified ~
(p.end + inputLength > p.input.length ? "" : "...");
Expand All @@ -140,11 +140,11 @@ version (tracer)
string consumed;
foreach (match; p.matches)
consumed ~= match;
result ~= format(" (l:%d, c:%d)\t", pos.line, pos.col) ~ name.stringified ~ " SUCCEEDED on " ~
result ~= format(" (l:%d, c:%d, i:%d)\t", pos.line + 1, pos.col + 1, pos.index) ~ name.stringified ~ " SUCCEEDED on " ~
consumed.stringified;
}
else
result ~= format(" (l:%d, c:%d)\t", pos.line, pos.col) ~ name.stringified ~ " FAILED on " ~
result ~= format(" (l:%d, c:%d, i:%d)\t", pos.line + 1, pos.col + 1, pos.index) ~ name.stringified ~ " FAILED on " ~
p.input[p.end .. min(p.input.length, p.end + inputLength)].stringified ~
(p.end + inputLength > p.input.length ? "" : "...");
return result;
Expand Down Expand Up @@ -1214,7 +1214,7 @@ template and(rules...) if (rules.length > 0)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(result, name, getName!(r)()));
}
ParseTree temp = r(result);
Expand All @@ -1239,7 +1239,7 @@ template and(rules...) if (rules.length > 0)
result.matches ~= temp.matches[$-1];
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceResultMsg(result, getName!(r)()));
decTraceLevel();
}
Expand All @@ -1250,7 +1250,7 @@ template and(rules...) if (rules.length > 0)
version (tracer)
{
foreach(i, r; rules)
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
{
trace(traceResultMsg(result, name));
break;
Expand Down Expand Up @@ -1454,7 +1454,7 @@ template or(rules...) if (rules.length > 0)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(p, name, getName!(r)()));
}
ParseTree temp = r(p);
Expand All @@ -1464,7 +1464,7 @@ template or(rules...) if (rules.length > 0)
temp.name = name;
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceResultMsg(temp, getName!(r)()));
decTraceLevel();
}
Expand All @@ -1474,7 +1474,7 @@ template or(rules...) if (rules.length > 0)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceResultMsg(temp, getName!(r)()));
}
enum errName = " (" ~ getName!(r)() ~")";
Expand Down Expand Up @@ -1891,7 +1891,7 @@ template zeroOrMore(alias r)
version (tracer)
{
incTraceLevel();
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(result, name, getName!(r)()));
}
auto temp = r(result);
Expand All @@ -1904,15 +1904,15 @@ template zeroOrMore(alias r)
result.end = temp.end;
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(result, name, getName!(r)()));
}
temp = r(result);
}
result.successful = true;
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceResultMsg(result, getName!(r)()));
decTraceLevel();
}
Expand Down Expand Up @@ -2044,7 +2044,7 @@ template oneOrMore(alias r)
version (tracer)
{
incTraceLevel();
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(result, name, getName!(r)()));
}
auto temp = r(result);
Expand All @@ -2066,7 +2066,7 @@ template oneOrMore(alias r)
result.end = temp.end;
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(result, name, getName!(r)()));
}
temp = r(result);
Expand All @@ -2075,7 +2075,7 @@ template oneOrMore(alias r)
}
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceResultMsg(result, getName!(r)()));
decTraceLevel();
}
Expand Down Expand Up @@ -2182,7 +2182,7 @@ template option(alias r)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(p, name, getName!(r)()));
}
ParseTree result = r(p);
Expand Down Expand Up @@ -2276,7 +2276,7 @@ template posLookahead(alias r)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(p, name, getName!(r)()));
}
ParseTree temp = r(p);
Expand Down Expand Up @@ -2367,7 +2367,7 @@ template negLookahead(alias r)
{
version (tracer)
{
if (shouldTrace(getName!(r)()))
if (shouldTrace(getName!(r)(), p))
trace(traceMsg(p, name, getName!(r)()));
}
ParseTree temp = r(p);
Expand Down

0 comments on commit 25a8dd8

Please sign in to comment.