From 4677f319cb0d1091eca184f4f251650fde563a06 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Thu, 27 Jul 2023 20:07:36 +1000 Subject: [PATCH] Fix R4B liquid processor --- .../org/hl7/fhir/r4b/utils/FHIRLexer.java | 283 ++++++++++-------- .../hl7/fhir/r4b/utils/FHIRPathConstant.java | 20 ++ .../hl7/fhir/r4b/utils/FHIRPathEngine.java | 20 +- .../org/hl7/fhir/r4b/utils/LiquidEngine.java | 157 ++++++---- 4 files changed, 302 insertions(+), 178 deletions(-) create mode 100644 org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathConstant.java diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRLexer.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRLexer.java index ac28df765..64285888c 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRLexer.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRLexer.java @@ -48,24 +48,34 @@ import org.hl7.fhir.utilities.Utilities; public class FHIRLexer { public class FHIRLexerException extends FHIRException { - public FHIRLexerException() { - super(); - } + private SourceLocation location; - public FHIRLexerException(String message, Throwable cause) { - super(message, cause); - } +// public FHIRLexerException() { +// super(); +// } +// +// public FHIRLexerException(String message, Throwable cause) { +// super(message, cause); +// } +// +// public FHIRLexerException(String message) { +// super(message); +// } +// +// public FHIRLexerException(Throwable cause) { +// super(cause); +// } - public FHIRLexerException(String message) { + public FHIRLexerException(String message, SourceLocation location) { super(message); + this.location = location; } - public FHIRLexerException(Throwable cause) { - super(cause); + public SourceLocation getLocation() { + return location; } } - private String source; private int cursor; private int currentStart; @@ -75,8 +85,10 @@ public class FHIRLexer { private SourceLocation currentStartLocation; private int id; private String name; - private boolean liquidMode; // in liquid mode, || terminates the expression and hands the parser back to the - // host + private boolean liquidMode; // in liquid mode, || terminates the expression and hands the parser back to the host + private SourceLocation commentLocation; + private boolean metadataFormat; + private boolean allowDoubleQuotes; public FHIRLexer(String source, String name) throws FHIRLexerException { this.source = source == null ? "" : source; @@ -84,35 +96,44 @@ public class FHIRLexer { currentLocation = new SourceLocation(1, 1); next(); } - public FHIRLexer(String source, int i) throws FHIRLexerException { this.source = source; this.cursor = i; currentLocation = new SourceLocation(1, 1); next(); } - + public FHIRLexer(String source, int i, boolean allowDoubleQuotes) throws FHIRLexerException { + this.source = source; + this.cursor = i; + this.allowDoubleQuotes = allowDoubleQuotes; + currentLocation = new SourceLocation(1, 1); + next(); + } + public FHIRLexer(String source, String name, boolean metadataFormat, boolean allowDoubleQuotes) throws FHIRLexerException { + this.source = source == null ? "" : source; + this.name = name == null ? "??" : name; + this.metadataFormat = metadataFormat; + this.allowDoubleQuotes = allowDoubleQuotes; + currentLocation = new SourceLocation(1, 1); + next(); + } public String getCurrent() { return current; } - public SourceLocation getCurrentLocation() { return currentLocation; } public boolean isConstant() { - return !Utilities.noString(current) && ((current.charAt(0) == '\'' || current.charAt(0) == '"') - || current.charAt(0) == '@' || current.charAt(0) == '%' || current.charAt(0) == '-' || current.charAt(0) == '+' - || (current.charAt(0) >= '0' && current.charAt(0) <= '9') || current.equals("true") || current.equals("false") - || current.equals("{}")); + return FHIRPathConstant.isFHIRPathConstant(current); } public boolean isFixedName() { - return current != null && (current.charAt(0) == '`'); + return FHIRPathConstant.isFHIRPathFixedName(current); } public boolean isStringConstant() { - return current.charAt(0) == '\'' || current.charAt(0) == '"' || current.charAt(0) == '`'; + return FHIRPathConstant.isFHIRPathStringConstant(current); } public String take() throws FHIRLexerException { @@ -124,7 +145,7 @@ public class FHIRLexer { public int takeInt() throws FHIRLexerException { String s = current; if (!Utilities.isInteger(s)) - throw error("Found " + current + " expecting an integer"); + throw error("Found "+current+" expecting an integer"); next(); return Integer.parseInt(s); } @@ -139,12 +160,10 @@ public class FHIRLexer { if (current.equals("*") || current.equals("**")) return true; - if ((current.charAt(0) >= 'A' && current.charAt(0) <= 'Z') - || (current.charAt(0) >= 'a' && current.charAt(0) <= 'z')) { - for (int i = 1; i < current.length(); i++) - if (!((current.charAt(1) >= 'A' && current.charAt(1) <= 'Z') - || (current.charAt(1) >= 'a' && current.charAt(1) <= 'z') - || (current.charAt(1) >= '0' && current.charAt(1) <= '9'))) + if ((current.charAt(0) >= 'A' && current.charAt(0) <= 'Z') || (current.charAt(0) >= 'a' && current.charAt(0) <= 'z')) { + for (int i = 1; i < current.length(); i++) + if (!( (current.charAt(1) >= 'A' && current.charAt(1) <= 'Z') || (current.charAt(1) >= 'a' && current.charAt(1) <= 'z') || + (current.charAt(1) >= '0' && current.charAt(1) <= '9'))) return false; return true; } @@ -152,11 +171,11 @@ public class FHIRLexer { } public FHIRLexerException error(String msg) { - return error(msg, currentLocation.toString()); + return error(msg, currentLocation.toString(), currentLocation); } - public FHIRLexerException error(String msg, String location) { - return new FHIRLexerException("Error @" + location + ": " + msg); + public FHIRLexerException error(String msg, String location, SourceLocation loc) { + return new FHIRLexerException("Error @"+location+": "+msg, loc); } public void next() throws FHIRLexerException { @@ -166,34 +185,30 @@ public class FHIRLexer { currentStartLocation = currentLocation; if (cursor < source.length()) { char ch = source.charAt(cursor); - if (ch == '!' || ch == '>' || ch == '<' || ch == ':' || ch == '-' || ch == '=') { + if (ch == '!' || ch == '>' || ch == '<' || ch == ':' || ch == '-' || ch == '=') { cursor++; - if (cursor < source.length() - && (source.charAt(cursor) == '=' || source.charAt(cursor) == '~' || source.charAt(cursor) == '-') - || (ch == '-' && source.charAt(cursor) == '>')) + if (cursor < source.length() && (source.charAt(cursor) == '=' || source.charAt(cursor) == '~' || source.charAt(cursor) == '-') || (ch == '-' && source.charAt(cursor) == '>')) cursor++; current = source.substring(currentStart, cursor); - } else if (ch == '.') { + } else if (ch == '.' ) { cursor++; - if (cursor < source.length() && (source.charAt(cursor) == '.')) + if (cursor < source.length() && (source.charAt(cursor) == '.')) cursor++; current = source.substring(currentStart, cursor); } else if (ch >= '0' && ch <= '9') { - cursor++; + cursor++; boolean dotted = false; - while (cursor < source.length() && ((source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') - || (source.charAt(cursor) == '.') && !dotted)) { + while (cursor < source.length() && ((source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || (source.charAt(cursor) == '.') && !dotted)) { if (source.charAt(cursor) == '.') dotted = true; cursor++; } - if (source.charAt(cursor - 1) == '.') + if (source.charAt(cursor-1) == '.') cursor--; current = source.substring(currentStart, cursor); - } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { - while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') - || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') - || (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == '_')) + } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { + while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') || + (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == '_')) cursor++; current = source.substring(currentStart, cursor); } else if (ch == '%') { @@ -204,19 +219,20 @@ public class FHIRLexer { cursor++; cursor++; } else - while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') - || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') - || (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == ':' - || source.charAt(cursor) == '-')) - cursor++; + while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') || + (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == ':' || source.charAt(cursor) == '-')) + cursor++; current = source.substring(currentStart, cursor); } else if (ch == '/') { cursor++; if (cursor < source.length() && (source.charAt(cursor) == '/')) { - // this is en error - should already have been skipped - error("This shouldn't happen?"); + // we've run into metadata + cursor++; + cursor++; + current = source.substring(currentStart, cursor); + } else { + current = source.substring(currentStart, cursor); } - current = source.substring(currentStart, cursor); } else if (ch == '$') { cursor++; while (cursor < source.length() && (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z')) @@ -228,42 +244,42 @@ public class FHIRLexer { if (ch == '}') cursor++; current = source.substring(currentStart, cursor); - } else if (ch == '"') { + } else if (ch == '"' && allowDoubleQuotes) { cursor++; boolean escape = false; while (cursor < source.length() && (escape || source.charAt(cursor) != '"')) { if (escape) escape = false; - else + else escape = (source.charAt(cursor) == '\\'); cursor++; } if (cursor == source.length()) throw error("Unterminated string"); cursor++; - current = "\"" + source.substring(currentStart + 1, cursor - 1) + "\""; + current = "\""+source.substring(currentStart+1, cursor-1)+"\""; } else if (ch == '`') { cursor++; boolean escape = false; while (cursor < source.length() && (escape || source.charAt(cursor) != '`')) { if (escape) escape = false; - else + else escape = (source.charAt(cursor) == '\\'); cursor++; } if (cursor == source.length()) throw error("Unterminated string"); cursor++; - current = "`" + source.substring(currentStart + 1, cursor - 1) + "`"; - } else if (ch == '\'') { + current = "`"+source.substring(currentStart+1, cursor-1)+"`"; + } else if (ch == '\''){ cursor++; char ech = ch; boolean escape = false; while (cursor < source.length() && (escape || source.charAt(cursor) != ech)) { if (escape) escape = false; - else + else escape = (source.charAt(cursor) == '\\'); cursor++; } @@ -272,32 +288,32 @@ public class FHIRLexer { cursor++; current = source.substring(currentStart, cursor); if (ech == '\'') - current = "\'" + current.substring(1, current.length() - 1) + "\'"; + current = "\'"+current.substring(1, current.length() - 1)+"\'"; } else if (ch == '`') { cursor++; boolean escape = false; while (cursor < source.length() && (escape || source.charAt(cursor) != '`')) { if (escape) escape = false; - else + else escape = (source.charAt(cursor) == '\\'); cursor++; } if (cursor == source.length()) throw error("Unterminated string"); cursor++; - current = "`" + source.substring(currentStart + 1, cursor - 1) + "`"; + current = "`"+source.substring(currentStart+1, cursor-1)+"`"; } else if (ch == '|' && liquidMode) { cursor++; ch = source.charAt(cursor); if (ch == '|') cursor++; current = source.substring(currentStart, cursor); - } else if (ch == '@') { + } else if (ch == '@'){ int start = cursor; cursor++; while (cursor < source.length() && isDateChar(source.charAt(cursor), start)) - cursor++; + cursor++; current = source.substring(currentStart, cursor); } else { // if CharInSet(ch, ['.', ',', '(', ')', '=', '$']) then cursor++; @@ -308,28 +324,35 @@ public class FHIRLexer { private void skipWhitespaceAndComments() { comments.clear(); + commentLocation = null; boolean last13 = false; boolean done = false; while (cursor < source.length() && !done) { - if (cursor < source.length() - 1 && "//".equals(source.substring(cursor, cursor + 2))) { - int start = cursor + 2; - while (cursor < source.length() && !((source.charAt(cursor) == '\r') || source.charAt(cursor) == '\n')) { - cursor++; + if (cursor < source.length() -1 && "//".equals(source.substring(cursor, cursor+2)) && !isMetadataStart()) { + if (commentLocation == null) { + commentLocation = currentLocation.copy(); + } + int start = cursor+2; + while (cursor < source.length() && !((source.charAt(cursor) == '\r') || source.charAt(cursor) == '\n')) { + cursor++; } comments.add(source.substring(start, cursor).trim()); - } else if (cursor < source.length() - 1 && "/*".equals(source.substring(cursor, cursor + 2))) { - int start = cursor + 2; - while (cursor < source.length() - 1 && !"*/".equals(source.substring(cursor, cursor + 2))) { - last13 = currentLocation.checkChar(source.charAt(cursor), last13); - cursor++; + } else if (cursor < source.length() - 1 && "/*".equals(source.substring(cursor, cursor+2))) { + if (commentLocation == null) { + commentLocation = currentLocation.copy(); } - if (cursor >= source.length() - 1) { + int start = cursor+2; + while (cursor < source.length() - 1 && !"*/".equals(source.substring(cursor, cursor+2))) { + last13 = currentLocation.checkChar(source.charAt(cursor), last13); + cursor++; + } + if (cursor >= source.length() -1) { error("Unfinished comment"); } else { comments.add(source.substring(start, cursor).trim()); cursor = cursor + 2; } - } else if (Character.isWhitespace(source.charAt(cursor))) { + } else if (Utilities.isWhitespace(source.charAt(cursor))) { last13 = currentLocation.checkChar(source.charAt(cursor), last13); cursor++; } else { @@ -337,32 +360,30 @@ public class FHIRLexer { } } } - - private boolean isDateChar(char ch, int start) { - int eot = source.charAt(start + 1) == 'T' ? 10 : 20; - - return ch == '-' || ch == ':' || ch == 'T' || ch == '+' || ch == 'Z' || Character.isDigit(ch) - || (cursor - start == eot && ch == '.' && cursor < source.length() - 1 - && Character.isDigit(source.charAt(cursor + 1))); + + private boolean isMetadataStart() { + return metadataFormat && cursor < source.length() - 2 && "///".equals(source.substring(cursor, cursor+3)); + } + + private boolean isDateChar(char ch,int start) { + int eot = source.charAt(start+1) == 'T' ? 10 : 20; + + return ch == '-' || ch == ':' || ch == 'T' || ch == '+' || ch == 'Z' || Character.isDigit(ch) || (cursor-start == eot && ch == '.' && cursor < source.length()-1&& Character.isDigit(source.charAt(cursor+1))); } - public boolean isOp() { return ExpressionNode.Operation.fromCode(current) != null; } - public boolean done() { return currentStart >= source.length(); } - public int nextId() { id++; return id; } - public SourceLocation getCurrentStartLocation() { return currentStartLocation; } - + // special case use public void setCurrent(String current) { this.current = current; @@ -387,7 +408,7 @@ public class FHIRLexer { if (hasComments()) { String s = comments.get(0); comments.remove(0); - return s; + return s; } else { return null; } @@ -396,32 +417,31 @@ public class FHIRLexer { public boolean hasToken(String kw) { return !done() && kw.equals(current); } - public boolean hasToken(String... names) { - if (done()) + if (done()) return false; for (String s : names) if (s.equals(current)) return true; return false; } - + public void token(String kw) throws FHIRLexerException { - if (!kw.equals(current)) - throw error("Found \"" + current + "\" expecting \"" + kw + "\""); + if (!kw.equals(current)) + throw error("Found \""+current+"\" expecting \""+kw+"\""); next(); } - + public String readConstant(String desc) throws FHIRLexerException { if (!isStringConstant()) - throw error("Found " + current + " expecting \"[" + desc + "]\""); + throw error("Found "+current+" expecting \"["+desc+"]\""); return processConstant(take()); } public String readFixedName(String desc) throws FHIRLexerException { if (!isFixedName()) - throw error("Found " + current + " expecting \"[" + desc + "]\""); + throw error("Found "+current+" expecting \"["+desc+"]\""); return processFixedName(take()); } @@ -429,21 +449,21 @@ public class FHIRLexer { public String processConstant(String s) throws FHIRLexerException { StringBuilder b = new StringBuilder(); int i = 1; - while (i < s.length() - 1) { + while (i < s.length()-1) { char ch = s.charAt(i); if (ch == '\\') { i++; switch (s.charAt(i)) { - case 't': + case 't': b.append('\t'); break; case 'r': b.append('\r'); break; - case 'n': + case 'n': b.append('\n'); break; - case 'f': + case 'f': b.append('\f'); break; case '\'': @@ -455,20 +475,20 @@ public class FHIRLexer { case '`': b.append('`'); break; - case '\\': + case '\\': b.append('\\'); break; - case '/': + case '/': b.append('/'); break; case 'u': i++; - int uc = Integer.parseInt(s.substring(i, i + 4), 16); + int uc = Integer.parseInt(s.substring(i, i+4), 16); b.append((char) uc); i = i + 4; break; default: - throw new FHIRLexerException("Unknown character escape \\" + s.charAt(i)); + throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation); } } else { b.append(ch); @@ -477,25 +497,25 @@ public class FHIRLexer { } return b.toString(); } - + public String processFixedName(String s) throws FHIRLexerException { StringBuilder b = new StringBuilder(); int i = 1; - while (i < s.length() - 1) { + while (i < s.length()-1) { char ch = s.charAt(i); if (ch == '\\') { i++; switch (s.charAt(i)) { - case 't': + case 't': b.append('\t'); break; case 'r': b.append('\r'); break; - case 'n': + case 'n': b.append('\n'); break; - case 'f': + case 'f': b.append('\f'); break; case '\'': @@ -504,20 +524,20 @@ public class FHIRLexer { case '"': b.append('"'); break; - case '\\': + case '\\': b.append('\\'); break; - case '/': + case '/': b.append('/'); break; case 'u': i++; - int uc = Integer.parseInt(s.substring(i, i + 4), 16); + int uc = Integer.parseInt(s.substring(i, i+4), 16); b.append((char) uc); i = i + 4; break; default: - throw new FHIRLexerException("Unknown character escape \\" + s.charAt(i)); + throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation); } } else { b.append(ch); @@ -530,9 +550,8 @@ public class FHIRLexer { public void skipToken(String token) throws FHIRLexerException { if (getCurrent().equals(token)) next(); - + } - public String takeDottedToken() throws FHIRLexerException { StringBuilder b = new StringBuilder(); b.append(take()); @@ -542,21 +561,43 @@ public class FHIRLexer { } return b.toString(); } - + public int getCurrentStart() { return currentStart; } - public String getSource() { return source; } - public boolean isLiquidMode() { return liquidMode; } - public void setLiquidMode(boolean liquidMode) { this.liquidMode = liquidMode; } - + public SourceLocation getCommentLocation() { + return this.commentLocation; + } + public boolean isMetadataFormat() { + return metadataFormat; + } + public void setMetadataFormat(boolean metadataFormat) { + this.metadataFormat = metadataFormat; + } + public List cloneComments() { + List res = new ArrayList<>(); + res.addAll(getComments()); + return res; + } + public String tokenWithTrailingComment(String token) { + int line = getCurrentLocation().getLine(); + token(token); + if (getComments().size() > 0 && getCommentLocation().getLine() == line) { + return getFirstComment(); + } else { + return null; + } + } + public boolean isAllowDoubleQuotes() { + return allowDoubleQuotes; + } } \ No newline at end of file diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathConstant.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathConstant.java new file mode 100644 index 000000000..464bf7bc6 --- /dev/null +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathConstant.java @@ -0,0 +1,20 @@ +package org.hl7.fhir.r4b.utils; + +import org.hl7.fhir.utilities.Utilities; + +public class FHIRPathConstant { + + public static boolean isFHIRPathConstant(String string) { + return !Utilities.noString(string) && ((string.charAt(0) == '\'' || string.charAt(0) == '"') || string.charAt(0) == '@' || string.charAt(0) == '%' || + string.charAt(0) == '-' || string.charAt(0) == '+' || (string.charAt(0) >= '0' && string.charAt(0) <= '9') || + string.equals("true") || string.equals("false") || string.equals("{}")); + } + + public static boolean isFHIRPathFixedName(String string) { + return string != null && (string.charAt(0) == '`'); + } + + public static boolean isFHIRPathStringConstant(String string) { + return string.charAt(0) == '\'' || string.charAt(0) == '"' || string.charAt(0) == '`'; + } +} diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathEngine.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathEngine.java index 73a81aacf..589b8585f 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathEngine.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/FHIRPathEngine.java @@ -282,6 +282,7 @@ public class FHIRPathEngine { // host private boolean doNotEnforceAsSingletonRule; private boolean doNotEnforceAsCaseSensitive; + private boolean allowDoubleQuotes; // if the fhir path expressions are allowed to use constants beyond those // defined in the specification @@ -531,7 +532,7 @@ public class FHIRPathEngine { } public ExpressionNode parse(String path, String name) throws FHIRLexerException { - FHIRLexer lexer = new FHIRLexer(path, name); + FHIRLexer lexer = new FHIRLexer(path, name, false, allowDoubleQuotes); if (lexer.done()) { throw lexer.error("Path cannot be empty"); } @@ -572,7 +573,7 @@ public class FHIRPathEngine { * @throws Exception */ public ExpressionNodeWithOffset parsePartial(String path, int i) throws FHIRLexerException { - FHIRLexer lexer = new FHIRLexer(path, i); + FHIRLexer lexer = new FHIRLexer(path, i, allowDoubleQuotes); if (lexer.done()) { throw lexer.error("Path cannot be empty"); } @@ -1400,8 +1401,7 @@ public class FHIRPathEngine { private boolean checkParamCount(FHIRLexer lexer, SourceLocation location, ExpressionNode exp, int count) throws FHIRLexerException { if (exp.getParameters().size() != count) { - throw lexer.error("The function \"" + exp.getName() + "\" requires " + Integer.toString(count) + " parameters", - location.toString()); + throw lexer.error("The function \"" + exp.getName() + "\" requires " + Integer.toString(count) + " parameters"); } return true; } @@ -1410,7 +1410,7 @@ public class FHIRPathEngine { int countMax) throws FHIRLexerException { if (exp.getParameters().size() < countMin || exp.getParameters().size() > countMax) { throw lexer.error("The function \"" + exp.getName() + "\" requires between " + Integer.toString(countMin) - + " and " + Integer.toString(countMax) + " parameters", location.toString()); + + " and " + Integer.toString(countMax) + " parameters"); } return true; } @@ -6722,4 +6722,14 @@ public class FHIRPathEngine { this.liquidMode = liquidMode; } + public ProfileUtilities getProfileUtilities() { + return profileUtilities; + } + + public boolean isAllowDoubleQuotes() { + return allowDoubleQuotes; + } + public void setAllowDoubleQuotes(boolean allowDoubleQuotes) { + this.allowDoubleQuotes = allowDoubleQuotes; + } } diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/LiquidEngine.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/LiquidEngine.java index cb746bbfe..831edd7ef 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/LiquidEngine.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/LiquidEngine.java @@ -50,6 +50,7 @@ import org.hl7.fhir.r4b.model.ValueSet; import org.hl7.fhir.r4b.utils.FHIRPathEngine.ExpressionNodeWithOffset; import org.hl7.fhir.r4b.utils.FHIRPathEngine.IEvaluationContext; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.xhtml.NodeType; import org.hl7.fhir.utilities.xhtml.XhtmlNode; @@ -70,17 +71,27 @@ public class LiquidEngine implements IEvaluationContext { private class LiquidEngineContext { private Object externalContext; - private Map vars = new HashMap<>(); + private Map loopVars = new HashMap<>(); + private Map globalVars = new HashMap<>(); public LiquidEngineContext(Object externalContext) { super(); this.externalContext = externalContext; + globalVars = new HashMap<>(); + } + + public LiquidEngineContext(Object externalContext, LiquidEngineContext existing) { + super(); + this.externalContext = externalContext; + loopVars.putAll(existing.loopVars); + globalVars = existing.globalVars; } public LiquidEngineContext(LiquidEngineContext existing) { super(); externalContext = existing.externalContext; - vars.putAll(existing.vars); + loopVars.putAll(existing.loopVars); + globalVars = existing.globalVars; } } @@ -120,6 +131,7 @@ public class LiquidEngine implements IEvaluationContext { } return b.toString(); } + private abstract class LiquidNode { protected void closeUp() { @@ -148,10 +160,10 @@ public class LiquidEngine implements IEvaluationContext { } } - + private enum LiquidFilter { PREPEND; - + public static LiquidFilter fromCode(String code) { if ("prepend".equals(code)) { return PREPEND; @@ -163,15 +175,14 @@ public class LiquidEngine implements IEvaluationContext { private class LiquidExpressionNode { private LiquidFilter filter; // null at root private ExpressionNode expression; // null for some filters - public LiquidExpressionNode(LiquidFilter filter, ExpressionNode expression) { super(); this.filter = filter; this.expression = expression; } - + } - + private class LiquidStatement extends LiquidNode { private String statement; private List compiled = new ArrayList<>(); @@ -179,7 +190,7 @@ public class LiquidEngine implements IEvaluationContext { @Override public void evaluate(StringBuilder b, Base resource, LiquidEngineContext ctxt) throws FHIRException { if (compiled.size() == 0) { - FHIRLexer lexer = new FHIRLexer(statement, "liquid statement"); + FHIRLexer lexer = new FHIRLexer(statement, "liquid statement", false, true); lexer.setLiquidMode(true); compiled.add(new LiquidExpressionNode(null, engine.parse(lexer))); while (!lexer.done()) { @@ -188,7 +199,7 @@ public class LiquidEngine implements IEvaluationContext { String f = lexer.getCurrent(); LiquidFilter filter = LiquidFilter.fromCode(f); if (filter == null) { - lexer.error("Unknown Liquid filter '" + f + "'"); + lexer.error(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_FILTER, f)); } lexer.next(); if (!lexer.done() && lexer.getCurrent().equals(":")) { @@ -198,21 +209,20 @@ public class LiquidEngine implements IEvaluationContext { compiled.add(new LiquidExpressionNode(filter, null)); } } else { - lexer.error("Unexpected syntax parsing liquid statement"); + lexer.error(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_SYNTAX)); } } } - + String t = null; for (LiquidExpressionNode i : compiled) { if (i.filter == null) { // first t = stmtToString(ctxt, engine.evaluate(ctxt, resource, resource, resource, i.expression)); - } else - switch (i.filter) { - case PREPEND: - t = stmtToString(ctxt, engine.evaluate(ctxt, resource, resource, resource, i.expression)) + t; - break; - } + } else switch (i.filter) { + case PREPEND: + t = stmtToString(ctxt, engine.evaluate(ctxt, resource, resource, resource, i.expression)) + t; + break; + } } b.append(t); } @@ -221,10 +231,7 @@ public class LiquidEngine implements IEvaluationContext { StringBuilder b = new StringBuilder(); boolean first = true; for (Base i : items) { - if (first) - first = false; - else - b.append(", "); + if (first) first = false; else b.append(", "); String s = renderingSupport != null ? renderingSupport.renderForLiquid(ctxt.externalContext, i) : null; b.append(s != null ? s : engine.convertToString(i)); } @@ -312,6 +319,30 @@ public class LiquidEngine implements IEvaluationContext { } } + private class LiquidAssign extends LiquidNode { + private String varName; + private String expression; + private ExpressionNode compiled; + @Override + public void evaluate(StringBuilder b, Base resource, LiquidEngineContext ctxt) throws FHIRException { + if (compiled == null) { + boolean dbl = engine.isAllowDoubleQuotes(); + engine.setAllowDoubleQuotes(true); + ExpressionNodeWithOffset po = engine.parsePartial(expression, 0); + compiled = po.getNode(); + engine.setAllowDoubleQuotes(dbl); + } + List list = engine.evaluate(ctxt, resource, resource, resource, compiled); + if (list.isEmpty()) { + ctxt.globalVars.remove(varName); + } else if (list.size() == 1) { + ctxt.globalVars.put(varName, list.get(0)); + } else { + throw new Error("Assign returned a list?"); + } + } + } + private class LiquidFor extends LiquidNode { private String varName; private String condition; @@ -349,8 +380,11 @@ public class LiquidEngine implements IEvaluationContext { } if (limit >= 0 && i == limit) { break; + } + if (lctxt.globalVars.containsKey(varName)) { + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ALREADY_ASSIGNED, varName)); } - lctxt.vars.put(varName, o); + lctxt.loopVars.put(varName, o); boolean wantBreak = false; for (LiquidNode n : body) { try { @@ -379,7 +413,7 @@ public class LiquidEngine implements IEvaluationContext { } else if (cnt.startsWith("limit")) { cnt = cnt.substring(5).trim(); if (!cnt.startsWith(":")) { - throw new FHIRException("Exception evaluating " + src + ": limit is not followed by ':'"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_COLON, src)); } cnt = cnt.substring(1).trim(); int i = 0; @@ -387,14 +421,14 @@ public class LiquidEngine implements IEvaluationContext { i++; } if (i == 0) { - throw new FHIRException("Exception evaluating " + src + ": limit is not followed by a number"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NUMBER, src)); } limit = Integer.parseInt(cnt.substring(0, i)); cnt = cnt.substring(i); } else if (cnt.startsWith("offset")) { cnt = cnt.substring(6).trim(); if (!cnt.startsWith(":")) { - throw new FHIRException("Exception evaluating " + src + ": limit is not followed by ':'"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_COLON, src)); } cnt = cnt.substring(1).trim(); int i = 0; @@ -402,14 +436,14 @@ public class LiquidEngine implements IEvaluationContext { i++; } if (i == 0) { - throw new FHIRException("Exception evaluating " + src + ": limit is not followed by a number"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NUMBER, src)); } offset = Integer.parseInt(cnt.substring(0, i)); cnt = cnt.substring(i); } else { - throw new FHIRException("Exception evaluating " + src + ": unexpected content at " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_UNEXPECTED, cnt)); } - } + } } } @@ -422,9 +456,9 @@ public class LiquidEngine implements IEvaluationContext { String src = includeResolver.fetchInclude(LiquidEngine.this, page); LiquidParser parser = new LiquidParser(src); LiquidDocument doc = parser.parse(page); - LiquidEngineContext nctxt = new LiquidEngineContext(ctxt.externalContext); + LiquidEngineContext nctxt = new LiquidEngineContext(ctxt.externalContext, ctxt); Tuple incl = new Tuple(); - nctxt.vars.put("include", incl); + nctxt.loopVars.put("include", incl); for (String s : params.keySet()) { incl.addProperty(s, engine.evaluate(ctxt, resource, resource, resource, params.get(s))); } @@ -481,13 +515,11 @@ public class LiquidEngine implements IEvaluationContext { cnt = "," + cnt.substring(5).trim(); while (!Utilities.noString(cnt)) { if (!cnt.startsWith(",")) { - throw new FHIRException( - "Script " + name + ": Script " + name + ": Found " + cnt.charAt(0) + " expecting ',' parsing cycle"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_EXPECTING, name, cnt.charAt(0), ',')); } cnt = cnt.substring(1).trim(); if (!cnt.startsWith("\"")) { - throw new FHIRException( - "Script " + name + ": Script " + name + ": Found " + cnt.charAt(0) + " expecting '\"' parsing cycle"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_EXPECTING, name, cnt.charAt(0), '"')); } cnt = cnt.substring(1); int i = 0; @@ -495,7 +527,7 @@ public class LiquidEngine implements IEvaluationContext { i++; } if (i == cnt.length()) { - throw new FHIRException("Script " + name + ": Script " + name + ": Found unterminated string parsing cycle"); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_UNTERMINATED, name)); } res.list.add(cnt.substring(0, i)); cnt = cnt.substring(i + 1).trim(); @@ -527,9 +559,10 @@ public class LiquidEngine implements IEvaluationContext { list.add(parseCycle(cnt)); else if (cnt.startsWith("include ")) list.add(parseInclude(cnt.substring(7).trim())); + else if (cnt.startsWith("assign ")) + list.add(parseAssign(cnt.substring(6).trim())); else - throw new FHIRException( - "Script " + name + ": Script " + name + ": Unknown flow control statement " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_FLOW_STMT,name, cnt)); } else { // next2() == '{' list.add(parseStatement()); } @@ -543,8 +576,7 @@ public class LiquidEngine implements IEvaluationContext { n.closeUp(); if (terminators.length > 0) if (!isTerminator(close, terminators)) - throw new FHIRException( - "Script " + name + ": Script " + name + ": Found end of script looking for " + terminators); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_NOEND, name, terminators)); return close; } @@ -588,7 +620,7 @@ public class LiquidEngine implements IEvaluationContext { while (i < cnt.length() && !Character.isWhitespace(cnt.charAt(i))) i++; if (i == cnt.length() || i == 0) - throw new FHIRException("Script " + name + ": Error reading include: " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name + ": Error reading include: " + cnt)); LiquidInclude res = new LiquidInclude(); res.page = cnt.substring(0, i); while (i < cnt.length() && Character.isWhitespace(cnt.charAt(i))) @@ -598,10 +630,10 @@ public class LiquidEngine implements IEvaluationContext { while (i < cnt.length() && cnt.charAt(i) != '=') i++; if (i >= cnt.length() || j == i) - throw new FHIRException("Script " + name + ": Error reading include: " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name, cnt)); String n = cnt.substring(j, i); if (res.params.containsKey(n)) - throw new FHIRException("Script " + name + ": Error reading include: " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name, cnt)); i++; ExpressionNodeWithOffset t = engine.parsePartial(cnt, i); i = t.getOffset(); @@ -618,13 +650,16 @@ public class LiquidEngine implements IEvaluationContext { i++; LiquidFor res = new LiquidFor(); res.varName = cnt.substring(0, i); + if ("include".equals(res.varName)) { + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ILLEGAL, res.varName)); + } while (Character.isWhitespace(cnt.charAt(i))) i++; int j = i; while (!Character.isWhitespace(cnt.charAt(i))) i++; if (!"in".equals(cnt.substring(j, i))) - throw new FHIRException("Script " + name + ": Script " + name + ": Error reading loop: " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_LOOP, name, cnt)); res.condition = cnt.substring(i).trim(); parseList(res.body, false, new String[] { "endloop" }); return res; @@ -636,13 +671,16 @@ public class LiquidEngine implements IEvaluationContext { i++; LiquidFor res = new LiquidFor(); res.varName = cnt.substring(0, i); + if ("include".equals(res.varName)) { + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ILLEGAL, res.varName)); + } while (Character.isWhitespace(cnt.charAt(i))) i++; int j = i; while (!Character.isWhitespace(cnt.charAt(i))) i++; if (!"in".equals(cnt.substring(j, i))) - throw new FHIRException("Script " + name + ": Script " + name + ": Error reading loop: " + cnt); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_LOOP, name, cnt)); res.condition = cnt.substring(i).trim(); String term = parseList(res.body, true, new String[] { "endfor", "else" }); if ("else".equals(term)) { @@ -651,6 +689,21 @@ public class LiquidEngine implements IEvaluationContext { return res; } + private LiquidNode parseAssign(String cnt) throws FHIRException { + int i = 0; + while (!Character.isWhitespace(cnt.charAt(i))) + i++; + LiquidAssign res = new LiquidAssign(); + res.varName = cnt.substring(0, i); + while (Character.isWhitespace(cnt.charAt(i))) + i++; + int j = i; + while (!Character.isWhitespace(cnt.charAt(i))) + i++; + res.expression = cnt.substring(i).trim(); + return res; + } + private String parseTag(char ch) throws FHIRException { grab(); grab(); @@ -659,7 +712,7 @@ public class LiquidEngine implements IEvaluationContext { b.append(grab()); } if (!(next1() == '%' && next2() == '}')) - throw new FHIRException("Script " + name + ": Unterminated Liquid statement {% " + b.toString()); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NOTERM, name, "{% " + b.toString())); grab(); grab(); return b.toString().trim(); @@ -673,7 +726,7 @@ public class LiquidEngine implements IEvaluationContext { b.append(grab()); } if (!(next1() == '}' && next2() == '}')) - throw new FHIRException("Script " + name + ": Unterminated Liquid statement {{ " + b.toString()); + throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NOTERM, name, "{{ " + b.toString())); grab(); grab(); LiquidStatement res = new LiquidStatement(); @@ -686,8 +739,10 @@ public class LiquidEngine implements IEvaluationContext { @Override public List resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException { LiquidEngineContext ctxt = (LiquidEngineContext) appContext; - if (ctxt.vars.containsKey(name)) - return new ArrayList(Arrays.asList(ctxt.vars.get(name))); + if (ctxt.loopVars.containsKey(name)) + return new ArrayList(Arrays.asList(ctxt.loopVars.get(name))); + if (ctxt.globalVars.containsKey(name)) + return new ArrayList(Arrays.asList(ctxt.globalVars.get(name))); if (externalHostServices == null) return new ArrayList(); return externalHostServices.resolveConstant(ctxt.externalContext, name, beforeContext); @@ -716,8 +771,7 @@ public class LiquidEngine implements IEvaluationContext { } @Override - public TypeDetails checkFunction(Object appContext, String functionName, List parameters) - throws PathEngineException { + public TypeDetails checkFunction(Object appContext, String functionName, List parameters) throws PathEngineException { if (externalHostServices == null) return null; LiquidEngineContext ctxt = (LiquidEngineContext) appContext; @@ -725,8 +779,7 @@ public class LiquidEngine implements IEvaluationContext { } @Override - public List executeFunction(Object appContext, List focus, String functionName, - List> parameters) { + public List executeFunction(Object appContext, List focus, String functionName, List> parameters) { if (externalHostServices == null) return null; LiquidEngineContext ctxt = (LiquidEngineContext) appContext;