Skip to content

Commit

Permalink
\n and \t handling - fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
bgalek committed Jun 27, 2024
1 parent 6bb9202 commit a9b4d92
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
34 changes: 30 additions & 4 deletions src/main/java/pl/allegro/tech/opel/OpelParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import org.parboiled.annotations.SuppressSubnodes;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@BuildParseTree
public class OpelParser extends BaseParser<OpelNode> {

private final Pattern escapeCharacterPattern = Pattern.compile("\\\\.");

final ImplicitConversion implicitConversion;
final OpelNodeFactory nodeFactory;

Expand Down Expand Up @@ -343,14 +347,36 @@ protected Rule fromStringLiteral(String string) {
return String(string).label("'" + string + "'");
}

/**
* Escapes special characters in string literals.
* Explicitly converts \t and \n to their corresponding whitespace characters.
* All other escaped characters are left as they are.
*/
protected String escapeString(String string) {
Matcher matcher = escapeCharacterPattern.matcher(string);
StringBuilder result = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '\\') {
i++;
while (matcher.find()) {
String replacement;
switch (matcher.group()) {
case "\\t":
replacement = "\t";
break;
case "\\r":
replacement = "\r";
break;
case "\\n":
replacement = "\n";
break;
case "\\\\":
replacement = "\\\\";
break;
default:
replacement = matcher.group().substring(1);
break;
}
result.append(string.charAt(i));
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
return result.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException

import static OpelEngineBuilder.create
import static pl.allegro.tech.opel.TestUtil.constFunctionReturning
import static pl.allegro.tech.opel.TestUtil.functions

class OpelEngineIntegrationSpec extends Specification {
Expand Down Expand Up @@ -74,9 +73,9 @@ xyz'"""
def 'should evaluate expression with whitespaces "#input"'() {
given:
def engine = create()
.withImplicitConversion(String, BigDecimal, { string -> new BigDecimal(string) })
.withImplicitConversion(BigDecimal, String, { decimal -> decimal.toPlainString() })
.build()
.withImplicitConversion(String, BigDecimal, { string -> new BigDecimal(string) })
.withImplicitConversion(BigDecimal, String, { decimal -> decimal.toPlainString() })
.build()

expect:
engine.eval(input).get() == expResult
Expand Down Expand Up @@ -109,9 +108,9 @@ xyz'"""
def 'should evaluate expression which starts with whitespace(s): "#input"'() {
given:
def engine = create()
.withImplicitConversion(String, BigDecimal, { string -> new BigDecimal(string) })
.withImplicitConversion(BigDecimal, String, { decimal -> decimal.toPlainString() })
.build()
.withImplicitConversion(String, BigDecimal, { string -> new BigDecimal(string) })
.withImplicitConversion(BigDecimal, String, { decimal -> decimal.toPlainString() })
.build()

expect:
engine.eval(input).get() == expResult
Expand Down Expand Up @@ -305,4 +304,28 @@ xyz'"""
counter1 == 1
counter2 == 0
}

@Unroll
def 'should not treat \\n and \\t characters as escaped but as whitespace control characters'() {
given:
def engine = create().build()

expect:
engine.eval(input).get() == expResult

where:
input || expResult
/'Guns\'n\'Roses'.startsWith('Guns\'n\'R')/ || true //\' is a ' character (escaped)
/'\\slash\\'.startsWith('\\')/ || true //\\ is a \ character (escaped)
/'pest'.startsWith('\pest')/ || true //\p is a p character (escaped)
/'\tsomething'.startsWith('\t')/ || true //\t is a tab character
/'\nsomething'.startsWith('\n')/ || true //\n is a newline character
/'\rsomething'.startsWith('\r')/ || true //\r is a RF character
/'\r\nsomething'.startsWith('\r\n')/ || true //\r\n is CRLF character
/'test'.startsWith('\test')/ || false //\t is replaced by tab character not t character
/'nest'.startsWith('\nest')/ || false //\n is replaced by newline character not n character
/'rest'.startsWith('\rest')/ || false //\r is replaced by RF character not r character
/'rust'.startsWith('\r\nust')/ || false //\r\n is replaced with newline character not rn characters

}
}

0 comments on commit a9b4d92

Please sign in to comment.