summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2019-03-01 06:37:58 +0100
committerAkim Demaille <akim.demaille@gmail.com>2019-03-02 09:56:41 +0100
commitbb0310a35342d041055701ca0891f5a80e699763 (patch)
tree32c7a6c6bc503a7295f17c9855404e0ac2b84c70 /examples
parentf8408562f8439654261418406296e4108d2a995f (diff)
downloadbison-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.y25
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;
}