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/, /, src/core/, bashast/, test/, utils/
Date: Thu, 28 Apr 2011 06:20:07
Message-Id: e86cc25a58e07c717606dbfd95d2497a9dd6f638.betelgeuse@gentoo
1 commit: e86cc25a58e07c717606dbfd95d2497a9dd6f638
2 Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
3 AuthorDate: Tue Apr 26 08:22:20 2011 +0000
4 Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
5 CommitDate: Thu Apr 28 02:52:42 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=e86cc25a
7
8 Core: use reference/pointer for the interpreter object
9
10 We don't share interpreter object and it's safer to use reference
11 rather than shared_ptr. Raw pointer is used in the generated C
12 source code.
13
14 ---
15 Makefile.am | 6 +--
16 bashast/libbashWalker.g | 7 ++--
17 src/core/{parser_builder.cpp => bash_ast.cpp} | 27 ++++++++------
18 src/core/{parser_builder.h => bash_ast.h} | 23 ++++++------
19 src/core/walker_builder.cpp | 36 -------------------
20 src/core/walker_builder.h | 46 -------------------------
21 src/libbash.cpp | 12 +++---
22 test/walker_test.cpp | 20 ++++++-----
23 utils/ast_printer.cpp | 16 ++++----
24 9 files changed, 56 insertions(+), 137 deletions(-)
25
26 diff --git a/Makefile.am b/Makefile.am
27 index 7202cef..aefcf51 100644
28 --- a/Makefile.am
29 +++ b/Makefile.am
30 @@ -164,10 +164,8 @@ libcppbash_la_SOURCES = src/common.h \
31 src/core/interpreter.cpp \
32 src/core/interpreter.h \
33 src/core/symbols.hpp \
34 - src/core/parser_builder.cpp \
35 - src/core/parser_builder.h \
36 - src/core/walker_builder.cpp \
37 - src/core/walker_builder.h \
38 + src/core/bash_ast.cpp \
39 + src/core/bash_ast.h \
40 $(GENERATED_WALKER_C) \
41 $(GENERATED_WALKER_H)
42
43
44 diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
45 index b8fd25a..e278d1b 100644
46 --- a/bashast/libbashWalker.g
47 +++ b/bashast/libbashWalker.g
48 @@ -27,12 +27,11 @@ options
49
50 @includes{
51
52 - #include <memory>
53 #include <string>
54 #include <vector>
55
56 class interpreter;
57 - void set_interpreter(std::shared_ptr<interpreter> w);
58 + void set_interpreter(interpreter* w);
59
60 }
61
62 @@ -50,9 +49,9 @@ options
63
64 @members{
65
66 - static std::shared_ptr<interpreter> walker;
67 + static interpreter* walker = 0;
68
69 - void set_interpreter(std::shared_ptr<interpreter> w)
70 + void set_interpreter(interpreter* w)
71 {
72 walker = w;
73 }
74
75 diff --git a/src/core/parser_builder.cpp b/src/core/bash_ast.cpp
76 similarity index 86%
77 rename from src/core/parser_builder.cpp
78 rename to src/core/bash_ast.cpp
79 index c136e90..3010774 100644
80 --- a/src/core/parser_builder.cpp
81 +++ b/src/core/bash_ast.cpp
82 @@ -17,21 +17,21 @@
83 along with libbash. If not, see <http://www.gnu.org/licenses/>.
84 */
85 ///
86 -/// \file parser_builder.cpp
87 +/// \file bash_ast.cpp
88 /// \author Mu Qiao
89 -/// \brief a class that helps build libbashParser from istream
90 +/// \brief implementation that helps interpret from istream
91 ///
92
93 -#include "core/parser_builder.h"
94 +#include "core/bash_ast.h"
95
96 #include <sstream>
97
98 #include "core/interpreter_exception.h"
99 #include "libbashLexer.h"
100 #include "libbashParser.h"
101 -#include "walker_builder.h"
102 +#include "libbashWalker.h"
103
104 -parser_builder::parser_builder(std::istream& source)
105 +bash_ast::bash_ast(std::istream& source)
106 {
107 std::stringstream stream;
108 stream << source.rdbuf();
109 @@ -44,7 +44,7 @@ parser_builder::parser_builder(std::istream& source)
110 init_parser();
111 }
112
113 -parser_builder::~parser_builder()
114 +bash_ast::~bash_ast()
115 {
116 nodes->free(nodes);
117 psr->free(psr);
118 @@ -53,7 +53,7 @@ parser_builder::~parser_builder()
119 input->close(input);
120 }
121
122 -void parser_builder::init_parser()
123 +void bash_ast::init_parser()
124 {
125 lxr = libbashLexerNew(input);
126 if ( lxr == NULL )
127 @@ -72,18 +72,21 @@ void parser_builder::init_parser()
128 nodes = antlr3CommonTreeNodeStreamNewTree(langAST->tree, ANTLR3_SIZE_HINT);
129 }
130
131 -walker_builder parser_builder::create_walker_builder()
132 +void bash_ast::interpret_with(interpreter& walker)
133 {
134 - return walker_builder(nodes);
135 + set_interpreter(&walker);
136 + plibbashWalker treePsr = libbashWalkerNew(nodes);
137 + treePsr->start(treePsr);
138 + treePsr->free(treePsr);
139 }
140
141 -std::string parser_builder::get_dot_graph()
142 +std::string bash_ast::get_dot_graph()
143 {
144 pANTLR3_STRING graph = nodes->adaptor->makeDot(nodes->adaptor, langAST->tree);
145 return std::string(reinterpret_cast<char*>(graph->chars));
146 }
147
148 -std::string parser_builder::get_string_tree()
149 +std::string bash_ast::get_string_tree()
150 {
151 return std::string(reinterpret_cast<char*>(
152 langAST->tree->toStringTree(langAST->tree)->chars));
153 @@ -102,7 +105,7 @@ namespace
154 }
155 }
156
157 -std::string parser_builder::get_tokens(std::function<std::string(ANTLR3_INT32)> token_map)
158 +std::string bash_ast::get_tokens(std::function<std::string(ANTLR3_INT32)> token_map)
159 {
160 std::stringstream result;
161 int line_counter = 1;
162
163 diff --git a/src/core/parser_builder.h b/src/core/bash_ast.h
164 similarity index 75%
165 rename from src/core/parser_builder.h
166 rename to src/core/bash_ast.h
167 index cbfa13c..d7ae27c 100644
168 --- a/src/core/parser_builder.h
169 +++ b/src/core/bash_ast.h
170 @@ -17,9 +17,9 @@
171 along with libbash. If not, see <http://www.gnu.org/licenses/>.
172 */
173 ///
174 -/// \file parser_builder.h
175 +/// \file bash_ast.h
176 /// \author Mu Qiao
177 -/// \brief a class that helps build libbashParser from istream
178 +/// \brief a class that helps interpret from istream
179 ///
180
181 #ifndef LIBBASH_CORE_PARSER_BUILDER_H_
182 @@ -35,12 +35,11 @@
183 struct libbashLexer_Ctx_struct;
184 struct libbashParser_Ctx_struct;
185 struct libbashParser_start_return_struct;
186 -class walker_builder;
187 +class interpreter;
188
189 -/// \class parser_builder
190 -/// \brief a wrapper class that creates libbashParser. It also know
191 -/// enough information to create walker_builder object.
192 -class parser_builder
193 +/// \class bash_ast
194 +/// \brief a wrapper class that helps interpret from istream
195 +class bash_ast
196 {
197 pANTLR3_INPUT_STREAM input;
198 std::string script;
199 @@ -52,13 +51,13 @@ class parser_builder
200
201 void init_parser();
202 public:
203 - explicit parser_builder(std::istream& source);
204 - ~parser_builder();
205 + explicit bash_ast(std::istream& source);
206 + ~bash_ast();
207
208 ///
209 - /// \brief factory method that creates walker_builder
210 - /// \return walker_builder object
211 - walker_builder create_walker_builder();
212 + /// \brief interpret the script with a given interpreter
213 + /// \param the interpreter object
214 + void interpret_with(interpreter& walker);
215 std::string get_dot_graph();
216 std::string get_string_tree();
217 std::string get_tokens(std::function<std::string(ANTLR3_INT32)> token_map);
218
219 diff --git a/src/core/walker_builder.cpp b/src/core/walker_builder.cpp
220 deleted file mode 100644
221 index dbe47d9..0000000
222 --- a/src/core/walker_builder.cpp
223 +++ /dev/null
224 @@ -1,36 +0,0 @@
225 -/*
226 - Please use git log for copyright holder and year information
227 -
228 - This file is part of libbash.
229 -
230 - libbash is free software: you can redistribute it and/or modify
231 - it under the terms of the GNU General Public License as published by
232 - the Free Software Foundation, either version 2 of the License, or
233 - (at your option) any later version.
234 -
235 - libbash is distributed in the hope that it will be useful,
236 - but WITHOUT ANY WARRANTY; without even the implied warranty of
237 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
238 - GNU General Public License for more details.
239 -
240 - You should have received a copy of the GNU General Public License
241 - along with libbash. If not, see <http://www.gnu.org/licenses/>.
242 -*/
243 -///
244 -/// \file walker_builder.cpp
245 -/// \author Mu Qiao
246 -/// \brief a class that helps build a libbashWalker from istream
247 -///
248 -
249 -#include "core/walker_builder.h"
250 -
251 -#include "core/interpreter.h"
252 -#include "libbashWalker.h"
253 -
254 -walker_builder::walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes): walker(new interpreter)
255 -{
256 - set_interpreter(walker);
257 - plibbashWalker treePsr = libbashWalkerNew(nodes);
258 - treePsr->start(treePsr);
259 - treePsr->free(treePsr);
260 -}
261
262 diff --git a/src/core/walker_builder.h b/src/core/walker_builder.h
263 deleted file mode 100644
264 index 6b6714b..0000000
265 --- a/src/core/walker_builder.h
266 +++ /dev/null
267 @@ -1,46 +0,0 @@
268 -/*
269 - Please use git log for copyright holder and year information
270 -
271 - This file is part of libbash.
272 -
273 - libbash is free software: you can redistribute it and/or modify
274 - it under the terms of the GNU General Public License as published by
275 - the Free Software Foundation, either version 2 of the License, or
276 - (at your option) any later version.
277 -
278 - libbash is distributed in the hope that it will be useful,
279 - but WITHOUT ANY WARRANTY; without even the implied warranty of
280 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
281 - GNU General Public License for more details.
282 -
283 - You should have received a copy of the GNU General Public License
284 - along with libbash. If not, see <http://www.gnu.org/licenses/>.
285 -*/
286 -///
287 -/// \file walker_builder.h
288 -/// \author Mu Qiao
289 -/// \brief a class that helps build a libbashWalker
290 -///
291 -
292 -#ifndef LIBBASH_CORE_WALKER_BUILDER_H_
293 -#define LIBBASH_CORE_WALKER_BUILDER_H_
294 -
295 -#include <memory>
296 -
297 -#include <antlr3.h>
298 -
299 -class interpreter;
300 -
301 -/// \class walker_builder
302 -/// \brief a wrapper class that creates libbashWalker
303 -class walker_builder
304 -{
305 -public:
306 - /// \var public::walker
307 - /// \brief the interpreter object that contains all runtime information
308 - std::shared_ptr<interpreter> walker;
309 -
310 - explicit walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes);
311 -};
312 -
313 -#endif
314
315 diff --git a/src/libbash.cpp b/src/libbash.cpp
316 index 0d1eadd..4fb4102 100644
317 --- a/src/libbash.cpp
318 +++ b/src/libbash.cpp
319 @@ -27,8 +27,7 @@
320 #include <fstream>
321
322 #include "core/interpreter.h"
323 -#include "core/parser_builder.h"
324 -#include "core/walker_builder.h"
325 +#include "core/bash_ast.h"
326
327 namespace libbash
328 {
329 @@ -39,11 +38,12 @@ namespace libbash
330 std::ifstream input(path.c_str());
331 if(!input)
332 throw interpreter_exception("Unable to create fstream for script: " + path);
333 - parser_builder pbuilder(input);
334 - walker_builder wbuilder = pbuilder.create_walker_builder();
335 + interpreter walker;
336 + bash_ast ast(input);
337 + ast.interpret_with(walker);
338
339 - for(auto iter = wbuilder.walker->begin(); iter != wbuilder.walker->end(); ++iter)
340 + for(auto iter = walker.begin(); iter != walker.end(); ++iter)
341 iter->second->get_all_values<std::string>(variables[iter->first]);
342 - wbuilder.walker->get_all_function_names(functions);
343 + walker.get_all_function_names(functions);
344 }
345 }
346
347 diff --git a/test/walker_test.cpp b/test/walker_test.cpp
348 index 6f13176..c3aacbb 100644
349 --- a/test/walker_test.cpp
350 +++ b/test/walker_test.cpp
351 @@ -27,17 +27,17 @@
352 #include <gtest/gtest.h>
353
354 #include "core/interpreter.h"
355 -#include "core/parser_builder.h"
356 -#include "core/walker_builder.h"
357 +#include "core/bash_ast.h"
358
359 static void check_string_assignment(const char* script,
360 const std::string& name,
361 const char* exp_value)
362 {
363 + interpreter walker;
364 std::istringstream input(script);
365 - parser_builder pbuilder(input);
366 - walker_builder wbuilder = pbuilder.create_walker_builder();
367 - EXPECT_STREQ(exp_value, wbuilder.walker->resolve<std::string>(name).c_str());
368 + bash_ast ast(input);
369 + ast.interpret_with(walker);
370 + EXPECT_STREQ(exp_value, walker.resolve<std::string>(name).c_str());
371 }
372
373 #define TEST_STRING_ASSIGNMENT(name, script, var_name, exp_value)\
374 @@ -62,13 +62,15 @@ TEST_STRING_ASSIGNMENT(str_assignment6,
375
376 TEST(array_index, out_of_bound)
377 {
378 + interpreter walker;
379 +
380 std::string script = "a[-1]=\"1\"";
381 std::istringstream input(script);
382 - parser_builder pbuilder(input);
383 - EXPECT_THROW(pbuilder.create_walker_builder(), interpreter_exception);
384 + bash_ast ast(input);
385 + EXPECT_THROW(ast.interpret_with(walker), interpreter_exception);
386
387 std::string script2 = "a=(1 2 [-5]=1)";
388 std::istringstream input2(script2);
389 - parser_builder pbuilder2(input2);
390 - EXPECT_THROW(pbuilder2.create_walker_builder(), interpreter_exception);
391 + bash_ast ast2(input2);
392 + EXPECT_THROW(ast2.interpret_with(walker), interpreter_exception);
393 }
394
395 diff --git a/utils/ast_printer.cpp b/utils/ast_printer.cpp
396 index 66d8c8a..dae4233 100644
397 --- a/utils/ast_printer.cpp
398 +++ b/utils/ast_printer.cpp
399 @@ -33,7 +33,7 @@
400 #include <boost/spirit/include/qi.hpp>
401 #include <boost/fusion/include/adapt_struct.hpp>
402
403 -#include "core/parser_builder.h"
404 +#include "core/bash_ast.h"
405 #include "libbashParser.h"
406
407 namespace po = boost::program_options;
408 @@ -50,15 +50,15 @@ BOOST_FUSION_ADAPT_STRUCT(
409
410 static void print_ast(std::istream& input, bool silent, bool dot)
411 {
412 - parser_builder parser(input);
413 + bash_ast ast(input);
414
415 if(silent)
416 return;
417
418 if(dot)
419 - std::cout << parser.get_dot_graph() << std::endl;
420 + std::cout << ast.get_dot_graph() << std::endl;
421 else
422 - std::cout << parser.get_string_tree() << std::endl;
423 + std::cout << ast.get_string_tree() << std::endl;
424 }
425
426 static inline std::string token_mapper(std::unordered_map<ANTLR3_INT32, std::string> token_map,
427 @@ -90,14 +90,14 @@ static inline void print_token(std::istream& input,
428 if(silent)
429 return;
430
431 - parser_builder parser(input);
432 + bash_ast ast(input);
433 std::unordered_map<ANTLR3_INT32, std::string> token_map;
434
435 if(build_token_map(token_map, token_path))
436 {
437 - std::cout << parser.get_tokens(std::bind(&token_mapper,
438 - token_map,
439 - std::placeholders::_1))
440 + std::cout << ast.get_tokens(std::bind(&token_mapper,
441 + token_map,
442 + std::placeholders::_1))
443 << std::endl;
444 }
445 else