From 8084137eb06402e8627e9dc726d9e533f2562a8f Mon Sep 17 00:00:00 2001 From: Radoslaw Cybulski Date: Thu, 18 Jan 2018 10:55:58 +0100 Subject: [PATCH] Adds support for comment (# character) in batch scripts Character # begins comment, which lasts until end of line. Change-Id: Ia6ecfcea2116eb071a6ff77174b1369204e3e850 --- src/batch/Lexer.cpp | 14 ++++++++++++++ tests/no-ui-scenarios/BatchExecTests.cpp | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/batch/Lexer.cpp b/src/batch/Lexer.cpp index 1a60e72..9ea5d12 100644 --- a/src/batch/Lexer.cpp +++ b/src/batch/Lexer.cpp @@ -371,6 +371,19 @@ class Lexer addToken(index, TokenType::IDENTIFIER); return true; } + + /** + * @brief Tries to parse comment character (#) + * + * # character starts comment, which continues until end of the current line + */ + bool tryComment() + { + if (source[sourceIndex] != '#') return false; + auto currentLine = line; + while (sourceIndex < source.size() && currentLine == line) skipCharacter(); + return true; + } /** * @brief Tries to parse operator * @@ -416,6 +429,7 @@ public: newLine(); while (errorMessage.empty() && sourceIndex < source.size()) { if (skipWhiteSpaces() || + tryComment() || tryIdentifierOrKeyword() || tryOperator() || tryString() || diff --git a/tests/no-ui-scenarios/BatchExecTests.cpp b/tests/no-ui-scenarios/BatchExecTests.cpp index 7ff7af1..08c57e9 100644 --- a/tests/no-ui-scenarios/BatchExecTests.cpp +++ b/tests/no-ui-scenarios/BatchExecTests.cpp @@ -318,6 +318,16 @@ TEST_F(ScriptTest, point) , 1); } +TEST_F(ScriptTest, comments) +{ + execute( + "v = 0 #\n" + "v = 1# v = 2\n" + "# v = 3\n" + "assert(v == 1)\n" + , 1); +} + int main(int argc, char *argv[]) { try { -- 2.7.4