use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method parse.
// --- public API -------------------------------------------------------
/**
* Parse a path for later use using execute
*
* @param path
* @return
* @throws PathEngineException
* @throws Exception
*/
public ExpressionNode parse(String path) throws FHIRLexerException {
FHIRLexer lexer = new FHIRLexer(path);
if (lexer.done())
throw lexer.error("Path cannot be empty");
ExpressionNode result = parseExpression(lexer, true);
if (!lexer.done())
throw lexer.error("Premature ExpressionNode termination at unexpected token \"" + lexer.getCurrent() + "\"");
result.check();
return result;
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method gatherPrecedence.
private ExpressionNode gatherPrecedence(FHIRLexer lexer, ExpressionNode start, EnumSet<Operation> ops) {
assert (start.isProximal());
// is there anything to do?
boolean work = false;
ExpressionNode focus = start.getOpNext();
if (ops.contains(start.getOperation())) {
while (focus != null && focus.getOperation() != null) {
work = work || !ops.contains(focus.getOperation());
focus = focus.getOpNext();
}
} else {
while (focus != null && focus.getOperation() != null) {
work = work || ops.contains(focus.getOperation());
focus = focus.getOpNext();
}
}
if (!work)
return start;
// entry point: tricky
ExpressionNode group;
if (ops.contains(start.getOperation())) {
group = newGroup(lexer, start);
group.setProximal(true);
focus = start;
start = group;
} else {
ExpressionNode node = start;
focus = node.getOpNext();
while (!ops.contains(focus.getOperation())) {
node = focus;
focus = focus.getOpNext();
}
group = newGroup(lexer, focus);
node.setOpNext(group);
}
// focus points at the group.group
do {
// run until we find the end of the sequence
while (ops.contains(focus.getOperation())) focus = focus.getOpNext();
if (focus.getOperation() != null) {
group.setOperation(focus.getOperation());
group.setOpNext(focus.getOpNext());
focus.setOperation(null);
focus.setOpNext(null);
// now look for another sequence, and start it
ExpressionNode node = group;
focus = group.getOpNext();
if (focus != null) {
while (focus == null && !ops.contains(focus.getOperation())) {
node = focus;
focus = focus.getOpNext();
}
if (focus != null) {
// && (focus.Operation in Ops) - must be true
group = newGroup(lexer, focus);
node.setOpNext(group);
}
}
}
} while (focus != null && focus.getOperation() != null);
return start;
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method newGroup.
private ExpressionNode newGroup(FHIRLexer lexer, ExpressionNode next) {
ExpressionNode result = new ExpressionNode(lexer.nextId());
result.setKind(Kind.Group);
result.setGroup(next);
result.getGroup().setProximal(true);
return result;
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method parseExpression.
private ExpressionNode parseExpression(FHIRLexer lexer, boolean proximal) throws FHIRLexerException {
ExpressionNode result = new ExpressionNode(lexer.nextId());
SourceLocation c = lexer.getCurrentStartLocation();
result.setStart(lexer.getCurrentLocation());
// special:
if (lexer.getCurrent().equals("-")) {
lexer.take();
lexer.setCurrent("-" + lexer.getCurrent());
}
if (lexer.getCurrent().equals("+")) {
lexer.take();
lexer.setCurrent("+" + lexer.getCurrent());
}
if (lexer.isConstant(false)) {
checkConstant(lexer.getCurrent(), lexer);
result.setConstant(lexer.take());
result.setKind(Kind.Constant);
result.setEnd(lexer.getCurrentLocation());
} else if ("(".equals(lexer.getCurrent())) {
lexer.next();
result.setKind(Kind.Group);
result.setGroup(parseExpression(lexer, true));
if (!")".equals(lexer.getCurrent()))
throw lexer.error("Found " + lexer.getCurrent() + " expecting a \")\"");
result.setEnd(lexer.getCurrentLocation());
lexer.next();
} else {
if (!lexer.isToken() && !lexer.getCurrent().startsWith("\""))
throw lexer.error("Found " + lexer.getCurrent() + " expecting a token name");
if (lexer.getCurrent().startsWith("\""))
result.setName(lexer.readConstant("Path Name"));
else
result.setName(lexer.take());
result.setEnd(lexer.getCurrentLocation());
if (!result.checkName())
throw lexer.error("Found " + result.getName() + " expecting a valid token name");
if ("(".equals(lexer.getCurrent())) {
Function f = Function.fromCode(result.getName());
FunctionDetails details = null;
if (f == null) {
details = hostServices != null ? hostServices.resolveFunction(result.getName()) : null;
if (details == null)
throw lexer.error("The name " + result.getName() + " is not a valid function name");
f = Function.Custom;
}
result.setKind(Kind.Function);
result.setFunction(f);
lexer.next();
while (!")".equals(lexer.getCurrent())) {
result.getParameters().add(parseExpression(lexer, true));
if (",".equals(lexer.getCurrent()))
lexer.next();
else if (!")".equals(lexer.getCurrent()))
throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - either a \",\" or a \")\" expected");
}
result.setEnd(lexer.getCurrentLocation());
lexer.next();
checkParameters(lexer, c, result, details);
} else
result.setKind(Kind.Name);
}
ExpressionNode focus = result;
if ("[".equals(lexer.getCurrent())) {
lexer.next();
ExpressionNode item = new ExpressionNode(lexer.nextId());
item.setKind(Kind.Function);
item.setFunction(ExpressionNode.Function.Item);
item.getParameters().add(parseExpression(lexer, true));
if (!lexer.getCurrent().equals("]"))
throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - a \"]\" expected");
lexer.next();
result.setInner(item);
focus = item;
}
if (".".equals(lexer.getCurrent())) {
lexer.next();
focus.setInner(parseExpression(lexer, false));
}
result.setProximal(proximal);
if (proximal) {
while (lexer.isOp()) {
focus.setOperation(ExpressionNode.Operation.fromCode(lexer.getCurrent()));
focus.setOpStart(lexer.getCurrentStartLocation());
focus.setOpEnd(lexer.getCurrentLocation());
lexer.next();
focus.setOpNext(parseExpression(lexer, false));
focus = focus.getOpNext();
}
result = organisePrecedence(lexer, result);
}
return result;
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method parse.
/**
* Parse a path that is part of some other syntax
*
* @return
* @throws PathEngineException
* @throws Exception
*/
public ExpressionNode parse(FHIRLexer lexer) throws FHIRLexerException {
ExpressionNode result = parseExpression(lexer, true);
result.check();
return result;
}
Aggregations