d: tests: various style fixes

* tests/calc.at, tests/scanner.at: Here.
(yylex): Report values directly, without storing them to the union
first.
This commit is contained in:
Adela Vais
2021-01-25 17:59:06 +02:00
committed by Akim Demaille
parent b4582f1918
commit 689b184983
2 changed files with 28 additions and 39 deletions

View File

@@ -539,7 +539,7 @@ m4_define([AT_CALC_YYLEX(d)],
import std.stdio; import std.stdio;
auto calcLexer(R)(R range) auto calcLexer(R)(R range)
if (isInputRange!R && is (ElementType!R : dchar)) if (isInputRange!R && is (ElementType!R : dchar))
{ {
return new CalcLexer!R(range); return new CalcLexer!R(range);
} }
@@ -556,17 +556,14 @@ auto calcLexer (File f)
} }
class CalcLexer(R) : Lexer class CalcLexer(R) : Lexer
if (isInputRange!R && is (ElementType!R : dchar)) if (isInputRange!R && is (ElementType!R : dchar))
{ {
R input; R input;
this(R r) { this(R r) { input = r; }
input = r;
}
]AT_YYERROR_DEFINE[ ]AT_YYERROR_DEFINE[
]AT_LOCATION_IF([[
Value value_;]AT_LOCATION_IF([[
Location location; Location location;
]])[ ]])[
int parseInt () int parseInt ()
@@ -574,27 +571,27 @@ class CalcLexer(R) : Lexer
auto res = 0; auto res = 0;
import std.uni : isNumber; import std.uni : isNumber;
while (input.front.isNumber) while (input.front.isNumber)
{ {
res = res * 10 + (input.front - '0');]AT_LOCATION_IF([[ res = res * 10 + (input.front - '0');]AT_LOCATION_IF([[
location.end.column += 1;]])[ location.end.column += 1;]])[
input.popFront; input.popFront;
} }
return res; return res;
} }
Symbol yylex () Symbol yylex ()
{]AT_LOCATION_IF([[ {]AT_LOCATION_IF([[
location.begin = location.end;]])[ location.step();]])[
import std.uni : isWhite, isNumber; import std.uni : isWhite, isNumber;
// Skip initial spaces // Skip initial spaces
while (!input.empty && input.front != '\n' && isWhite (input.front)) while (!input.empty && input.front != '\n' && isWhite (input.front))
{ {
input.popFront;]AT_LOCATION_IF([[ input.popFront;]AT_LOCATION_IF([[
location.begin.column += 1; location.end.column += 1;]])[
location.end.column += 1;]])[ }]AT_LOCATION_IF([[
} location.step();]])[
// EOF. // EOF.
if (input.empty) if (input.empty)
@@ -602,28 +599,25 @@ class CalcLexer(R) : Lexer
// Numbers. // Numbers.
if (input.front.isNumber) if (input.front.isNumber)
{ return Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, parseInt]AT_LOCATION_IF([[, location]])[);
value_.ival = parseInt;
return Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, value_.ival]AT_LOCATION_IF([[, location]])[);
}
// Individual characters // Individual characters
auto c = input.front;]AT_LOCATION_IF([[ auto c = input.front;]AT_LOCATION_IF([[
if (c == '\n') if (c == '\n')
{ {
location.end.line += 1; location.end.line += 1;
location.end.column = 1; location.end.column = 1;
} }
else else
location.end.column += 1;]])[ location.end.column += 1;]])[
input.popFront; input.popFront;
// An explicit error raised by the scanner. */ // An explicit error raised by the scanner. */
if (c == '#') if (c == '#')
{ {
stderr.writeln (]AT_LOCATION_IF([location, ": ", ])["syntax error: invalid character: '#'"); stderr.writeln (]AT_LOCATION_IF([location, ": ", ])["syntax error: invalid character: '#'");
return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, location]])[); return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, location]])[);
} }
switch (c) switch (c)
{ {

View File

@@ -94,7 +94,7 @@ m4_define([AT_RAW_YYLEX(d)],
import std.stdio; import std.stdio;
auto yyLexer(R)(R range) auto yyLexer(R)(R range)
if (isInputRange!R && is (ElementType!R : dchar)) if (isInputRange!R && is (ElementType!R : dchar))
{ {
return new YYLexer!R(range); return new YYLexer!R(range);
} }
@@ -105,18 +105,14 @@ auto yyLexer ()
} }
class YYLexer(R) : Lexer class YYLexer(R) : Lexer
if (isInputRange!R && is (ElementType!R : dchar)) if (isInputRange!R && is (ElementType!R : dchar))
{ {
R input; R input;
this(R r) { this(R r) { input = r; }
input = r;
}
]AT_YYERROR_DEFINE[ ]AT_YYERROR_DEFINE[
Value value_;
Symbol yylex () Symbol yylex ()
{ {
import std.uni : isNumber; import std.uni : isNumber;
@@ -131,8 +127,7 @@ class YYLexer(R) : Lexer
switch (c) switch (c)
{ {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
value_.val = c - '0'; return Symbol(TokenKind.NUM, c - '0');
return Symbol(TokenKind.NUM, value_.val);
case '+': return Symbol(TokenKind.PLUS); case '+': return Symbol(TokenKind.PLUS);
case '-': return Symbol(TokenKind.MINUS); case '-': return Symbol(TokenKind.MINUS);
case '*': return Symbol(TokenKind.STAR); case '*': return Symbol(TokenKind.STAR);