From 27b128a6ea0e552627eeb72692100df7d9a33c82 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Fri, 23 Nov 2018 18:52:36 +0100 Subject: [PATCH] Add single and multi-line comments --- .../warppi/math/rules/dsl/frontend/Lexer.java | 21 ++++++++++++++++++- .../math/rules/dsl/frontend/LexerTest.java | 14 ++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Lexer.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Lexer.java index ea0f5ccc..7a79ff7a 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Lexer.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Lexer.java @@ -52,7 +52,6 @@ public class Lexer { case ']': emitToken(RIGHT_BRACKET); break; case '=': emitToken(EQUALS); break; case '*': emitToken(TIMES); break; - case '/': emitToken(DIVIDE); break; case '^': emitToken(POWER); break; case '+': @@ -71,6 +70,16 @@ public class Lexer { } break; + case '/': + if (matchChar('/')) { + singleLineComment(); + } else if (matchChar('*')) { + multiLineComment(); + } else { + emitToken(DIVIDE); + } + break; + default: if (isAsciiDigit(current)) { number(); @@ -82,6 +91,16 @@ public class Lexer { } } + private void singleLineComment() { + matchWhile(c -> c != '\n'); + } + + private void multiLineComment() { + while (!(matchChar('*') && matchChar('/'))) { + popChar(); + } + } + private void number() { matchWhile(Lexer::isAsciiDigit); if (matchChar('.') && matchWhile(Lexer::isAsciiDigit) == 0) { diff --git a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/LexerTest.java b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/LexerTest.java index 5eea2ccf..2aca9d8c 100644 --- a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/LexerTest.java +++ b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/LexerTest.java @@ -14,9 +14,9 @@ public class LexerTest { final Lexer lexer = new Lexer( "reduction TestRule_123:\n" + " x + y * z = -(a_123 +- 3 / 2.2) -> [\n" + - " x^a_123 = cos(pi) - log(e, e),\n" + - " undefined,\n" + - "]\n" + " x^a_123 = cos(pi) - log(e, e), // comment\n" + + " undefined, /*\n" + + "comment */ ]\n" ); final List expected = Arrays.asList( new Token(REDUCTION, "reduction", 0), @@ -54,10 +54,10 @@ public class LexerTest { new Token(E, "e", 94), new Token(RIGHT_PAREN, ")", 95), new Token(COMMA, ",", 96), - new Token(UNDEFINED, "undefined", 102), - new Token(COMMA, ",", 111), - new Token(RIGHT_BRACKET, "]", 113), - new Token(EOF, "", 115) + new Token(UNDEFINED, "undefined", 113), + new Token(COMMA, ",", 122), + new Token(RIGHT_BRACKET, "]", 138), + new Token(EOF, "", 140) ); assertEquals(expected, lexer.lex()); }