public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/libbash:master commit in: src/, src/core/, src/core/tests/, src/builtins/
@ 2011-05-24 14:40 Petteri Räty
  0 siblings, 0 replies; only message in thread
From: Petteri Räty @ 2011-05-24 14:40 UTC (permalink / raw
  To: gentoo-commits

commit:     f1b94eec5bb067fa5fc4e6778c18b407b57e3c47
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue May 24 09:53:40 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue May 24 14:24:14 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=f1b94eec

Core: refactor bash_ast constructor

We use the string as script path now. That means you don't have to
create ifstream yourself. Use std::stringstream if you need to
provide an in-memory script.

---
 src/builtins/let_builtin.cpp     |    4 +++-
 src/builtins/source_builtin.cpp  |    6 +-----
 src/core/bash_ast.cpp            |   14 ++++++++++++++
 src/core/bash_ast.h              |    7 ++-----
 src/core/tests/bash_ast_test.cpp |   12 +++++-------
 src/libbash.cpp                  |   22 ++++------------------
 6 files changed, 29 insertions(+), 36 deletions(-)

diff --git a/src/builtins/let_builtin.cpp b/src/builtins/let_builtin.cpp
index dc4be13..43f28a3 100644
--- a/src/builtins/let_builtin.cpp
+++ b/src/builtins/let_builtin.cpp
@@ -21,6 +21,8 @@
 /// \author Mu Qiao
 /// \brief implementation for the let builtin
 ///
+#include <sstream>
+
 #include <boost/algorithm/string/join.hpp>
 
 #include "core/bash_ast.h"
@@ -30,7 +32,7 @@
 int let_builtin::exec(const std::vector<std::string>& bash_args)
 {
   std::string expression(boost::algorithm::join(bash_args, " "));
-  bash_ast ast(expression, &bash_ast::parser_arithmetics);
+  bash_ast ast(std::stringstream(expression), &bash_ast::parser_arithmetics);
   ast.interpret_with(_walker, &bash_ast::walker_arithmetics);
 
   return ast.get_error_count();

diff --git a/src/builtins/source_builtin.cpp b/src/builtins/source_builtin.cpp
index 4e781ab..87f8a5d 100644
--- a/src/builtins/source_builtin.cpp
+++ b/src/builtins/source_builtin.cpp
@@ -48,11 +48,7 @@ int source_builtin::exec(const std::vector<std::string>& bash_args)
   auto& stored_ast = ast_cache[path];
   if(!stored_ast)
   {
-    std::ifstream input(path);
-    if(!input)
-      throw interpreter_exception(path + " can't be read");
-
-    stored_ast.reset(new bash_ast(input));
+    stored_ast.reset(new bash_ast(path));
     if(stored_ast->get_error_count())
       std::cerr << path << " could not be parsed properly" << std::endl;
   }

diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp
index af0205b..8fbc4f7 100644
--- a/src/core/bash_ast.cpp
+++ b/src/core/bash_ast.cpp
@@ -21,6 +21,7 @@
 /// \author Mu Qiao
 /// \brief a wrapper class that helps interpret from istream and string
 ///
+#include <fstream>
 
 #include "core/interpreter_exception.h"
 #include "libbashLexer.h"
@@ -37,6 +38,19 @@ bash_ast::bash_ast(const std::istream& source,
   init_parser(script);
 }
 
+bash_ast::bash_ast(const std::string& script_path,
+                   std::function<pANTLR3_BASE_TREE(plibbashParser)> p): parse(p)
+{
+  std::stringstream stream;
+  std::ifstream file_stream(script_path);
+  if(!file_stream)
+    throw interpreter_exception(script_path + " can't be read");
+
+  stream << file_stream.rdbuf();
+  script = stream.str();
+  init_parser(script);
+}
+
 bash_ast::~bash_ast()
 {
   nodes->free(nodes);

diff --git a/src/core/bash_ast.h b/src/core/bash_ast.h
index 33590c8..d7c160f 100644
--- a/src/core/bash_ast.h
+++ b/src/core/bash_ast.h
@@ -60,11 +60,8 @@ public:
   bash_ast(const std::istream& source,
            std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start);
 
-  bash_ast(const std::string& script,
-           std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start): parse(p)
-  {
-    init_parser(script);
-  }
+  bash_ast(const std::string& script_path,
+           std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start);
 
   ~bash_ast();
 

diff --git a/src/core/tests/bash_ast_test.cpp b/src/core/tests/bash_ast_test.cpp
index dbf23e8..d5061ca 100644
--- a/src/core/tests/bash_ast_test.cpp
+++ b/src/core/tests/bash_ast_test.cpp
@@ -23,6 +23,7 @@
 ///
 
 #include <fstream>
+#include <sstream>
 #include <string>
 
 #include <gtest/gtest.h>
@@ -33,26 +34,23 @@
 
 TEST(bash_ast, parse_illegal_script)
 {
-  std::ifstream input(get_src_dir() + std::string("/scripts/illegal_script.sh"));
-  bash_ast ast(input);
+  bash_ast ast(get_src_dir() + std::string("/scripts/illegal_script.sh"));
   EXPECT_NE(0, ast.get_error_count());
 }
 
 TEST(bash_ast, parse_legal_script)
 {
-  std::ifstream input(get_src_dir() + std::string("/scripts/source_true.sh"));
-  bash_ast ast(input);
+  bash_ast ast(get_src_dir() + std::string("/scripts/source_true.sh"));
   EXPECT_EQ(0, ast.get_error_count());
 
-  std::ifstream input2(get_src_dir() + std::string("/scripts/source_false.sh"));
-  bash_ast ast2(input2);
+  bash_ast ast2(get_src_dir() + std::string("/scripts/source_false.sh"));
   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);
+  bash_ast ast(std::stringstream(expr), bash_ast::parser_arithmetics);
   interpreter walker;
   EXPECT_EQ(3, ast.interpret_with(walker, &bash_ast::walker_arithmetics));
 }

diff --git a/src/libbash.cpp b/src/libbash.cpp
index d0c3661..bc94433 100644
--- a/src/libbash.cpp
+++ b/src/libbash.cpp
@@ -32,7 +32,6 @@
 namespace internal
 {
   int interpret(interpreter& walker,
-                const std::ifstream& input,
                 const std::string& path,
                 std::unordered_map<std::string, std::vector<std::string>>& variables,
                 std::vector<std::string>& functions)
@@ -45,7 +44,7 @@ namespace internal
     walker.define("0", path, true);
     variables.clear();
 
-    bash_ast ast(input);
+    bash_ast ast(path);
     ast.interpret_with(walker);
     result += ast.get_error_count();
 
@@ -64,13 +63,8 @@ namespace libbash
                 std::unordered_map<std::string, std::vector<std::string>>& variables,
                 std::vector<std::string>& functions)
   {
-    std::ifstream input(target_path.c_str());
-    if(!input)
-      throw interpreter_exception("Unable to create fstream for script: " + target_path);
-
     interpreter walker;
-
-    return internal::interpret(walker, input, target_path, variables, functions);
+    return internal::interpret(walker, target_path, variables, functions);
   }
 
   int interpret(const std::string& target_path,
@@ -78,18 +72,10 @@ namespace libbash
                 std::unordered_map<std::string, std::vector<std::string>>& variables,
                 std::vector<std::string>& functions)
   {
-    std::ifstream input(target_path.c_str());
-    if(!input)
-      throw interpreter_exception("Unable to create fstream for script: " + target_path);
-
-    std::ifstream preload(preload_path.c_str());
-    if(!preload)
-      throw interpreter_exception("Unable to create fstream for script: " + preload_path);
-
     interpreter walker;
 
     // Preloading
-    bash_ast preload_ast(preload);
+    bash_ast preload_ast(preload_path);
     preload_ast.interpret_with(walker);
     int result = preload_ast.get_error_count();
     if(result)
@@ -98,6 +84,6 @@ namespace libbash
       return result;
     }
 
-    return internal::interpret(walker, input, target_path, variables, functions);
+    return internal::interpret(walker, target_path, variables, functions);
   }
 }



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-05-24 14:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-24 14:40 [gentoo-commits] proj/libbash:master commit in: src/, src/core/, src/core/tests/, src/builtins/ Petteri Räty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox