Пример #1
1
  /** Parses the source to a CST. You can retrieve it with getCST(). */
  public void parse() throws CompilationFailedException {
    if (this.phase > Phases.PARSING) {
      throw new GroovyBugError("parsing is already complete");
    }

    if (this.phase == Phases.INITIALIZATION) {
      nextPhase();
    }

    //
    // Create a reader on the source and run the parser.

    Reader reader = null;
    try {
      reader = source.getReader();

      // lets recreate the parser each time as it tends to keep around state
      parserPlugin = getConfiguration().getPluginFactory().createParserPlugin();

      cst = parserPlugin.parseCST(this, reader);
      sourceSummary = parserPlugin.getSummary();

      reader.close();

    } catch (IOException e) {
      getErrorCollector().addFatalError(new SimpleMessage(e.getMessage(), this));
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    }
  }
Пример #2
0
  /** Generates an AST from the CST. You can retrieve it with getAST(). */
  public void convert() throws CompilationFailedException {
    if (this.phase == Phases.PARSING && this.phaseComplete) {
      gotoPhase(Phases.CONVERSION);
    }

    if (this.phase != Phases.CONVERSION) {
      throw new GroovyBugError("SourceUnit not ready for convert()");
    }

    //
    // Build the AST

    try {
      this.ast = parserPlugin.buildAST(this, this.classLoader, this.cst);

      this.ast.setDescription(this.name);
    } catch (SyntaxException e) {
      getErrorCollector().addError(new SyntaxErrorMessage(e, this));
    }

    String property =
        (String)
            AccessController.doPrivileged(
                new PrivilegedAction() {
                  public Object run() {
                    return System.getProperty("groovy.ast");
                  }
                });

    if ("xml".equals(property)) {
      saveAsXML(name, ast);
    }
  }