Console: Make the parser interface callback based
[senf.git] / Console / Parse.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Parse public header */
25
26 #ifndef HH_Parse_
27 #define HH_Parse_ 1
28
29 // Custom includes
30 #include <string>
31 #include <vector>
32 #include <boost/utility.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/range/iterator_range.hpp>
35 #include <boost/function.hpp>
36
37 //#include "Parse.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace console {
42
43     namespace detail { struct ParserAccess; }
44
45     /** \brief
46       */
47     class ArgumentToken
48     {
49     public:
50         std::string const & value() const;
51
52     protected:
53
54     private:
55         explicit ArgumentToken(std::string token);
56
57         std::string token_;
58
59         friend class detail::ParserAccess;
60     };
61
62
63     /** \brief
64       */
65     class ParseCommandInfo
66     {
67         typedef std::vector<ArgumentToken> Tokens;
68         typedef std::vector<std::string> CommandPath;
69         
70     public:
71         typedef CommandPath::const_iterator path_iterator;
72         typedef Tokens::const_iterator token_iterator;
73         typedef boost::iterator_range<token_iterator> argument_value_type;
74
75
76     private:
77         typedef std::vector<argument_value_type> Arguments;
78
79     public:
80         typedef Arguments::const_iterator argument_iterator;
81         typedef Arguments::size_type size_type;
82
83         typedef boost::iterator_range<path_iterator> CommandPathRange;
84         typedef boost::iterator_range<argument_iterator> ArgumentsRange;
85         typedef boost::iterator_range<token_iterator> TokensRange;
86
87         enum BuiltinCommand { NoBuiltin, 
88                               BuiltinCD, 
89                               BuiltinLS, 
90                               BuiltinPUSHD, 
91                               BuiltinPOPD,
92                               BuiltinEXIT };
93
94         BuiltinCommand builtin() const;
95         CommandPathRange commandPath() const;
96         ArgumentsRange arguments() const;
97         TokensRange tokens() const;
98         
99     protected:
100
101     private:
102         void init();
103         void setBuiltin(BuiltinCommand builtin);
104         void setCommand(std::vector<std::string> & commandPath);
105         void startArgument();
106         void endArgument();
107         void addToken(ArgumentToken const & token);
108         void finalize();
109
110         struct MakeRange;
111
112         std::vector<std::string> commandPath_;
113
114         typedef std::pair<Tokens::size_type, Tokens::size_type> TempArgumentRange;
115         typedef std::vector<TempArgumentRange> TempArguments;
116
117         BuiltinCommand builtin_;
118         Tokens tokens_;
119         Arguments arguments_;
120         TempArguments tempArguments_;
121
122         friend class detail::ParserAccess;
123     };
124
125     std::ostream & operator<<(std::ostream & stream, ParseCommandInfo const & info);
126
127     /** \brief
128       */
129     class CommandParser
130         : boost::noncopyable
131     {
132     public:
133         ///////////////////////////////////////////////////////////////////////////
134         // Types
135
136         typedef boost::function<void (ParseCommandInfo const &)> Callback;
137
138         ///////////////////////////////////////////////////////////////////////////
139         ///\name Structors and default members
140         ///@{
141
142         CommandParser();
143         ~CommandParser();
144
145         ///@}
146         ///////////////////////////////////////////////////////////////////////////
147
148         bool parse(std::string command, Callback cb);
149
150     private:
151         struct Impl;
152
153         Impl & impl();
154
155         boost::scoped_ptr<Impl> impl_;
156     };
157
158 }}
159
160 ///////////////////////////////hh.e////////////////////////////////////////
161 #include "Parse.cci"
162 //#include "Parse.ct"
163 //#include "Parse.cti"
164 #endif
165
166 \f
167 // Local Variables:
168 // mode: c++
169 // fill-column: 100
170 // comment-column: 40
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"
175 // End: