Skip to content

Commit

Permalink
Expression STRICT_ESCAPES mode
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 13, 2024
1 parent f3eefdd commit 9fc9859
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void rewind(final int newNext) {

private static Node parseString(Itr itr, final boolean allowExpr, final boolean endOnBrace, final boolean endOnColon,
final EnumSet<Flag> flags) {
boolean escapeExpression = false;
int ignoreBraceLevel = 0;
final List<Node> list = new ArrayList<>();
int start = itr.getNextIdx();
Expand All @@ -258,6 +259,22 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
// treat as plain content
continue;
}
if (flags.contains(Flag.STRICT_ESCAPES)) {
if (!itr.hasNext()) {
continue;
} else if (itr.peekNext() == '$') {
escapeExpression = true;
list.add(new LiteralNode(itr.getStr(), start, itr.getNextIdx()));
start = itr.getNextIdx();
continue;
} else if (itr.peekNext() != '{') {
continue;
} else if (escapeExpression) {
escapeExpression = false;
start = itr.getNextIdx();
continue;
}
}
// check to see if it's a dangling $
if (!itr.hasNext()) {
if (!flags.contains(Flag.LENIENT_SYNTAX)) {
Expand Down Expand Up @@ -614,6 +631,21 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
start = itr.getNextIdx();
continue;
}
} else if (flags.contains(Flag.STRICT_ESCAPES)) {
int escape = itr.getNextIdx();
if (itr.hasNext() && itr.peekNext() == '$') {
itr.next();
if (itr.hasNext() && itr.peekNext() == '{') {
list.add(new LiteralNode(itr.getStr(), start, escape - 1));
list.add(new LiteralNode(itr.getStr(), escape, itr.getNextIdx()));
start = itr.getNextIdx();
} else if (itr.hasNext() && itr.peekNext() == '$') {
list.add(new LiteralNode(itr.getStr(), start, escape));
start = itr.getPrevIdx();
itr.prev();
}
}
continue;
}
// TP 42
// otherwise, just...
Expand Down Expand Up @@ -694,6 +726,11 @@ public enum Flag {
* character.
*/
ESCAPES,
/**
* Escaping <code>$</code> with <code>$$</code> or <code>/$</code> only applies when <code>{</code> follows
* the initial escaped <code>$</code>.
*/
STRICT_ESCAPES,
/**
* Treat expressions containing a double-colon delimiter as special, encoding the entire content into the key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,98 @@ void expressions() {
b.append(c.getExpandedDefault());
}));
}

@Test
void strictEscapes() {
assertEquals("$", Expression.compile("$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$$", Expression.compile("$$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("\\$", Expression.compile("\\$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("\\$$", Expression.compile("\\$$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$$foo", Expression.compile("$$foo", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$", Expression.compile("foo$$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$$foo", Expression.compile("$$foo", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$bar", Expression.compile("foo$$bar", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("${foo}", Expression.compile("$${foo}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$${foo}", Expression.compile("$$${foo}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$${foo}$", Expression.compile("$$${foo}$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$${foo}$$", Expression.compile("$$${foo}$$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo${bar}", Expression.compile("foo$${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$${bar}", Expression.compile("foo$$${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$$${bar}", Expression.compile("foo$$$$${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$$${bar}$$$baz", Expression.compile("foo$$$$${bar}$$$baz", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$$$", Expression.compile("foo$$$$", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("${foo:bar}", Expression.compile("$${foo:bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("$${foo:bar}", Expression.compile("$$${foo:bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("${foo:}", Expression.compile("$${foo:${bar}}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("${foo:${bar}}", Expression.compile("$${foo:$${bar}}", STRICT_ESCAPES).evaluate((c, b) -> {
}));

assertEquals("", Expression.compile("${foo}", STRICT_ESCAPES).evaluate((c, b) -> {
assertEquals("foo", c.getKey());
}));
assertEquals("", Expression.compile("${foo}${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
if ("foo".equals(c.getKey()))
assertEquals("foo", c.getKey());
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foobar", Expression.compile("foo${foo}${bar}bar", STRICT_ESCAPES).evaluate((c, b) -> {
if ("foo".equals(c.getKey()))
assertEquals("foo", c.getKey());
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foo${foo}bar", Expression.compile("foo$${foo}${bar}bar", STRICT_ESCAPES).evaluate((c, b) -> {
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foo${foo}bar", Expression.compile("foo$${foo${bar}}bar", STRICT_ESCAPES).evaluate((c, b) -> {
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("", Expression.compile("${}", STRICT_ESCAPES).evaluate((c, b) -> {
assertEquals("", c.getKey());
}));
assertEquals("", Expression.compile("${:}", STRICT_ESCAPES).evaluate((c, b) -> {
assertEquals("", c.getKey());
}));

assertEquals("${foo}", Expression.compile("\\${foo}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("${foo}bar", Expression.compile("\\${foo}bar", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("\\$\\{%s}", Expression.compile("\\$\\{%s}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo${bar}", Expression.compile("foo\\${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo\\${bar}", Expression.compile("foo\\\\${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));

assertEquals("foo\\${bar}", Expression.compile("foo\\$${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$${bar}", Expression.compile("foo$\\${bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
assertEquals("foo$$\\{bar}", Expression.compile("foo$$\\{bar}", STRICT_ESCAPES).evaluate((c, b) -> {
}));
}
}

0 comments on commit 9fc9859

Please sign in to comment.