Skip to content
This repository
tag: 1.2.5
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 77 lines (69 sloc) 1.934 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "xhp.hpp"
#include "xhp_preprocess.hpp"
#include <sstream>
using namespace std;
extern int xhpdebug;
#include <iostream>
XHPResult xhp_preprocess(istream &in, string &out, bool isEval, string &errDescription, uint32_t &errLineno) {

  // Read stream to string
  stringbuf sb;
  in >> noskipws >> &sb;
  string buffer = sb.str();
  return xhp_preprocess(buffer, out, isEval, errDescription, errLineno);
}

XHPResult xhp_preprocess(string &in, string &out, bool isEval, string &errDescription, uint32_t &errLineno) {

  // Create a flex buffer
  in.reserve(in.size() + 1);
  char* buffer = const_cast<char*>(in.c_str());
  buffer[in.size() + 1] = 0; // need double NULL for scan_buffer

  // Does this maybe contain XHP?
  bool maybe_xhp = false;
  for (const char* jj = buffer; *jj; ++jj) {
    if (*jj == '<') { // </a>
      if (jj[1] == '/') {
        maybe_xhp = true;
        break;
      }
    } else if (*jj == '/') { // <a />
      if (jj[1] == '>') {
        maybe_xhp = true;
        break;
      }
    } else if (*jj == ':') { // :fb:thing
      if ((jj[1] >= 'a' && jj[1] <= 'z') || (jj[1] >= 'A' && jj[1] <= 'Z')) {
        maybe_xhp = true;
      }
    } else if (!memcmp(jj, "element", 7)) {
      maybe_xhp = true;
      break;
    }
  }

  // Early bail
  if (!maybe_xhp) {
    return XHPDidNothing;
  }

  // Parse the PHP
  void* scanner;
  code_rope new_code;
  yy_extra_type extra;
  extra.insert_token = isEval ? T_OPEN_TAG_FAKE : 0;
  xhplex_init(&scanner);
  xhpset_extra(&extra, scanner);
  xhp_scan_buffer(buffer, in.size() + 2, scanner);
#ifdef DEBUG
  xhpdebug = 1;
#endif
  xhpparse(scanner, &new_code);
  xhplex_destroy(scanner);

  // Check to see what happened
  if (extra.terminated) {
    errDescription = extra.error;
    errLineno = extra.lineno;
    return XHPErred;
  } else if (!extra.used) {
    return XHPDidNothing;
  } else {
    out = new_code.c_str();
    return XHPRewrote;
  }
}
Something went wrong with that request. Please try again.