protected static void addregexpr(final TokenProduction p) {
   Integer ii;
   rexprlist.add(p);
   if (Options.getUserTokenManager()) {
     if (p.lexStates == null || p.lexStates.length != 1 || !p.lexStates[0].equals("DEFAULT")) {
       JavaCCErrors.warning(
           p,
           "Ignoring lexical state specifications since option "
               + "USER_TOKEN_MANAGER has been set to true.");
     }
   }
   if (p.lexStates == null) {
     return;
   }
   for (int i = 0; i < p.lexStates.length; i++) {
     for (int j = 0; j < i; j++) {
       if (p.lexStates[i].equals(p.lexStates[j])) {
         JavaCCErrors.parse_error(
             p, "Multiple occurrence of \"" + p.lexStates[i] + "\" in lexical state list.");
       }
     }
     if (lexstate_S2I.get(p.lexStates[i]) == null) {
       ii = new Integer(nextFreeLexState++);
       lexstate_S2I.put(p.lexStates[i], ii);
       lexstate_I2S.put(ii, p.lexStates[i]);
       simple_tokens_table.put(p.lexStates[i], new java.util.Hashtable());
     }
   }
 }
  public void CheckUnmatchability() {
    RegularExpression curRE;
    int numStrings = 0;

    for (int i = 0; i < getChoices().size(); i++) {
      if (!(curRE = (RegularExpression) getChoices().get(i)).private_rexp
          &&
          // curRE instanceof RJustName &&
          curRE.ordinal > 0
          && curRE.ordinal < ordinal
          && LexGen.lexStates[curRE.ordinal] == LexGen.lexStates[ordinal]) {
        if (label != null)
          JavaCCErrors.warning(
              this,
              "Regular Expression choice : " + curRE.label + " can never be matched as : " + label);
        else
          JavaCCErrors.warning(
              this,
              "Regular Expression choice : "
                  + curRE.label
                  + " can never be matched as token of kind : "
                  + ordinal);
      }

      if (!curRE.private_rexp && curRE instanceof RStringLiteral) numStrings++;
    }
  }
 protected static void add_token_manager_decls(final Token t, final java.util.List decls) {
   if (token_mgr_decls != null) {
     JavaCCErrors.parse_error(t, "Multiple occurrence of \"TOKEN_MGR_DECLS\".");
   } else {
     token_mgr_decls = decls;
     if (Options.getUserTokenManager()) {
       JavaCCErrors.warning(
           t,
           "Ignoring declarations in \"TOKEN_MGR_DECLS\" since option "
               + "USER_TOKEN_MANAGER has been set to true.");
     }
   }
 }
 protected static void makeTryBlock(
     final Token tryLoc,
     final Container result,
     final Container nestedExp,
     final List types,
     final List ids,
     final List catchblks,
     final List finallyblk) {
   if (catchblks.size() == 0 && finallyblk == null) {
     JavaCCErrors.parse_error(
         tryLoc, "Try block must contain at least one catch or finally block.");
     return;
   }
   final TryBlock tblk = new TryBlock();
   tblk.setLine(tryLoc.beginLine);
   tblk.setColumn(tryLoc.beginColumn);
   tblk.exp = (Expansion) (nestedExp.member);
   tblk.exp.parent = tblk;
   tblk.exp.ordinal = 0;
   tblk.types = types;
   tblk.ids = ids;
   tblk.catchblks = catchblks;
   tblk.finallyblk = finallyblk;
   result.member = tblk;
 }
 protected static char character_descriptor_assign(final Token t, final String s) {
   if (s.length() != 1) {
     JavaCCErrors.parse_error(t, "String in character list may contain only one character.");
     return ' ';
   } else {
     return s.charAt(0);
   }
 }
 protected static char character_descriptor_assign(
     final Token t, final String s, final String left) {
   if (s.length() != 1) {
     JavaCCErrors.parse_error(t, "String in character list may contain only one character.");
     return ' ';
   } else if ((left.charAt(0)) > (s.charAt(0))) {
     JavaCCErrors.parse_error(
         t,
         "Right end of character range \'"
             + s
             + "\' has a lower ordinal value than the left end of character range \'"
             + left
             + "\'.");
     return left.charAt(0);
   } else {
     return s.charAt(0);
   }
 }
 protected static void insertionpointerrors(final Token t) {
   while (first_cu_token != t) {
     add_cu_token_here.add(first_cu_token);
     first_cu_token = first_cu_token.next;
   }
   if (!insertionpoint1set || !insertionpoint2set) {
     JavaCCErrors.parse_error(
         t, "Parser class has not been defined between PARSER_BEGIN and PARSER_END.");
   }
 }
Example #8
0
  public static void createOutputDir(File outputDir) {
    if (!outputDir.exists()) {
      JavaCCErrors.warning(
          "Output directory \"" + outputDir + "\" does not exist. Creating the directory.");

      if (!outputDir.mkdirs()) {
        JavaCCErrors.semantic_error("Cannot create the output directory : " + outputDir);
        return;
      }
    }

    if (!outputDir.isDirectory()) {
      JavaCCErrors.semantic_error("\"" + outputDir + " is not a valid output directory.");
      return;
    }

    if (!outputDir.canWrite()) {
      JavaCCErrors.semantic_error(
          "Cannot write to the output output directory : \"" + outputDir + "\"");
      return;
    }
  }
 protected static void setinsertionpoint(final Token t, final int no) {
   do {
     add_cu_token_here.add(first_cu_token);
     first_cu_token = first_cu_token.next;
   } while (first_cu_token != t);
   if (no == 1) {
     if (insertionpoint1set) {
       JavaCCErrors.parse_error(t, "Multiple declaration of parser class.");
     } else {
       insertionpoint1set = true;
       add_cu_token_here = cu_to_insertion_point_2;
     }
   } else {
     add_cu_token_here = cu_from_insertion_point_2;
     insertionpoint2set = true;
   }
   first_cu_token = t;
 }
Example #10
0
  @Override
  public void start() throws MetaParseException, IOException {
    if (JavaCCErrors.getErrorCount() != 0) {
      throw new MetaParseException();
    }

    if (!Options.getBuildParser()) {
      return;
    }

    File path = new File(Options.getOutputDirectory(), state.parserClass() + ".java");
    OutputFile outputFile = new OutputFile(path);
    IndentingPrintWriter out = outputFile.getPrintWriter();
    try {
      generate(out);
    } finally {
      out.close();
    }
  }
Example #11
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())"
  }
 protected static void compare(final Token t, final String id1, final String id2) {
   if (!id2.equals(id1)) {
     JavaCCErrors.parse_error(
         t, "Name " + id2 + " must be the same as that used at PARSER_BEGIN (" + id1 + ")");
   }
 }
 protected static String remove_escapes_and_quotes(final Token t, final String str) {
   String retval = "";
   int index = 1;
   char ch, ch1;
   int ordinal;
   while (index < str.length() - 1) {
     if (str.charAt(index) != '\\') {
       retval += str.charAt(index);
       index++;
       continue;
     }
     index++;
     ch = str.charAt(index);
     if (ch == 'b') {
       retval += '\b';
       index++;
       continue;
     }
     if (ch == 't') {
       retval += '\t';
       index++;
       continue;
     }
     if (ch == 'n') {
       retval += '\n';
       index++;
       continue;
     }
     if (ch == 'f') {
       retval += '\f';
       index++;
       continue;
     }
     if (ch == 'r') {
       retval += '\r';
       index++;
       continue;
     }
     if (ch == '"') {
       retval += '\"';
       index++;
       continue;
     }
     if (ch == '\'') {
       retval += '\'';
       index++;
       continue;
     }
     if (ch == '\\') {
       retval += '\\';
       index++;
       continue;
     }
     if (ch >= '0' && ch <= '7') {
       ordinal = (ch) - ('0');
       index++;
       ch1 = str.charAt(index);
       if (ch1 >= '0' && ch1 <= '7') {
         ordinal = ordinal * 8 + (ch1) - ('0');
         index++;
         ch1 = str.charAt(index);
         if (ch <= '3' && ch1 >= '0' && ch1 <= '7') {
           ordinal = ordinal * 8 + (ch1) - ('0');
           index++;
         }
       }
       retval += (char) ordinal;
       continue;
     }
     if (ch == 'u') {
       index++;
       ch = str.charAt(index);
       if (hexchar(ch)) {
         ordinal = hexval(ch);
         index++;
         ch = str.charAt(index);
         if (hexchar(ch)) {
           ordinal = ordinal * 16 + hexval(ch);
           index++;
           ch = str.charAt(index);
           if (hexchar(ch)) {
             ordinal = ordinal * 16 + hexval(ch);
             index++;
             ch = str.charAt(index);
             if (hexchar(ch)) {
               ordinal = ordinal * 16 + hexval(ch);
               index++;
               continue;
             }
           }
         }
       }
       JavaCCErrors.parse_error(
           t,
           "Encountered non-hex character '"
               + ch
               + "' at position "
               + index
               + " of string "
               + "- Unicode escape must have 4 hex digits after it.");
       return retval;
     }
     JavaCCErrors.parse_error(
         t, "Illegal escape sequence '\\" + ch + "' at position " + index + " of string.");
     return retval;
   }
   return retval;
 }