1 // Copyright 2012 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/config/parser.hpp"
31 #include <lutok/exceptions.hpp>
32 #include <lutok/operations.hpp>
33 #include <lutok/stack_cleaner.hpp>
34 #include <lutok/state.ipp>
36 #include "utils/config/exceptions.hpp"
37 #include "utils/config/lua_module.hpp"
38 #include "utils/config/tree.ipp"
39 #include "utils/fs/path.hpp"
40 #include "utils/logging/macros.hpp"
41 #include "utils/noncopyable.hpp"
43 namespace config = utils::config;
46 // History of configuration file versions:
48 // 2 - Changed the syntax() call to take only a version number, instead of the
49 // word 'config' as the first argument and the version as the second one.
50 // Files now start with syntax(2) instead of syntax('config', 1).
52 // 1 - Initial version.
55 /// Internal implementation of the parser.
56 struct utils::config::parser::impl : utils::noncopyable {
57 /// Pointer to the parent parser. Needed for callbacks.
60 /// The Lua state used by this parser to process the configuration file.
63 /// The tree to be filed in by the configuration parameters, as provided by
67 /// Whether syntax() has been called or not.
70 /// Constructs a new implementation.
72 /// \param parent_ Pointer to the class being constructed.
73 /// \param config_tree_ The configuration tree provided by the user.
74 impl(parser* const parent_, tree& config_tree_) :
75 _parent(parent_), _tree(config_tree_), _syntax_called(false)
79 friend void lua_syntax(lutok::state&);
81 /// Callback executed by the Lua syntax() function.
83 /// \param syntax_version The syntax format version as provided by the
84 /// configuration file in the call to syntax().
86 syntax_callback(const int syntax_version)
89 throw syntax_error("syntax() can only be called once");
90 _syntax_called = true;
92 // Allow the parser caller to populate the tree with its own schema
93 // depending on the format/version combination.
94 _parent->setup(_tree, syntax_version);
96 // Export the config module to the Lua state so that all global variable
97 // accesses are redirected to the configuration tree.
98 config::redirect(_state, _tree);
107 lua_syntax(lutok::state& state)
109 if (!state.is_number(-1))
110 throw config::value_error("Last argument to syntax must be a number");
111 const int syntax_version = state.to_integer(-1);
113 if (syntax_version == 1) {
114 if (state.get_top() != 2)
115 throw config::value_error("Version 1 files need two arguments to "
117 if (!state.is_string(-2) || state.to_string(-2) != "config")
118 throw config::value_error("First argument to syntax must be "
119 "'config' for version 1 files");
121 if (state.get_top() != 1)
122 throw config::value_error("syntax() only takes one argument");
125 state.get_global("_config_parser");
126 config::parser::impl* impl =
127 *state.to_userdata< config::parser::impl* >(-1);
130 impl->syntax_callback(syntax_version);
136 } // anonymous namespace
139 /// Constructs a new parser.
141 /// \param [in,out] config_tree The configuration tree into which the values set
142 /// in the configuration file will be stored.
143 config::parser::parser(tree& config_tree) :
144 _pimpl(new impl(this, config_tree))
146 lutok::stack_cleaner cleaner(_pimpl->_state);
148 _pimpl->_state.push_cxx_function(lua_syntax);
149 _pimpl->_state.set_global("syntax");
150 *_pimpl->_state.new_userdata< config::parser::impl* >() = _pimpl.get();
151 _pimpl->_state.set_global("_config_parser");
156 config::parser::~parser(void)
161 /// Parses a configuration file.
163 /// \post The tree registered during the construction of this class is updated
164 /// to contain the values read from the configuration file. If the processing
165 /// fails, the state of the output tree is undefined.
167 /// \param file The path to the file to process.
169 /// \throw syntax_error If there is any problem processing the file.
171 config::parser::parse(const fs::path& file)
174 lutok::do_file(_pimpl->_state, file.str(), 0, 0, 0);
175 } catch (const lutok::error& e) {
176 throw syntax_error(e.what());
179 if (!_pimpl->_syntax_called)
180 throw syntax_error("No syntax defined (no call to syntax() found)");