Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/example/techniques/nabialek.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.1 KB
Line 
1/*=============================================================================
2    Copyright (c) 2003 Sam Nabialek
3    Copyright (c) 2003-2004 Joel de Guzman
4    http://spirit.sourceforge.net/
5
6    Use, modification and distribution is subject to the Boost Software
7    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10#include <iostream>
11
12#define BOOST_SPIRIT_DEBUG
13
14#include <boost/spirit/core.hpp>
15#include <boost/spirit/error_handling.hpp>
16#include <boost/spirit/iterator.hpp>
17#include <boost/spirit/symbols.hpp>
18#include <boost/spirit/utility.hpp>
19
20using namespace boost::spirit;
21
22template <typename Rule>
23struct SetRest
24{
25    SetRest(Rule& the_rule)
26    : m_the_rule(the_rule)
27    {
28    }
29
30    void operator()(Rule* new_rule) const
31    {
32        m_the_rule = *new_rule;
33    }
34
35private:
36
37    Rule& m_the_rule;
38};
39
40
41struct nabialek_trick : public grammar<nabialek_trick>
42{
43    template <typename ScannerT>
44    struct definition
45    {
46        typedef rule<ScannerT> rule_t;
47
48        rule_t name;
49        rule_t line;
50        rule_t rest;
51        rule_t main;
52        rule_t one;
53        rule_t two;
54
55        symbols<rule_t*> continuations;
56
57        definition(nabialek_trick const& self)
58        {
59            name = lexeme_d[repeat_p(1,20)[alnum_p | '_']];
60
61            one = name;
62            two = name >> ',' >> name;
63
64            continuations.add
65                ("one", &one)
66                ("two", &two)
67                ;
68
69            line = continuations[SetRest<rule_t>(rest)] >> rest;
70            main = *line;
71
72            BOOST_SPIRIT_DEBUG_RULE(name);
73            BOOST_SPIRIT_DEBUG_RULE(line);
74            BOOST_SPIRIT_DEBUG_RULE(rest);
75            BOOST_SPIRIT_DEBUG_RULE(main);
76            BOOST_SPIRIT_DEBUG_RULE(one);
77            BOOST_SPIRIT_DEBUG_RULE(two);
78        }
79
80        rule_t const&
81        start() const
82        {
83            return main;
84        }
85    };
86};
87
88int
89main()
90{
91    nabialek_trick g;
92    parse("one only\none again\ntwo first,second", g, space_p);
93    return 0;
94}
Note: See TracBrowser for help on using the repository browser.