public static String parseQualifiedIdentifier(Node node) { final String[] qualifiedIdentifier = {null}; node.apply( new DepthFirstAdapter() { @Override public void caseAQualifiedIdentifier(AQualifiedIdentifier node) { if (qualifiedIdentifier[0] != null) // avoid processing multiple QIs return; qualifiedIdentifier[0] = ""; super.caseAQualifiedIdentifier(node); } public final void caseAQualifiedIdentifierBase(AQualifiedIdentifierBase node) { Assert.isTrue(qualifiedIdentifier[0] != null); qualifiedIdentifier[0] += Util.stripEscaping(node.getIdentifier().getText()); super.caseAQualifiedIdentifierBase(node); } public final void caseAQualifiedIdentifierTail(AQualifiedIdentifierTail node) { Assert.isTrue(qualifiedIdentifier[0] != null); qualifiedIdentifier[0] += NamedElement.SEPARATOR; super.caseAQualifiedIdentifierTail(node); } }); Assert.isNotNull(qualifiedIdentifier[0]); return qualifiedIdentifier[0]; }
protected ValueSpecification parseLiteralValue(Node node) { final ValueSpecification[] result = {null}; node.apply( new DepthFirstAdapter() { @Override public void caseTFalse(TFalse node) { final Classifier booleanType = findBuiltInType("Boolean", node); result[0] = MDDExtensionUtils.buildBasicValue( namespaceTracker.currentPackage(), booleanType, Boolean.FALSE.toString()); } @Override public void caseTInteger(TInteger node) { final Classifier integerType = findBuiltInType("Integer", node); result[0] = MDDExtensionUtils.buildBasicValue( namespaceTracker.currentPackage(), integerType, node.getText()); } @Override public void caseTNull(TNull node) { result[0] = MDDUtil.createLiteralNull(namespaceTracker.currentPackage()); result[0].setType(findBuiltInType(TypeUtils.NULL_TYPE, node)); } @Override public void caseTReal(TReal node) { final Classifier doubleType = findBuiltInType("Double", node); result[0] = MDDExtensionUtils.buildBasicValue( namespaceTracker.currentPackage(), doubleType, node.getText()); } @Override public void caseTString(TString node) { final Classifier stringType = findBuiltInType("String", node); String text = node.getText(); result[0] = MDDExtensionUtils.buildBasicValue( namespaceTracker.currentPackage(), stringType, text.substring(1, text.length() - 1)); } @Override public void caseTTrue(TTrue node) { final Classifier booleanType = findBuiltInType("Boolean", node); result[0] = MDDExtensionUtils.buildBasicValue( namespaceTracker.currentPackage(), booleanType, Boolean.TRUE.toString()); } }); return result[0]; }
/** * Returns the last token found in the given tree or <code>null</code> if none is found (an empty * production). * * @return a token, or <code>null</code> */ public static Token findToken(Node node) { if (node == null) return null; final Token[] found = {null}; node.apply( new DepthFirstAdapter() { public void defaultCase(Node node) { if (node instanceof Token) found[0] = (Token) node; } }); return found[0]; }
public static String parseIdentifier(Node node) { if (node == null) return null; final String[] identifier = {null}; node.apply( new DepthFirstAdapter() { public void caseTIdentifier(TIdentifier node) { // ignore other identifiers under the given node if (identifier[0] == null) identifier[0] = Util.stripEscaping(node.getText()); } }); return identifier[0]; }
public static <T extends Node> T findChild(Node node, final Class<T> required) { if (node == null) return null; try { node.apply( new DepthFirstAdapter() { @Override public void defaultIn(Node node) { if (required.isInstance(node)) // we found it! throw new NodeFoundTrap(node); } }); } catch (NodeFoundTrap t) { return (T) t.getFound(); } return null; }
protected ValueSpecification parseValueSpecification(Node node, final Type expectedType) { ValueSpecification asLiteralValue = parseLiteralValue(node); if (asLiteralValue != null) return asLiteralValue; final ValueSpecification[] result = {null}; if (!(expectedType instanceof Enumeration)) { problemBuilder.addError("Enumeration literal expected ", node); throw new AbortedStatementCompilationException(); } node.apply( new DepthFirstAdapter() { @Override public void caseTIdentifier(TIdentifier node) { String literalName = node.getText().trim(); Enumeration targetEnumeration = (Enumeration) expectedType; EnumerationLiteral enumerationValue = ((Enumeration) targetEnumeration).getOwnedLiteral(literalName); if (enumerationValue == null) { problemBuilder.addError( "Unknown enumeration literal '" + literalName + "' in '" + targetEnumeration.getName() + "'", node); } InstanceValue valueSpec = (InstanceValue) namespaceTracker .currentPackage() .createPackagedElement(null, IRepository.PACKAGE.getInstanceValue()); valueSpec.setInstance(enumerationValue); valueSpec.setType(targetEnumeration); result[0] = valueSpec; } }); if (result[0] == null) problemBuilder.addWarning("Value specification not supported", node); return result[0]; }
public static <T extends Node> T findParent(Node node, Class<T> required) { if (node == null) return null; if (required.isInstance(node.parent())) return (T) node.parent(); return findParent(node.parent(), required); }