PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/xhp/xhp_preprocess.cpp

http://github.com/facebook/xhp
C++ | 89 lines | 59 code | 10 blank | 20 comment | 5 complexity | e368535c09d265156823a03feca2fe13 MD5 | raw file
Possible License(s): MIT, MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2009 - 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE.PHP, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "xhp.hpp"
  17. #include "xhp_preprocess.hpp"
  18. #include "fastpath.hpp"
  19. #include <sstream>
  20. using namespace std;
  21. extern int xhpdebug;
  22. #include <iostream>
  23. XHPResult xhp_preprocess(istream &in, string &out, bool isEval, string &errDescription, uint32_t &errLineno) {
  24. // Read stream to string
  25. stringbuf sb;
  26. in >> noskipws >> &sb;
  27. string buffer = sb.str();
  28. return xhp_preprocess(buffer, out, isEval, errDescription, errLineno);
  29. }
  30. XHPResult xhp_preprocess(string &in, string &out, bool isEval, string &errDescription, uint32_t &errLineno) {
  31. xhp_flags_t flags;
  32. memset(&flags, 0, sizeof(xhp_flags_t));
  33. flags.eval = isEval;
  34. flags.short_tags = true;
  35. flags.idx_expr = true;
  36. flags.include_debug = true;
  37. flags.emit_namespaces = false;
  38. return xhp_preprocess(in, out, errDescription, errLineno, flags);
  39. }
  40. XHPResult xhp_preprocess(std::string &in, std::string &out, std::string &errDescription, uint32_t &errLineno, const xhp_flags_t &flags) {
  41. // Early bail if the code doesn't contain anything that looks like XHP
  42. char* buffer = const_cast<char*>(in.c_str());
  43. if (!xhp_fastpath(buffer, in.length(), flags)) {
  44. return XHPDidNothing;
  45. }
  46. // Create a flex buffer
  47. in.reserve(in.size() + 1);
  48. buffer = const_cast<char*>(in.c_str());
  49. buffer[in.size() + 1] = 0; // need double NULL for scan_buffer
  50. // Parse the PHP
  51. void* scanner;
  52. code_rope new_code;
  53. yy_extra_type extra;
  54. extra.idx_expr = flags.idx_expr;
  55. extra.include_debug = flags.include_debug;
  56. extra.insert_token = flags.eval ? T_OPEN_TAG_FAKE : 0;
  57. extra.short_tags = flags.short_tags;
  58. extra.asp_tags = flags.asp_tags;
  59. extra.emit_namespaces = flags.emit_namespaces;
  60. xhplex_init(&scanner);
  61. xhpset_extra(&extra, scanner);
  62. xhp_scan_buffer(buffer, in.size() + 2, scanner);
  63. #ifdef DEBUG
  64. xhpdebug = 1;
  65. #endif
  66. xhpparse(scanner, &new_code);
  67. xhplex_destroy(scanner);
  68. // Check to see what happened
  69. if (extra.terminated) {
  70. errDescription = extra.error;
  71. errLineno = extra.lineno;
  72. return XHPErred;
  73. } else if (!extra.used) {
  74. return XHPDidNothing;
  75. } else {
  76. out = new_code.c_str();
  77. return XHPRewrote;
  78. }
  79. }