• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
__anon20faf3ed0102(std::string const& source, fs::path input_path)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 auto compare = [](fs::path input_path, fs::path expect_path)
__anon20faf3ed0202(fs::path input_path, fs::path expect_path) 66 {
67    testing::compare(input_path, expect_path, parse);
68    ++num_files_tested;
69 };
70 
main(int argc,char * argv[])71 int main(int argc, char* argv[])
72 {
73     if (argc < 2)
74     {
75        std::cout << "usage: " << fs::path(argv[0]).filename() << " path/to/test/files" << std::endl;
76        return -1;
77     }
78 
79     std::cout << "===================================================================================================" << std::endl;
80     std::cout << "Testing: " << fs::absolute(fs::path(argv[1])) << std::endl;
81     int r = testing::for_each_file(fs::path(argv[1]), compare);
82     if (r == 0)
83         std::cout << num_files_tested << " files tested." << std::endl;
84     std::cout << "===================================================================================================" << std::endl;
85     return r;
86 }
87