diff options
author | Akim Demaille <akim.demaille@gmail.com> | 2019-03-01 06:37:58 +0100 |
---|---|---|
committer | Akim Demaille <akim.demaille@gmail.com> | 2019-03-02 09:56:41 +0100 |
commit | bb0310a35342d041055701ca0891f5a80e699763 (patch) | |
tree | 32c7a6c6bc503a7295f17c9855404e0ac2b84c70 /examples | |
parent | f8408562f8439654261418406296e4108d2a995f (diff) | |
download | bison-bb0310a35342d041055701ca0891f5a80e699763.tar.gz |
d: simplify the API to build the scanner of the example
* examples/d/calc.y (calcLexer): Add an overload for File.
Use it.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/d/calc.y | 25 |
1 files 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; } |