示例#1
0
  private void runTest(
      String doc,
      ISVPreProcIncFileProvider inc_provider,
      String images[],
      SVDBLocation locations[]) {

    SVPreProcessor2 preproc =
        new SVPreProcessor2(getName(), new StringInputStream(doc), inc_provider, new FileMapper());

    SVPreProcOutput output = preproc.preprocess();

    List<SVPreProcOutput.FileChangeInfo> file_map = output.getFileMap();

    for (SVPreProcOutput.FileChangeInfo e : file_map) {
      fLog.debug("FileMap Entry: " + e.fStartIdx + " " + e.fFileId + " " + e.fLineno);
    }

    SVLexer lexer = new SVLexer();
    lexer.init(null, output);

    System.out.println("Output:\n" + output.dump());

    SVToken t;
    int idx = 0;
    while ((t = lexer.consumeToken()) != null) {
      fLog.debug(
          "Token: "
              + t.getImage()
              + " @ "
              + t.getStartLocation().getFileId()
              + ":"
              + t.getStartLocation().getLine());
      assertEquals(images[idx], t.getImage());
      assertEquals(
          "File ID of " + images[idx] + "(" + idx + ")",
          locations[idx].getFileId(),
          t.getStartLocation().getFileId());
      assertEquals(
          "Line of " + images[idx] + "(" + idx + ")",
          locations[idx].getLine(),
          t.getStartLocation().getLine());
      idx++;
    }
  }
  public SVPreProcOutput preprocess() {
    int ch, last_ch = -1;
    int end_comment1 = -1, end_comment2 = -1;
    long start, end;
    boolean in_string = false;
    boolean ifdef_enabled = true;
    boolean found_single_line_comment = false;

    start = System.currentTimeMillis();

    while ((ch = get_ch()) != -1) {
      found_single_line_comment = false;
      if (!in_string) {
        // Handle comment
        if (ch == '/') {
          int ch2 = get_ch();

          if (ch2 == '/') {
            output(' ');
            found_single_line_comment = true;
            beginComment();
            while ((ch = get_ch()) != -1 && ch != '\n' && ch != '\r') {
              fCommentBuffer.append((char) ch);
            }
            fCommentBuffer.append('\n');

            // Handle
            if (ch == '\r') {
              ch = get_ch();
              if (ch != '\n') {
                unget_ch(ch);
              }
            }
            ch = '\n';
            last_ch = ' ';
          } else if (ch2 == '*') {
            end_comment1 = -1;
            end_comment2 = -1;

            output(' ');

            beginComment();
            while ((ch = get_ch()) != -1) {
              end_comment1 = end_comment2;
              end_comment2 = ch;

              if (end_comment1 == '*' && end_comment2 == '/') {
                endComment();
                break;
              } else {
                fCommentBuffer.append((char) ch);
              }
            }
            ch = ' ';
            last_ch = ' ';
          } else {
            unget_ch(ch2);
          }
        }

        if (!Character.isWhitespace(ch) && fInComment) {
          // Send accumlated comment to observer
          endComment();
        }

        if (ch == '`') {
          // Processing an ifdef may affect enablement
          handle_preproc_directive();
          ifdef_enabled = ifdef_enabled();
          if (!ifdef_enabled) {
            output(' ');
          }
        } else {
          if (ch == '"' && last_ch != '\\') {
            // Enter string
            in_string = true;
          }
          if (ifdef_enabled) {
            output((char) ch);
          }
        }
      } else {
        // In String
        if (ch == '"' && last_ch != '\\') {
          in_string = false;
        }
        if (ifdef_enabled) {
          output((char) ch);
        }
      }

      // Consecutive back-slashes convert to
      // a single backslash. For tracking purposes,
      // convert to space
      if (last_ch == '\\' && ch == '\\') {
        last_ch = ' ';
      } else {
        last_ch = ch;
      }

      if (fInComment && !found_single_line_comment && ch == '\n') {
        endComment();
      }
    }

    SVPreProcOutput ret = new SVPreProcOutput(fOutput, null, fFileMap, fFileList);
    ret.setFileTree(fInputCurr.getFileTree());

    // Clean up after any unbalanced pre-processor directives
    cleanup_preproc_leftovers();

    // Leave final file
    fInputCurr.close();

    // Finally, save the full pre-processor state to the final file
    /*
    last_file.fFileTree.fDefinedMacros.clear();
    for (Entry<String, SVDBMacroDef> e : fMacroMap.entrySet()) {
    	if (!e.getKey().equals("__FILE__") && !e.getKey().equals("__LINE__")) {
    		last_file.fFileTree.fDefinedMacros.put(e.getKey(), e.getValue());
    	}
    }
     */

    end = System.currentTimeMillis();

    if (fIndexStats != null) {
      if (fInputCurr.getInput() instanceof SVFileBuffer) {
        fIndexStats.incLastIndexFileReadTime(((SVFileBuffer) fInputCurr.getInput()).getReadTime());
      }
      fIndexStats.incNumLines(fInputCurr.getLineCount());
      fIndexStats.incLastIndexPreProcessTime(end - start);
      fIndexStats.incNumProcessedFiles();
    }

    return ret;
  }