Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/kyua/utils/config/parser.cpp
48081 views
1
// Copyright 2012 The Kyua Authors.
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7
//
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.
16
//
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.
28
29
#include "utils/config/parser.hpp"
30
31
#include <lutok/exceptions.hpp>
32
#include <lutok/operations.hpp>
33
#include <lutok/stack_cleaner.hpp>
34
#include <lutok/state.ipp>
35
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"
42
43
namespace config = utils::config;
44
45
46
// History of configuration file versions:
47
//
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).
51
//
52
// 1 - Initial version.
53
54
55
/// Internal implementation of the parser.
56
struct utils::config::parser::impl : utils::noncopyable {
57
/// Pointer to the parent parser. Needed for callbacks.
58
parser* _parent;
59
60
/// The Lua state used by this parser to process the configuration file.
61
lutok::state _state;
62
63
/// The tree to be filed in by the configuration parameters, as provided by
64
/// the caller.
65
config::tree& _tree;
66
67
/// Whether syntax() has been called or not.
68
bool _syntax_called;
69
70
/// Constructs a new implementation.
71
///
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)
76
{
77
}
78
79
friend void lua_syntax(lutok::state&);
80
81
/// Callback executed by the Lua syntax() function.
82
///
83
/// \param syntax_version The syntax format version as provided by the
84
/// configuration file in the call to syntax().
85
void
86
syntax_callback(const int syntax_version)
87
{
88
if (_syntax_called)
89
throw syntax_error("syntax() can only be called once");
90
_syntax_called = true;
91
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);
95
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);
99
}
100
};
101
102
103
namespace {
104
105
106
static int
107
lua_syntax(lutok::state& state)
108
{
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);
112
113
if (syntax_version == 1) {
114
if (state.get_top() != 2)
115
throw config::value_error("Version 1 files need two arguments to "
116
"syntax()");
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");
120
} else {
121
if (state.get_top() != 1)
122
throw config::value_error("syntax() only takes one argument");
123
}
124
125
state.get_global("_config_parser");
126
config::parser::impl* impl =
127
*state.to_userdata< config::parser::impl* >(-1);
128
state.pop(1);
129
130
impl->syntax_callback(syntax_version);
131
132
return 0;
133
}
134
135
136
} // anonymous namespace
137
138
139
/// Constructs a new parser.
140
///
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))
145
{
146
lutok::stack_cleaner cleaner(_pimpl->_state);
147
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");
152
}
153
154
155
/// Destructor.
156
config::parser::~parser(void)
157
{
158
}
159
160
161
/// Parses a configuration file.
162
///
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.
166
///
167
/// \param file The path to the file to process.
168
///
169
/// \throw syntax_error If there is any problem processing the file.
170
void
171
config::parser::parse(const fs::path& file)
172
{
173
try {
174
lutok::do_file(_pimpl->_state, file.str(), 0, 0, 0);
175
} catch (const lutok::error& e) {
176
throw syntax_error(e.what());
177
}
178
179
if (!_pimpl->_syntax_called)
180
throw syntax_error("No syntax defined (no call to syntax() found)");
181
}
182
183