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 25, 2024
1 parent f3eefdd commit 25912c6
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 11 deletions.
103 changes: 92 additions & 11 deletions expression/src/main/java/io/smallrye/common/expression/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ public static Expression compile(String string, Flag... flags) {
public static Expression compile(String string, EnumSet<Flag> flags) {
Assert.checkNotNullParam("string", string);
Assert.checkNotNullParam("flags", flags);

if (flags.contains(Flag.STRICT_ESCAPES) && flags.contains(Flag.ESCAPES)) {
throw Messages.msg.mutuallyExclusiveEscapeFlags();
}

if (flags.contains(Flag.STRICT_ESCAPES) && flags.contains(Flag.MINI_EXPRS)) {
throw Messages.msg.incompatibleMiniExpsStrictEscapes();
}

final Node content;
final Itr itr;
if (flags.contains(Flag.NO_TRIM)) {
Expand Down Expand Up @@ -260,6 +269,9 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
}
// check to see if it's a dangling $
if (!itr.hasNext()) {
if (flags.contains(Flag.STRICT_ESCAPES)) {
continue;
}
if (!flags.contains(Flag.LENIENT_SYNTAX)) {
// TP 2
throw invalidExpressionSyntax(itr.getStr(), idx);
Expand All @@ -269,16 +281,16 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
start = itr.getNextIdx();
continue;
}
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx));
}
// next char should be an expression starter of some sort
idx = itr.getNextIdx();
ch = itr.next();
switch (ch) {
case '{': {
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx - 1));
}
// ${
boolean general = flags.contains(Flag.GENERAL_EXPANSION) && itr.hasNext() && itr.peekNext() == '{';
// consume double-{
Expand Down Expand Up @@ -407,18 +419,47 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
}
case '$': {
// $$
if (flags.contains(Flag.MINI_EXPRS)) {
// TP 13
list.add(new ExpressionNode(false, LiteralNode.DOLLAR, Node.NULL));
if (flags.contains(Flag.STRICT_ESCAPES)) {
if (itr.hasNext()) {
switch (itr.peekNext()) {
case '{':
list.add(new LiteralNode(itr.getStr(), start, idx));
break;
case '$':
list.add(new LiteralNode(itr.getStr(), start, idx));
itr.prev();
break;
default:
list.add(new LiteralNode(itr.getStr(), start, itr.getNextIdx()));
break;
}
} else {
list.add(new LiteralNode(itr.getStr(), start, itr.getNextIdx()));
}
} else {
// just resolve $$ to $
// TP 14
list.add(LiteralNode.DOLLAR);
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx - 1));
}
if (flags.contains(Flag.MINI_EXPRS)) {
// TP 13
list.add(new ExpressionNode(false, LiteralNode.DOLLAR, Node.NULL));
} else {
// just resolve $$ to $
// TP 14
list.add(LiteralNode.DOLLAR);
}
}
start = itr.getNextIdx();
continue;
}
case '}': {
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx - 1));
}
// $}
if (flags.contains(Flag.MINI_EXPRS)) {
// TP 15
Expand Down Expand Up @@ -452,6 +493,11 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
//throw Assert.unreachableCode();
}
case ':': {
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx - 1));
}
// $:
if (flags.contains(Flag.MINI_EXPRS)) {
// $: is an expression
Expand Down Expand Up @@ -485,7 +531,22 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
}
//throw Assert.unreachableCode();
}
case '\\': {
if (flags.contains(Flag.STRICT_ESCAPES)) {
if (itr.hasNext() && itr.peekNext() == '$') {
list.add(new LiteralNode(itr.getStr(), start, idx));
start = itr.getNextIdx();
itr.next();
continue;
}
}
}
default: {
// enqueue what we have acquired so far
if (idx > start) {
// TP 4
list.add(new LiteralNode(itr.getStr(), start, idx - 1));
}
// $ followed by anything else
if (flags.contains(Flag.MINI_EXPRS)) {
// TP 25
Expand Down Expand Up @@ -614,6 +675,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 +770,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 @@ -17,4 +17,10 @@ interface Messages {

@Message(id = 1002, value = "No system property found named \"%s\"")
IllegalArgumentException unresolvedSystemProperty(String name);

@Message(id = 1003, value = "Flags ESCAPES and STRICT_ESCAPES are mutually exclusive")
IllegalArgumentException mutuallyExclusiveEscapeFlags();

@Message(id = 1004, value = "Flags MINI_EXPRS and STRICT_ESCAPES are incompatible")
IllegalArgumentException incompatibleMiniExpsStrictEscapes();
}
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 25912c6

Please sign in to comment.