1 | // Copyright (c) 2001-2010 Hartmut Kaiser |
2 | // Copyright (c) 2016 Jeffrey E. Trull |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #include <boost/spirit/include/lex_plain_token.hpp> |
7 | |
8 | #include <boost/spirit/include/lex_lexertl.hpp> |
9 | #include <boost/spirit/include/qi_parse.hpp> |
10 | #include <boost/spirit/include/qi_operator.hpp> |
11 | |
12 | #include <boost/core/lightweight_test.hpp> |
13 | |
14 | #include <string> |
15 | |
16 | namespace qi = boost::spirit::qi; |
17 | namespace lex = boost::spirit::lex; |
18 | |
19 | enum tokenids |
20 | { |
21 | // left tokens |
22 | IDLPAREN = lex::min_token_id, |
23 | IDLANGLE, |
24 | IDLBRACE, |
25 | IDLSQUARE, |
26 | // right tokens |
27 | IDRPAREN, |
28 | IDRANGLE, |
29 | IDRBRACE, |
30 | IDRSQUARE, |
31 | IDANY |
32 | }; |
33 | |
34 | template <typename Lexer> |
35 | struct delimiter_tokens : lex::lexer<Lexer> |
36 | { |
37 | delimiter_tokens() |
38 | { |
39 | this->self = |
40 | lex::char_('(', IDLPAREN) |
41 | | lex::char_(')', IDRPAREN) |
42 | | lex::char_('<', IDLANGLE) |
43 | | lex::char_('>', IDRANGLE) |
44 | | lex::char_('{', IDLBRACE) |
45 | | lex::char_('}', IDRBRACE) |
46 | | lex::char_('[', IDLSQUARE) |
47 | | lex::char_(']', IDRSQUARE) |
48 | | lex::string("." , IDANY) |
49 | ; |
50 | } |
51 | }; |
52 | |
53 | int main() |
54 | { |
55 | typedef lex::lexertl::token< |
56 | std::string::iterator, boost::mpl::vector<std::string> |
57 | > token_type; |
58 | |
59 | typedef lex::lexertl::lexer<token_type> lexer_type; |
60 | delimiter_tokens<lexer_type> delims; |
61 | |
62 | // two test cases for the token range |
63 | std::string angled_delimiter_str("<canvas>" ); |
64 | |
65 | using qi::token; |
66 | // angle brackets |
67 | std::string::iterator beg = angled_delimiter_str.begin(); |
68 | BOOST_TEST(lex::tokenize_and_parse( |
69 | beg, angled_delimiter_str.end(), |
70 | delims, |
71 | token(IDLPAREN, IDLSQUARE) |
72 | >> +token(IDANY) |
73 | >> token(IDRPAREN, IDRSQUARE))); |
74 | |
75 | std::string paren_delimiter_str("(setq foo nil)" ); |
76 | beg = paren_delimiter_str.begin(); |
77 | BOOST_TEST(lex::tokenize_and_parse( |
78 | beg, paren_delimiter_str.end(), |
79 | delims, |
80 | token(IDLPAREN, IDLSQUARE) |
81 | >> +token(IDANY) |
82 | >> token(IDRPAREN, IDRSQUARE))); |
83 | |
84 | // reset and use a regular plain token |
85 | beg = paren_delimiter_str.begin(); |
86 | BOOST_TEST(lex::tokenize_and_parse( |
87 | beg, paren_delimiter_str.end(), |
88 | delims, |
89 | token(IDLPAREN) >> +token(IDANY) >> token(IDRPAREN))); |
90 | |
91 | return boost::report_errors(); |
92 | } |
93 | |