Add single and multi-line comments

This commit is contained in:
Riccardo Azzolini 2018-11-23 18:52:36 +01:00
parent da91a5df33
commit 27b128a6ea
2 changed files with 27 additions and 8 deletions

View File

@ -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) {

View File

@ -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<Token> 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());
}