Ticket #10548: example.cpp

File example.cpp, 1.4 KB (added by mattia.penati@…, 8 years ago)

A minimal example

Line 
1#include <boost/spirit/include/lex_lexertl.hpp>
2#include <boost/bind.hpp>
3#include <boost/ref.hpp>
4#include <iostream>
5
6namespace lex = boost::spirit::lex;
7
8enum class token { word, eol, char_ };
9
10template <class Lexer>
11struct word_count_tokens: lex::lexer<Lexer> {
12 word_count_tokens() {
13 this->self.add
14 ("[^ \t\n]+", token::word)
15 ("\n" , token::eol)
16 ("." , token::char_);
17 }
18};
19
20struct counter {
21 using result_type = bool;
22
23 template <class Token>
24 bool operator()(Token const &t, int &c, int w, int &l) {
25 switch(t.id()) {
26 case token::word:
27 ++w;
28 c += t.value().size();
29 break;
30 case token::eol:
31 ++l;
32 ++c;
33 break;
34 case token::char_:
35 ++c;
36 break;
37 }
38 return true;
39 }
40};
41
42int main(int argc, char *argv[]) {
43 int c{0}, w{0}, l{0};
44 const std::string text{"test test\nline test"};
45 const char * first = text.data(),
46 * last = text.data() + text.size();
47
48 word_count_tokens<lex::lexertl::lexer<> > word_count_functor;
49 bool r = lex::tokenize(first, last, word_count_functor,
50 boost::bind(counter(), _1, boost::ref(c), boost::ref(w), boost::ref(l)));
51
52 std::cout << "result: " << r << std::endl;
53 std::cout << c << '\t' << w << '\t' << l << std::endl;
54
55 return 0;
56}