コード例 #1
0
 /**
  * Walk the tree looking for METHOD_DEFs. Any simple METHOD_DEF (one without TYPE_PARAMETERS)
  * lacking an access specifier is given public access.
  *
  * @param node
  */
 private void makeSimpleMethodsPublic(final AST node) {
   if (node.getType() == PdeTokenTypes.METHOD_DEF) {
     final AST mods = node.getFirstChild();
     final AST oldFirstMod = mods.getFirstChild();
     for (AST mod = oldFirstMod; mod != null; mod = mod.getNextSibling()) {
       final int t = mod.getType();
       if (t == PdeTokenTypes.LITERAL_private
           || t == PdeTokenTypes.LITERAL_protected
           || t == PdeTokenTypes.LITERAL_public) {
         return;
       }
     }
     if (mods.getNextSibling().getType() == PdeTokenTypes.TYPE_PARAMETERS) {
       return;
     }
     final CommonHiddenStreamToken publicToken =
         new CommonHiddenStreamToken(PdeTokenTypes.LITERAL_public, "public") {
           {
             setHiddenAfter(new CommonHiddenStreamToken(PdeTokenTypes.WS, " "));
           }
         };
     final AST publicNode = new CommonASTWithHiddenTokens(publicToken);
     publicNode.setNextSibling(oldFirstMod);
     mods.setFirstChild(publicNode);
   } else {
     for (AST kid = node.getFirstChild(); kid != null; kid = kid.getNextSibling())
       makeSimpleMethodsPublic(kid);
   }
 }
コード例 #2
0
  protected void prepareVersioned(AST updateNode, AST versioned) throws SemanticException {
    UpdateStatement updateStatement = (UpdateStatement) updateNode;
    FromClause fromClause = updateStatement.getFromClause();
    if (versioned != null) {
      // Make sure that the persister is versioned
      Queryable persister = fromClause.getFromElement().getQueryable();
      if (!persister.isVersioned()) {
        throw new SemanticException(
            "increment option specified for update of non-versioned entity");
      }

      VersionType versionType = persister.getVersionType();
      if (versionType instanceof UserVersionType) {
        throw new SemanticException(
            "user-defined version types not supported for increment option");
      }

      AST eq = getASTFactory().create(HqlSqlTokenTypes.EQ, "=");
      AST versionPropertyNode = generateVersionPropertyNode(persister);

      eq.setFirstChild(versionPropertyNode);

      AST versionIncrementNode = null;
      if (Date.class.isAssignableFrom(versionType.getReturnedClass())) {
        versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PARAM, "?");
        ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification(versionType);
        ((ParameterNode) versionIncrementNode).setHqlParameterSpecification(paramSpec);
        parameters.add(0, paramSpec);
      } else {
        // Not possible to simply re-use the versionPropertyNode here as it causes
        // OOM errors due to circularity :(
        versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PLUS, "+");
        versionIncrementNode.setFirstChild(generateVersionPropertyNode(persister));
        versionIncrementNode.addChild(getASTFactory().create(HqlSqlTokenTypes.IDENT, "1"));
      }

      eq.addChild(versionIncrementNode);

      evaluateAssignment(eq, persister, 0);

      AST setClause = updateStatement.getSetClause();
      AST currentFirstSetElement = setClause.getFirstChild();
      setClause.setFirstChild(eq);
      eq.setNextSibling(currentFirstSetElement);
    }
  }
コード例 #3
0
  private AST generateSyntheticDotNodeForNonQualifiedPropertyRef(
      AST property, FromElement fromElement) {
    AST dot = getASTFactory().create(DOT, "{non-qualified-property-ref}");
    // TODO : better way?!?
    ((DotNode) dot).setPropertyPath(((FromReferenceNode) property).getPath());

    IdentNode syntheticAlias = (IdentNode) getASTFactory().create(IDENT, "{synthetic-alias}");
    syntheticAlias.setFromElement(fromElement);
    syntheticAlias.setResolved();

    dot.setFirstChild(syntheticAlias);
    dot.addChild(property);

    return dot;
  }
コード例 #4
0
  /**
   * preprocesses a pde file and writes out a java file
   *
   * @return the class name of the exported Java
   */
  private String write(final String program, final PrintWriter stream)
      throws SketchException, RecognitionException, TokenStreamException {

    // Match on the uncommented version, otherwise code inside comments used
    // http://code.google.com/p/processing/issues/detail?id=1404
    String uncomment = scrubComments(program);
    PdeRecognizer parser = createParser(program);
    if (PUBLIC_CLASS.matcher(uncomment).find()) {
      try {
        final PrintStream saved = System.err;
        try {
          // throw away stderr for this tentative parse
          System.setErr(new PrintStream(new ByteArrayOutputStream()));
          parser.javaProgram();
        } finally {
          System.setErr(saved);
        }
        setMode(Mode.JAVA);
      } catch (Exception e) {
        // I can't figure out any other way of resetting the parser.
        parser = createParser(program);
        parser.pdeProgram();
      }
    } else if (FUNCTION_DECL.matcher(uncomment).find()) {
      setMode(Mode.ACTIVE);
      parser.activeProgram();
    } else {
      parser.pdeProgram();
    }

    // set up the AST for traversal by PdeEmitter
    //
    ASTFactory factory = new ASTFactory();
    AST parserAST = parser.getAST();
    AST rootNode = factory.create(ROOT_ID, "AST ROOT");
    rootNode.setFirstChild(parserAST);

    makeSimpleMethodsPublic(rootNode);

    // unclear if this actually works, but it's worth a shot
    //
    // ((CommonAST)parserAST).setVerboseStringConversion(
    //  true, parser.getTokenNames());
    // (made to use the static version because of jikes 1.22 warning)
    BaseAST.setVerboseStringConversion(true, parser.getTokenNames());

    final String className;
    if (mode == Mode.JAVA) {
      // if this is an advanced program, the classname is already defined.
      className = getFirstClassName(parserAST);
    } else {
      className = this.name;
    }

    // if 'null' was passed in for the name, but this isn't
    // a 'java' mode class, then there's a problem, so punt.
    //
    if (className == null) return null;

    // debug
    if (false) {
      final StringWriter buf = new StringWriter();
      final PrintWriter bufout = new PrintWriter(buf);
      writeDeclaration(bufout, className);
      new PdeEmitter(this, bufout).print(rootNode);
      writeFooter(bufout, className);
      debugAST(rootNode, true);
      System.err.println(buf.toString());
    }

    writeDeclaration(stream, className);
    new PdeEmitter(this, stream).print(rootNode);
    writeFooter(stream, className);

    // if desired, serialize the parse tree to an XML file.  can
    // be viewed usefully with Mozilla or IE
    if (Preferences.getBoolean("preproc.output_parse_tree")) {
      writeParseTree("parseTree.xml", parserAST);
    }

    return className;
  }