• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2015 Joel de Guzman
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 
10 #include <iostream>
11 #include "test.hpp"
12 
13 // Custom string type with a C-style string conversion.
14 struct custom_string_c
15 {
custom_string_ccustom_string_c16     custom_string_c(char c) { str[0] = c; str[1] = '\0'; }
17 
operator char*custom_string_c18     operator char*() { return str; }
operator char const*custom_string_c19     operator char const*() const { return str; }
20 
21 private:
22     char str[2];
23 };
24 
25 int
main()26 main()
27 {
28     using spirit_test::test;
29     using spirit_test::test_attr;
30     using boost::spirit::x3::symbols;
31     using boost::spirit::x3::no_case;
32 
33     { // basics
34         symbols<int> sym;
35 
36         sym.add
37             ("Joel")
38             ("Ruby")
39             ("Tenji")
40             ("Tutit")
41             ("Kim")
42             ("Joey")
43             ("Joeyboy")
44         ;
45 
46         BOOST_TEST((test("Joel", sym)));
47         BOOST_TEST((test("Ruby", sym)));
48         BOOST_TEST((test("Tenji", sym)));
49         BOOST_TEST((test("Tutit", sym)));
50         BOOST_TEST((test("Kim", sym)));
51         BOOST_TEST((test("Joey", sym)));
52         BOOST_TEST((test("Joeyboy", sym)));
53         BOOST_TEST((!test("XXX", sym)));
54 
55         // test copy
56         symbols<int> sym2;
57         sym2 = sym;
58         BOOST_TEST((test("Joel", sym2)));
59         BOOST_TEST((test("Ruby", sym2)));
60         BOOST_TEST((test("Tenji", sym2)));
61         BOOST_TEST((test("Tutit", sym2)));
62         BOOST_TEST((test("Kim", sym2)));
63         BOOST_TEST((test("Joey", sym2)));
64         BOOST_TEST((!test("XXX", sym2)));
65 
66         // make sure it plays well with other parsers
67         BOOST_TEST((test("Joelyo", sym >> "yo")));
68 
69         sym.remove
70             ("Joel")
71             ("Ruby")
72         ;
73 
74         BOOST_TEST((!test("Joel", sym)));
75         BOOST_TEST((!test("Ruby", sym)));
76     }
77 
78     { // comma syntax
79         symbols<int> sym;
80         sym += "Joel", "Ruby", "Tenji", "Tutit", "Kim", "Joey";
81 
82         BOOST_TEST((test("Joel", sym)));
83         BOOST_TEST((test("Ruby", sym)));
84         BOOST_TEST((test("Tenji", sym)));
85         BOOST_TEST((test("Tutit", sym)));
86         BOOST_TEST((test("Kim", sym)));
87         BOOST_TEST((test("Joey", sym)));
88         BOOST_TEST((!test("XXX", sym)));
89 
90         sym -= "Joel", "Ruby";
91 
92         BOOST_TEST((!test("Joel", sym)));
93         BOOST_TEST((!test("Ruby", sym)));
94     }
95 
96     { // no-case handling
97         using namespace boost::spirit::x3::ascii;
98 
99         symbols<int> sym;
100         // NOTE: make sure all entries are in lower-case!!!
101         sym = "joel", "ruby", "tenji", "tutit", "kim", "joey";
102 
103         BOOST_TEST((test("joel", no_case[sym])));
104         BOOST_TEST((test("ruby", no_case[sym])));
105         BOOST_TEST((test("tenji", no_case[sym])));
106         BOOST_TEST((test("tutit", no_case[sym])));
107         BOOST_TEST((test("kim", no_case[sym])));
108         BOOST_TEST((test("joey", no_case[sym])));
109 
110         BOOST_TEST((test("JOEL", no_case[sym])));
111         BOOST_TEST((test("RUBY", no_case[sym])));
112         BOOST_TEST((test("TENJI", no_case[sym])));
113         BOOST_TEST((test("TUTIT", no_case[sym])));
114         BOOST_TEST((test("KIM", no_case[sym])));
115         BOOST_TEST((test("JOEY", no_case[sym])));
116 
117         // make sure it plays well with other parsers
118         BOOST_TEST((test("Joelyo", no_case[sym] >> "yo")));
119     }
120 
121     { // attributes
122         symbols<int> sym;
123 
124         sym.add
125             ("Joel", 1)
126             ("Ruby", 2)
127             ("Tenji", 3)
128             ("Tutit", 4)
129             ("Kim", 5)
130             ("Joey", 6)
131         ;
132 
133         int i;
134         BOOST_TEST((test_attr("Joel", sym, i)));
135         BOOST_TEST(i == 1);
136         BOOST_TEST((test_attr("Ruby", sym, i)));
137         BOOST_TEST(i == 2);
138         BOOST_TEST((test_attr("Tenji", sym, i)));
139         BOOST_TEST(i == 3);
140         BOOST_TEST((test_attr("Tutit", sym, i)));
141         BOOST_TEST(i == 4);
142         BOOST_TEST((test_attr("Kim", sym, i)));
143         BOOST_TEST(i == 5);
144         BOOST_TEST((test_attr("Joey", sym, i)));
145         BOOST_TEST(i == 6);
146         BOOST_TEST((!test_attr("XXX", sym, i)));
147 
148         // double add:
149 
150         sym.add("Joel", 265);
151         BOOST_TEST((test_attr("Joel", sym, i)));
152         BOOST_TEST(i == 1);
153     }
154 
155     { // actions
156         using boost::spirit::x3::_attr;
157 
158         symbols<int> sym;
159         sym.add
160             ("Joel", 1)
161             ("Ruby", 2)
162             ("Tenji", 3)
163             ("Tutit", 4)
164             ("Kim", 5)
165             ("Joey", 6)
166         ;
167 
168         int i;
169         auto f = [&](auto& ctx){ i = _attr(ctx); };
170 
171         BOOST_TEST((test("Joel", sym[f])));
172         BOOST_TEST(i == 1);
173         BOOST_TEST((test("Ruby", sym[f])));
174         BOOST_TEST(i == 2);
175         BOOST_TEST((test("Tenji", sym[f])));
176         BOOST_TEST(i == 3);
177         BOOST_TEST((test("Tutit", sym[f])));
178         BOOST_TEST(i == 4);
179         BOOST_TEST((test("Kim", sym[f])));
180         BOOST_TEST(i == 5);
181         BOOST_TEST((test("Joey", sym[f])));
182         BOOST_TEST(i == 6);
183         BOOST_TEST((!test("XXX", sym[f])));
184     }
185 
186     return boost::report_errors();
187 }
188