• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 1998-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 
12 using namespace std;
13 
14 #include <boost/spirit/include/classic_core.hpp>
15 using namespace BOOST_SPIRIT_CLASSIC_NS;
16 
17 ///////////////////////////////////////////////////////////////////////////////
18 //
19 //  Operators tests
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22 void
operators_tests()23 operators_tests()
24 {
25     parse_info<char const*> pi;
26 
27     pi = parse("Hello World", str_p("Hello ") >> "World");
28     BOOST_TEST(pi.hit);
29     BOOST_TEST(pi.full);
30     BOOST_TEST(pi.length == 11);
31     BOOST_TEST(*pi.stop == 0);
32 
33     pi = parse("Banana", str_p("Banana") | "Pineapple");
34     BOOST_TEST(pi.hit);
35     BOOST_TEST(pi.full);
36     BOOST_TEST(pi.length == 6);
37     BOOST_TEST(*pi.stop == 0);
38 
39     pi = parse("Pineapple", str_p("Banana") | "Pineapple");
40     BOOST_TEST(pi.hit);
41     BOOST_TEST(pi.full);
42     BOOST_TEST(pi.length == 9);
43     BOOST_TEST(*pi.stop == 0);
44 
45     pi = parse("a.2  ", alpha_p || ('.' >> digit_p));
46     BOOST_TEST(pi.hit);
47     BOOST_TEST(pi.length == 3);
48 
49     pi = parse("a  ", alpha_p || ('.' >> digit_p));
50     BOOST_TEST(pi.hit);
51     BOOST_TEST(pi.length == 1);
52 
53     pi = parse(".1  ", alpha_p || ('.' >> digit_p));
54     BOOST_TEST(pi.hit);
55     BOOST_TEST(pi.length == 2);
56 
57     pi = parse("1.a  ", alpha_p || ('.' >> digit_p));
58     BOOST_TEST(!pi.hit);
59 
60     pi = parse("abcdefghij", +xdigit_p & +alpha_p);
61     BOOST_TEST(pi.hit);
62     BOOST_TEST(pi.length == 6);
63 
64     pi = parse("abcdefghij", +alpha_p & +xdigit_p);
65     BOOST_TEST(!pi.hit);
66 
67     pi = parse("abcdefghij", +digit_p & +alpha_p);
68     BOOST_TEST(!pi.hit);
69 
70     pi = parse("abcdefghij", +alpha_p & +digit_p);
71     BOOST_TEST(!pi.hit);
72 
73     // Test for bug reported by Yusaku Sugai here:
74     // http://article.gmane.org/gmane.comp.parsers.spirit.general/8544
75     pi = parse( "foo", (anychar_p & anychar_p), ch_p(' ') );
76     BOOST_TEST(pi.hit);
77 
78     pi = parse("F", xdigit_p - range_p('5','8'));
79     BOOST_TEST(pi.hit);
80     BOOST_TEST(pi.length == 1);
81 
82     pi = parse("0", xdigit_p - range_p('5','8'));
83     BOOST_TEST(pi.hit);
84     BOOST_TEST(pi.length == 1);
85 
86     pi = parse("4", xdigit_p - range_p('5','8'));
87     BOOST_TEST(pi.hit);
88     BOOST_TEST(pi.length == 1);
89 
90     pi = parse("9", xdigit_p - range_p('5','8'));
91     BOOST_TEST(pi.hit);
92     BOOST_TEST(pi.length == 1);
93 
94     pi = parse("5", xdigit_p - range_p('5','8'));
95     BOOST_TEST(!pi.hit);
96 
97     pi = parse("7", xdigit_p - range_p('5','8'));
98     BOOST_TEST(!pi.hit);
99 
100     pi = parse("x*/", anychar_p - "*/");
101     BOOST_TEST(pi.hit);
102     BOOST_TEST(pi.length == 1);
103 
104     pi = parse("*/", anychar_p - "*/");
105     BOOST_TEST(!pi.hit);
106 
107     pi = parse("switcher  ", str_p("switcher") - "switch");
108     BOOST_TEST(pi.hit);
109     BOOST_TEST(pi.length == 8);
110 
111     pi = parse("Z", alpha_p ^ xdigit_p);
112     BOOST_TEST(pi.hit);
113     BOOST_TEST(pi.length == 1);
114 
115     pi = parse("1", alpha_p ^ xdigit_p);
116     BOOST_TEST(pi.hit);
117     BOOST_TEST(pi.length == 1);
118 
119     pi = parse("B", alpha_p ^ xdigit_p);
120     BOOST_TEST(!pi.hit);
121 
122     pi = parse("B", !alpha_p);
123     BOOST_TEST(pi.hit);
124     BOOST_TEST(pi.length == 1);
125 
126     pi = parse("1", !alpha_p);
127     BOOST_TEST(pi.hit);
128     BOOST_TEST(pi.length == 0);
129     BOOST_TEST(*pi.stop == '1');
130 
131     pi = parse("1234 5678 1234 5678", *(+digit_p >> *space_p));
132     BOOST_TEST(pi.hit);
133     BOOST_TEST(pi.full);
134     BOOST_TEST(pi.length == 19);
135     BOOST_TEST(*pi.stop == 0);
136 
137     pi = parse("abcdefghijklmnopqrstuvwxyz123", *alpha_p);
138     BOOST_TEST(pi.hit);
139     BOOST_TEST(pi.length == 26);
140 
141     pi = parse("1234+5678*1234/5678", +digit_p % (ch_p('+') | '*' | '/'));
142     BOOST_TEST(pi.hit);
143     BOOST_TEST(pi.full);
144     BOOST_TEST(pi.length == 19);
145     BOOST_TEST(*pi.stop == 0);
146 
147     pi = parse("1234+", +digit_p % '+');
148     BOOST_TEST(pi.hit);
149     BOOST_TEST(!pi.full);
150     BOOST_TEST(pi.length == 4);
151 }
152 
153 ///////////////////////////////////////////////////////////////////////////////
154 //
155 //  Main
156 //
157 ///////////////////////////////////////////////////////////////////////////////
158 int
main()159 main()
160 {
161     operators_tests();
162     return boost::report_errors();
163 }
164 
165