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