From bb0310a35342d041055701ca0891f5a80e699763 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 1 Mar 2019 06:37:58 +0100 Subject: [PATCH] d: simplify the API to build the scanner of the example * examples/d/calc.y (calcLexer): Add an overload for File. Use it. --- examples/d/calc.y | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/examples/d/calc.y b/examples/d/calc.y index c97e4f3a..fbabda94 100644 --- a/examples/d/calc.y +++ b/examples/d/calc.y @@ -54,6 +54,7 @@ exp: %% import std.range.primitives; +import std.stdio; auto calcLexer(R)(R range) if (isInputRange!R && is (ElementType!R : dchar)) @@ -61,6 +62,17 @@ auto calcLexer(R)(R range) return new CalcLexer!R(range); } +auto calcLexer (File f) +{ + import std.algorithm : map, joiner; + import std.utf : byDchar; + + return f.byChunk(1024) // avoid making a syscall roundtrip per char + .map!(chunk => cast(char[]) chunk) // because byChunk returns ubyte[] + .joiner // combine chunks into a single virtual range of char + .calcLexer; // forward to other overload +} + class CalcLexer(R) : Lexer if (isInputRange!R && is (ElementType!R : dchar)) { @@ -127,17 +139,8 @@ class CalcLexer(R) : Lexer int main () { - import std.algorithm : map, joiner; - import std.stdio; - import std.utf : byDchar; - - auto l = stdin - .byChunk(1024) // avoid making a syscall roundtrip per char - .map!(chunk => cast(char[]) chunk) // because byChunk returns ubyte[] - .joiner // combine chunks into a single virtual range of char - .calcLexer; - - Calc p = new Calc (l); + auto l = calcLexer (stdin); + auto p = new Calc (l); p.parse (); return l.exit_status; }