From 8007188dc94eb6d8de7619d5fc73a5fbff0dca09 Mon Sep 17 00:00:00 2001 From: Bastiaan Veelo Date: Sat, 25 Feb 2017 23:47:11 +0100 Subject: [PATCH 1/2] Follow editor conventions: start lines and columns at 1. Include the index in trace messages. --- pegged/peg.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pegged/peg.d b/pegged/peg.d index 6f9259a6..1704c4a0 100644 --- a/pegged/peg.d +++ b/pegged/peg.d @@ -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 ? "" : "..."); @@ -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; From 3caaf0228b62337ff861d2f1b66330b1efc0cdaa Mon Sep 17 00:00:00 2001 From: Bastiaan Veelo Date: Sun, 26 Feb 2017 00:05:36 +0100 Subject: [PATCH 2/2] Including the parse tree in the trace condition allows for switching on the tracer from a certain position in the input onward. --- pegged/examples/extended_pascal/source/app.d | 7 +-- pegged/peg.d | 46 ++++++++++---------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pegged/examples/extended_pascal/source/app.d b/pegged/examples/extended_pascal/source/app.d index 1ba873c1..6dcde0a9 100644 --- a/pegged/examples/extended_pascal/source/app.d +++ b/pegged/examples/extended_pascal/source/app.d @@ -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; } diff --git a/pegged/peg.d b/pegged/peg.d index 1704c4a0..2e811f35 100644 --- a/pegged/peg.d +++ b/pegged/peg.d @@ -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; @@ -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; } @@ -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; @@ -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. */ @@ -1217,7 +1217,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); @@ -1242,7 +1242,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(); } @@ -1253,7 +1253,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; @@ -1457,7 +1457,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); @@ -1467,7 +1467,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(); } @@ -1477,7 +1477,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)() ~")"; @@ -1894,7 +1894,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); @@ -1907,7 +1907,7 @@ 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); @@ -1915,7 +1915,7 @@ template zeroOrMore(alias r) result.successful = true; version (tracer) { - if (shouldTrace(getName!(r)())) + if (shouldTrace(getName!(r)(), p)) trace(traceResultMsg(result, getName!(r)())); decTraceLevel(); } @@ -2047,7 +2047,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); @@ -2069,7 +2069,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); @@ -2078,7 +2078,7 @@ template oneOrMore(alias r) } version (tracer) { - if (shouldTrace(getName!(r)())) + if (shouldTrace(getName!(r)(), p)) trace(traceResultMsg(result, getName!(r)())); decTraceLevel(); } @@ -2185,7 +2185,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); @@ -2279,7 +2279,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); @@ -2370,7 +2370,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);