Fixed bug (and added corresponding unit test) whereby the combinator check would fail if the first element was at end-of-source

This commit is contained in:
Tristan B. Velloza Kildaire 2021-03-02 23:29:19 +02:00
parent 28ab54e852
commit 68f9bb6523
1 changed files with 13 additions and 2 deletions

View File

@ -61,12 +61,12 @@ public final class Lexer
/* Check if we need to do combinators (e.g. for ||, &&) */
/* TODO: Second operand in condition out of bounds */
if(currentChar == '|' && sourceCode[position+1] == '|')
if(currentChar == '|' && (position+1) != sourceCode.length && sourceCode[position+1] == '|')
{
splitterToken = "||";
position += 2;
}
else if(currentChar == '&' && sourceCode[position+1] == '&')
else if(currentChar == '&' && (position+1) != sourceCode.length && sourceCode[position+1] == '&')
{
splitterToken = "&&";
position += 2;
@ -185,6 +185,17 @@ unittest
assert(currentLexer.getTokens() == ["hello", "\"world\"","||"]);
}
/* Test input: `hello "world"|` */
unittest
{
import std.algorithm.comparison;
string sourceCode = "hello \"world\";|";
Lexer currentLexer = new Lexer(sourceCode);
currentLexer.performLex();
gprintln("Collected "~to!(string)(currentLexer.getTokens()));
assert(currentLexer.getTokens() == ["hello", "\"world\"",";", "|"]);
}
/* Test input: ` hello` */
unittest
{