Commit 0e80f8b2 authored by Alastair Robertson's avatar Alastair Robertson

Make parser reentrant

parent 6d6dec86
...@@ -28,6 +28,14 @@ ExternalProject_Add(bcc ...@@ -28,6 +28,14 @@ ExternalProject_Add(bcc
BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static BUILD_COMMAND ${CMAKE_COMMAND} --build . --target bcc-static
) )
find_package(BISON REQUIRED)
find_package(FLEX REQUIRED)
bison_target(bison_parser src/parser.yy ${CMAKE_BINARY_DIR}/parser.tab.cc)
flex_target(flex_lexer src/lexer.l ${CMAKE_BINARY_DIR}/lex.yy.cc)
add_flex_bison_dependency(flex_lexer bison_parser)
add_library(parser ${BISON_bison_parser_OUTPUTS} ${FLEX_flex_lexer_OUTPUTS})
target_include_directories(parser PUBLIC src ${CMAKE_BINARY_DIR})
add_subdirectory(src/arch) add_subdirectory(src/arch)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(tests) add_subdirectory(tests)
find_package(BISON REQUIRED)
find_package(FLEX REQUIRED)
find_package(LLVM REQUIRED CONFIG) find_package(LLVM REQUIRED CONFIG)
bison_target(parser parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.tab.cc)
flex_target(lexer lexer.l ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.cc)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS})
...@@ -18,17 +11,15 @@ add_executable(bpftrace ...@@ -18,17 +11,15 @@ add_executable(bpftrace
codegen_llvm.cpp codegen_llvm.cpp
driver.cpp driver.cpp
irbuilderbpf.cpp irbuilderbpf.cpp
lex.yy.cc
main.cpp main.cpp
map.cpp map.cpp
mapkey.cpp mapkey.cpp
parser.tab.cc
printer.cpp printer.cpp
semantic_analyser.cpp semantic_analyser.cpp
types.cpp types.cpp
) )
target_link_libraries(bpftrace arch) target_link_libraries(bpftrace arch parser)
llvm_map_components_to_libnames(llvm_libs bpfcodegen ipo irreader mcjit) llvm_map_components_to_libnames(llvm_libs bpfcodegen ipo irreader mcjit)
target_link_libraries(bpftrace ${llvm_libs}) target_link_libraries(bpftrace ${llvm_libs})
......
...@@ -3,31 +3,46 @@ ...@@ -3,31 +3,46 @@
#include "driver.h" #include "driver.h"
extern FILE *yyin; extern FILE *yyin;
extern void *yy_scan_string(const char *yystr); extern void *yy_scan_string(const char *yy_str, yyscan_t yyscanner);
extern void yyset_in(FILE *_in_str, yyscan_t yyscanner);
extern int yylex_init(yyscan_t *scanner);
extern int yylex_destroy (yyscan_t yyscanner);
namespace bpftrace { namespace bpftrace {
Driver::Driver()
{
yylex_init(&scanner_);
parser_ = std::make_unique<Parser>(*this, scanner_);
}
Driver::~Driver()
{
yylex_destroy(scanner_);
}
int Driver::parse_stdin() int Driver::parse_stdin()
{ {
return parser_.parse(); return parser_->parse();
} }
int Driver::parse_str(const std::string &script) int Driver::parse_str(const std::string &script)
{ {
void *buffer = yy_scan_string(script.c_str()); yy_scan_string(script.c_str(), scanner_);
int result = parser_.parse(); int result = parser_->parse();
free(buffer);
return result; return result;
} }
int Driver::parse_file(const std::string &f) int Driver::parse_file(const std::string &f)
{ {
if (!(yyin = fopen(f.c_str(), "r"))) { FILE *file;
if (!(file = fopen(f.c_str(), "r"))) {
std::cerr << "Error: Could not open file '" << f << "'" << std::endl; std::cerr << "Error: Could not open file '" << f << "'" << std::endl;
return -1; return -1;
} }
int result = parser_.parse(); yyset_in(file, scanner_);
fclose(yyin); int result = parser_->parse();
fclose(file);
return result; return result;
} }
......
#pragma once #pragma once
#include <memory>
#include "ast.h" #include "ast.h"
#include "parser.tab.hh" #include "parser.tab.hh"
#define YY_DECL bpftrace::Parser::symbol_type yylex(bpftrace::Driver &driver) typedef void* yyscan_t;
#define YY_DECL bpftrace::Parser::symbol_type yylex(bpftrace::Driver &driver, yyscan_t yyscanner)
YY_DECL; YY_DECL;
namespace bpftrace { namespace bpftrace {
class Driver { class Driver
{
public: public:
Driver() : parser_(*this) { } Driver();
~Driver();
int parse_stdin(); int parse_stdin();
int parse_str(const std::string &script); int parse_str(const std::string &script);
...@@ -29,7 +34,8 @@ public: ...@@ -29,7 +34,8 @@ public:
ast::Program *root_; ast::Program *root_;
private: private:
Parser parser_; std::unique_ptr<Parser> parser_;
yyscan_t scanner_;
}; };
} // namespace bpftrace } // namespace bpftrace
%option yylineno nodefault noyywrap %option yylineno nodefault noyywrap noinput nounput
%option never-interactive %option never-interactive
%option reentrant
%{ %{
#include "driver.h" #include "driver.h"
#include "parser.tab.hh" #include "parser.tab.hh"
#undef yywrap #undef yywrap
#define yywrap() 1 #define yywrap(x) 1
static bpftrace::location loc; static bpftrace::location loc;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
%define parse.error verbose %define parse.error verbose
%param { bpftrace::Driver &driver } %param { bpftrace::Driver &driver }
%param { void *yyscanner }
%locations %locations
// Forward declarations of classes referenced in the parser // Forward declarations of classes referenced in the parser
......
...@@ -7,18 +7,19 @@ include_directories(${CMAKE_BINARY_DIR}/src) ...@@ -7,18 +7,19 @@ include_directories(${CMAKE_BINARY_DIR}/src)
add_executable(bpftrace_test add_executable(bpftrace_test
main.cpp main.cpp
# parser.cpp parser.cpp
semantic_analyser.cpp semantic_analyser.cpp
${CMAKE_SOURCE_DIR}/src/ast.cpp ${CMAKE_SOURCE_DIR}/src/ast.cpp
${CMAKE_SOURCE_DIR}/src/attached_probe.cpp ${CMAKE_SOURCE_DIR}/src/attached_probe.cpp
${CMAKE_SOURCE_DIR}/src/bpftrace.cpp ${CMAKE_SOURCE_DIR}/src/bpftrace.cpp
${CMAKE_SOURCE_DIR}/src/driver.cpp
${CMAKE_SOURCE_DIR}/src/map.cpp ${CMAKE_SOURCE_DIR}/src/map.cpp
${CMAKE_SOURCE_DIR}/src/mapkey.cpp ${CMAKE_SOURCE_DIR}/src/mapkey.cpp
${CMAKE_SOURCE_DIR}/src/semantic_analyser.cpp ${CMAKE_SOURCE_DIR}/src/semantic_analyser.cpp
${CMAKE_SOURCE_DIR}/src/types.cpp ${CMAKE_SOURCE_DIR}/src/types.cpp
) )
target_link_libraries(bpftrace_test arch) target_link_libraries(bpftrace_test arch parser)
add_dependencies(bpftrace_test bcc-build) add_dependencies(bpftrace_test bcc-build)
ExternalProject_Get_Property(bcc source_dir binary_dir) ExternalProject_Get_Property(bcc source_dir binary_dir)
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "driver.h"
TEST(test1, aaa) namespace bpftrace {
TEST(Parser, test0)
{ {
Driver driver;
std::string s = "kprobe:sys_open { @x = 1; }";
EXPECT_EQ(driver.parse_str(s), 0);
} }
TEST(Parser, test1)
{
Driver driver;
std::string s = "kprobe:sys_open { @x = 1; }";
EXPECT_EQ(driver.parse_str(s), 0);
}
} // namespace bpftrace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment