• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2003 Martin Wille
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #include <iostream>
10 #include <cstring>
11 #include <boost/detail/lightweight_test.hpp>
12 
13 // This test program only includes the epsilon.hpp header from Spirit
14 #include <boost/spirit/include/classic_epsilon.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16 #include "impl/var.hpp"
17 #include "impl/string_length.hpp"
18 
19 using namespace test;
20 static BOOST_SPIRIT_CLASSIC_NS::parse_info<char const *> pi;
21 
22 ////////////////////////////////////////////////
23 // These macros are used with BOOST_TEST
24 #define matches (pi.hit)
25 #define full_match (pi.hit && pi.full)
26 #define partial_match (pi.hit && !pi.full)
27 #define no_match (!pi.hit && !pi.full)
28 #define zero_length_match (pi.length == 0)
29 #define stop_equals_start (pi.stop == s)
30 
31 template<typename ParserT>
32 static void
parse(char const * s,ParserT const & p,bool match)33 parse(char const *s, ParserT const &p, bool match)
34 {
35 
36     pi = BOOST_SPIRIT_CLASSIC_NS::parse(s, s + test_impl::string_length(s), p);
37     if (match)
38     {
39         BOOST_TEST(matches);
40         BOOST_TEST(zero_length_match);
41         BOOST_TEST(stop_equals_start);
42     }
43     else
44     {
45         BOOST_TEST(no_match);
46     }
47 }
48 
49 static char const empty[] = "";
50 static char const not_empty[] = "asdfgh";
51 
52 ////////////////////////////////////////////////
53 // Test wether epsilon_p/eps_p work as
54 // primitive parsers
55 static void
epsilon_as_primitive()56 epsilon_as_primitive()
57 {
58     // This test case also is a compile time check wether
59     // both eps_p and epsilon_p are present.
60 
61     parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
62     BOOST_TEST(full_match);
63     parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
64     BOOST_TEST(partial_match);
65 
66     parse(empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
67     BOOST_TEST(full_match);
68     parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
69     BOOST_TEST(partial_match);
70 }
71 
72 ////////////////////////////////////////////////
73 // Test wether epsilon_p/eps_p work correctly as
74 // a parser generator for creating parsers from
75 // functors
76 static void
epsilon_as_parser_generator_for_functors()77 epsilon_as_parser_generator_for_functors()
78 {
79     bool       flag = false;
80     parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
81     BOOST_TEST(no_match);
82 
83     flag = true;
84     parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
85     BOOST_TEST(full_match);
86 }
87 
88 ////////////////////////////////////////////////
89 // Test wether epsilon_p/eps_p work correctly as
90 // a parser generator for creating parsers from
91 // other parsers
92 static void
epsilon_as_parser_generator_for_parsers()93 epsilon_as_parser_generator_for_parsers()
94 {
95     // This test case uses a parser created by epsilon_p
96     // as body-parser for another invokation of epsilon_p
97 
98     bool        flag = false;
99     parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
100             BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
101     BOOST_TEST(no_match);
102 
103     flag = true;
104     parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
105             BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
106     BOOST_TEST(full_match);
107 }
108 
109 ////////////////////////////////////////////////
110 // Test wether epsilon_p/eps_p support negation
111 static void
negation_operator_for_epsilon()112 negation_operator_for_epsilon()
113 {
114     bool       flag = false;
115     parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
116     BOOST_TEST(full_match);
117     parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
118     BOOST_TEST(no_match);
119 
120     flag = true;
121     parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
122     BOOST_TEST(no_match);
123     parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
124     BOOST_TEST(full_match);
125 }
126 
127 int
main()128 main()
129 {
130     epsilon_as_primitive();
131     epsilon_as_parser_generator_for_functors();
132     epsilon_as_parser_generator_for_parsers();
133     negation_operator_for_epsilon();
134 
135     return boost::report_errors();
136 }
137