From cd99a1a923c652c4fc4fec8a6bc6f0db83b8b787 Mon Sep 17 00:00:00 2001 From: Adela Vais Date: Sat, 2 Jan 2021 23:38:30 +0200 Subject: [PATCH] d: examples: calc: use of std.conv.parse for location From Dlang v2.095.0 onwards, std.conv.parse reports the number of consumed characters. * examples/d/calc/calc.y: Here. --- examples/d/calc/calc.y | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 832d7788..640e246a 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -129,13 +129,27 @@ if (isInputRange!R && is(ElementType!R : dchar)) if (input.front.isNumber) { int lenChars = 0; - auto copy = input; - import std.conv : parse; - value_.ival = input.parse!int; - while (!input.empty && copy.front != input.front) + import std.compiler : version_minor; + static if (version_minor >= 95) { - lenChars++; - copy.popFront; + // from Dlang v2.095.0 onwards std.conv.parse reports + // the number of consumed characters + import std.typecons : Flag, Yes; + import std.conv : parse; + auto parsed = parse!(int, R, Yes.doCount)(input); + value_.ival = parsed.data; + lenChars = cast(int) parsed.count; + } + else + { + auto copy = input; + import std.conv : parse; + value.ival = input.parse!int; + while (!input.empty && copy.front != input.front) + { + lenChars++; + copy.popFront; + } } location.begin = location.end; location.end.column += lenChars;