muScript: support comments
This commit is contained in:
parent
6f180900b6
commit
a5fa86a6c7
@ -32,7 +32,7 @@ public class Lexer {
|
||||
}
|
||||
|
||||
// Scan expression
|
||||
skipWhitespace();
|
||||
skipWhitespaceAndComments();
|
||||
if (isAtEnd()) {
|
||||
createToken(Token.EOF);
|
||||
return;
|
||||
@ -132,13 +132,32 @@ public class Lexer {
|
||||
}
|
||||
}
|
||||
|
||||
private void skipWhitespace() {
|
||||
private void skipWhitespaceAndComments() {
|
||||
while (true) {
|
||||
if (isAtEnd()) return;
|
||||
char c = peek();
|
||||
|
||||
switch (c) {
|
||||
switch (peek()) {
|
||||
case ' ', '\r', '\t', '\n' -> advance();
|
||||
case '/' -> {
|
||||
switch (peekNext()) {
|
||||
case '/' -> {
|
||||
while (!isAtEnd() && peek() != '\r' && peek() != '\n') advance();
|
||||
}
|
||||
case '*' -> {
|
||||
advance();
|
||||
advance();
|
||||
while (!isAtEnd() && (peek() != '*' || peekNext() != '/')) advance();
|
||||
if (!isAtEnd()) {
|
||||
advance();
|
||||
advance();
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
start = current;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
start = current;
|
||||
return;
|
||||
|
@ -0,0 +1,33 @@
|
||||
package io.gitlab.jfronny.muscript.test;
|
||||
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.gitlab.jfronny.muscript.test.util.MuTestUtil.makeArgs;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CommentTest {
|
||||
@Test
|
||||
void singleLine() {
|
||||
assertEquals(2, Parser.parseScript("""
|
||||
n = 5
|
||||
n = 2
|
||||
// n = 3
|
||||
n
|
||||
""").run(makeArgs()).asNumber().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiLine() {
|
||||
assertEquals(2, Parser.parseScript("""
|
||||
n = 2
|
||||
/* n = 3 */
|
||||
/*
|
||||
n = 3
|
||||
n = 4
|
||||
n = 5
|
||||
*/
|
||||
n
|
||||
""").run(makeArgs()).asNumber().getValue());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user