1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/config/parser.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <lutok/exceptions.hpp>
32*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
33*11be35a1SLionel Sambuc #include <lutok/stack_cleaner.hpp>
34*11be35a1SLionel Sambuc #include <lutok/state.ipp>
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc #include "utils/config/exceptions.hpp"
37*11be35a1SLionel Sambuc #include "utils/config/lua_module.hpp"
38*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
39*11be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
40*11be35a1SLionel Sambuc #include "utils/noncopyable.hpp"
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc namespace config = utils::config;
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc // History of configuration file versions:
46*11be35a1SLionel Sambuc //
47*11be35a1SLionel Sambuc // 2 - Changed the syntax() call to take only a version number, instead of the
48*11be35a1SLionel Sambuc // word 'config' as the first argument and the version as the second one.
49*11be35a1SLionel Sambuc // Files now start with syntax(2) instead of syntax('config', 1).
50*11be35a1SLionel Sambuc //
51*11be35a1SLionel Sambuc // 1 - Initial version.
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc /// Internal implementation of the parser.
55*11be35a1SLionel Sambuc struct utils::config::parser::impl : utils::noncopyable {
56*11be35a1SLionel Sambuc /// Pointer to the parent parser. Needed for callbacks.
57*11be35a1SLionel Sambuc parser* _parent;
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc /// The Lua state used by this parser to process the configuration file.
60*11be35a1SLionel Sambuc lutok::state _state;
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc /// The tree to be filed in by the configuration parameters, as provided by
63*11be35a1SLionel Sambuc /// the caller.
64*11be35a1SLionel Sambuc config::tree& _tree;
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc /// Whether syntax() has been called or not.
67*11be35a1SLionel Sambuc bool _syntax_called;
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc /// Constructs a new implementation.
70*11be35a1SLionel Sambuc ///
71*11be35a1SLionel Sambuc /// \param parent_ Pointer to the class being constructed.
72*11be35a1SLionel Sambuc /// \param config_tree_ The configuration tree provided by the user.
implutils::config::parser::impl73*11be35a1SLionel Sambuc impl(parser* const parent_, tree& config_tree_) :
74*11be35a1SLionel Sambuc _parent(parent_), _tree(config_tree_), _syntax_called(false)
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc }
77*11be35a1SLionel Sambuc
78*11be35a1SLionel Sambuc friend void lua_syntax(lutok::state&);
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc /// Callback executed by the Lua syntax() function.
81*11be35a1SLionel Sambuc ///
82*11be35a1SLionel Sambuc /// \param syntax_version The syntax format version as provided by the
83*11be35a1SLionel Sambuc /// configuration file in the call to syntax().
84*11be35a1SLionel Sambuc void
syntax_callbackutils::config::parser::impl85*11be35a1SLionel Sambuc syntax_callback(const int syntax_version)
86*11be35a1SLionel Sambuc {
87*11be35a1SLionel Sambuc if (_syntax_called)
88*11be35a1SLionel Sambuc throw syntax_error("syntax() can only be called once");
89*11be35a1SLionel Sambuc _syntax_called = true;
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc // Allow the parser caller to populate the tree with its own schema
92*11be35a1SLionel Sambuc // depending on the format/version combination.
93*11be35a1SLionel Sambuc _parent->setup(_tree, syntax_version);
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc // Export the config module to the Lua state so that all global variable
96*11be35a1SLionel Sambuc // accesses are redirected to the configuration tree.
97*11be35a1SLionel Sambuc config::redirect(_state, _tree);
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc };
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc namespace {
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc /// Implementation of the Lua syntax() function.
106*11be35a1SLionel Sambuc ///
107*11be35a1SLionel Sambuc /// The syntax() function has to be called by configuration files as the very
108*11be35a1SLionel Sambuc /// first thing they do. Once called, this function populates the configuration
109*11be35a1SLionel Sambuc /// tree based on the syntax version and then continues to process the rest of
110*11be35a1SLionel Sambuc /// the file.
111*11be35a1SLionel Sambuc ///
112*11be35a1SLionel Sambuc /// \pre state(-2) The syntax format name, if a v1 file.
113*11be35a1SLionel Sambuc /// \pre state(-1) The syntax format version.
114*11be35a1SLionel Sambuc ///
115*11be35a1SLionel Sambuc /// \param state The Lua state to operate in.
116*11be35a1SLionel Sambuc ///
117*11be35a1SLionel Sambuc /// \return The number of results pushed onto the stack; always 0.
118*11be35a1SLionel Sambuc static int
lua_syntax(lutok::state & state)119*11be35a1SLionel Sambuc lua_syntax(lutok::state& state)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc if (!state.is_number(-1))
122*11be35a1SLionel Sambuc throw config::value_error("Last argument to syntax must be a number");
123*11be35a1SLionel Sambuc const int syntax_version = state.to_integer(-1);
124*11be35a1SLionel Sambuc
125*11be35a1SLionel Sambuc if (syntax_version == 1) {
126*11be35a1SLionel Sambuc if (state.get_top() != 2)
127*11be35a1SLionel Sambuc throw config::value_error("Version 1 files need two arguments to "
128*11be35a1SLionel Sambuc "syntax()");
129*11be35a1SLionel Sambuc if (!state.is_string(-2) || state.to_string(-2) != "config")
130*11be35a1SLionel Sambuc throw config::value_error("First argument to syntax must be "
131*11be35a1SLionel Sambuc "'config' for version 1 files");
132*11be35a1SLionel Sambuc } else {
133*11be35a1SLionel Sambuc if (state.get_top() != 1)
134*11be35a1SLionel Sambuc throw config::value_error("syntax() only takes one argument");
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc state.get_global("_config_parser");
138*11be35a1SLionel Sambuc config::parser::impl* impl = *state.to_userdata< config::parser::impl* >();
139*11be35a1SLionel Sambuc state.pop(1);
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc impl->syntax_callback(syntax_version);
142*11be35a1SLionel Sambuc
143*11be35a1SLionel Sambuc return 0;
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc } // anonymous namespace
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc
150*11be35a1SLionel Sambuc /// Constructs a new parser.
151*11be35a1SLionel Sambuc ///
152*11be35a1SLionel Sambuc /// \param [in,out] config_tree The configuration tree into which the values set
153*11be35a1SLionel Sambuc /// in the configuration file will be stored.
parser(tree & config_tree)154*11be35a1SLionel Sambuc config::parser::parser(tree& config_tree) :
155*11be35a1SLionel Sambuc _pimpl(new impl(this, config_tree))
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc lutok::stack_cleaner cleaner(_pimpl->_state);
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc _pimpl->_state.push_cxx_function(lua_syntax);
160*11be35a1SLionel Sambuc _pimpl->_state.set_global("syntax");
161*11be35a1SLionel Sambuc *_pimpl->_state.new_userdata< config::parser::impl* >() = _pimpl.get();
162*11be35a1SLionel Sambuc _pimpl->_state.set_global("_config_parser");
163*11be35a1SLionel Sambuc }
164*11be35a1SLionel Sambuc
165*11be35a1SLionel Sambuc
166*11be35a1SLionel Sambuc /// Destructor.
~parser(void)167*11be35a1SLionel Sambuc config::parser::~parser(void)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc }
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc /// Parses a configuration file.
173*11be35a1SLionel Sambuc ///
174*11be35a1SLionel Sambuc /// \post The tree registered during the construction of this class is updated
175*11be35a1SLionel Sambuc /// to contain the values read from the configuration file. If the processing
176*11be35a1SLionel Sambuc /// fails, the state of the output tree is undefined.
177*11be35a1SLionel Sambuc ///
178*11be35a1SLionel Sambuc /// \param file The path to the file to process.
179*11be35a1SLionel Sambuc ///
180*11be35a1SLionel Sambuc /// \throw syntax_error If there is any problem processing the file.
181*11be35a1SLionel Sambuc void
parse(const fs::path & file)182*11be35a1SLionel Sambuc config::parser::parse(const fs::path& file)
183*11be35a1SLionel Sambuc {
184*11be35a1SLionel Sambuc try {
185*11be35a1SLionel Sambuc lutok::do_file(_pimpl->_state, file.str());
186*11be35a1SLionel Sambuc } catch (const lutok::error& e) {
187*11be35a1SLionel Sambuc throw syntax_error(e.what());
188*11be35a1SLionel Sambuc }
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc if (!_pimpl->_syntax_called)
191*11be35a1SLionel Sambuc throw syntax_error("No syntax defined (no call to syntax() found)");
192*11be35a1SLionel Sambuc }
193