From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QOWDr-0005Ny-Hy for garchives@archives.gentoo.org; Mon, 23 May 2011 14:35:07 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B75731C02C; Mon, 23 May 2011 14:34:20 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 750451C02C for ; Mon, 23 May 2011 14:34:20 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id D26841B402A for ; Mon, 23 May 2011 14:34:19 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 38C968050A for ; Mon, 23 May 2011 14:34:19 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/ X-VCS-Repository: proj/libbash X-VCS-Files: src/core/bash_ast.cpp src/core/bash_ast.h src/core/tests/bash_ast_test.cpp X-VCS-Directories: src/core/ src/core/tests/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: f802859cbbb82e3376925f2ad7ec3f1ba238f5f8 Date: Mon, 23 May 2011 14:34:19 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 0d69f5ef14c01b4acd60afb94173908a commit: f802859cbbb82e3376925f2ad7ec3f1ba238f5f8 Author: Mu Qiao gentoo org> AuthorDate: Fri May 20 07:08:29 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Mon May 23 15:02:51 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3Df802859c Core: generalize the functionality of bash_ast The bash_ast class was for building AST from the start rule. Now its functionality is generalized and the AST can be built and interpreted from any rule. The bash_ast can be built from std::string now. --- src/core/bash_ast.cpp | 73 +++++++++++++++++++++-----------= ----- src/core/bash_ast.h | 56 +++++++++++++++++++++++++---- src/core/tests/bash_ast_test.cpp | 9 +++++ 3 files changed, 98 insertions(+), 40 deletions(-) diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp index d70520e..af0205b 100644 --- a/src/core/bash_ast.cpp +++ b/src/core/bash_ast.cpp @@ -19,34 +19,22 @@ /// /// \file bash_ast.cpp /// \author Mu Qiao -/// \brief implementation that helps interpret from istream +/// \brief a wrapper class that helps interpret from istream and string /// =20 -#include "core/bash_ast.h" - -#include - #include "core/interpreter_exception.h" -#include "core/interpreter.h" #include "libbashLexer.h" #include "libbashParser.h" -#include "libbashWalker.h" =20 -bash_ast::bash_ast(std::istream& source): error_count(0) +#include "core/bash_ast.h" + +bash_ast::bash_ast(const std::istream& source, + std::function p): = parse(p) { std::stringstream stream; stream << source.rdbuf(); script =3D stream.str(); - - input =3D antlr3NewAsciiStringInPlaceStream( - reinterpret_cast(const_cast(script.c_str()))= , - script.size(), - NULL); - - if(input =3D=3D NULL) - throw interpreter_exception("Unable to open file " + script + " due = to malloc() failure"); - - init_parser(); + init_parser(script); } =20 bash_ast::~bash_ast() @@ -58,8 +46,16 @@ bash_ast::~bash_ast() input->close(input); } =20 -void bash_ast::init_parser() +void bash_ast::init_parser(const std::string& script) { + input =3D antlr3NewAsciiStringInPlaceStream( + reinterpret_cast(const_cast(script.c_str())), + script.size(), + NULL); + + if(input =3D=3D NULL) + throw interpreter_exception("Unable to open file " + script + " due = to malloc() failure"); + lexer =3D libbashLexerNew(input); if ( lexer =3D=3D NULL ) { @@ -85,29 +81,20 @@ void bash_ast::init_parser() return; } =20 - ast.reset(new libbashParser_start_return(parser->start(parser))); + ast =3D parse(parser); error_count =3D parser->pParser->rec->getNumberOfSyntaxErrors(parser->= pParser->rec); - nodes =3D antlr3CommonTreeNodeStreamNewTree(ast->tree, ANTLR3_SIZE_HIN= T); -} - -void bash_ast::interpret_with(interpreter& walker) -{ - set_interpreter(&walker); - plibbashWalker treeparser =3D libbashWalkerNew(nodes); - treeparser->start(treeparser); - treeparser->free(treeparser); + nodes =3D antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT); } =20 std::string bash_ast::get_dot_graph() { - pANTLR3_STRING graph =3D nodes->adaptor->makeDot(nodes->adaptor, ast->= tree); + pANTLR3_STRING graph =3D nodes->adaptor->makeDot(nodes->adaptor, ast); return std::string(reinterpret_cast(graph->chars)); } =20 std::string bash_ast::get_string_tree() { - return std::string(reinterpret_cast( - ast->tree->toStringTree(ast->tree)->chars)); + return std::string(reinterpret_cast(ast->toStringTree(ast)->cha= rs)); } =20 namespace @@ -161,3 +148,25 @@ std::string bash_ast::get_tokens(std::function token_ =20 return result.str(); } + +void bash_ast::walker_start(plibbashWalker tree_parser) +{ + tree_parser->start(tree_parser); +} + +int bash_ast::walker_arithmetics(plibbashWalker tree_parser) +{ + return tree_parser->arithmetics(tree_parser); +} + +pANTLR3_BASE_TREE bash_ast::parser_start(plibbashParser parser) +{ + return parser->start(parser).tree; +} + +pANTLR3_BASE_TREE bash_ast::parser_arithmetics(plibbashParser parser) +{ + return parser->arithmetics(parser).tree; +} + + diff --git a/src/core/bash_ast.h b/src/core/bash_ast.h index 168d012..33590c8 100644 --- a/src/core/bash_ast.h +++ b/src/core/bash_ast.h @@ -19,12 +19,14 @@ /// /// \file bash_ast.h /// \author Mu Qiao -/// \brief a class that helps interpret from istream +/// \brief a class that helps interpret from istream and string /// =20 #ifndef LIBBASH_CORE_PARSER_BUILDER_H_ #define LIBBASH_CORE_PARSER_BUILDER_H_ =20 +#include +#include #include #include #include @@ -32,13 +34,14 @@ =20 #include =20 +#include "libbashWalker.h" + struct libbashLexer_Ctx_struct; struct libbashParser_Ctx_struct; -struct libbashParser_start_return_struct; class interpreter; =20 /// \class bash_ast -/// \brief a wrapper class that helps interpret from istream +/// \brief a wrapper class that helps interpret from istream and string class bash_ast { pANTLR3_INPUT_STREAM input; @@ -46,26 +49,63 @@ class bash_ast libbashLexer_Ctx_struct* lexer; pANTLR3_COMMON_TOKEN_STREAM token_stream; libbashParser_Ctx_struct* parser; - std::unique_ptr ast; + pANTLR3_BASE_TREE ast; pANTLR3_COMMON_TREE_NODE_STREAM nodes; int error_count; + std::function parse; + + void init_parser(const std::string& script); =20 - void init_parser(); public: - explicit bash_ast(std::istream& source); + bash_ast(const std::istream& source, + std::function p= =3Dparser_start); + + bash_ast(const std::string& script, + std::function p= =3Dparser_start): parse(p) + { + init_parser(script); + } + ~bash_ast(); =20 int get_error_count() const { return error_count; } + + static void walker_start(plibbashWalker tree_parser); + + static int walker_arithmetics(plibbashWalker tree_parser); + + static pANTLR3_BASE_TREE parser_start(libbashParser_Ctx_struct* parser= ); + + static pANTLR3_BASE_TREE parser_arithmetics(libbashParser_Ctx_struct* = parser); + /// /// \brief interpret the script with a given interpreter /// \param the interpreter object - void interpret_with(interpreter& walker); + /// \return the interpreted result + template + typename std::result_of::type + interpret_with(interpreter& walker, Functor walk) + { + set_interpreter(&walker); + std::unique_ptr> p_tree_parser( + libbashWalkerNew(nodes), + [](plibbashWalker tree_parser) { tree_parser->free(tree_parser);= }); + return walk(p_tree_parser.get()); + } + + void interpret_with(interpreter& walker) + { + interpret_with(walker, walker_start); + } + std::string get_dot_graph(); + std::string get_string_tree(); - std::string get_tokens(std::function token_= map); + + std::string get_tokens(std::function); }; =20 #endif diff --git a/src/core/tests/bash_ast_test.cpp b/src/core/tests/bash_ast_t= est.cpp index 9c4421c..dbf23e8 100644 --- a/src/core/tests/bash_ast_test.cpp +++ b/src/core/tests/bash_ast_test.cpp @@ -28,6 +28,7 @@ #include =20 #include "core/bash_ast.h" +#include "core/interpreter.h" #include "test.h" =20 TEST(bash_ast, parse_illegal_script) @@ -47,3 +48,11 @@ TEST(bash_ast, parse_legal_script) bash_ast ast2(input2); EXPECT_EQ(0, ast2.get_error_count()); } + +TEST(bash_ast, parse_arithmetics) +{ + std::string expr("1 + 2"); + bash_ast ast(expr, bash_ast::parser_arithmetics); + interpreter walker; + EXPECT_EQ(3, ast.interpret_with(walker, &bash_ast::walker_arithmetics)= ); +}