3
#include <boost/foreach.hpp>
6
#include <lua5.1/lua.hpp>
8
#include "lib/lua/include/lua.h"
9
#include "lib/lua/include/lualib.h"
10
#include "lib/lua/include/lauxlib.h"
14
#include "./LuaParser.hpp"
16
void LuaTable::Print(int depth) const {
17
std::string tabs = "";
18
for (int i = 0; i < depth; i++) {
22
for (std::map<LuaTable*, LuaTable*>::const_iterator it = TblTblPairs.begin(); it != TblTblPairs.end(); it++) {
23
std::cout << tabs << "k<tbl>: ";
24
std::cout << std::endl;
25
it->first->Print(depth + 1);
26
std::cout << tabs << "v<tbl>: ";
27
std::cout << std::endl;
28
it->second->Print(depth + 1);
30
for (std::map<LuaTable*, std::string>::const_iterator it = TblStrPairs.begin(); it != TblStrPairs.end(); it++) {
31
std::cout << tabs << "k<tbl>: ";
32
std::cout << std::endl;
33
it->first->Print(depth + 1);
34
std::cout << tabs << "v<str>: " << it->second;
35
std::cout << std::endl;
37
for (std::map<LuaTable*, int>::const_iterator it = TblIntPairs.begin(); it != TblIntPairs.end(); it++) {
38
std::cout << tabs << "k<tbl>: ";
39
std::cout << std::endl;
40
it->first->Print(depth + 1);
41
std::cout << tabs << "v<int>: " << it->second;
42
std::cout << std::endl;
45
for (std::map<std::string, LuaTable*>::const_iterator it = StrTblPairs.begin(); it != StrTblPairs.end(); it++) {
46
std::cout << tabs << "k<str>: " << it->first;
47
std::cout << ", v<tbl>: ";
48
std::cout << std::endl;
49
it->second->Print(depth + 1);
51
for (std::map<std::string, std::string>::const_iterator it = StrStrPairs.begin(); it != StrStrPairs.end(); it++) {
52
std::cout << tabs << "k<str>: " << it->first;
53
std::cout << ", v<str>: " << it->second;
54
std::cout << std::endl;
56
for (std::map<std::string, int>::const_iterator it = StrIntPairs.begin(); it != StrIntPairs.end(); it++) {
57
std::cout << tabs << "k<str>: " << it->first;
58
std::cout << ", v<int>: " << it->second;
59
std::cout << std::endl;
62
for (std::map<int, LuaTable*>::const_iterator it = IntTblPairs.begin(); it != IntTblPairs.end(); it++) {
63
std::cout << tabs << "k<int>: " << it->first;
64
std::cout << ", v<tbl>: ";
65
std::cout << std::endl;
66
it->second->Print(depth + 1);
68
for (std::map<int, std::string>::const_iterator it = IntStrPairs.begin(); it != IntStrPairs.end(); it++) {
69
std::cout << tabs << "k<int>: " << it->first;
70
std::cout << ", v<str>: " << it->second;
71
std::cout << std::endl;
73
for (std::map<int, int>::const_iterator it = IntIntPairs.begin(); it != IntIntPairs.end(); it++) {
74
std::cout << tabs << "k<int>: " << it->first;
75
std::cout << ", v<int>: " << it->second;
76
std::cout << std::endl;
80
void LuaTable::Parse(lua_State* ls, int depth) {
81
assert(lua_istable(ls, -1));
83
assert(lua_istable(ls, -2));
85
while (lua_next(ls, -2) != 0) {
86
assert(lua_istable(ls, -3));
88
switch (lua_type(ls, -2)) {
90
LuaTable* key = new LuaTable();
92
switch (lua_type(ls, -1)) {
94
TblTblPairs[key] = new LuaTable();
95
TblTblPairs[key]->Parse(ls, depth + 1);
99
key->Parse(ls, depth + 1);
102
TblStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
105
key->Parse(ls, depth + 1);
108
TblIntPairs[key] = lua_tointeger(ls, -1);
111
key->Parse(ls, depth + 1);
119
const std::string key = AIUtil::StringToLower(lua_tostring(ls, -2));
121
switch (lua_type(ls, -1)) {
123
StrTblPairs[key] = new LuaTable();
124
StrTblPairs[key]->Parse(ls, depth + 1);
127
StrStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
130
StrIntPairs[key] = lua_tointeger(ls, -1);
139
const int key = lua_tointeger(ls, -2);
141
switch (lua_type(ls, -1)) {
143
IntTblPairs[key] = new LuaTable();
144
IntTblPairs[key]->Parse(ls, depth + 1);
147
IntStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
150
IntIntPairs[key] = lua_tointeger(ls, -1);
160
assert(lua_istable(ls, -1));
163
void LuaTable::GetTblTblKeys(std::list<LuaTable*>* keys) const { BOOST_FOREACH(TblTblPair p, TblTblPairs) { keys->push_back(p.first); } }
164
void LuaTable::GetTblStrKeys(std::list<LuaTable*>* keys) const { BOOST_FOREACH(TblStrPair p, TblStrPairs) { keys->push_back(p.first); } }
165
void LuaTable::GetTblIntKeys(std::list<LuaTable*>* keys) const { BOOST_FOREACH(TblIntPair p, TblIntPairs) { keys->push_back(p.first); } }
166
void LuaTable::GetStrTblKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrTblPair p, StrTblPairs) { keys->push_back(p.first); } }
167
void LuaTable::GetStrStrKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrStrPair p, StrStrPairs) { keys->push_back(p.first); } }
168
void LuaTable::GetStrIntKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrIntPair p, StrIntPairs) { keys->push_back(p.first); } }
169
void LuaTable::GetIntTblKeys(std::list<int>* keys) const { BOOST_FOREACH(IntTblPair p, IntTblPairs) { keys->push_back(p.first); } }
170
void LuaTable::GetIntStrKeys(std::list<int>* keys) const { BOOST_FOREACH(IntStrPair p, IntStrPairs) { keys->push_back(p.first); } }
171
void LuaTable::GetIntIntKeys(std::list<int>* keys) const { BOOST_FOREACH(IntIntPair p, IntIntPairs) { keys->push_back(p.first); } }
173
const LuaTable* LuaTable::GetTblVal(LuaTable* key, LuaTable* defVal) const {
174
const std::map<LuaTable*, LuaTable*>::const_iterator it = TblTblPairs.find(key);
175
return ((it != TblTblPairs.end())? it->second: defVal);
177
const LuaTable* LuaTable::GetTblVal(const std::string& key, LuaTable* defVal) const {
178
const std::map<std::string, LuaTable*>::const_iterator it = StrTblPairs.find(key);
179
return ((it != StrTblPairs.end())? it->second: defVal);
181
const LuaTable* LuaTable::GetTblVal(int key, LuaTable* defVal) const {
182
const std::map<int, LuaTable*>::const_iterator it = IntTblPairs.find(key);
183
return ((it != IntTblPairs.end())? it->second: defVal);
186
const std::string& LuaTable::GetStrVal(LuaTable* key, const std::string& defVal) const {
187
const std::map<LuaTable*, std::string>::const_iterator it = TblStrPairs.find(key);
188
return ((it != TblStrPairs.end())? it->second: defVal);
190
const std::string& LuaTable::GetStrVal(const std::string& key, const std::string& defVal) const {
191
const std::map<std::string, std::string>::const_iterator it = StrStrPairs.find(key);
192
return ((it != StrStrPairs.end())? it->second: defVal);
194
const std::string& LuaTable::GetStrVal(int key, const std::string& defVal) const {
195
const std::map<int, std::string>::const_iterator it = IntStrPairs.find(key);
196
return ((it != IntStrPairs.end())? it->second: defVal);
199
int LuaTable::GetIntVal(LuaTable* key, int defVal) const {
200
const std::map<LuaTable*, int>::const_iterator it = TblIntPairs.find(key);
201
return ((it != TblIntPairs.end())? it->second: defVal);
203
int LuaTable::GetIntVal(const std::string& key, int defVal) const {
204
const std::map<std::string, int>::const_iterator it = StrIntPairs.find(key);
205
return ((it != StrIntPairs.end())? it->second: defVal);
207
int LuaTable::GetIntVal(int key, int defVal) const {
208
const std::map<int, int>::const_iterator it = IntIntPairs.find(key);
209
return ((it != IntIntPairs.end())? it->second: defVal);
214
LuaParser::LuaParser(): luaState(lua_open()), root(NULL) {
215
luaL_openlibs(luaState);
217
LuaParser::~LuaParser() {
218
for (std::map<std::string, LuaTable*>::iterator it = tables.begin(); it != tables.end(); it++) {
226
bool LuaParser::Execute(const std::string& file, const std::string& table) {
229
int loadErr = 0; // 0 | LUA_ERRFILE | LUA_ERRSYNTAX | LUA_ERRMEM
230
int callErr = 0; // 0 | LUA_ERRRUN | LUA_ERRMEM | LUA_ERRERR
232
if ((loadErr = luaL_loadfile(luaState, file.c_str())) != 0 || (callErr = lua_pcall(luaState, 0, 0, 0)) != 0) {
233
error = std::string(lua_tostring(luaState, -1));
234
lua_pop(luaState, 1);
238
if (tables.find(file) == tables.end()) {
239
tables[file] = new LuaTable();
242
assert(lua_gettop(luaState) == 0);
243
lua_getglobal(luaState, table.c_str());
245
if (lua_isnil(luaState, -1) == 0) {
246
assert(lua_istable(luaState, -1));
247
root->Parse(luaState, 0);
250
error = "no global table variable \'" + table + "\' declared in chunk \'" + file + "\'";
253
lua_pop(luaState, 1);
254
assert(lua_gettop(luaState) == 0);