Fixed bug whereby `isNumericalStr()` returned true for empty strings.

This fixes problems whereby if the current build up was empty and a field accessor was being attempted then lexing would fail.
This commit is contained in:
Tristan B. Velloza Kildaire 2022-09-27 09:29:01 +02:00
parent 3d54c4deee
commit 806814f01c
1 changed files with 29 additions and 1 deletions

View File

@ -172,7 +172,7 @@ public final class Lexer
/* Enable floating point mode and go to next iteration*/
floatMode = true;
gprintln("Halo");
gprintln("Float mode just got enabled: Current build up: \""~currentToken~"\"");
column++;
position++;
continue;
@ -583,8 +583,25 @@ public final class Lexer
}
/**
* Given a string return true if all characters
* are digits, false otherwise and false if
* the string is empty
*/
private static bool isNumericalStr(string input)
{
/**
* If the given input is empty then return false
*/
if(input.length == 0)
{
return false;
}
/**
* If there are any characters in the string then
* check if all are digits
*/
for(ulong i = 0; i < input.length; i++)
{
char character = input[i];
@ -784,4 +801,15 @@ unittest
}
/* Test input: `1.5` */
unittest
{
import std.algorithm.comparison;
string sourceCode = "1.5";
Lexer currentLexer = new Lexer(sourceCode);
currentLexer.performLex();
gprintln("Collected "~to!(string)(currentLexer.getTokens()));
assert(currentLexer.getTokens() == [new Token("1.5", 0, 0)]);
}
/* TODO: Add more tests */