From 28a8b56ce3e50c949fae3c6cc9b4bf7a86665726 Mon Sep 17 00:00:00 2001 From: Siphalor Date: Sat, 8 Nov 2025 12:28:19 +0100 Subject: [PATCH] fix(serde-hjson): Fix bug with slashes embedded in block comments --- .../tweed5/data/hjson/HjsonLexer.java | 2 ++ .../tweed5/data/hjson/HjsonLexerTest.java | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tweed5/serde-hjson/src/main/java/de/siphalor/tweed5/data/hjson/HjsonLexer.java b/tweed5/serde-hjson/src/main/java/de/siphalor/tweed5/data/hjson/HjsonLexer.java index 128b047..3476ecc 100644 --- a/tweed5/serde-hjson/src/main/java/de/siphalor/tweed5/data/hjson/HjsonLexer.java +++ b/tweed5/serde-hjson/src/main/java/de/siphalor/tweed5/data/hjson/HjsonLexer.java @@ -423,6 +423,8 @@ public class HjsonLexer implements AutoCloseable { lastWasAsterisk = true; } else if (lastWasAsterisk && codePoint == '/') { break; + } else { + lastWasAsterisk = false; } } } diff --git a/tweed5/serde-hjson/src/test/java/de/siphalor/tweed5/data/hjson/HjsonLexerTest.java b/tweed5/serde-hjson/src/test/java/de/siphalor/tweed5/data/hjson/HjsonLexerTest.java index e876498..70586a3 100644 --- a/tweed5/serde-hjson/src/test/java/de/siphalor/tweed5/data/hjson/HjsonLexerTest.java +++ b/tweed5/serde-hjson/src/test/java/de/siphalor/tweed5/data/hjson/HjsonLexerTest.java @@ -1,5 +1,6 @@ package de.siphalor.tweed5.data.hjson; +import lombok.SneakyThrows; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; @@ -128,6 +129,28 @@ class HjsonLexerTest { assertGeneralEof(lexer, new HjsonReadPosition(1, input.length() + 1)); } + @ParameterizedTest + @CsvSource(textBlock = """ + '// 123 + true', TRUE + '# hash comments are cool + false', FALSE + '/* + multiline + comment + */456', NUMBER + '/* multiline comment + * with/slashes + */456', NUMBER + """) + @SneakyThrows + void chompComment(String input, HjsonLexerToken.Type tokenType) { + HjsonLexer lexer = createLexer(input); + + HjsonLexerToken token = lexer.nextGeneralToken(); + assertEquals(tokenType, token.type()); + } + private HjsonLexer createLexer(String input) { return new HjsonLexer(new StringReader(input)); } @@ -138,4 +161,4 @@ class HjsonLexerTest { assertDoesNotThrow(lexer::nextGeneralToken) ); } -} \ No newline at end of file +}