3b059af0d0a3518222cc0e13fbc0722a313b138a
[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
36 //#include "Parse.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41
42     namespace detail { struct ParserAccess; }
43
44     /** \brief
45       */
46     class ArgumentToken
47     {
48     public:
49         std::string const & value() const;
50
51     protected:
52
53     private:
54         explicit ArgumentToken(std::string token);
55
56         std::string token_;
57
58         friend class detail::ParserAccess;
59     };
60
61
62     /** \brief
63       */
64     class ParseCommandInfo
65     {
66         typedef std::vector<ArgumentToken> Tokens;
67         typedef std::vector<std::string> CommandPath;
68         
69     public:
70         typedef CommandPath::const_iterator path_iterator;
71         typedef Tokens::const_iterator token_iterator;
72         typedef boost::iterator_range<token_iterator> argument_value_type;
73
74
75     private:
76         typedef std::vector<argument_value_type> Arguments;
77
78     public:
79         typedef Arguments::const_iterator argument_iterator;
80         typedef Arguments::size_type size_type;
81
82         typedef boost::iterator_range<path_iterator> CommandPathRange;
83         typedef boost::iterator_range<argument_iterator> ArgumentsRange;
84         typedef boost::iterator_range<token_iterator> TokensRange;
85
86         enum BuiltinCommand { NoBuiltin, BuiltinCD, BuiltinLS };
87
88         BuiltinCommand builtin() const;
89         CommandPathRange commandPath() const;
90         ArgumentsRange arguments() const;
91         TokensRange tokens() const;
92         
93     protected:
94
95     private:
96         void init();
97         void setBuiltin(BuiltinCommand builtin);
98         void setCommand(std::vector<std::string> & commandPath);
99         void startArgument();
100         void endArgument();
101         void addToken(ArgumentToken const & token);
102         void finalize();
103
104         struct MakeRange;
105
106         std::vector<std::string> commandPath_;
107
108         typedef std::pair<Tokens::size_type, Tokens::size_type> TempArgumentRange;
109         typedef std::vector<TempArgumentRange> TempArguments;
110
111         BuiltinCommand builtin_;
112         Tokens tokens_;
113         Arguments arguments_;
114         TempArguments tempArguments_;
115
116         friend class detail::ParserAccess;
117     };
118
119     std::ostream & operator<<(std::ostream & stream, ParseCommandInfo const & info);
120
121     /** \brief
122       */
123     class SingleCommandParser
124         : boost::noncopyable
125     {
126     public:
127         ///////////////////////////////////////////////////////////////////////////
128         // Types
129
130         ///////////////////////////////////////////////////////////////////////////
131         ///\name Structors and default members
132         ///@{
133
134         SingleCommandParser();
135         ~SingleCommandParser();
136
137         ///@}
138         ///////////////////////////////////////////////////////////////////////////
139
140         bool parseCommand(std::string command, ParseCommandInfo & info);
141
142     private:
143         struct Impl;
144
145         Impl & impl();
146
147         boost::scoped_ptr<Impl> impl_;
148     };
149
150 }}
151
152 ///////////////////////////////hh.e////////////////////////////////////////
153 #include "Parse.cci"
154 //#include "Parse.ct"
155 //#include "Parse.cti"
156 #endif
157
158 \f
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // comment-column: 40
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"
167 // End: