use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseConceptMap.
private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
lexer.token("conceptmap");
ConceptMap map = new ConceptMap();
String id = lexer.readConstant("map id");
if (id.startsWith("#")) {
throw lexer.error("Concept Map identifier must start with #");
}
map.setId(id);
// todo: how to add this to the text format
map.setStatus(PublicationStatus.DRAFT);
result.getContained().add(map);
lexer.token("{");
// lexer.token("source");
// map.setSource(new UriType(lexer.readConstant("source")));
// lexer.token("target");
// map.setSource(new UriType(lexer.readConstant("target")));
Map<String, String> prefixes = new HashMap<String, String>();
while (lexer.hasToken("prefix")) {
lexer.token("prefix");
String n = lexer.take();
lexer.token("=");
String v = lexer.readConstant("prefix url");
prefixes.put(n, v);
}
while (lexer.hasToken("unmapped")) {
lexer.token("unmapped");
lexer.token("for");
String n = readPrefix(prefixes, lexer);
ConceptMapGroupComponent g = getGroup(map, n, null);
lexer.token("=");
String v = lexer.take();
if (v.equals("provided")) {
g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
} else {
throw lexer.error("Only unmapped mode PROVIDED is supported at this time");
}
}
while (!lexer.hasToken("}")) {
String srcs = readPrefix(prefixes, lexer);
lexer.token(":");
String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
ConceptMapEquivalence eq = readEquivalence(lexer);
String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
SourceElementComponent e = g.addElement();
e.setCode(sc);
if (e.getCode().startsWith("\"")) {
e.setCode(lexer.processConstant(e.getCode()));
}
TargetElementComponent tgt = e.addTarget();
tgt.setEquivalence(eq);
if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
lexer.token(":");
tgt.setCode(lexer.take());
if (tgt.getCode().startsWith("\"")) {
tgt.setCode(lexer.processConstant(tgt.getCode()));
}
}
tgt.setComment(lexer.getFirstComment());
}
lexer.token("}");
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parse.
public StructureMap parse(String text, String srcName) throws FHIRException {
FHIRLexer lexer = new FHIRLexer(text, srcName);
if (lexer.done())
throw lexer.error("Map Input cannot be empty");
lexer.token("map");
StructureMap result = new StructureMap();
result.setUrl(lexer.readConstant("url"));
lexer.token("=");
result.setName(lexer.readConstant("name"));
result.setDescription(lexer.getAllComments());
while (lexer.hasToken("conceptmap")) parseConceptMap(result, lexer);
while (lexer.hasToken("uses")) parseUses(result, lexer);
while (lexer.hasToken("imports")) parseImports(result, lexer);
while (!lexer.done()) {
parseGroup(result, lexer);
}
return result;
}
use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method processConstantString.
private String processConstantString(String s, FHIRLexer lexer) throws FHIRLexerException {
StringBuilder b = new StringBuilder();
int i = 1;
while (i < s.length() - 1) {
char ch = s.charAt(i);
if (ch == '\\') {
i++;
switch(s.charAt(i)) {
case 't':
b.append('\t');
break;
case 'r':
b.append('\r');
break;
case 'n':
b.append('\n');
break;
case 'f':
b.append('\f');
break;
case '\'':
b.append('\'');
break;
case '"':
b.append('"');
break;
case '`':
b.append('`');
break;
case '\\':
b.append('\\');
break;
case '/':
b.append('/');
break;
case 'u':
i++;
int uc = Integer.parseInt(s.substring(i, i + 4), 16);
b.append((char) uc);
i = i + 3;
break;
default:
throw lexer.error("Unknown character escape \\" + s.charAt(i));
}
i++;
} else {
b.append(ch);
i++;
}
}
return b.toString();
}
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) throws FHIRException {
StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
list.add(rule);
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 (lexer.hasToken("make")) {
lexer.token("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);
}
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
}
lexer.skipComments();
}
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;
}
Aggregations