1 // Copyright 2012 Google Inc. 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/logging/macros.hpp" 40 #include "utils/noncopyable.hpp" 41 42 namespace config = utils::config; 43 44 45 // History of configuration file versions: 46 // 47 // 2 - Changed the syntax() call to take only a version number, instead of the 48 // word 'config' as the first argument and the version as the second one. 49 // Files now start with syntax(2) instead of syntax('config', 1). 50 // 51 // 1 - Initial version. 52 53 54 /// Internal implementation of the parser. 55 struct utils::config::parser::impl : utils::noncopyable { 56 /// Pointer to the parent parser. Needed for callbacks. 57 parser* _parent; 58 59 /// The Lua state used by this parser to process the configuration file. 60 lutok::state _state; 61 62 /// The tree to be filed in by the configuration parameters, as provided by 63 /// the caller. 64 config::tree& _tree; 65 66 /// Whether syntax() has been called or not. 67 bool _syntax_called; 68 69 /// Constructs a new implementation. 70 /// 71 /// \param parent_ Pointer to the class being constructed. 72 /// \param config_tree_ The configuration tree provided by the user. 73 impl(parser* const parent_, tree& config_tree_) : 74 _parent(parent_), _tree(config_tree_), _syntax_called(false) 75 { 76 } 77 78 friend void lua_syntax(lutok::state&); 79 80 /// Callback executed by the Lua syntax() function. 81 /// 82 /// \param syntax_version The syntax format version as provided by the 83 /// configuration file in the call to syntax(). 84 void 85 syntax_callback(const int syntax_version) 86 { 87 if (_syntax_called) 88 throw syntax_error("syntax() can only be called once"); 89 _syntax_called = true; 90 91 // Allow the parser caller to populate the tree with its own schema 92 // depending on the format/version combination. 93 _parent->setup(_tree, syntax_version); 94 95 // Export the config module to the Lua state so that all global variable 96 // accesses are redirected to the configuration tree. 97 config::redirect(_state, _tree); 98 } 99 }; 100 101 102 namespace { 103 104 105 /// Implementation of the Lua syntax() function. 106 /// 107 /// The syntax() function has to be called by configuration files as the very 108 /// first thing they do. Once called, this function populates the configuration 109 /// tree based on the syntax version and then continues to process the rest of 110 /// the file. 111 /// 112 /// \pre state(-2) The syntax format name, if a v1 file. 113 /// \pre state(-1) The syntax format version. 114 /// 115 /// \param state The Lua state to operate in. 116 /// 117 /// \return The number of results pushed onto the stack; always 0. 118 static int 119 lua_syntax(lutok::state& state) 120 { 121 if (!state.is_number(-1)) 122 throw config::value_error("Last argument to syntax must be a number"); 123 const int syntax_version = state.to_integer(-1); 124 125 if (syntax_version == 1) { 126 if (state.get_top() != 2) 127 throw config::value_error("Version 1 files need two arguments to " 128 "syntax()"); 129 if (!state.is_string(-2) || state.to_string(-2) != "config") 130 throw config::value_error("First argument to syntax must be " 131 "'config' for version 1 files"); 132 } else { 133 if (state.get_top() != 1) 134 throw config::value_error("syntax() only takes one argument"); 135 } 136 137 state.get_global("_config_parser"); 138 config::parser::impl* impl = *state.to_userdata< config::parser::impl* >(); 139 state.pop(1); 140 141 impl->syntax_callback(syntax_version); 142 143 return 0; 144 } 145 146 147 } // anonymous namespace 148 149 150 /// Constructs a new parser. 151 /// 152 /// \param [in,out] config_tree The configuration tree into which the values set 153 /// in the configuration file will be stored. 154 config::parser::parser(tree& config_tree) : 155 _pimpl(new impl(this, config_tree)) 156 { 157 lutok::stack_cleaner cleaner(_pimpl->_state); 158 159 _pimpl->_state.push_cxx_function(lua_syntax); 160 _pimpl->_state.set_global("syntax"); 161 *_pimpl->_state.new_userdata< config::parser::impl* >() = _pimpl.get(); 162 _pimpl->_state.set_global("_config_parser"); 163 } 164 165 166 /// Destructor. 167 config::parser::~parser(void) 168 { 169 } 170 171 172 /// Parses a configuration file. 173 /// 174 /// \post The tree registered during the construction of this class is updated 175 /// to contain the values read from the configuration file. If the processing 176 /// fails, the state of the output tree is undefined. 177 /// 178 /// \param file The path to the file to process. 179 /// 180 /// \throw syntax_error If there is any problem processing the file. 181 void 182 config::parser::parse(const fs::path& file) 183 { 184 try { 185 lutok::do_file(_pimpl->_state, file.str()); 186 } catch (const lutok::error& e) { 187 throw syntax_error(e.what()); 188 } 189 190 if (!_pimpl->_syntax_called) 191 throw syntax_error("No syntax defined (no call to syntax() found)"); 192 } 193