Beispiel #1
0
  private void generate(IndentingPrintWriter out) throws IOException {
    printHeader(out);

    out.indent();

    ParseEngine parseEngine = new ParseEngine(state, semanticize);
    parseEngine.build(out);

    printBoilerplate(parseEngine, out);

    out.unindent();

    printFooter(out);
  }
Beispiel #2
0
  public static void start() throws MetaParseException {

    Token t = null;

    if (JavaCCErrors.get_error_count() != 0) throw new MetaParseException();

    if (Options.getBuildParser()) {

      try {
        ostr =
            new PrintWriter(
                new BufferedWriter(
                    new FileWriter(new File(Options.getOutputDirectory(), cu_name + ".java")),
                    8192));
      } catch (IOException e) {
        JavaCCErrors.semantic_error("Could not open file " + cu_name + ".java for writing.");
        throw new Error();
      }

      List tn = new ArrayList(toolNames);
      tn.add(toolName);
      ostr.println("/* " + getIdString(tn, cu_name + ".java") + " */");

      boolean implementsExists = false;

      if (cu_to_insertion_point_1.size() != 0) {
        boolean printImports = true;
        printTokenSetup((Token) (cu_to_insertion_point_1.get(0)));
        ccol = 1;
        for (Iterator it = cu_to_insertion_point_1.iterator(); it.hasNext(); ) {
          t = (Token) it.next();
          if (printImports && (t.kind == PUBLIC || t.kind == CLASS)) {
            printImports = false;
            ostr.println("");
            ostr.println("import com.intellij.psi.tree.IElementType;");
            ostr.println("import com.intellij.lang.PsiBuilder;");
            ostr.println("import java.util.ArrayList;");
          }
          if (t.kind == IMPLEMENTS) {
            implementsExists = true;
          } else if (t.kind == CLASS) {
            implementsExists = false;
          }
          printToken(t, ostr);
        }
      }
      if (implementsExists) {
        ostr.print(", ");
      } else {
        ostr.print(" implements ");
      }
      ostr.print(cu_name + "Constants ");
      if (cu_to_insertion_point_2.size() != 0) {
        printTokenSetup((Token) (cu_to_insertion_point_2.get(0)));
        for (Iterator it = cu_to_insertion_point_2.iterator(); it.hasNext(); ) {
          t = (Token) it.next();
          printToken(t, ostr);
        }
      }

      ostr.println("");
      ostr.println("");

      ParseEngine.build(ostr);

      ostr.println("  private final PsiBuilder builder;");
      ostr.println("  public " + cu_name + "(PsiBuilder builder) {");
      ostr.println("    this.builder = builder;");
      ostr.println("  }");

      if (jj2index != 0) {
        ostr.println("  " + staticOpt() + "private int jj_la;");
        ostr.println(
            "  "
                + staticOpt()
                + "private ArrayList<IElementType> tokens = new ArrayList<IElementType>();");
        ostr.println("  " + staticOpt() + "private int currentIndex = 0;");
        ostr.println("  " + staticOpt() + "private int maxIndex = 0;");
        if (lookaheadNeeded) {
          ostr.println("  /** Whether we are looking ahead. */");
          ostr.println("  " + staticOpt() + "private boolean jj_lookingAhead = false;");
          ostr.println("  " + staticOpt() + "private boolean jj_semLA;");
        }
      }
      ostr.println("");

      ostr.println("  " + staticOpt() + "private void rollbackTo(int scanpos) {");
      ostr.println("    currentIndex = scanpos;");
      ostr.println("  }");
      ostr.println("  " + staticOpt() + "private void init(int la) {");
      ostr.println("    jj_la = la;");
      ostr.println("    tokens.clear();");
      ostr.println("    tokens.add(builder.getTokenType());");
      ostr.println("    currentIndex = 0;");
      ostr.println("    maxIndex = 0;");
      ostr.println("  }");
      ostr.println(
          "  " + staticOpt() + "private IElementType jj_consume_token(IElementType type) {");
      ostr.println("    IElementType actualType = builder.getTokenType();");
      ostr.println("    if (actualType == type) {");
      ostr.println("      builder.advanceLexer();");
      ostr.println("    } else {");
      ostr.println("      builder.error(\"Expected \" + type + \", but get: \" + actualType);");
      if (!Options.getAutomaticErrorRecovery()) ostr.println("      throw new ParseException();");
      ostr.println("    }");
      ostr.println("    return type;");

      ostr.println("  }");
      ostr.println("");
      if (jj2index != 0) {
        ostr.println("  static private final class LookaheadSuccess extends java.lang.Error { }");
        ostr.println(
            "  " + staticOpt() + "final private LookaheadSuccess jj_ls = new LookaheadSuccess();");
        ostr.println("  " + staticOpt() + "private void jj_on_la1() {");
        ostr.println("    advanceLexer();");
        ostr.println("    jj_test_jj_la();");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private void jj_test_jj_la() {");
        ostr.println("    if (jj_la == 0 && maxIndex == currentIndex)");
        ostr.println("       throw jj_ls;");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private IElementType advanceLexer() {");
        ostr.println("    if (maxIndex == currentIndex) {");
        ostr.println("      IElementType result = tokens.get(currentIndex);");
        ostr.println("      builder.advanceLexer();");
        ostr.println("      tokens.add(builder.getTokenType());");
        ostr.println("      maxIndex++;");
        ostr.println("      currentIndex++;");
        ostr.println("      jj_la--;");
        ostr.println("      return result;");
        ostr.println("    }");
        ostr.println("    return tokens.get(currentIndex++);");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private boolean jj_scan_token(IElementType kind) {");
        ostr.println("    IElementType nextType = advanceLexer();");
        ostr.println("    if (nextType != kind) return true;");
        ostr.println("    jj_test_jj_la();");
        ostr.println("    return false;");
        ostr.println("  }");
        ostr.println("");
      }
      ostr.println("");
      ostr.println("/** Get the next Token. Use getNextTokenType instead. ");
      ostr.println("  " + staticOpt() + "final public Token getNextToken() {");
      if (Options.getCacheTokens()) {
        ostr.println("    if ((token = jj_nt).next != null) jj_nt = jj_nt.next;");
        ostr.println("    else jj_nt = jj_nt.next = token_source.getNextToken();");
      } else {
        ostr.println("    if (token.next != null) token = token.next;");
        ostr.println("    else token = token.next = token_source.getNextToken();");
        ostr.println("    jj_ntk = -1;");
      }
      if (Options.getErrorReporting()) {
        ostr.println("    jj_gen++;");
      }
      if (Options.getDebugParser()) {
        ostr.println("      trace_token(token, \" (in getNextToken)\");");
      }
      ostr.println("    return token;");
      ostr.println("  } */");
      ostr.println("");
      ostr.println("/** Get the specific Token. */");
      ostr.println("  " + staticOpt() + "final public IElementType getTokenType(int index) {");
      if (lookaheadNeeded) {
        //        ostr.println("    Token t = jj_lookingAhead ? jj_scanpos : token;");
      } else {
        ostr.println("    IElementType t = null;");
      }
      ostr.println("    for (int i = 0; i < index; i++) {");
      ostr.println("      t = builder.getTokenType();");
      ostr.println("    }");
      ostr.println("    return t;");
      ostr.println("  }");
      ostr.println("");
      ostr.println("    private IElementType getType() {");
      ostr.println("      return builder.getTokenType();");
      ostr.println("    }");
      ostr.println("");

      if (Options.getDebugParser()) {
        ostr.println("  " + staticOpt() + "private int trace_indent = 0;");
        ostr.println("  " + staticOpt() + "private boolean trace_enabled = true;");
        ostr.println("");
        ostr.println("/** Enable tracing. */");
        ostr.println("  " + staticOpt() + "final public void enable_tracing() {");
        ostr.println("    trace_enabled = true;");
        ostr.println("  }");
        ostr.println("");
        ostr.println("/** Disable tracing. */");
        ostr.println("  " + staticOpt() + "final public void disable_tracing() {");
        ostr.println("    trace_enabled = false;");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private void trace_call(String s) {");
        ostr.println("    if (trace_enabled) {");
        ostr.println("      for (int i = 0; i < trace_indent; i++) { System.out.print(\" \"); }");
        ostr.println("      System.out.println(\"Call:   \" + s);");
        ostr.println("    }");
        ostr.println("    trace_indent = trace_indent + 2;");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private void trace_return(String s) {");
        ostr.println("    trace_indent = trace_indent - 2;");
        ostr.println("    if (trace_enabled) {");
        ostr.println("      for (int i = 0; i < trace_indent; i++) { System.out.print(\" \"); }");
        ostr.println("      System.out.println(\"Return: \" + s);");
        ostr.println("    }");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private void trace_token(Token t, String where) {");
        ostr.println("    if (trace_enabled) {");
        ostr.println("      for (int i = 0; i < trace_indent; i++) { System.out.print(\" \"); }");
        ostr.println("      System.out.print(\"Consumed token: <\" + tokenImage[t.kind]);");
        ostr.println(
            "      if (t.kind != 0 && !tokenImage[t.kind].equals(\"\\\"\" + t.image + \"\\\"\")) {");
        ostr.println("        System.out.print(\": \\\"\" + t.image + \"\\\"\");");
        ostr.println("      }");
        ostr.println(
            "      System.out.println(\" at line \" + t.beginLine + "
                + "\" column \" + t.beginColumn + \">\" + where);");
        ostr.println("    }");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  " + staticOpt() + "private void trace_scan(Token t1, int t2) {");
        ostr.println("    if (trace_enabled) {");
        ostr.println("      for (int i = 0; i < trace_indent; i++) { System.out.print(\" \"); }");
        ostr.println("      System.out.print(\"Visited token: <\" + tokenImage[t1.kind]);");
        ostr.println(
            "      if (t1.kind != 0 && !tokenImage[t1.kind].equals(\"\\\"\" + t1.image + \"\\\"\")) {");
        ostr.println("        System.out.print(\": \\\"\" + t1.image + \"\\\"\");");
        ostr.println("      }");
        ostr.println(
            "      System.out.println(\" at line \" + t1.beginLine + \""
                + " column \" + t1.beginColumn + \">; Expected token: <\" + tokenImage[t2] + \">\");");
        ostr.println("    }");
        ostr.println("  }");
        ostr.println("");
      } else {
        ostr.println("  /** Enable tracing. */");
        ostr.println("  " + staticOpt() + "final public void enable_tracing() {");
        ostr.println("  }");
        ostr.println("");
        ostr.println("  /** Disable tracing. */");
        ostr.println("  " + staticOpt() + "final public void disable_tracing() {");
        ostr.println("  }");
        ostr.println("");
      }

      if (cu_from_insertion_point_2.size() != 0) {
        printTokenSetup((Token) (cu_from_insertion_point_2.get(0)));
        ccol = 1;
        for (Iterator it = cu_from_insertion_point_2.iterator(); it.hasNext(); ) {
          t = (Token) it.next();
          printToken(t, ostr);
        }
        printTrailingComments(t, ostr);
      }
      ostr.println("");

      ostr.close();
    } // matches "if (Options.getBuildParser())"
  }