parse_rexpr_test.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <iostream>
  7. #include <iterator>
  8. #include <algorithm>
  9. #include <sstream>
  10. #include "../rexpr/ast.hpp"
  11. #include "../rexpr/rexpr.hpp"
  12. #include "../rexpr/error_handler.hpp"
  13. #include "../rexpr/config.hpp"
  14. #include "../rexpr/printer.hpp"
  15. #include "testing.hpp"
  16. namespace fs = boost::filesystem;
  17. namespace testing = boost::spirit::x3::testing;
  18. auto parse = [](std::string const& source, fs::path input_path)-> std::string
  19. {
  20. std::stringstream out;
  21. using rexpr::parser::iterator_type;
  22. iterator_type iter(source.begin());
  23. iterator_type const end(source.end());
  24. // Our AST
  25. rexpr::ast::rexpr ast;
  26. // Our error handler
  27. using boost::spirit::x3::with;
  28. using rexpr::parser::error_handler_type;
  29. using rexpr::parser::error_handler_tag;
  30. error_handler_type error_handler(iter, end, out, input_path.string()); // Our error handler
  31. // Our parser
  32. auto const parser =
  33. // we pass our error handler to the parser so we can access
  34. // it later on in our on_error and on_sucess handlers
  35. with<error_handler_tag>(std::ref(error_handler))
  36. [
  37. rexpr::rexpr()
  38. ];
  39. // Go forth and parse!
  40. using boost::spirit::x3::ascii::space;
  41. bool success = phrase_parse(iter, end, parser, space, ast);
  42. if (success)
  43. {
  44. if (iter != end)
  45. error_handler(iter, "Error! Expecting end of input here: ");
  46. else
  47. rexpr::ast::rexpr_printer{out}(ast);
  48. }
  49. return out.str();
  50. };
  51. int num_files_tested = 0;
  52. auto compare = [](fs::path input_path, fs::path expect_path)
  53. {
  54. testing::compare(input_path, expect_path, parse);
  55. ++num_files_tested;
  56. };
  57. int main(int argc, char* argv[])
  58. {
  59. if (argc < 2)
  60. {
  61. std::cout << "usage: " << fs::path(argv[0]).filename() << " path/to/test/files" << std::endl;
  62. return -1;
  63. }
  64. std::cout << "===================================================================================================" << std::endl;
  65. std::cout << "Testing: " << fs::absolute(fs::path(argv[1])) << std::endl;
  66. int r = testing::for_each_file(fs::path(argv[1]), compare);
  67. if (r == 0)
  68. std::cout << num_files_tested << " files tested." << std::endl;
  69. std::cout << "===================================================================================================" << std::endl;
  70. return r;
  71. }