diff options
author | Akim Demaille <akim.demaille@gmail.com> | 2021-01-17 08:17:50 +0100 |
---|---|---|
committer | Akim Demaille <akim.demaille@gmail.com> | 2021-01-17 08:38:51 +0100 |
commit | fb144c021c535c687b023601c7d3f40820074dfb (patch) | |
tree | 9abe09e17b68ef635303e45343662a0fd0aa1620 /examples | |
parent | 5aaa93ae727cb396b85d0c9875e0f61020f259eb (diff) | |
download | bison-fb144c021c535c687b023601c7d3f40820074dfb.tar.gz |
d: examples: reduce scopes
* data/skeletons/lalr1.d (YYLocation.step): New.
* examples/d/calc/calc.y (Lexer): Reduce scopes to avoid uninitialized
varibles.
Factor the handling of locations.
We don't need lenChars.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/d/calc/calc.y | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 83e378e4..d5d1274e 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -115,10 +115,10 @@ if (isInputRange!R && is(ElementType!R : dchar)) // Skip initial spaces while (!input.empty && input.front != '\n' && isWhite(input.front)) { - location.begin = location.end; location.end.column++; input.popFront; } + location.step(); if (input.empty) return Symbol(TokenKind.YYEOF, location); @@ -126,8 +126,6 @@ if (isInputRange!R && is(ElementType!R : dchar)) // Numbers. if (input.front.isNumber) { - int ival; - int lenChars = 0; import std.compiler : version_minor; static if (version_minor >= 95) { @@ -136,29 +134,26 @@ if (isInputRange!R && is(ElementType!R : dchar)) import std.typecons : Flag, Yes; import std.conv : parse; auto parsed = parse!(int, R, Yes.doCount)(input); - ival = parsed.data; - lenChars = cast(int) parsed.count; + int ival = parsed.data; + location.end.column += cast(int) parsed.count; } else { auto copy = input; import std.conv : parse; - ival = input.parse!int; + int ival = input.parse!int; while (!input.empty && copy.front != input.front) { - lenChars++; + location.end.column++; copy.popFront; } } - location.begin = location.end; - location.end.column += lenChars; return Symbol(TokenKind.NUM, ival, location); } // Individual characters auto ch = input.front; input.popFront; - location.begin = location.end; location.end.column++; switch (ch) { |