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