Beispiel #1
0
  private static Path createPathFromIdentifier(ASTStatement ast) throws SyntaxException {

    try {
      String pathname = ast.getIdentifier();
      assert (pathname != null);
      return new Path(pathname);
    } catch (EvaluationException ee) {
      throw SyntaxException.create(ast.getSourceRange(), ee);
    } catch (SyntaxException se) {
      throw se.addExceptionInfo(ast.getSourceRange(), null);
    }
  }
Beispiel #2
0
  public static Operation newOperation(SourceRange sourceRange, Operation... ops)
      throws SyntaxException {

    assert (ops.length == 1);

    Operation result = null;

    // Attempt to optimize this operation.
    if (ops[0] instanceof Element) {

      try {
        LongProperty a = (LongProperty) ops[0];
        result = execute(a);
      } catch (ClassCastException cce) {
        throw new EvaluationException(MessageUtils.format(MSG_INVALID_ARGS_BITNOT), sourceRange);
      } catch (EvaluationException ee) {
        throw SyntaxException.create(sourceRange, ee);
      }

    } else {
      result = new BitNot(sourceRange, ops);
    }

    return result;
  }
Beispiel #3
0
  private static Operation astToFunction(ASTFunction node) throws SyntaxException {

    // Convert each of the arguments and check that the operation can appear
    // in a restricted context.
    int count = node.jjtGetNumChildren();
    Operation[] ops = new Operation[count];
    for (int i = 0; i < count; i++) {
      ops[i] = (astToDml((ASTOperation) node.jjtGetChild(i), true));
      ops[i].checkRestrictedContext();
    }

    // Explicitly disallow automatic variables being called as functions.
    if (AUTOMATIC_VARIABLES.contains(node.getName())) {
      throw SyntaxException.create(
          node.getSourceRange(), MSG_INVALID_FUNCTION_NAME, node.getName());
    }

    // Process 'normal' built-in functions. These don't need special
    // processing of the arguments. The constructor takes a source range and
    // a list of operations.
    Method c = functionConstructors.get(node.getName());
    if (c != null) {

      // Built-in function. Look up constructor in table and create new
      // instance.
      try {

        return (Operation) c.invoke(null, node.getSourceRange(), ops);

      } catch (InvocationTargetException ite) {

        // There may be SyntaxExceptions thrown during construction.
        // If so, rethrow them. All other exceptions are not expected
        // and should generate a compiler error.
        Throwable t = ite.getCause();
        if (t instanceof SyntaxException) {
          throw (SyntaxException) t;
        } else {
          CompilerError error = CompilerError.create(MSG_UNEXPECTED_EXCEPTION_ENCOUNTERED);
          error.initCause(t);
          throw error;
        }

      } catch (IllegalAccessException iae) {
        CompilerError error = CompilerError.create(MSG_UNEXPECTED_EXCEPTION_ENCOUNTERED);
        error.initCause(iae);
        throw error;

      } catch (ClassCastException cce) {
        CompilerError error = CompilerError.create(MSG_UNEXPECTED_EXCEPTION_ENCOUNTERED);
        error.initCause(cce);
        throw error;
      }
    } else {

      // User-defined function. Create generic function wrapper.
      return new Function(node.getSourceRange(), node.getName(), ops);
    }
  }
Beispiel #4
0
  private static Element runDefaultDml(Operation dml) throws SyntaxException {

    // If the argument is null, return null as the value immediately.
    if (dml == null) {
      return null;
    }

    // Create a nearly empty execution context. There are no global
    // variables by default (including no 'self' variable). Only the
    // standard built-in functions are accessible.
    Context context = new CompileTimeContext();

    Element value = null;

    // IF this is an AbstractOperation, pull out the source location.
    SourceRange sourceRange = null;
    if (dml instanceof AbstractOperation) {
      AbstractOperation op = (AbstractOperation) dml;
      sourceRange = op.getSourceRange();
    }

    // Execute the DML block. The block must evaluate to an Element. Any
    // error is fatal for the compilation.
    try {
      value = context.executeDmlBlock(dml);
    } catch (EvaluationException ee) {
      SyntaxException se = SyntaxException.create(sourceRange, MSG_DEF_VALUE_NOT_CONSTANT);
      se.initCause(ee);
      throw se;
    }

    // The default value cannot be either undef or null. Throw a syntax
    // error if that is the case.
    if (value instanceof Undef) {
      throw SyntaxException.create(sourceRange, MSG_DEF_VALUE_CANNOT_BE_UNDEF);
    } else if (value instanceof Null) {
      throw SyntaxException.create(sourceRange, MSG_DEF_VALUE_CANNOT_BE_UNDEF);
    }

    // Looks Ok; return the value.
    return value;
  }
Beispiel #5
0
  public static HashResource createRootElement(String rootElement) throws SyntaxException {

    if (rootElement == null || "".equals(rootElement.trim())) {

      return new HashResource();

    } else {

      try {

        PanParser parser = new PanParser(new StringReader(rootElement));
        ASTOperation ast = parser.dml();
        Operation dml = PanParserAstUtils.astToDml(ast, true);
        return (HashResource) dml.execute(new CompileTimeContext());

      } catch (SyntaxException e) {
        throw SyntaxException.create(null, MSG_INVALID_SYNTAX_ROOT_ELEMENT, e.getMessage());
      } catch (ClassCastException e) {
        throw SyntaxException.create(null, MSG_INVALID_TYPE_FOR_ROOT_ELEMENT);
      }
    }
  }
Beispiel #6
0
  private CompilerOptions createCompilerOptions() throws MojoExecutionException {

    LinkedList<File> includeDirectories = new LinkedList<File>();
    includeDirectories.add(sourceDirectory);

    try {
      return new CompilerOptions(
          Pattern.compile(debugNsInclude),
          Pattern.compile(debugNsExclude),
          maxIteration,
          maxRecursion,
          formatters,
          outputDir,
          includeDirectories,
          warningsFromString(warnings),
          null,
          null,
          initialData,
          nthread);

    } catch (SyntaxException e) {
      throw new MojoExecutionException("error creating compiler options: " + e.getMessage());
    }
  }
Beispiel #7
0
  private static Path convertAstToPrefixStatement(ASTStatement ast) throws SyntaxException {

    // Sanity check.
    assert (ast.getStatementType() == StatementType.PREFIX);

    if (!"".equals(ast.getIdentifier())) {

      // Normal path was given check that it is absolute.
      Path path = createPathFromIdentifier(ast);
      if (!path.isAbsolute()) {
        throw SyntaxException.create(
            ast.getSourceRange(), MessageUtils.MSG_PREFIX_MUST_BE_ABSOLUTE_PATH, path.toString());
      }
      return path;

    } else {

      // Empty path was given so just return null to indicate there is no
      // prefix.
      return null;
    }
  }