From a5b9837c8026f430941c000ec9397ddca08eceaf Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Wed, 3 Mar 2021 00:11:16 +0200 Subject: [PATCH] Added escape sequence support --- source/tlang/commandline/lexer.d | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/source/tlang/commandline/lexer.d b/source/tlang/commandline/lexer.d index c8356b5..82f95e7 100644 --- a/source/tlang/commandline/lexer.d +++ b/source/tlang/commandline/lexer.d @@ -34,8 +34,11 @@ public final class Lexer ulong position; char currentChar; + /* Whether we are in a string "we are here" or not */ bool stringMode; + bool escapeMode; + while(position < sourceCode.length) { // gprintln("SrcCodeLen: "~to!(string)(sourceCode.length)); @@ -119,6 +122,30 @@ public final class Lexer position++; } + else if(currentChar == '\\') + { + /* You can only use these in strings */ + if(stringMode) + { + /* Check if we have a next character */ + if(position+1 != sourceCode.length && isValidEscape(sourceCode[position+1])) + { + /* Add to the string */ + currentToken ~= "\\"~sourceCode[position+1]; + + position += 2; + } + /* If we don't have a next character then raise error */ + else + { + gprintln("Unfinished escape sequence", DebugType.ERROR); + } + } + else + { + gprintln("Escape sequences can only be used within strings", DebugType.ERROR); + } + } else { currentToken ~= currentChar; @@ -151,6 +178,11 @@ public final class Lexer character == '{' || character == '}' || character == '=' || character == '|' || character == '^' || character == '!'; } + + public bool isValidEscape(char character) + { + return true; /* TODO: Implement me */ + } } /* Test input: `hello "world";` */ @@ -217,4 +249,16 @@ unittest currentLexer.performLex(); gprintln("Collected "~to!(string)(currentLexer.getTokens())); assert(currentLexer.getTokens() == ["hello", ";"]); -} \ No newline at end of file +} + +/* 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\\\"\""]); +} +