1 // Copyright (c) 2014-2020 Dr. Colin Hirsch and Daniel Frey
2 // Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3 
4 #include "test.hpp"
5 
6 namespace TAO_PEGTL_NAMESPACE
7 {
8    namespace test1
9    {
10       struct state1
11       {
12          char c;
13 
14          template< typename ParseInput >
state1TAO_PEGTL_NAMESPACE::test1::state115          state1( const ParseInput& /*unused*/, std::string& /*unused*/ )
16             : c()
17          {}
18 
19          template< typename ParseInput >
successTAO_PEGTL_NAMESPACE::test1::state120          void success( const ParseInput& /*unused*/, std::string& s ) const
21          {
22             s += c;
23          }
24       };
25 
26       struct fobble
27          : sor< state< state1, alpha >, digit >
28       {};
29 
30       struct fibble
31          : until< eof, fobble >
32       {};
33 
34       template< typename Rule >
35       struct action1
36       {};
37 
38       template<>
39       struct action1< alpha >
40       {
41          template< typename ActionInput >
applyTAO_PEGTL_NAMESPACE::test1::action142          static void apply( const ActionInput& in, state1& s )
43          {
44             assert( in.size() == 1 );
45             s.c = in.begin()[ 0 ];
46          }
47       };
48 
state_test()49       void state_test()
50       {
51          std::string result;
52          memory_input in( "dk41sk41xk3", __FUNCTION__ );
53          parse< fibble, action1 >( in, result );
54          TAO_PEGTL_TEST_ASSERT( result == "dkskxk" );
55       }
56 
57       template< typename Rule >
58       struct action0
59       {};
60 
61       static int i0 = 0;
62 
63       template<>
64       struct action0< alpha >
65       {
apply0TAO_PEGTL_NAMESPACE::test1::action066          static void apply0()
67          {
68             ++i0;
69          }
70       };
71 
72       template<>
73       struct action0< digit >
74       {
apply0TAO_PEGTL_NAMESPACE::test1::action075          static void apply0( std::string& s )
76          {
77             s += '0';
78          }
79       };
80 
apply0_test()81       void apply0_test()
82       {
83          memory_input ina( "abcdefgh", __FUNCTION__ );
84          parse< star< alpha >, action0 >( ina );
85          TAO_PEGTL_TEST_ASSERT( i0 == 8 );
86          std::string s0;
87          memory_input ind( "12345678", __FUNCTION__ );
88          parse< star< digit >, action0 >( ind, s0 );
89          TAO_PEGTL_TEST_ASSERT( s0 == "00000000" );
90       }
91 
92       const std::size_t count_byte = 12345;
93       const std::size_t count_line = 42;
94       const std::size_t count_column = 12;
95 
96       const char* count_source = "count_source";
97 
98       template< typename Rule >
99       struct count_action
100       {
101          template< typename ActionInput >
applyTAO_PEGTL_NAMESPACE::test1::count_action102          static void apply( const ActionInput& in )
103          {
104             TAO_PEGTL_TEST_ASSERT( in.iterator().byte == count_byte );
105             TAO_PEGTL_TEST_ASSERT( in.iterator().line == count_line );
106             TAO_PEGTL_TEST_ASSERT( in.iterator().column == count_column );
107             TAO_PEGTL_TEST_ASSERT( in.input().source() == count_source );
108             TAO_PEGTL_TEST_ASSERT( in.size() == 1 );
109             TAO_PEGTL_TEST_ASSERT( in.begin() + 1 == in.end() );
110             TAO_PEGTL_TEST_ASSERT( in.peek_char() == 'f' );
111             TAO_PEGTL_TEST_ASSERT( in.peek_uint8() == static_cast< unsigned char >( 'f' ) );
112          }
113       };
114 
count_test()115       void count_test()
116       {
117          const char* foo = "f";
118          memory_input in( foo, foo + 1, count_source, count_byte, count_line, count_column );
119          const auto result = parse< must< alpha >, count_action >( in );
120          TAO_PEGTL_TEST_ASSERT( result );
121       }
122 
123    }  // namespace test1
124 
unit_test()125    void unit_test()
126    {
127       test1::state_test();
128       test1::apply0_test();
129       test1::count_test();
130    }
131 
132 }  // namespace TAO_PEGTL_NAMESPACE
133 
134 #include "main.hpp"
135