Gentoo Archives: gentoo-commits

From: "Petteri Räty" <betelgeuse@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/libbash:master commit in: src/, scripts/, /, src/core/, test/
Date: Sat, 02 Apr 2011 15:50:53
Message-Id: 9c079e2625fefe91fb00c512e2253dab53844b33.betelgeuse@gentoo
1 commit: 9c079e2625fefe91fb00c512e2253dab53844b33
2 Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
3 AuthorDate: Sat Apr 2 02:56:18 2011 +0000
4 Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
5 CommitDate: Sat Apr 2 15:46:49 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=9c079e26
7
8 Implement a public interface for variable retrieval
9
10 The interface will parse a specified script and return the
11 variables defined in the script. interpreter_exception will be
12 thrown if anything goes wrong while parsing.
13
14 ---
15 Makefile.am | 14 +++-
16 scripts/var_def.ebuild | 10 +++
17 src/core/interpreter_exception.h | 4 +-
18 src/libbash.cpp | 71 ++++++++++++++++++++
19 src/{core/interpreter_exception.h => libbash.h} | 32 +++++----
20 .../interpreter_exception.h => test/api_test.cpp | 28 +++-----
21 6 files changed, 125 insertions(+), 34 deletions(-)
22
23 diff --git a/Makefile.am b/Makefile.am
24 index e8bbdb6..b6b2616 100644
25 --- a/Makefile.am
26 +++ b/Makefile.am
27 @@ -80,6 +80,7 @@ cppunittests_SOURCES = test/run_tests.cpp \
28 src/builtins/tests/echo_tests.cpp \
29 src/builtins/tests/boolean_tests.cpp \
30 test/post_check.cpp \
31 + test/api_test.cpp \
32 test/walker_test.cpp
33 cppunittests_LDADD = libcppbash.la \
34 $(GTEST_LIBS) \
35 @@ -92,6 +93,10 @@ GENERATED_PARSER_C = libbashLexer.c libbashParser.c
36 GENERATED_PARSER_H = libbashLexer.h libbashParser.h
37 GENERATED_WALKER_C = libbashWalker.c
38 GENERATED_WALKER_H = libbashWalker.h
39 +BUILT_SOURCES = $(GENERATED_PARSER_C) \
40 + $(GENERATED_PARSER_H) \
41 + $(GENERATED_WALKER_C) \
42 + $(GENERATED_WALKER_H)
43 CLEANFILES = $(GENERATED_PARSER_C) \
44 $(GENERATED_PARSER_H) \
45 $(GENERATED_WALKER_C) \
46 @@ -106,17 +111,22 @@ CLEANFILES = $(GENERATED_PARSER_C) \
47 walker.run
48
49 lib_LTLIBRARIES = libcppbash.la
50 -libcppbash_la_SOURCES = src/cppbash_builtin.cpp \
51 +libcppbash_la_SOURCES = src/common.h \
52 + src/libbash.h \
53 + src/libbash.cpp \
54 + src/cppbash_builtin.cpp \
55 src/cppbash_builtin.h \
56 src/builtins/echo_builtin.cpp \
57 src/builtins/echo_builtin.h \
58 src/builtins/boolean_builtins.h \
59 $(GENERATED_PARSER_C) \
60 + $(GENERATED_PARSER_H) \
61 src/core/interpreter_exception.h \
62 src/core/interpreter.cpp \
63 src/core/interpreter.h \
64 src/core/symbols.hpp \
65 - $(GENERATED_WALKER_C)
66 + $(GENERATED_WALKER_C) \
67 + $(GENERATED_WALKER_H)
68
69 HIDDEN_FLAGS = -fvisibility=hidden \
70 -fvisibility-inlines-hidden
71
72 diff --git a/scripts/var_def.ebuild b/scripts/var_def.ebuild
73 new file mode 100644
74 index 0000000..7100b84
75 --- /dev/null
76 +++ b/scripts/var_def.ebuild
77 @@ -0,0 +1,10 @@
78 +EAPI="1"
79 +DESCRIPTION="SunPinyin is a SLM (Statistical Language Model) based IME"
80 +HOMEPAGE="http://sunpinyin.googlecode.com"
81 +SRC_URI="http://open-gram.googlecode.com/files/dict.utf8.tar.bz2
82 + http://open-gram.googlecode.com/files/lm_sc.t3g.arpa.tar.bz2"
83 +LICENSE="LGPL-2.1 CDDL"
84 +SLOT="0"
85 +KEYWORDS="~amd64 ~x86"
86 +IUSE="abc"
87 +RDEPEND="dev-db/sqlite:3"
88
89 diff --git a/src/core/interpreter_exception.h b/src/core/interpreter_exception.h
90 index 785c6c3..bfce335 100644
91 --- a/src/core/interpreter_exception.h
92 +++ b/src/core/interpreter_exception.h
93 @@ -28,11 +28,13 @@
94 #include <stdexcept>
95 #include <string>
96
97 +#include "common.h"
98 +
99 ///
100 /// \class interpreter_exception
101 /// \brief runtime exception occured during interpreting
102 ///
103 -class interpreter_exception: public std::runtime_error
104 +class LIBBASH_API interpreter_exception: public std::runtime_error
105 {
106 public:
107 explicit interpreter_exception(const std::string& err_msg):
108
109 diff --git a/src/libbash.cpp b/src/libbash.cpp
110 new file mode 100644
111 index 0000000..60076b5
112 --- /dev/null
113 +++ b/src/libbash.cpp
114 @@ -0,0 +1,71 @@
115 +/*
116 + Please use git log for copyright holder and year information
117 +
118 + This file is part of libbash.
119 +
120 + libbash is free software: you can redistribute it and/or modify
121 + it under the terms of the GNU General Public License as published by
122 + the Free Software Foundation, either version 2 of the License, or
123 + (at your option) any later version.
124 +
125 + libbash is distributed in the hope that it will be useful,
126 + but WITHOUT ANY WARRANTY; without even the implied warranty of
127 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128 + GNU General Public License for more details.
129 +
130 + You should have received a copy of the GNU General Public License
131 + along with libbash. If not, see <http://www.gnu.org/licenses/>.
132 +*/
133 +///
134 +/// \file libbash.cpp
135 +/// \author Mu Qiao
136 +/// \brief implementation for libbash interface
137 +///
138 +
139 +#include "libbash.h"
140 +
141 +#include <antlr3defs.h>
142 +
143 +#include "core/interpreter_exception.h"
144 +#include "libbashLexer.h"
145 +#include "libbashParser.h"
146 +#include "libbashWalker.h"
147 +
148 +namespace libbash
149 +{
150 +
151 + void interpret(const std::string& path,
152 + std::unordered_map<std::string, std::string>& variables)
153 + {
154 + pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew(
155 + reinterpret_cast<pANTLR3_UINT8>(const_cast<char *>(path.c_str())));
156 + if ( input == NULL )
157 + throw interpreter_exception("Unable to create input stream for script: " + path);
158 +
159 + plibbashLexer lxr = libbashLexerNew(input);
160 + if ( lxr == NULL )
161 + throw interpreter_exception("Unable to create the lexer due to malloc() failure");
162 +
163 + pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(
164 + ANTLR3_SIZE_HINT, lxr->pLexer->rec->state->tokSource);
165 + if (tstream == NULL)
166 + throw interpreter_exception("Out of memory trying to allocate token stream");
167 +
168 + plibbashParser psr = libbashParserNew(tstream);
169 + if (psr == NULL)
170 + throw interpreter_exception("Out of memory trying to allocate parser");
171 +
172 + std::shared_ptr<interpreter> walker(new interpreter);
173 + set_interpreter(walker);
174 +
175 + libbashParser_start_return langAST = psr->start(psr);
176 + pANTLR3_COMMON_TREE_NODE_STREAM nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, ANTLR3_SIZE_HINT);
177 + plibbashWalker treePsr = libbashWalkerNew(nodes);
178 + treePsr->start(treePsr);
179 +
180 + for(auto iter = walker->begin(); iter != walker->end(); ++iter)
181 + {
182 + variables[iter->first]=std::static_pointer_cast<variable>(iter->second)->get_value<std::string>();
183 + }
184 + }
185 +}
186
187 diff --git a/src/core/interpreter_exception.h b/src/libbash.h
188 similarity index 58%
189 copy from src/core/interpreter_exception.h
190 copy to src/libbash.h
191 index 785c6c3..d265b5b 100644
192 --- a/src/core/interpreter_exception.h
193 +++ b/src/libbash.h
194 @@ -17,26 +17,30 @@
195 along with libbash. If not, see <http://www.gnu.org/licenses/>.
196 */
197 ///
198 -/// \file interpreter_exception.h
199 +/// \file libbash.h
200 /// \author Mu Qiao
201 -/// \brief implementation for interpreter_exception
202 +/// \brief public interface for libbash
203 ///
204
205 -#ifndef INTERPRETER_EXCEPTION_H_
206 -#define INTERPRETER_EXCEPTION_H_
207 +#ifndef LIBBASH_H
208 +#define LIBBASH_H
209
210 -#include <stdexcept>
211 +#include <memory>
212 #include <string>
213 +#include <unordered_map>
214
215 -///
216 -/// \class interpreter_exception
217 -/// \brief runtime exception occured during interpreting
218 -///
219 -class interpreter_exception: public std::runtime_error
220 +#include "common.h"
221 +#include "core/interpreter.h"
222 +
223 +namespace libbash
224 {
225 -public:
226 - explicit interpreter_exception(const std::string& err_msg):
227 - runtime_error(err_msg){}
228 -};
229 + ///
230 + /// \brief interpret a script specifid by path, return a map filled with
231 + /// variables defined in the script
232 + /// \param the path of target script
233 + /// \param the map to store variables
234 + void LIBBASH_API interpret(const std::string& path,
235 + std::unordered_map<std::string, std::string>& variables);
236 +}
237
238 #endif
239
240 diff --git a/src/core/interpreter_exception.h b/test/api_test.cpp
241 similarity index 62%
242 copy from src/core/interpreter_exception.h
243 copy to test/api_test.cpp
244 index 785c6c3..5972f14 100644
245 --- a/src/core/interpreter_exception.h
246 +++ b/test/api_test.cpp
247 @@ -17,26 +17,20 @@
248 along with libbash. If not, see <http://www.gnu.org/licenses/>.
249 */
250 ///
251 -/// \file interpreter_exception.h
252 +/// \file api_test.cpp
253 /// \author Mu Qiao
254 -/// \brief implementation for interpreter_exception
255 +/// \brief series of unit tests for the public interface
256 ///
257
258 -#ifndef INTERPRETER_EXCEPTION_H_
259 -#define INTERPRETER_EXCEPTION_H_
260 +#include <gtest/gtest.h>
261
262 -#include <stdexcept>
263 -#include <string>
264 +#include "libbash.h"
265
266 -///
267 -/// \class interpreter_exception
268 -/// \brief runtime exception occured during interpreting
269 -///
270 -class interpreter_exception: public std::runtime_error
271 -{
272 -public:
273 - explicit interpreter_exception(const std::string& err_msg):
274 - runtime_error(err_msg){}
275 -};
276 +using namespace std;
277
278 -#endif
279 +TEST(libbashapi, bad_path)
280 +{
281 + std::unordered_map<std::string, std::string> variables;
282 + EXPECT_THROW(libbash::interpret("not exist", variables),
283 + interpreter_exception);
284 +}