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.
This commit is contained in:
Akim Demaille
2021-01-17 08:17:50 +01:00
parent 5aaa93ae72
commit fb144c021c
2 changed files with 18 additions and 13 deletions

View File

@@ -164,7 +164,8 @@ public struct ]b4_location_type[
* Create a <code>Location</code> denoting an empty range located at * Create a <code>Location</code> denoting an empty range located at
* a given point. * a given point.
* @@param loc The position at which the range is anchored. */ * @@param loc The position at which the range is anchored. */
public this (Position loc) { public this(Position loc)
{
this.begin = this.end = loc; this.begin = this.end = loc;
} }
@@ -172,16 +173,25 @@ public struct ]b4_location_type[
* Create a <code>Location</code> from the endpoints of the range. * Create a <code>Location</code> from the endpoints of the range.
* @@param begin The first position included in the range. * @@param begin The first position included in the range.
* @@param end The first position beyond the range. */ * @@param end The first position beyond the range. */
public this (Position begin, Position end) public this(Position begin, Position end)
{ {
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
} }
/**
* Reset initial location to final location.
*/
public void step()
{
this.begin = this.end;
}
/** /**
* A representation of the location. * A representation of the location.
*/ */
public string toString () const { public string toString() const
{
auto end_col = 0 < end.column ? end.column - 1 : 0; auto end_col = 0 < end.column ? end.column - 1 : 0;
auto res = begin.toString (); auto res = begin.toString ();
if (end.filename && begin.filename != end.filename) if (end.filename && begin.filename != end.filename)

View File

@@ -115,10 +115,10 @@ if (isInputRange!R && is(ElementType!R : dchar))
// Skip initial spaces // Skip initial spaces
while (!input.empty && input.front != '\n' && isWhite(input.front)) while (!input.empty && input.front != '\n' && isWhite(input.front))
{ {
location.begin = location.end;
location.end.column++; location.end.column++;
input.popFront; input.popFront;
} }
location.step();
if (input.empty) if (input.empty)
return Symbol(TokenKind.YYEOF, location); return Symbol(TokenKind.YYEOF, location);
@@ -126,8 +126,6 @@ if (isInputRange!R && is(ElementType!R : dchar))
// Numbers. // Numbers.
if (input.front.isNumber) if (input.front.isNumber)
{ {
int ival;
int lenChars = 0;
import std.compiler : version_minor; import std.compiler : version_minor;
static if (version_minor >= 95) static if (version_minor >= 95)
{ {
@@ -136,29 +134,26 @@ if (isInputRange!R && is(ElementType!R : dchar))
import std.typecons : Flag, Yes; import std.typecons : Flag, Yes;
import std.conv : parse; import std.conv : parse;
auto parsed = parse!(int, R, Yes.doCount)(input); auto parsed = parse!(int, R, Yes.doCount)(input);
ival = parsed.data; int ival = parsed.data;
lenChars = cast(int) parsed.count; location.end.column += cast(int) parsed.count;
} }
else else
{ {
auto copy = input; auto copy = input;
import std.conv : parse; import std.conv : parse;
ival = input.parse!int; int ival = input.parse!int;
while (!input.empty && copy.front != input.front) while (!input.empty && copy.front != input.front)
{ {
lenChars++; location.end.column++;
copy.popFront; copy.popFront;
} }
} }
location.begin = location.end;
location.end.column += lenChars;
return Symbol(TokenKind.NUM, ival, location); return Symbol(TokenKind.NUM, ival, location);
} }
// Individual characters // Individual characters
auto ch = input.front; auto ch = input.front;
input.popFront; input.popFront;
location.begin = location.end;
location.end.column++; location.end.column++;
switch (ch) switch (ch)
{ {