Skip to content

Commit 98c0c00

Browse files
committed
refactor: Whitespace tokens can no longer be multi-line
Multiple whitespace tokens will be created once a newline is encountered in the input.
1 parent cf9193b commit 98c0c00

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/main/java/org/codejive/properties/PropertiesParser.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.Reader;
55
import java.util.Objects;
66
import java.util.Spliterators;
7+
import java.util.function.BiFunction;
78
import java.util.function.Consumer;
89
import java.util.function.Function;
910
import java.util.stream.Stream;
@@ -88,29 +89,31 @@ public Token nextToken() throws IOException {
8889
if (isEof(ch)) {
8990
return null;
9091
}
91-
Function<Integer, Boolean> isValid = (c) -> false;
92+
int oldch = -1;
93+
BiFunction<Integer, Integer, Boolean> isValid = (c, oldc) -> false;
9294
Type nextState = null;
9395
if (state == null) {
9496
if (isCommentChar(ch)) {
9597
state = Type.COMMENT;
96-
isValid = this::isNotEol;
98+
isValid = (c, oldc) -> !isEol(c) && !isEof(c);
9799
} else if (isWhitespaceChar(ch)) {
98100
state = Type.WHITESPACE;
99-
isValid = this::isWhitespaceChar;
101+
isValid = (c, oldc) -> isWhitespaceChar(c) && !isEol(oldc);
100102
} else {
101103
state = Type.KEY;
102-
isValid = (c) -> !isSeparatorChar(c);
104+
isValid = (c, oldc) -> !isSeparatorChar(c);
103105
nextState = Type.SEPARATOR;
104106
}
105107
} else if (state == Type.SEPARATOR) {
106-
isValid = this::isSeparatorChar;
108+
isValid = (c, oldc) -> isSeparatorChar(c);
107109
nextState = Type.VALUE;
108110
} else if (state == Type.VALUE) {
109-
isValid = this::isNotEol;
111+
isValid = (c, oldc) -> !isEol(c) && !isEof(c);
110112
}
111113
while (true) {
112-
if (isValid.apply(ch)) {
114+
if (isValid.apply(ch, oldch)) {
113115
addChar(readChar());
116+
oldch = ch;
114117
ch = peekChar();
115118
} else {
116119
String text = (state == Type.VALUE || state == Type.COMMENT) ? trimmedString() : string();
@@ -221,7 +224,7 @@ private boolean isSeparatorChar(int ch) {
221224
}
222225

223226
private boolean isWhitespaceChar(int ch) {
224-
return ch == ' ' || ch == '\t' || ch == '\f' || ch == '\n' || ch == '\r';
227+
return ch == ' ' || ch == '\t' || ch == '\f' || isEol(ch);
225228
}
226229

227230
private boolean isCommentChar(int ch) {
@@ -233,8 +236,8 @@ private boolean isHexDigitChar(int ch) {
233236
return Character.isDigit(ch) || (uch >= 'A' && uch <= 'F');
234237
}
235238

236-
private boolean isNotEol(int ch) {
237-
return ch != '\n' && ch != '\r' && !isEof(ch);
239+
private boolean isEol(int ch) {
240+
return ch == '\n' || ch == '\r';
238241
}
239242

240243
private boolean isEof(int ch) {

src/test/java/org/codejive/properties/TestPropertiesParser.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public class TestPropertiesParser {
3131
+ "multiline = one \\\n"
3232
+ " two \\\n\r"
3333
+ "\tthree\n"
34-
+ "key.4 = \\u1234\n"
35-
+ "# final comment";
34+
+ "key.4 = \\u1234\n\r"
35+
+ " # final comment";
3636

3737
@Test
3838
void testTokens() throws IOException {
@@ -45,7 +45,8 @@ void testTokens() throws IOException {
4545
new Token(Type.COMMENT, "#comment1"),
4646
new Token(Type.WHITESPACE, "\n"),
4747
new Token(Type.COMMENT, "# comment2"),
48-
new Token(Type.WHITESPACE, " \n\n"),
48+
new Token(Type.WHITESPACE, " \n"),
49+
new Token(Type.WHITESPACE, "\n"),
4950
new Token(Type.COMMENT, "! comment3"),
5051
new Token(Type.WHITESPACE, "\n"),
5152
new Token(Type.KEY, "one"),
@@ -61,7 +62,8 @@ void testTokens() throws IOException {
6162
new Token(Type.KEY, "three"),
6263
new Token(Type.SEPARATOR, "="),
6364
new Token(Type.VALUE, "and escapes\\n\\t\\r\\f", "and escapes\n\t\r\f"),
64-
new Token(Type.WHITESPACE, "\n "),
65+
new Token(Type.WHITESPACE, "\n"),
66+
new Token(Type.WHITESPACE, " "),
6567
new Token(Type.KEY, "\\ with\\ spaces", " with spaces"),
6668
new Token(Type.SEPARATOR, " = "),
6769
new Token(Type.VALUE, "everywhere"),
@@ -77,7 +79,8 @@ void testTokens() throws IOException {
7779
new Token(Type.KEY, "key.4"),
7880
new Token(Type.SEPARATOR, " = "),
7981
new Token(Type.VALUE, "\\u1234", "\u1234"),
80-
new Token(Type.WHITESPACE, "\n"),
82+
new Token(Type.WHITESPACE, "\n\r"),
83+
new Token(Type.WHITESPACE, " "),
8184
new Token(Type.COMMENT, "# final comment"))));
8285
}
8386

0 commit comments

Comments
 (0)