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