示例#1
0
String highlight_php(CStrRef source, int line) {
  Lock lock(Eval::Parser::s_lock);

  const char *input = source.data();
  istringstream iss(input);
  StringBuffer res;

  Eval::Scanner scanner(new ylmm::basic_buffer(iss, false, true),
                        true, false, true);
  Eval::Token tok1, tok2;
  std::vector<pair<int, Eval::Token> > ahead_tokens;
  ylmm::basic_location loc;

  const char *colorComment = NULL, *endComment = NULL;
  get_color(365 /* T_COMMENT */, 0, 0, colorComment, endComment);

  int prev = 0;
  int tokid = scanner.getNextToken(tok1, loc);
  int next = 0;
  while (tokid) {
    // look ahead
    next = scanner.getNextToken(tok2, loc);
    while (next == 370 /* T_WHITESPACE */ ||
           next == 365 /* T_COMMENT */ ||
           next == 366 /* T_DOC_COMMENT */) {
      ahead_tokens.push_back(pair<int, Eval::Token>(next, tok2));
      next = scanner.getNextToken(tok2, loc);
    }

    if (tokid < 256) {
      res.append((char)tokid);
    } else {
      const char *color = NULL, *end = NULL;
      get_color(tokid, prev, next, color, end);

      const std::string &text = tok1.getText();
      int offset = 0;
      if (text[0] == '$') {
        res.append('$');
        offset = 1;
      }
      append_line_no(res, text.c_str() + offset, line, color, end);
    }

    if (!ahead_tokens.empty()) {
      for (unsigned int i = 0; i < ahead_tokens.size(); i++) {
        bool comment = ahead_tokens[i].first != 370 /* T_WHITESPACE */;
        append_line_no(res, ahead_tokens[i].second.getText().c_str(), line,
                       comment ? colorComment : NULL,
                       comment ? endComment : NULL);
      }
      ahead_tokens.clear();
    }

    if (!(tokid == 370 /* T_WHITESPACE */ ||
          tokid == 365 /* T_COMMENT */ ||
          tokid == 366 /* T_DOC_COMMENT */)) {
      prev = tokid;
    }
    tok1 = tok2;
    tokid = next;
  }

  append_line_no(res, NULL, line, NULL, NULL);
  return res.detach();
}
示例#2
0
String highlight_php(CStrRef source, int line /* = 0 */,
                     int lineFocus0 /* = 0 */, int charFocus0 /* = 0 */,
                     int lineFocus1 /* = 0 */, int charFocus1 /* = 0 */) {
  StringBuffer res;
  Scanner scanner(source.data(), source.size(),
                  Scanner::AllowShortTags | Scanner::ReturnAllTokens);
  ScannerToken tok1, tok2;
  std::vector<pair<int, string> > ahead_tokens;
  Location loc1, loc2;

  const char *colorComment = NULL, *endComment = NULL;
  get_color(T_COMMENT, 0, 0, colorComment, endComment);

  int prev = 0;
  int tokid = scanner.getNextToken(tok1, loc1);
  int next = 0;
  while (tokid) {
    // look ahead
    next = scanner.getNextToken(tok2, loc2);
    while (next == T_WHITESPACE ||
           next == T_COMMENT ||
           next == T_DOC_COMMENT) {

      string text = tok2.text();
      string hcolor = check_char_highlight(lineFocus0, charFocus0,
                                           lineFocus1, charFocus1, loc2);
      if (!hcolor.empty()) {
        text = hcolor + text + ANSI_COLOR_END;
      }

      ahead_tokens.push_back(pair<int, string>(next, text));
      next = scanner.getNextToken(tok2, loc2);
    }

    string hcolor = check_char_highlight(lineFocus0, charFocus0,
                                         lineFocus1, charFocus1, loc1);

    if (tokid < 256) {
      if (!hcolor.empty()) {
        res.append(hcolor);
        res.append((char)tokid);
        res.append(ANSI_COLOR_END);
      } else {
        res.append((char)tokid);
      }
    } else {
      const char *color = NULL, *end = NULL;
      get_color(tokid, prev, next, color, end);
      if (!hcolor.empty()) {
        color = hcolor.c_str();
        end = ANSI_COLOR_END;
      }

      const std::string &text = tok1.text();
      int offset = 0;
      if (text[0] == '$') {
        if (!hcolor.empty()) {
          res.append(hcolor);
          res.append('$');
          res.append(ANSI_COLOR_END);
        } else {
          res.append('$');
        }
        offset = 1;
      }
      append_line_no(res, text.c_str() + offset, line, color, end,
                     lineFocus0, charFocus0, lineFocus1, charFocus1);
    }

    if (!ahead_tokens.empty()) {
      for (unsigned int i = 0; i < ahead_tokens.size(); i++) {
        bool comment = ahead_tokens[i].first != T_WHITESPACE;
        append_line_no(res, ahead_tokens[i].second.c_str(), line,
                       comment ? colorComment : NULL,
                       comment ? endComment : NULL,
                       lineFocus0, charFocus0, lineFocus1, charFocus1);
      }
      ahead_tokens.clear();
    }

    if (!(tokid == T_WHITESPACE || tokid == T_COMMENT ||
          tokid == T_DOC_COMMENT)) {
      prev = tokid;
    }
    tok1 = tok2;
    loc1 = loc2;
    tokid = next;
  }

  append_line_no(res, NULL, line, NULL, NULL,
                 lineFocus0, charFocus0, lineFocus1, charFocus1);
  return res.detach();
}