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

commit:     986e52e9f046d786664ce7d874fb48f08768a458
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Thu Apr  7 07:15:53 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Apr  7 13:28:21 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=986e52e9

Extract parser/walker building logic into classes

There are multiple places creating parser/walker to interpret
scripts. Now the logic is extracted to parser_builder and
walker_builder.

---
 Makefile.am                 |    4 ++
 src/core/parser_builder.cpp |   78 +++++++++++++++++++++++++++++++++++++++++++
 src/core/parser_builder.h   |   63 ++++++++++++++++++++++++++++++++++
 src/core/walker_builder.cpp |   36 ++++++++++++++++++++
 src/core/walker_builder.h   |   46 +++++++++++++++++++++++++
 src/libbash.cpp             |   50 +++++----------------------
 6 files changed, 236 insertions(+), 41 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 2da69a2..96fb525 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -135,6 +135,10 @@ libcppbash_la_SOURCES = src/common.h \
 						src/core/interpreter.cpp \
 						src/core/interpreter.h \
 						src/core/symbols.hpp \
+						src/core/parser_builder.cpp \
+						src/core/parser_builder.h \
+						src/core/walker_builder.cpp \
+						src/core/walker_builder.h \
 						$(GENERATED_WALKER_C) \
 						$(GENERATED_WALKER_H)
 

diff --git a/src/core/parser_builder.cpp b/src/core/parser_builder.cpp
new file mode 100644
index 0000000..f72d9b2
--- /dev/null
+++ b/src/core/parser_builder.cpp
@@ -0,0 +1,78 @@
+/*
+   Please use git log for copyright holder and year information
+
+   This file is part of libbash.
+
+   libbash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 2 of the License, or
+   (at your option) any later version.
+
+   libbash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file parser_builder.cpp
+/// \author Mu Qiao
+/// \brief a class that helps build libbashParser from istream
+///
+
+#include "core/parser_builder.h"
+
+#include <sstream>
+
+#include "core/interpreter_exception.h"
+#include "libbashLexer.h"
+#include "libbashParser.h"
+#include "walker_builder.h"
+
+parser_builder::parser_builder(std::istream& source)
+{
+  std::stringstream stream;
+  stream << source.rdbuf();
+  script = stream.str();
+
+  input = antlr3NewAsciiStringInPlaceStream(
+      reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script.c_str())),
+      script.size(),
+      NULL);
+  init_parser();
+}
+
+parser_builder::~parser_builder()
+{
+  nodes->free(nodes);
+  psr->free(psr);
+  tstream->free(tstream);
+  lxr->free(lxr);
+  input->close(input);
+}
+
+void parser_builder::init_parser()
+{
+  lxr = libbashLexerNew(input);
+  if ( lxr == NULL )
+    throw interpreter_exception("Unable to create the lexer due to malloc() failure");
+
+  tstream = antlr3CommonTokenStreamSourceNew(
+      ANTLR3_SIZE_HINT, lxr->pLexer->rec->state->tokSource);
+  if (tstream == NULL)
+    throw interpreter_exception("Out of memory trying to allocate token stream");
+
+  psr = libbashParserNew(tstream);
+  if (psr == NULL)
+    throw interpreter_exception("Out of memory trying to allocate parser");
+
+  langAST.reset(new libbashParser_start_return(psr->start(psr)));
+  nodes = antlr3CommonTreeNodeStreamNewTree(langAST->tree, ANTLR3_SIZE_HINT);
+}
+
+walker_builder parser_builder::create_walker_builder()
+{
+  return walker_builder(nodes);
+}

diff --git a/src/core/parser_builder.h b/src/core/parser_builder.h
new file mode 100644
index 0000000..1a35b9f
--- /dev/null
+++ b/src/core/parser_builder.h
@@ -0,0 +1,63 @@
+/*
+   Please use git log for copyright holder and year information
+
+   This file is part of libbash.
+
+   libbash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 2 of the License, or
+   (at your option) any later version.
+
+   libbash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file parser_builder.h
+/// \author Mu Qiao
+/// \brief a class that helps build libbashParser from istream
+///
+
+#ifndef PARSER_BUILDER_H
+#define PARSER_BUILDER_H
+
+#include <istream>
+#include <memory>
+#include <string>
+
+#include <antlr3.h>
+
+struct libbashLexer_Ctx_struct;
+struct libbashParser_Ctx_struct;
+struct libbashParser_start_return_struct;
+class walker_builder;
+
+/// \class parser_builder
+/// \brief a wrapper class that creates libbashParser. It also know
+///        enough information to create walker_builder object.
+class parser_builder
+{
+  pANTLR3_INPUT_STREAM input;
+  std::string script;
+  libbashLexer_Ctx_struct* lxr;
+  pANTLR3_COMMON_TOKEN_STREAM tstream;
+  libbashParser_Ctx_struct* psr;
+  std::shared_ptr<libbashParser_start_return_struct> langAST;
+  pANTLR3_COMMON_TREE_NODE_STREAM nodes;
+
+  void init_parser();
+public:
+  parser_builder(std::istream& source);
+  ~parser_builder();
+
+  ///
+  /// \brief factory method that creates walker_builder
+  /// \return walker_builder object
+  walker_builder create_walker_builder();
+};
+
+#endif

diff --git a/src/core/walker_builder.cpp b/src/core/walker_builder.cpp
new file mode 100644
index 0000000..dbe47d9
--- /dev/null
+++ b/src/core/walker_builder.cpp
@@ -0,0 +1,36 @@
+/*
+   Please use git log for copyright holder and year information
+
+   This file is part of libbash.
+
+   libbash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 2 of the License, or
+   (at your option) any later version.
+
+   libbash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file walker_builder.cpp
+/// \author Mu Qiao
+/// \brief a class that helps build a libbashWalker from istream
+///
+
+#include "core/walker_builder.h"
+
+#include "core/interpreter.h"
+#include "libbashWalker.h"
+
+walker_builder::walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes): walker(new interpreter)
+{
+  set_interpreter(walker);
+  plibbashWalker treePsr = libbashWalkerNew(nodes);
+  treePsr->start(treePsr);
+  treePsr->free(treePsr);
+}

diff --git a/src/core/walker_builder.h b/src/core/walker_builder.h
new file mode 100644
index 0000000..fc2fbfe
--- /dev/null
+++ b/src/core/walker_builder.h
@@ -0,0 +1,46 @@
+/*
+   Please use git log for copyright holder and year information
+
+   This file is part of libbash.
+
+   libbash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 2 of the License, or
+   (at your option) any later version.
+
+   libbash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file walker_builder.h
+/// \author Mu Qiao
+/// \brief a class that helps build a libbashWalker
+///
+
+#ifndef WALKER_BUILDER_H
+#define WALKER_BUILDER_H
+
+#include <memory>
+
+#include <antlr3.h>
+
+class interpreter;
+
+/// \class walker_builder
+/// \brief a wrapper class that creates libbashWalker
+class walker_builder
+{
+public:
+  /// \var public::walker
+  /// \brief the interpreter object that contains all runtime information
+  std::shared_ptr<interpreter> walker;
+
+  walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes);
+};
+
+#endif

diff --git a/src/libbash.cpp b/src/libbash.cpp
index 19ca4b1..4060fbf 100644
--- a/src/libbash.cpp
+++ b/src/libbash.cpp
@@ -24,55 +24,23 @@
 
 #include "libbash.h"
 
-#include <antlr3defs.h>
+#include <fstream>
 
-#include "core/interpreter_exception.h"
-#include "libbashLexer.h"
-#include "libbashParser.h"
-#include "libbashWalker.h"
+#include "core/parser_builder.h"
+#include "core/walker_builder.h"
 
 namespace libbash
 {
-
   void interpret(const std::string& path,
                  std::unordered_map<std::string, std::string>& variables)
   {
-    pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew(
-        reinterpret_cast<pANTLR3_UINT8>(const_cast<char *>(path.c_str())));
-    if ( input == NULL )
-      throw interpreter_exception("Unable to create input stream for script: " + path);
-
-    plibbashLexer lxr = libbashLexerNew(input);
-    if ( lxr == NULL )
-      throw interpreter_exception("Unable to create the lexer due to malloc() failure");
-
-    pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(
-        ANTLR3_SIZE_HINT, lxr->pLexer->rec->state->tokSource);
-    if (tstream == NULL)
-      throw interpreter_exception("Out of memory trying to allocate token stream");
-
-    plibbashParser psr = libbashParserNew(tstream);
-    if (psr == NULL)
-      throw interpreter_exception("Out of memory trying to allocate parser");
-
-    std::shared_ptr<interpreter> walker(new interpreter);
-    set_interpreter(walker);
-
-    libbashParser_start_return langAST = psr->start(psr);
-    pANTLR3_COMMON_TREE_NODE_STREAM nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, ANTLR3_SIZE_HINT);
-    plibbashWalker treePsr = libbashWalkerNew(nodes);
-    treePsr->start(treePsr);
-
-    treePsr->free(treePsr);
-    nodes->free(nodes);
-    psr->free(psr);
-    tstream->free(tstream);
-    lxr->free(lxr);
-    input->close(input);
+    std::ifstream input(path.c_str());
+    if(!input)
+      throw interpreter_exception("Unable to create fstream for script: " + path);
+    parser_builder pbuilder(input);
+    walker_builder wbuilder = pbuilder.create_walker_builder();
 
-    for(auto iter = walker->begin(); iter != walker->end(); ++iter)
-    {
+    for(auto iter = wbuilder.walker->begin(); iter != wbuilder.walker->end(); ++iter)
       variables[iter->first]=iter->second->get_value<std::string>();
-    }
   }
 }



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

only message in thread, other threads:[~2011-04-07 16:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-07 16:44 [gentoo-commits] proj/libbash:master commit in: src/, /, src/core/ 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