use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method parsePartial.
/**
* Parse a path for later use using execute
*
* @param path
* @return
* @throws PathEngineException
* @throws Exception
*/
public ExpressionNodeWithOffset parsePartial(String path, int i) throws FHIRLexerException {
FHIRLexer lexer = new FHIRLexer(path, i);
if (lexer.done()) {
throw lexer.error("Path cannot be empty");
}
ExpressionNode result = parseExpression(lexer, true);
result.check();
return new ExpressionNodeWithOffset(lexer.getCurrentStart(), 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 StructureMapUtilities method parseGroup.
private void parseGroup(StructureMap result, FHIRLexer lexer) throws FHIRException {
lexer.token("group");
StructureMapGroupComponent group = result.addGroup();
boolean newFmt = false;
if (lexer.hasToken("for")) {
lexer.token("for");
if ("type".equals(lexer.getCurrent())) {
lexer.token("type");
lexer.token("+");
lexer.token("types");
group.setTypeMode(StructureMapGroupTypeMode.TYPEANDTYPES);
} else {
lexer.token("types");
group.setTypeMode(StructureMapGroupTypeMode.TYPES);
}
} else
group.setTypeMode(StructureMapGroupTypeMode.NONE);
group.setName(lexer.take());
if (lexer.hasToken("(")) {
newFmt = true;
lexer.take();
while (!lexer.hasToken(")")) {
parseInput(group, lexer, true);
if (lexer.hasToken(","))
lexer.token(",");
}
lexer.take();
}
if (lexer.hasToken("extends")) {
lexer.next();
group.setExtends(lexer.take());
}
if (newFmt) {
group.setTypeMode(StructureMapGroupTypeMode.NONE);
if (lexer.hasToken("<")) {
lexer.token("<");
lexer.token("<");
if (lexer.hasToken("types")) {
group.setTypeMode(StructureMapGroupTypeMode.TYPES);
lexer.token("types");
} else {
lexer.token("type");
lexer.token("+");
group.setTypeMode(StructureMapGroupTypeMode.TYPEANDTYPES);
}
lexer.token(">");
lexer.token(">");
}
lexer.token("{");
}
lexer.skipComments();
if (newFmt) {
while (!lexer.hasToken("}")) {
if (lexer.done())
throw lexer.error("premature termination expecting 'endgroup'");
parseRule(result, group.getRule(), lexer, true);
}
} else {
while (lexer.hasToken("input")) parseInput(group, lexer, false);
while (!lexer.hasToken("endgroup")) {
if (lexer.done())
throw lexer.error("premature termination expecting 'endgroup'");
parseRule(result, group.getRule(), lexer, false);
}
}
lexer.next();
if (newFmt && lexer.hasToken(";"))
lexer.next();
lexer.skipComments();
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseRule.
private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer, boolean newFmt) throws FHIRException {
StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
list.add(rule);
if (!newFmt) {
rule.setName(lexer.takeDottedToken());
lexer.token(":");
lexer.token("for");
}
boolean done = false;
while (!done) {
parseSource(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
if ((newFmt && lexer.hasToken("->")) || (!newFmt && lexer.hasToken("make"))) {
lexer.token(newFmt ? "->" : "make");
done = false;
while (!done) {
parseTarget(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
}
if (lexer.hasToken("then")) {
lexer.token("then");
if (lexer.hasToken("{")) {
lexer.token("{");
if (lexer.hasComment()) {
rule.setDocumentation(lexer.take().substring(2).trim());
}
lexer.skipComments();
while (!lexer.hasToken("}")) {
if (lexer.done())
throw lexer.error("premature termination expecting '}' in nested group");
parseRule(map, rule.getRule(), lexer, newFmt);
}
lexer.token("}");
} else {
done = false;
while (!done) {
parseRuleReference(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
}
} else if (lexer.hasComment()) {
rule.setDocumentation(lexer.take().substring(2).trim());
}
if (isSimpleSyntax(rule)) {
rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
// with no parameter - e.g. imply what is to be created
rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE);
// no dependencies - imply what is to be done based on types
}
if (newFmt) {
if (lexer.isConstant()) {
if (lexer.isStringConstant()) {
rule.setName(lexer.readConstant("ruleName"));
} else {
rule.setName(lexer.take());
}
} else {
if (rule.getSource().size() != 1 || !rule.getSourceFirstRep().hasElement())
throw lexer.error("Complex rules must have an explicit name");
if (rule.getSourceFirstRep().hasType())
rule.setName(rule.getSourceFirstRep().getElement() + "-" + rule.getSourceFirstRep().getType());
else
rule.setName(rule.getSourceFirstRep().getElement());
}
lexer.token(";");
}
lexer.skipComments();
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseInput.
private void parseInput(StructureMapGroupComponent group, FHIRLexer lexer, boolean newFmt) throws FHIRException {
StructureMapGroupInputComponent input = group.addInput();
if (newFmt) {
input.setMode(StructureMapInputMode.fromCode(lexer.take()));
} else
lexer.token("input");
input.setName(lexer.take());
if (lexer.hasToken(":")) {
lexer.token(":");
input.setType(lexer.take());
}
if (!newFmt) {
lexer.token("as");
input.setMode(StructureMapInputMode.fromCode(lexer.take()));
if (lexer.hasComment()) {
input.setDocumentation(lexer.take().substring(2).trim());
}
lexer.skipToken(";");
lexer.skipComments();
}
}
Aggregations