Skip to content

Commit

Permalink
fix parsing name and quoted bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakobeha committed Jun 28, 2024
1 parent e5a0de6 commit 5e2fb28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/prlprg/parseprint/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ public String readQuoted(char quote) {
case 'r' -> sb.append('\r');
case 't' -> sb.append('\t');
case '"' -> sb.append('"');
case '\\' -> sb.append('\\');
case '\'' -> sb.append('\'');
case '`' -> sb.append('`');
case '\\' -> sb.append('\\');
case 'x' -> {
var hex = readFixedLength(2);
try {
Expand Down Expand Up @@ -362,7 +362,13 @@ public String readQuotedLiterally(char quote) {
return sb.toString();
} else if (c == '\\') {
switch (readChar()) {
case 'n', 'r', 't', '"', '\\', '\'', '`' -> {}
case 'n' -> sb.append('n');
case 'r' -> sb.append('r');
case 't' -> sb.append('t');
case '"' -> sb.append('"');
case '\'' -> sb.append('\'');
case '`' -> sb.append('`');
case '\\' -> sb.append('\\');
case 'x' -> {
var hex = readFixedLength(2);
try {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/prlprg/primitive/Names.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.prlprg.primitive;

import com.google.common.collect.ImmutableList;
import java.util.regex.Pattern;
import org.prlprg.parseprint.ParseException;
import org.prlprg.parseprint.PrettyPrintWriter;
import org.prlprg.parseprint.Scanner;
Expand All @@ -27,7 +28,7 @@ public static String quoteIfNecessary(String s) {
if (s.isEmpty()) {
throw new IllegalArgumentException("empty string is reserved for special symbols");
}
return isValidUnquoted(s) ? s : PrettyPrintWriter.use(w -> w.writeQuoted('`', s));
return isValid(s) ? s : PrettyPrintWriter.use(w -> w.writeQuoted('`', s));
}

/**
Expand All @@ -41,6 +42,8 @@ public static String unquoteIfNecessary(String s) {
: s;
}

private static final Pattern UNESCAPED_NOT_END_BACKTICK = Pattern.compile("(?<!\\\\)`(?!$)");

/**
* Whether the string {@linkplain #isValidUnquoted(String) is valid unquoted}, or is in quotes and
* not empty.
Expand All @@ -50,7 +53,7 @@ public static boolean isValid(String s) {
|| (s.startsWith("`")
&& s.endsWith("`")
&& s.length() > 2
&& !s.substring(1, s.length() - 1).replaceAll("\\`", "").contains("`"));
&& !UNESCAPED_NOT_END_BACKTICK.matcher(s).find(1));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/prlprg/parseprint/ScannerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ public void testReadPastEndOfLine() {
assertEquals("\n", scanner2.readPastEndOfLine());
assertEquals("3", scanner2.readPastEndOfLine());
}

@Test
public void testReadQuoted() {
var scanner = new Scanner("\"\\\"Hello\\\", \\\"world!\\\"\"");
assertEquals("\"Hello\", \"world!\"", scanner.readQuoted('"'));
scanner = new Scanner("\"\\\"Hello\\\", \\\"world!\\\"\"");
assertEquals("\"\\\"Hello\\\", \\\"world!\\\"\"", scanner.readQuotedLiterally('"'));
}
}

0 comments on commit 5e2fb28

Please sign in to comment.