1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright (c) 2020 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_JSON_PEGTL_CONTRIB_ANALYZE_HPP
#define TAO_JSON_PEGTL_CONTRIB_ANALYZE_HPP
#include <cassert>
#include <cstddef>
#include <iostream>
#include <map>
#include <set>
#include <stdexcept>
#include <string_view>
#include <utility>
#include <vector>
#include "../config.hpp"
#include "../demangle.hpp"
#include "analyze_traits.hpp"
#include "internal/set_stack_guard.hpp"
#include "../internal/dependent_false.hpp"
namespace TAO_JSON_PEGTL_NAMESPACE
{
namespace internal
{
struct analyze_entry
{
explicit analyze_entry( const analyze_type in_type ) noexcept
: type( in_type )
{}
const analyze_type type;
std::vector< std::string_view > subs;
};
class analyze_cycles_impl
{
public:
analyze_cycles_impl( analyze_cycles_impl&& ) = delete;
analyze_cycles_impl( const analyze_cycles_impl& ) = delete;
~analyze_cycles_impl() = default;
void operator=( analyze_cycles_impl&& ) = delete;
void operator=( const analyze_cycles_impl& ) = delete;
[[nodiscard]] std::size_t problems()
{
for( auto i = m_info.begin(); i != m_info.end(); ++i ) {
m_results[ i->first ] = work( i, false );
m_cache.clear();
}
return m_problems;
}
template< typename Rule >
[[nodiscard]] bool consumes() const
{
return m_results.at( demangle< Rule >() );
}
protected:
explicit analyze_cycles_impl( const bool verbose ) noexcept
: m_verbose( verbose ),
m_problems( 0 )
{}
[[nodiscard]] std::map< std::string_view, analyze_entry >::const_iterator find( const std::string_view name ) const noexcept
{
const auto iter = m_info.find( name );
assert( iter != m_info.end() );
return iter;
}
[[nodiscard]] bool work( const std::map< std::string_view, analyze_entry >::const_iterator& start, const bool accum )
{
if( const auto j = m_cache.find( start->first ); j != m_cache.end() ) {
return j->second;
}
if( const auto g = set_stack_guard( m_stack, start->first ) ) {
switch( start->second.type ) {
case analyze_type::any: {
bool a = false;
for( const auto& r : start->second.subs ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = true;
}
case analyze_type::opt: {
bool a = false;
for( const auto& r : start->second.subs ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = false;
}
case analyze_type::seq: {
bool a = false;
for( const auto& r : start->second.subs ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = a;
}
case analyze_type::sor: {
bool a = true;
for( const auto& r : start->second.subs ) {
a = a && work( find( r ), accum );
}
return m_cache[ start->first ] = a;
}
}
assert( false ); // LCOV_EXCL_LINE
}
if( !accum ) {
++m_problems;
if( m_verbose ) {
std::cerr << "problem: cycle without progress detected at rule class " << start->first << std::endl; // LCOV_EXCL_LINE
}
}
return m_cache[ start->first ] = accum;
}
const bool m_verbose;
std::size_t m_problems;
std::map< std::string_view, analyze_entry > m_info;
std::set< std::string_view > m_stack;
std::map< std::string_view, bool > m_cache;
std::map< std::string_view, bool > m_results;
};
template< typename Name >
std::string_view analyze_insert( std::map< std::string_view, analyze_entry >& info )
{
using Traits = analyze_traits< Name, typename Name::rule_t >;
const auto [ i, b ] = info.try_emplace( demangle< Name >(), Traits::type_v );
if( b ) {
analyze_insert_impl( typename Traits::subs_t(), i->second.subs, info );
}
return i->first;
}
template< typename... Subs >
void analyze_insert_impl( type_list< Subs... > /*unused*/, std::vector< std::string_view >& subs, std::map< std::string_view, analyze_entry >& info )
{
( subs.emplace_back( analyze_insert< Subs >( info ) ), ... );
}
template< typename Grammar >
class analyze_cycles
: public analyze_cycles_impl
{
public:
explicit analyze_cycles( const bool verbose )
: analyze_cycles_impl( verbose )
{
analyze_insert< Grammar >( m_info );
}
};
} // namespace internal
template< typename Grammar >
[[nodiscard]] std::size_t analyze( const bool verbose = true )
{
return internal::analyze_cycles< Grammar >( verbose ).problems();
}
} // namespace TAO_JSON_PEGTL_NAMESPACE
#endif
|