• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/detail/lightweight_test.hpp>
7 #include <boost/config/warning_disable.hpp>
8 
9 #include <boost/spirit/include/lex_lexertl.hpp>
10 #include <boost/spirit/include/qi_parse.hpp>
11 #include <boost/spirit/include/qi_operator.hpp>
12 
13 #include <string>
14 
15 namespace qi = boost::spirit::qi;
16 namespace lex = boost::spirit::lex;
17 
18 enum tokenids
19 {
20     // left tokens
21     IDLPAREN = lex::min_token_id,
22     IDLANGLE,
23     IDLBRACE,
24     IDLSQUARE,
25     // right tokens
26     IDRPAREN,
27     IDRANGLE,
28     IDRBRACE,
29     IDRSQUARE,
30     IDANY
31 };
32 
33 template <typename Lexer>
34 struct delimiter_tokens : lex::lexer<Lexer>
35 {
delimiter_tokensdelimiter_tokens36     delimiter_tokens()
37     {
38         this->self =
39                 lex::char_('(', IDLPAREN)
40             |   lex::char_(')', IDRPAREN)
41             |   lex::char_('<', IDLANGLE)
42             |   lex::char_('>', IDRANGLE)
43             |   lex::char_('{', IDLBRACE)
44             |   lex::char_('}', IDRBRACE)
45             |   lex::char_('[', IDLSQUARE)
46             |   lex::char_(']', IDRSQUARE)
47             |   lex::string(".", IDANY)
48             ;
49     }
50 };
51 
main()52 int main()
53 {
54     typedef lex::lexertl::token<
55         std::string::iterator, boost::mpl::vector<std::string>
56     > token_type;
57 
58     typedef lex::lexertl::lexer<token_type> lexer_type;
59     delimiter_tokens<lexer_type> delims;
60 
61     // two test cases for the token range
62     std::string angled_delimiter_str("<canvas>");
63 
64     using qi::token;
65     // angle brackets
66     std::string::iterator beg = angled_delimiter_str.begin();
67     BOOST_TEST(lex::tokenize_and_parse(
68                    beg, angled_delimiter_str.end(),
69                    delims,
70                    token(IDLPAREN, IDLSQUARE)
71                      >> +token(IDANY)
72                      >> token(IDRPAREN, IDRSQUARE)));
73 
74     std::string paren_delimiter_str("(setq foo nil)");
75     beg = paren_delimiter_str.begin();
76     BOOST_TEST(lex::tokenize_and_parse(
77                    beg, paren_delimiter_str.end(),
78                    delims,
79                    token(IDLPAREN, IDLSQUARE)
80                      >> +token(IDANY)
81                      >> token(IDRPAREN, IDRSQUARE)));
82 
83     // reset and use a regular plain token
84     beg = paren_delimiter_str.begin();
85     BOOST_TEST(lex::tokenize_and_parse(
86                    beg, paren_delimiter_str.end(),
87                    delims,
88                    token(IDLPAREN) >> +token(IDANY) >> token(IDRPAREN)));
89 
90     return boost::report_errors();
91 }
92