• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2003 Joel de Guzman
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 <boost/detail/lightweight_test.hpp>
11 #include <string>
12 
13 
14 #include "impl/string_length.hpp"
15 #include <boost/spirit/include/classic_core.hpp>
16 #include <boost/spirit/include/classic_assign_actor.hpp>
17 using namespace BOOST_SPIRIT_CLASSIC_NS;
18 
19 ///////////////////////////////////////////////////////////////////////////////
20 //
21 //  Directives tests
22 //
23 ///////////////////////////////////////////////////////////////////////////////
24 void
directives_test1()25 directives_test1()
26 {
27     char const* cpx = "H e l l o";
28     char const* cpx_first = cpx;
29     char const* cpx_last = cpx + test_impl::string_length(cpx);
30 
31     match<> hit;
32     typedef skipper_iteration_policy<iteration_policy> iter_policy;
33     scanner<char const*, scanner_policies<iter_policy> >
34         scanx(cpx_first, cpx_last);
35 
36     hit = str_p("Hello").parse(scanx);
37     BOOST_TEST(!hit);
38     scanx.first = cpx;
39 
40     hit = chseq_p("Hello").parse(scanx);
41     BOOST_TEST(!!hit);
42     scanx.first = cpx;
43 
44     char const* cp = "Hello \n\tWorld";
45     char const* cp_first = cp;
46     char const* cp_last = cp + test_impl::string_length(cp);
47 
48     scanner<char const*, scanner_policies<iter_policy> >
49         scan(cp_first, cp_last);
50 
51     hit = (+(alpha_p | punct_p)).parse(scan);
52     BOOST_TEST(!!hit);
53     BOOST_TEST(scan.first == scan.last);
54     scan.first = cp;
55 
56     hit = (+(lexeme_d[+(alpha_p | '\'')])).parse(scan);
57     BOOST_TEST(!!hit);
58     BOOST_TEST(scan.first == scan.last);
59     scan.first = cp;
60 
61     hit = (+(lexeme_d[lexeme_d[+anychar_p]])).parse(scan);
62     BOOST_TEST(!!hit);
63     BOOST_TEST(scan.first == scan.last);
64     scan.first = cp;
65 
66     hit = (str_p("Hello") >> "World").parse(scan);
67     BOOST_TEST(!!hit);
68     BOOST_TEST(scan.first == scan.last);
69     scan.first = cp;
70 
71     hit = as_lower_d[str_p("hello") >> "world"].parse(scan);
72     BOOST_TEST(!!hit);
73     BOOST_TEST(scan.first == scan.last);
74     scan.first = cp;
75 
76     hit = (+(as_lower_d[as_lower_d[+lower_p | '\'']])).parse(scan);
77     BOOST_TEST(!!hit);
78     BOOST_TEST(scan.first == scan.last);
79     scan.first = cp;
80 
81     char const* cpy = "123.456";
82     char const* cpy_first = cpy;
83     char const* cpy_last = cpy + test_impl::string_length(cpy);
84 
85     scanner<> scany(cpy_first, cpy_last);
86     hit = longest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany);
87     BOOST_TEST(!!hit);
88     BOOST_TEST(scany.first == scany.last);
89     scany.first = cpy;
90 
91     hit = shortest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany);
92     BOOST_TEST(!!hit);
93     BOOST_TEST(scany.first != scany.last);
94     scany.first = cpy;
95 
96     char const* cpz = "razamanaz";
97     char const* cpz_first = cpz;
98     char const* cpz_last = cpz + test_impl::string_length(cpz);
99 
100     scanner<> scanz(cpz_first, cpz_last);
101     hit = longest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz);
102     BOOST_TEST(!!hit);
103     BOOST_TEST(scanz.first == scanz.last);
104     scanz.first = cpz;
105 
106     hit = shortest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz);
107     BOOST_TEST(!!hit);
108     BOOST_TEST(scanz.first == cpz+4);
109     scanz.first = cpz;
110 
111 //  bounds_d
112 
113     parse_info<> pr = parse("123", limit_d(0, 60)[int_p]);
114     BOOST_TEST(!pr.hit);
115 
116     pr = parse("-2", limit_d(0, 60)[int_p]);
117     BOOST_TEST(!pr.hit);
118 
119     pr = parse("60", limit_d(0, 60)[int_p]);
120     BOOST_TEST(pr.hit);
121 
122     pr = parse("0", limit_d(0, 60)[int_p]);
123     BOOST_TEST(pr.hit);
124 
125     pr = parse("-2", min_limit_d(0)[int_p]);
126     BOOST_TEST(!pr.hit);
127 
128     pr = parse("-2", min_limit_d(-5)[int_p]);
129     BOOST_TEST(pr.hit);
130 
131     pr = parse("101", max_limit_d(100)[int_p]);
132     BOOST_TEST(!pr.hit);
133 
134     pr = parse("100", max_limit_d(100)[int_p]);
135     BOOST_TEST(pr.hit);
136 }
137 
138 struct identifier : public grammar<identifier>
139 {
140     template <typename ScannerT>
141     struct definition
142     {
definitionidentifier::definition143         definition(identifier const& /*self*/)
144         {
145             rr = +(alpha_p | '_');
146             r = lexeme_d[rr];
147         }
148 
149         rule<typename lexeme_scanner<ScannerT>::type> rr;
150         rule<ScannerT> r;
151 
152         rule<ScannerT> const&
startidentifier::definition153         start() const { return r; }
154     };
155 };
156 
157 void
directives_test2()158 directives_test2()
159 {
160     //  Test that lexeme_d does not skip trailing spaces
161 
162     std::string str1, str2;
163     identifier ident;
164 
165     parse("rock_n_roll never_dies ",
166 
167         ident[assign_a(str1)] >> ident[assign_a(str2)], space_p
168     );
169 
170     std::cout << '*' << str1 << ',' << str2 << '*' << std::endl;
171 
172 
173     BOOST_TEST(str1 == "rock_n_roll");
174     BOOST_TEST(str2 == "never_dies");
175 }
176 
177 ///////////////////////////////////////////////////////////////////////////////
178 //
179 //  Main
180 //
181 ///////////////////////////////////////////////////////////////////////////////
182 int
main()183 main()
184 {
185     directives_test1();
186     directives_test2();
187     return boost::report_errors();
188 }
189 
190