|
| 1 | +// Copyright (c) 2014-2017 Dr. Colin Hirsch and Daniel Frey |
| 2 | +// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ |
| 3 | + |
| 4 | +#ifndef TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_TRACER_HPP |
| 5 | +#define TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_TRACER_HPP |
| 6 | + |
| 7 | +#include <cassert> |
| 8 | +#include <iomanip> |
| 9 | +#include <iostream> |
| 10 | +#include <utility> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "../config.hpp" |
| 14 | +#include "../normal.hpp" |
| 15 | + |
| 16 | +#include "../internal/demangle.hpp" |
| 17 | + |
| 18 | +namespace tao |
| 19 | +{ |
| 20 | + namespace TAOCPP_JSON_PEGTL_NAMESPACE |
| 21 | + { |
| 22 | + struct trace_state |
| 23 | + { |
| 24 | + unsigned rule = 0; |
| 25 | + unsigned line = 0; |
| 26 | + std::vector< unsigned > stack; |
| 27 | + }; |
| 28 | + |
| 29 | + template< typename Rule > |
| 30 | + struct tracer |
| 31 | + : normal< Rule > |
| 32 | + { |
| 33 | + template< typename Input, typename... States > |
| 34 | + static void start( const Input& in, States&&... ) |
| 35 | + { |
| 36 | + std::cerr << in.position() << " start " << internal::demangle< Rule >() << std::endl; |
| 37 | + } |
| 38 | + |
| 39 | + template< typename Input > |
| 40 | + static void start( const Input& in, trace_state& ts ) |
| 41 | + { |
| 42 | + std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ++ts.rule << " " << in.position() << " start " << internal::demangle< Rule >() << std::endl; |
| 43 | + ts.stack.push_back( ts.rule ); |
| 44 | + } |
| 45 | + |
| 46 | + template< typename Input, typename... States > |
| 47 | + static void success( const Input& in, States&&... ) |
| 48 | + { |
| 49 | + std::cerr << in.position() << " success " << internal::demangle< Rule >() << std::endl; |
| 50 | + } |
| 51 | + |
| 52 | + template< typename Input > |
| 53 | + static void success( const Input& in, trace_state& ts ) |
| 54 | + { |
| 55 | + assert( !ts.stack.empty() ); |
| 56 | + std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ts.stack.back() << " " << in.position() << " success " << internal::demangle< Rule >() << std::endl; |
| 57 | + ts.stack.pop_back(); |
| 58 | + } |
| 59 | + |
| 60 | + template< typename Input, typename... States > |
| 61 | + static void failure( const Input& in, States&&... ) |
| 62 | + { |
| 63 | + std::cerr << in.position() << " failure " << internal::demangle< Rule >() << std::endl; |
| 64 | + } |
| 65 | + |
| 66 | + template< typename Input > |
| 67 | + static void failure( const Input& in, trace_state& ts ) |
| 68 | + { |
| 69 | + assert( !ts.stack.empty() ); |
| 70 | + std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ts.stack.back() << " " << in.position() << " failure " << internal::demangle< Rule >() << std::endl; |
| 71 | + ts.stack.pop_back(); |
| 72 | + } |
| 73 | + |
| 74 | + template< template< typename... > class Action, typename Input, typename... States > |
| 75 | + static void apply0( const Input&, States&&... st ) |
| 76 | + { |
| 77 | + std::cerr << "apply0 " << internal::demangle< Action< Rule > >() << std::endl; |
| 78 | + Action< Rule >::apply0( st... ); |
| 79 | + } |
| 80 | + |
| 81 | + template< template< typename... > class Action, typename Input > |
| 82 | + static void apply0( const Input&, trace_state& ts ) |
| 83 | + { |
| 84 | + std::cerr << std::setw( 6 ) << ++ts.line << " " << internal::demangle< Action< Rule > >() << "::apply0()" << std::endl; |
| 85 | + Action< Rule >::apply0( ts ); |
| 86 | + } |
| 87 | + |
| 88 | + template< template< typename... > class Action, typename Iterator, typename Input, typename... States > |
| 89 | + static void apply( const Iterator& begin, const Input& in, States&&... st ) |
| 90 | + { |
| 91 | + std::cerr << "apply " << internal::demangle< Action< Rule > >() << std::endl; |
| 92 | + using action_t = typename Input::action_t; |
| 93 | + const action_t action_input( begin, in ); |
| 94 | + Action< Rule >::apply( action_input, st... ); |
| 95 | + } |
| 96 | + |
| 97 | + template< template< typename... > class Action, typename Iterator, typename Input > |
| 98 | + static void apply( const Iterator& begin, const Input& in, trace_state& ts ) |
| 99 | + { |
| 100 | + std::cerr << std::setw( 6 ) << ++ts.line << " " << internal::demangle< Action< Rule > >() << "::apply()" << std::endl; |
| 101 | + using action_t = typename Input::action_t; |
| 102 | + const action_t action_input( begin, in ); |
| 103 | + Action< Rule >::apply( action_input, ts ); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + } // namespace TAOCPP_JSON_PEGTL_NAMESPACE |
| 108 | + |
| 109 | +} // namespace tao |
| 110 | + |
| 111 | +#endif |
0 commit comments