Class ExprCompiler<T extends org.faktorips.codegen.CodeFragment>

java.lang.Object
org.faktorips.fl.ExprCompiler<T>
Direct Known Subclasses:
JavaExprCompiler

public abstract class ExprCompiler<T extends org.faktorips.codegen.CodeFragment> extends Object
A compiler to compile expressions. This abstract class is target language agnostic and is by default implemented by the JavaExprCompiler.

This class is not thread safe!

  • Field Details

    • PREFIX

      public static final String PREFIX
      The prefix for all compiler messages.
      See Also:
    • INTERNAL_ERROR

      public static final String INTERNAL_ERROR
      An internal compiler error occurred during compilation. This message is generated if the compiler fails because of a bug, there is nothing wrong with the expression.
      See Also:
    • SYNTAX_ERROR

      public static final String SYNTAX_ERROR
      The expression has a syntax error, it does not confirm to the grammar.

      Example: 2 + + b does not conform to the grammar.

      See Also:
    • DATATYPE_CREATION_ERROR

      public static final String DATATYPE_CREATION_ERROR
      The expression contains a data type that can not be instantiated by this compiler.
      See Also:
    • LEXICAL_ERROR

      public static final String LEXICAL_ERROR
      The expression has a lexical error.
      See Also:
    • UNDEFINED_OPERATOR

      public static final String UNDEFINED_OPERATOR
      The operation like +, *, - can't be done on the provided types.

      Example: You can't multiply (*) two money values.

      See Also:
    • UNDEFINED_IDENTIFIER

      public static final String UNDEFINED_IDENTIFIER
      An identifier can't be resolved.

      Example: a * 2

      In the expression a is an identifier and it is possible that it can't be resolved by the {IdentifierResolver} the compiler uses.

      See Also:
    • UNKNOWN_QUALIFIER

      public static final String UNKNOWN_QUALIFIER
      A qualifier can't be resolved.

      Example: a.b(qual)

      In the expression a and b are identifiers, qual is a qualifier which does not correspond to any product component's unqualified name.

      See Also:
    • NO_ASSOCIATION_TARGET

      public static final String NO_ASSOCIATION_TARGET
      An identifier is resolved to an association but the association's target can't be found.

      Example: a.b.c * 2

      In the expression a, b, c are identifiers. If the {IdentifierResolver} the compiler uses can't identify the target types of associations a and b then an error message NO_ASSOCIATION_TARGET is returned.

      See Also:
    • NO_INDEX_FOR_1TO1_ASSOCIATION

      public static final String NO_INDEX_FOR_1TO1_ASSOCIATION
      An identifier is resolved to an 1to1 association but an index is also provided.

      Example: a.b[0]

      In the expression a and b are identifiers. The identifier b is resolved to a 1to1 association.

      See Also:
    • UNDEFINED_FUNCTION

      public static final String UNDEFINED_FUNCTION
      The expression contains a call to an undefined function.
      See Also:
    • WRONG_ARGUMENT_TYPES

      public static final String WRONG_ARGUMENT_TYPES
      The expression contains a function call to a function with wrong argument types.
      See Also:
    • WRONG_MONEY_LITERAL

      public static final String WRONG_MONEY_LITERAL
      The expression contains a literal that is identified by the parser as a money literal that doesn't have a valid currency.
      See Also:
    • AMBIGUOUS_FUNCTION_CALL

      public static final String AMBIGUOUS_FUNCTION_CALL
      The Expression calls a function, which could be resolved to several functions.
      See Also:
    • NULL_NOT_ALLOWED

      public static final String NULL_NOT_ALLOWED
      The expression contains a the expression null.
      See Also:
  • Constructor Details

    • ExprCompiler

      public ExprCompiler()
      Creates a new compiler. Messages returned by the compiler are generated using the default locale. A ConversionCodeGenerator, DatatypeHelperProvider and IdentifierResolver must be set via the corresponding setters.
    • ExprCompiler

      public ExprCompiler(Locale locale)
      Creates a new compiler. A ConversionCodeGenerator, DatatypeHelperProvider and IdentifierResolver must be set via the corresponding setters.
      Parameters:
      locale - The locale that is used to generate locale dependent messages.
    • ExprCompiler

      public ExprCompiler(Locale locale, IdentifierResolver<T> identifierResolver, org.faktorips.codegen.ConversionCodeGenerator<T> conversionCg, DatatypeHelperProvider<T> datatypeHelperProvider)
      Creates a new Compiler.
      Parameters:
      locale - the Locale used to generate locale dependent messages
      identifierResolver - the IdentifierResolver used to convert formula language identifiers to target language code
      conversionCg - the ConversionCodeGenerator used to convert between data types
      datatypeHelperProvider - the DatatypeHelperProvider used to get DatatypeHelpers that generate code for the creation and processing of values in their respective data types
  • Method Details

    • isValidIdentifier

      public static final boolean isValidIdentifier(String identifier)
      Verifies if the provided string is a valid identifier according to the identifier definition of the Fl-Parser.
    • registerDefaults

      protected abstract void registerDefaults()
      Registers the default operations.
    • register

      public void register(BinaryOperation<T> op)
      Registers the binary operation.
    • register

      public void register(UnaryOperation<T> op)
      Registers the unary operation.
    • setBinaryOperations

      public void setBinaryOperations(BinaryOperation<T>[] operations)
      Sets the BinaryOperations the compiler uses. Overwrites all operations previously registered.
      Throws:
      IllegalArgumentException - if operations is null.
    • setUnaryOperations

      public void setUnaryOperations(UnaryOperation<T>[] operations)
      Sets the UnaryOperations the compiler uses. Overwrites all operations previously registered.
      Throws:
      IllegalArgumentException - if operations is null.
    • getEnsureResultIsObject

      public boolean getEnsureResultIsObject()
      Returns the compiler's EnsureResultIsObject property.
      See Also:
    • setEnsureResultIsObject

      public void setEnsureResultIsObject(boolean newValue)
      Sets the compiler's EnsureResultIsObject property. If set to true, the compiler will check if an expression's type is a Java primitive before returning the result. If the type is a primitive the compiler will convert it to the appropriate wrapper class. E.g. the expression 2+4 is of type primitive int. If this property is set to true the compiler would wrap the resulting source code with a Integer(..).
    • getIdentifierResolver

      public IdentifierResolver<T> getIdentifierResolver()
      Returns the resolver the compiler uses to resolve identifiers.
    • setIdentifierResolver

      public void setIdentifierResolver(IdentifierResolver<T> resolver)
      Sets the IdentifierResolver the compiler uses to resolve identifiers.
      Throws:
      IllegalArgumentException - if resolver is null.
    • getConversionCodeGenerator

      public org.faktorips.codegen.ConversionCodeGenerator<T> getConversionCodeGenerator()
      Returns the ConversionCodeGenerator that defines the compiler's implicit conversions, e.g. convert a primitive int to an Integer.
    • setConversionCodeGenerator

      public void setConversionCodeGenerator(org.faktorips.codegen.ConversionCodeGenerator<T> ccg)
      Sets the ConversionCodeGenerator that the compiler uses for implicit conversions, e.g. convert a primitive int to an Integer.
      Throws:
      IllegalArgumentException - if ccg is null.
    • getLocale

      public Locale getLocale()
      Returns the Locale the compiler uses for it's messages.
    • setLocale

      public void setLocale(Locale locale)
      Sets the Locale the compiler uses to generate it's messages.
      Throws:
      IllegalArgumentException - if locale is null.
    • add

      public void add(FunctionResolver<T> fctResolver)
      Adds the function resolver to the ones used by the compiler to resolve function calls in expressions.
      Throws:
      IllegalArgumentException - if fctResolver is null.
    • remove

      public void remove(FunctionResolver<T> fctResolver)
      Removes the function resolver from the ones used by the compiler to resolve function calls. If the resolver hasn't been added before, this method does nothing.
      Throws:
      IllegalArgumentException - if fctResolver is null.
    • getFunctions

      public FlFunction<T>[] getFunctions()
      Return the functions supported by the compiler.
    • getAmbiguousFunctions

      public LinkedHashSet<FlFunction<T>> getAmbiguousFunctions(FlFunction<T>[] functions)
      Returns a Set of ambiguous FlFunctions, which the parser could not differentiate.

      Maybe the rolename of a table equals the qualified name of a table structure in the root package.

    • compile

      public CompilationResult<T> compile(String expr)
      Compiles the given expression string into source code. If the compilation is not successful, the result contains messages that describe the error/problem that has occurred. If the compilation is successful, the result contains source code that represents the expression along with the expression's Datatype. In this case the result does not contain any error messages, but may contain warnings or informations.
    • convertPrimitiveToWrapper

      protected abstract T convertPrimitiveToWrapper(org.faktorips.datatype.Datatype resultType, T codeFragment)
    • newParseTreeVisitor

      protected abstract ParseTreeVisitor<T> newParseTreeVisitor()
    • newCompilationResultImpl

      protected abstract AbstractCompilationResult<T> newCompilationResultImpl(org.faktorips.runtime.Message message)
    • newCompilationResultImpl

      protected abstract AbstractCompilationResult<T> newCompilationResultImpl(T sourcecode, org.faktorips.datatype.Datatype datatype)
    • parse

      protected SimpleNode parse(String expr) throws ParseException
      Throws:
      ParseException
    • parseExceptionToResult

      protected CompilationResult<T> parseExceptionToResult(ParseException e)
    • getDatatypeHelperProvider

      public DatatypeHelperProvider<T> getDatatypeHelperProvider()
      Returns:
      Returns the DatatypeHelperProvider.
    • setDatatypeHelperProvider

      public void setDatatypeHelperProvider(DatatypeHelperProvider<T> provider)
      Parameters:
      provider - The DatatypeHelperProvider to set.
    • getDatatypeHelper

      public org.faktorips.codegen.BaseDatatypeHelper<T> getDatatypeHelper(org.faktorips.datatype.Datatype type)
      Returns the code generation helper for the given type or null if no helper is available.
    • getLocalizedStrings

      public static org.faktorips.datatype.util.LocalizedStringsSet getLocalizedStrings()
    • getMatchingFunctionUsingConversion

      public CompilationResult<T> getMatchingFunctionUsingConversion(CompilationResult<T>[] argResults, org.faktorips.datatype.Datatype[] argTypes, String fctName)
    • getMatchingFunctionUsingConversionSingleArgument

      public CompilationResult<T> getMatchingFunctionUsingConversionSingleArgument(AbstractCompilationResult<T> argResult, org.faktorips.datatype.Datatype argTypes, String fctName)
    • getBinaryOperation

      public CompilationResult<T> getBinaryOperation(String operator, AbstractCompilationResult<T> lhsResult, AbstractCompilationResult<T> rhsResult)