* [gentoo-commits] proj/libbash:master commit in: /, src/core/, test/
@ 2011-04-07 16:44 Petteri Räty
0 siblings, 0 replies; 2+ messages in thread
From: Petteri Räty @ 2011-04-07 16:44 UTC (permalink / raw
To: gentoo-commits
commit: 367ee056bd8b8a7df609568b51b5e76b2991c092
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Thu Apr 7 07:46:33 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Apr 7 13:48:09 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=367ee056
Implement a helper program to visualize AST
Use ast_printer to print the AST of a script in graphviz .dot
format or as a tree string. The script can be supplied by standard
input stream or file path.
---
.gitignore | 1 +
Makefile.am | 6 ++-
src/core/parser_builder.cpp | 13 +++++
src/core/parser_builder.h | 2 +
test/ast_printer.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++
5 files changed, 131 insertions(+), 1 deletions(-)
diff --git a/.gitignore b/.gitignore
index 18020e7..ed9707b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@ Makefile
autom4te.cache
cppunittests
variable_printer
+ast_printer
libbash.g
libbash.tokens
bashast.tokens
diff --git a/Makefile.am b/Makefile.am
index 96fb525..77661e7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -78,7 +78,7 @@ endif
if HAVE_GTEST
TESTS += cppunittests
-check_PROGRAMS = cppunittests variable_printer
+check_PROGRAMS = cppunittests variable_printer ast_printer
cppunittests_SOURCES = test/run_tests.cpp \
src/core/tests/symbols_test.cpp \
@@ -96,6 +96,10 @@ cppunittests_LDFLAGS = -static
variable_printer_SOURCES = test/variable_printer.cpp
variable_printer_LDADD = libcppbash.la
+
+ast_printer_SOURCES = test/ast_printer.cpp
+ast_printer_LDADD = libcppbash.la
+ast_printer_LDFLAGS = -static
endif
GENERATED_PARSER_C = libbashLexer.c libbashParser.c
diff --git a/src/core/parser_builder.cpp b/src/core/parser_builder.cpp
index f72d9b2..19c3bbf 100644
--- a/src/core/parser_builder.cpp
+++ b/src/core/parser_builder.cpp
@@ -76,3 +76,16 @@ walker_builder parser_builder::create_walker_builder()
{
return walker_builder(nodes);
}
+
+std::string parser_builder::get_dot_graph()
+{
+ pANTLR3_STRING graph = nodes->adaptor->makeDot(nodes->adaptor, langAST->tree);
+ return std::string(reinterpret_cast<char*>(graph->chars));
+}
+
+std::string parser_builder::get_string_tree()
+{
+ return std::string(reinterpret_cast<char*>(
+ langAST->tree->toStringTree(langAST->tree)->chars));
+}
+
diff --git a/src/core/parser_builder.h b/src/core/parser_builder.h
index 1a35b9f..96ccdbf 100644
--- a/src/core/parser_builder.h
+++ b/src/core/parser_builder.h
@@ -58,6 +58,8 @@ public:
/// \brief factory method that creates walker_builder
/// \return walker_builder object
walker_builder create_walker_builder();
+ std::string get_dot_graph();
+ std::string get_string_tree();
};
#endif
diff --git a/test/ast_printer.cpp b/test/ast_printer.cpp
new file mode 100644
index 0000000..6c8258e
--- /dev/null
+++ b/test/ast_printer.cpp
@@ -0,0 +1,110 @@
+/*
+ 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 ast_printer.cpp
+/// \brief helper program to visualize AST
+///
+
+#include <fstream>
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+
+#include "core/parser_builder.h"
+#include "libbashParser.h"
+
+static void print_usage()
+{
+ std::cout<<
+ "Usage:\n"<<
+ "-f, --file FILE using FILE as input script, if this option and -e are\n"
+ " not specified, will use standard input\n"<<
+ "-e, --expr EXPR using EXPR as input script\n"<<
+ "-t, --tree print tree\n"<<
+ "-d, --dot print graphviz doc file (default)"<< std::endl;
+}
+
+int main(int argc, char** argv)
+{
+ bool dot = true;
+ char* path = 0;
+ std::string expr;
+
+ for(int i = 1; i < argc; ++i)
+ {
+ if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file"))
+ {
+ if(i + 1 == argc)
+ {
+ print_usage();
+ exit(EXIT_FAILURE);
+ }
+ path = argv[++i];
+ }
+ else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--dot"))
+ {
+ dot = true;
+ }
+ else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--tree"))
+ {
+ dot = false;
+ }
+ else if(!strcmp(argv[i], "-e") || !strcmp(argv[i], "--expr"))
+ {
+ if(i + 1 == argc)
+ {
+ print_usage();
+ exit(EXIT_FAILURE);
+ }
+ expr = argv[++i];
+ }
+ else
+ {
+ print_usage();
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ std::unique_ptr<parser_builder> parser;
+ if(path)
+ {
+ std::ifstream input(path);
+ if(!input)
+ {
+ std::cerr << "Unable to create fstream for " << path << std::endl;
+ exit(EXIT_FAILURE);
+ }
+ parser.reset(new parser_builder(input));
+ }
+ else if(expr.size())
+ {
+ std::istringstream input(expr);
+ parser.reset(new parser_builder(input));
+ }
+ else
+ {
+ parser.reset(new parser_builder(std::cin));
+ }
+
+ if(dot)
+ std::cout << parser->get_dot_graph() << std::endl;
+ else
+ std::cout << parser->get_string_tree() << std::endl;
+}
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: /, src/core/, test/
@ 2011-05-24 14:40 Petteri Räty
0 siblings, 0 replies; 2+ messages in thread
From: Petteri Räty @ 2011-05-24 14:40 UTC (permalink / raw
To: gentoo-commits
commit: 7bd3431fd711c257cb5d1c756f21545e5cc3d5ad
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue May 24 10:16:27 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue May 24 14:36:23 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=7bd3431f
Core: improve error reporting
Now the script name, line number and char position will be printed
out when error occurs. This improvement doesn't cover all cases.
---
Makefile.am | 3 ++-
src/core/bash_ast.cpp | 10 +++++++---
src/core/bash_ast.h | 2 +-
test/verify_error_output_test.sh | 5 +++++
4 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 267240e..bd0bf7b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -90,7 +90,7 @@ AM_CPPFLAGS += -D_GLIBCXX_DEBUG
endif
if HAVE_GTEST
-TESTS += cppunittests test/ast_printer_test.sh test/verify_bashs_test.sh
+TESTS += cppunittests test/ast_printer_test.sh test/verify_bashs_test.sh test/verify_error_output_test.sh
check_PROGRAMS = cppunittests
cppunittests_SOURCES = test/run_tests.cpp \
@@ -219,6 +219,7 @@ EXTRA_DIST = bashast/bashast.g \
bashast/features_script/features.sh.tokens \
test/ast_printer_test.sh \
test/verify_bashs_test.sh \
+ test/verify_error_output_test.sh \
scripts/source_false.sh \
scripts/source_true.sh \
scripts/source_return.sh \
diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp
index 8fbc4f7..8c23850 100644
--- a/src/core/bash_ast.cpp
+++ b/src/core/bash_ast.cpp
@@ -35,7 +35,7 @@ bash_ast::bash_ast(const std::istream& source,
std::stringstream stream;
stream << source.rdbuf();
script = stream.str();
- init_parser(script);
+ init_parser(script, "unknown source");
}
bash_ast::bash_ast(const std::string& script_path,
@@ -48,7 +48,7 @@ bash_ast::bash_ast(const std::string& script_path,
stream << file_stream.rdbuf();
script = stream.str();
- init_parser(script);
+ init_parser(script, script_path);
}
bash_ast::~bash_ast()
@@ -60,7 +60,7 @@ bash_ast::~bash_ast()
input->close(input);
}
-void bash_ast::init_parser(const std::string& script)
+void bash_ast::init_parser(const std::string& script, const std::string& script_path)
{
input = antlr3NewAsciiStringInPlaceStream(
reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script.c_str())),
@@ -70,6 +70,10 @@ void bash_ast::init_parser(const std::string& script)
if(input == NULL)
throw interpreter_exception("Unable to open file " + script + " due to malloc() failure");
+ input->fileName = input->strFactory->newStr(
+ input->strFactory,
+ reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script_path.c_str())));
+
lexer = libbashLexerNew(input);
if ( lexer == NULL )
{
diff --git a/src/core/bash_ast.h b/src/core/bash_ast.h
index d7c160f..9e388cb 100644
--- a/src/core/bash_ast.h
+++ b/src/core/bash_ast.h
@@ -54,7 +54,7 @@ class bash_ast
int error_count;
std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> parse;
- void init_parser(const std::string& script);
+ void init_parser(const std::string& script, const std::string& script_path);
public:
bash_ast(const std::istream& source,
diff --git a/test/verify_error_output_test.sh b/test/verify_error_output_test.sh
new file mode 100755
index 0000000..b3c2eed
--- /dev/null
+++ b/test/verify_error_output_test.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+illegal="${srcdir}/scripts/illegal_script.sh"
+output=$(./variable_printer "$illegal" 2>&1)
+[[ $output == "${illegal}(1) : error 5 : Unexpected token, at offset 3"* ]]
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-05-24 14:41 UTC | newest]
Thread overview: 2+ messages (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/core/, test/ Petteri Räty
-- strict thread matches above, loose matches on Subject: below --
2011-04-07 16:44 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