public static DSLExpression parse(InputStream input) { Parser parser = new Parser(input); DSLExpression result = parser.parseImpl(); if (parser.errors.errors.size() > 0) { StringBuilder msg = new StringBuilder(); for (String error : parser.errors.errors) { msg.append(error).append("\n"); } throw new InvalidExpressionException(msg.toString()); } return result; }
private void initializeGuards(SpecializationData specialization, DSLExpressionResolver resolver) { final TypeMirror booleanType = context.getType(boolean.class); List<String> guardDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards"); List<GuardExpression> guardExpressions = new ArrayList<>(); for (String guard : guardDefinitions) { GuardExpression guardExpression; DSLExpression expression = null; try { expression = DSLExpression.parse(guard); expression.accept(resolver); guardExpression = new GuardExpression(specialization, expression); if (!ElementUtils.typeEquals(expression.getResolvedType(), booleanType)) { guardExpression.addError("Incompatible return type %s. Guards must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(booleanType)); } } catch (InvalidExpressionException e) { guardExpression = new GuardExpression(specialization, null); guardExpression.addError("Error parsing expression '%s': %s", guard, e.getMessage()); } guardExpressions.add(guardExpression); } specialization.setGuards(guardExpressions); }
private void initializeGuards(SpecializationData specialization, DSLExpressionResolver resolver) { final TypeMirror booleanType = context.getType(boolean.class); List<String> guardDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards"); List<GuardExpression> guardExpressions = new ArrayList<>(); for (String guard : guardDefinitions) { GuardExpression guardExpression; DSLExpression expression = null; try { expression = DSLExpression.parse(guard); expression.accept(resolver); guardExpression = new GuardExpression(specialization, expression); if (!ElementUtils.typeEquals(expression.getResolvedType(), booleanType)) { guardExpression.addError("Incompatible return type %s. Guards must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(booleanType)); } } catch (InvalidExpressionException e) { guardExpression = new GuardExpression(specialization, null); guardExpression.addError("Error parsing expression '%s': %s", guard, e.getMessage()); } guardExpressions.add(guardExpression); } specialization.setGuards(guardExpressions); }
public static DSLExpression parse(InputStream input) { Parser parser = new Parser(input); DSLExpression result = parser.parseImpl(); if (parser.errors.errors.size() > 0) { StringBuilder msg = new StringBuilder(); for (String error : parser.errors.errors) { msg.append(error).append("\n"); } throw new InvalidExpressionException(msg.toString()); } return result; }
private void initializeLimit(SpecializationData specialization, DSLExpressionResolver resolver) { AnnotationValue annotationValue = ElementUtils.getAnnotationValue(specialization.getMessageAnnotation(), "limit"); String limitValue; if (annotationValue == null) { limitValue = ""; } else { limitValue = (String) annotationValue.getValue(); } if (limitValue.isEmpty()) { limitValue = "3"; } else if (!specialization.hasMultipleInstances()) { specialization.addWarning(annotationValue, "The limit expression has no effect. Multiple specialization instantiations are impossible for this specialization."); return; } TypeMirror expectedType = context.getType(int.class); try { DSLExpression expression = DSLExpression.parse(limitValue); expression.accept(resolver); if (!ElementUtils.typeEquals(expression.getResolvedType(), expectedType)) { specialization.addError(annotationValue, "Incompatible return type %s. Limit expressions must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(expectedType)); } if (specialization.isDynamicParameterBound(expression)) { specialization.addError(annotationValue, "Limit expressions must not bind dynamic parameter values."); } specialization.setLimitExpression(expression); } catch (InvalidExpressionException e) { specialization.addError(annotationValue, "Error parsing expression '%s': %s", limitValue, e.getMessage()); } }
public void visitNegate(Negate negate) { TypeMirror booleanType = context.getType(boolean.class); TypeMirror resolvedType = negate.getResolvedType(); if (!ElementUtils.typeEquals(resolvedType, booleanType)) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type %s.", "!", ElementUtils.getSimpleName(resolvedType))); } }
cacheExpression.addError("Error parsing expression '%s': %s", initializer, e.getMessage());
public void visitNegate(Negate negate) { TypeMirror booleanType = context.getType(boolean.class); TypeMirror resolvedType = negate.getResolvedType(); if (!ElementUtils.typeEquals(resolvedType, booleanType)) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type %s.", "!", ElementUtils.getSimpleName(resolvedType))); } }
private void initializeLimit(SpecializationData specialization, DSLExpressionResolver resolver) { AnnotationValue annotationValue = ElementUtils.getAnnotationValue(specialization.getMessageAnnotation(), "limit"); String limitValue; if (annotationValue == null) { limitValue = ""; } else { limitValue = (String) annotationValue.getValue(); } if (limitValue.isEmpty()) { limitValue = "3"; } else if (!specialization.hasMultipleInstances()) { specialization.addWarning(annotationValue, "The limit expression has no effect. Multiple specialization instantiations are impossible for this specialization."); return; } TypeMirror expectedType = context.getType(int.class); try { DSLExpression expression = DSLExpression.parse(limitValue); expression.accept(resolver); if (!ElementUtils.typeEquals(expression.getResolvedType(), expectedType)) { specialization.addError(annotationValue, "Incompatible return type %s. Limit expressions must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(expectedType)); } if (specialization.isDynamicParameterBound(expression)) { specialization.addError(annotationValue, "Limit expressions must not bind dynamic parameter values."); } specialization.setLimitExpression(expression); } catch (InvalidExpressionException e) { specialization.addError(annotationValue, "Error parsing expression '%s': %s", limitValue, e.getMessage()); } }
public void visitBinary(Binary binary) { String operator = binary.getOperator(); TypeMirror leftType = binary.getLeft().getResolvedType(); TypeMirror rightType = binary.getRight().getResolvedType(); if (!ElementUtils.areTypesCompatible(leftType, rightType)) { throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } TypeMirror booleanType = context.getType(boolean.class); boolean valid; if (LOGIC_OPERATORS.contains(operator)) { valid = ElementUtils.typeEquals(leftType, booleanType); } else if (COMPARABLE_OPERATORS.contains(operator)) { valid = ElementUtils.isPrimitive(leftType); } else if (IDENTITY_OPERATORS.contains(operator)) { valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; } else { throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); } binary.setResolvedType(booleanType); if (!valid) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } }
private void initializeAssumptions(SpecializationData specialization, DSLExpressionResolver resolver) { final DeclaredType assumptionType = context.getDeclaredType(Assumption.class); final TypeMirror assumptionArrayType = new ArrayCodeTypeMirror(assumptionType); final List<String> assumptionDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "assumptions"); List<AssumptionExpression> assumptionExpressions = new ArrayList<>(); int assumptionId = 0; for (String assumption : assumptionDefinitions) { AssumptionExpression assumptionExpression; DSLExpression expression = null; try { expression = DSLExpression.parse(assumption); expression.accept(resolver); assumptionExpression = new AssumptionExpression(specialization, expression, "assumption" + assumptionId); if (!ElementUtils.isAssignable(expression.getResolvedType(), assumptionType) && !ElementUtils.isAssignable(expression.getResolvedType(), assumptionArrayType)) { assumptionExpression.addError("Incompatible return type %s. Assumptions must be assignable to %s or %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(assumptionType), ElementUtils.getSimpleName(assumptionArrayType)); } if (specialization.isDynamicParameterBound(expression)) { specialization.addError("Assumption expressions must not bind dynamic parameter values."); } } catch (InvalidExpressionException e) { assumptionExpression = new AssumptionExpression(specialization, null, "assumption" + assumptionId); assumptionExpression.addError("Error parsing expression '%s': %s", assumption, e.getMessage()); } assumptionExpressions.add(assumptionExpression); } specialization.setAssumptionExpressions(assumptionExpressions); }
public void visitBinary(Binary binary) { String operator = binary.getOperator(); TypeMirror leftType = binary.getLeft().getResolvedType(); TypeMirror rightType = binary.getRight().getResolvedType(); if (!ElementUtils.areTypesCompatible(leftType, rightType)) { throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } TypeMirror booleanType = context.getType(boolean.class); boolean valid; if (LOGIC_OPERATORS.contains(operator)) { valid = ElementUtils.typeEquals(leftType, booleanType); } else if (COMPARABLE_OPERATORS.contains(operator)) { valid = ElementUtils.isPrimitive(leftType); } else if (IDENTITY_OPERATORS.contains(operator)) { valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; } else { throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); } binary.setResolvedType(booleanType); if (!valid) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } }
private void initializeAssumptions(SpecializationData specialization, DSLExpressionResolver resolver) { final DeclaredType assumptionType = context.getDeclaredType(Assumption.class); final TypeMirror assumptionArrayType = new ArrayCodeTypeMirror(assumptionType); final List<String> assumptionDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "assumptions"); List<AssumptionExpression> assumptionExpressions = new ArrayList<>(); int assumptionId = 0; for (String assumption : assumptionDefinitions) { AssumptionExpression assumptionExpression; DSLExpression expression = null; try { expression = DSLExpression.parse(assumption); expression.accept(resolver); assumptionExpression = new AssumptionExpression(specialization, expression, "assumption" + assumptionId); if (!ElementUtils.isAssignable(expression.getResolvedType(), assumptionType) && !ElementUtils.isAssignable(expression.getResolvedType(), assumptionArrayType)) { assumptionExpression.addError("Incompatible return type %s. Assumptions must be assignable to %s or %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(assumptionType), ElementUtils.getSimpleName(assumptionArrayType)); } if (specialization.isDynamicParameterBound(expression)) { specialization.addError("Assumption expressions must not bind dynamic parameter values."); } } catch (InvalidExpressionException e) { assumptionExpression = new AssumptionExpression(specialization, null, "assumption" + assumptionId); assumptionExpression.addError("Error parsing expression '%s': %s", assumption, e.getMessage()); } assumptionExpressions.add(assumptionExpression); assumptionId++; } specialization.setAssumptionExpressions(assumptionExpressions); }
public void visitIntLiteral(IntLiteral binary) { try { binary.setResolvedType(context.getType(int.class)); final int base; final String literal; if (binary.getLiteral().startsWith("0x")) { base = 16; literal = binary.getLiteral().substring(2); } else if (binary.getLiteral().startsWith("0b")) { base = 2; literal = binary.getLiteral().substring(2); } else if (binary.getLiteral().startsWith("0")) { base = 8; literal = binary.getLiteral(); } else { base = 10; literal = binary.getLiteral(); } binary.setResolvedValueInt(Integer.parseInt(literal, base)); } catch (NumberFormatException e) { throw new InvalidExpressionException(String.format("Type mismatch: cannot convert from String '%s' to int", binary.getLiteral())); } }
cacheExpression.addError("Error parsing expression '%s': %s", initializer, e.getMessage());
public void visitIntLiteral(IntLiteral binary) { try { binary.setResolvedType(context.getType(int.class)); final int base; final String literal; if (binary.getLiteral().startsWith("0x")) { base = 16; literal = binary.getLiteral().substring(2); } else if (binary.getLiteral().startsWith("0b")) { base = 2; literal = binary.getLiteral().substring(2); } else if (binary.getLiteral().startsWith("0")) { base = 8; literal = binary.getLiteral(); } else { base = 10; literal = binary.getLiteral(); } binary.setResolvedValueInt(Integer.parseInt(literal, base)); } catch (NumberFormatException e) { throw new InvalidExpressionException(String.format("Type mismatch: cannot convert from String '%s' to int", binary.getLiteral())); } }
throw new InvalidExpressionException(String.format("%s cannot be resolved.", variable.getName()));
if (foundWithName == null) { throw new InvalidExpressionException(String.format("The method %s is undefined for the enclosing scope.", call.getName())); } else { StringBuilder arguments = new StringBuilder(); throw new InvalidExpressionException(String.format("The method %s in the type %s is not applicable for the arguments %s.", //
if (foundWithName == null) { throw new InvalidExpressionException(String.format("The method %s is undefined for the enclosing scope.", call.getName())); } else { StringBuilder arguments = new StringBuilder(); throw new InvalidExpressionException(String.format("The method %s in the type %s is not applicable for the arguments %s.", //
throw new InvalidExpressionException(String.format("%s cannot be resolved.", variable.getName()));