Skip to content

Commit

Permalink
#967: Added CTPG script options parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tomuben committed Oct 1, 2024
1 parent 2a7c8b9 commit 1e817cd
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check_bazel_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ jobs:
- name: Script Options Parser Tests
run: |
export USE_BAZEL_VERSION=7.2.1
bazel test //base/script_options_parser/test/...
bazel test //base/script_options_parser/...
working-directory: ./exaudfclient/

2 changes: 1 addition & 1 deletion exaudfclient/base/javacontainer/script_options/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ cc_library(
name = "java_scriptoptionlines",
hdrs = [":scriptoptionlinesconverter.h"],
srcs = [":scriptoptionlinesparser.h", ":scriptoptionlinesconverter.h", ":scriptoptionlinesconverter.cc", ":scriptoptionlinesparserlegacy.h", ":scriptoptionlinesparserlegacy.cc"],
deps = ["//base/script_options_parser:scriptoptionlinesparser", "//base/exaudflib:header", "//base/exaudflib:exaudflib-deps"],
deps = ["//base/script_options_parser/legacy:scriptoptionlinesparser_legacy", "//base/exaudflib:header", "//base/exaudflib:exaudflib-deps"],
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "base/javacontainer/script_options/scriptoptionlinesparserlegacy.h"
#include "base/script_options_parser/scriptoptionlines.h"
#include "base/script_options_parser/legacy/scriptoptionlines.h"


namespace SWIGVMContainers {
Expand Down
7 changes: 0 additions & 7 deletions exaudfclient/base/script_options_parser/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "scriptoptionlinesparser",
hdrs = ["scriptoptionlines.h"],
srcs = ["scriptoptionlines.cc","scriptoptionlines.h"],
copts= ["-fno-lto"],
)
8 changes: 8 additions & 0 deletions exaudfclient/base/script_options_parser/ctpg/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "scriptoptionlinesparser_ctpg",
hdrs = ["scriptoptionlines_ctpg.h"],
srcs = ["scriptoptionlines_ctpg.cc","ctpg.hpp"],
copts= ["-fno-lto"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ using namespace exaudf_ctpg;
using namespace exaudf_ctpg::ftors;


namespace OptionsLineParser
namespace ExecutionGraph
{

namespace OptionsLineParser
{

namespace Parser {

namespace CTPG {


struct Option {
Expand All @@ -25,6 +27,8 @@ struct Option {

using options_type = std::vector<Option>;

namespace ParserInternals {

auto empty_options()
{
options_type ob;
Expand All @@ -39,7 +43,6 @@ auto to_options(Option&& e)

auto to_option(std::string_view key, std::string value, source_point sp_begin, source_point sp_end)
{
std::cerr << "Found option:" << key << " - '" << value << "'" << std::endl;
return Option{std::string(key), value, sp_begin, sp_end};
}

Expand All @@ -53,7 +56,7 @@ auto&& add_option(Option&& e, options_type&& ob)

constexpr char alpha_numeric_pattern[] = R"_([0-9a-zA-Z_]+)_";
constexpr char option_char_pattern[] = R"_([^;])_";
constexpr char whitespaces_pattern[] = R"_([ \x09]+)_";
constexpr char whitespaces_pattern[] = R"_([ \x09 \x0c \x0b]+)_";


constexpr char_term start_option_tag('%');
Expand Down Expand Up @@ -133,42 +136,60 @@ constexpr parser option_parser(
);

void parse(const std::string& code, options_type& result, std::function<void(const char*)> throwException) {

std::string::const_iterator it = code.begin();

//
// for ( std::string & line : lines) {
// std::cerr << "Parsing line: '" << line << "'" << std::endl;
//
// std::string t(line);
// std::cerr << "orig: " << reinterpret_cast<const void *>(line.c_str()) << std::endl;
// std::cerr << "cp: " << reinterpret_cast<const void *>(t.c_str()) << std::endl;
//
// auto res = option_parser.parse(
// parse_options{}/*.set_verbose(true)*/.set_skip_whitespace(false),
// buffers::string_buffer(std::move(t)),
// std::cout);
//
// std::cerr << "Parsing line: '" << line << "'" << std::endl;
// if (res.has_value())
// {
// // std::cout << res.value() << std::endl;
//
// for (const auto& w : res.value())
// {
// std::cout << w.key << " (" << w.value << ") pos: "<< w.start << "-" << w.end << std::endl;
// }
// }
// }
std::stringstream error_buffer;
auto res = option_parser.parse(
parse_options{}.set_skip_whitespace(false),
buffers::string_buffer(code.c_str()),
error_buffer);
if (res.has_value())
{
result = res.value();
}
else
{
std::stringstream ss;
ss << "Error parsing script options: " << error_buffer.str();
throwException(ss.str().c_str());
}
}

} //namespace Parser

void parseOptions(const std::string& code, OptionsLineParser::ParserResult & result, std::function<void(const char*)> throwException) {
} //namespace ParserInternals

void parseOptions(const std::string& code, options_map_t & result, std::function<void(const char*)> throwException) {
std::stringstream ss(code);
std::string line;
size_t current_index(0);
while(std::getline(ss, line, '\n')) {
if (!line.empty() && !std::all_of(line.begin(),line.end(), [](const char c) {return std::isspace(c);})) {
options_type parser_result;
ParserInternals::parse(line, parser_result, throwException);
for (const auto & option: parser_result)
{
ScriptOption entry = {
.value = option.value,
.idx_in_source = current_index + option.start.column - 1,
.size = option.end.column - option.start.column + 1
};
auto it_in_result = result.find(option.key);
if (it_in_result == result.end())
{
options_t new_options;
new_options.push_back(entry);
result.insert(std::make_pair(option.key, new_options));
}
else
{
it_in_result->second.push_back(entry);
}
}
}
current_index += line.size() + 1;
}
}




} // namespace CTPG

} // namespace OptionsLineParser

} // namespace ExecutionGraph
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef SCRIPTOPTIONLINESCTPG_H
#define SCRIPTOPTIONLINESCTPG_H

#include <string>
#include <functional>
#include <vector>
#include <map>
#include <ostream>

namespace ExecutionGraph
{

namespace OptionsLineParser
{

namespace CTPG
{

class ParserResult;

struct ScriptOption {
std::string value;
size_t idx_in_source;
size_t size;

bool operator==(const ScriptOption & right) const {
return value == right.value && idx_in_source == right.idx_in_source && size == right.size;
}
friend void PrintTo(const ScriptOption& option, std::ostream* os) {
*os << "(" << option.value << "," << option.idx_in_source << "," << option.size << ")";
}
};

using options_t = std::vector<ScriptOption>;

using options_map_t = std::map<std::string, options_t>;

/*!
* \brief extractOptionLine Extracts syntactically valid option lines of form %<option> <values> [<values>] from UDF scripts.
*
* \param code Reference to string where the script code is stored.
* \param result Result of all found options.
* \param throwException Function to be called to throw exception.
*
*/
void parseOptions(const std::string& code, options_map_t & result, std::function<void(const char*)> throwException);

} //namespace CTPG

} // namespace OptionsLineParser

} // namespace ExecutionGraph

#endif // SCRIPTOPTIONLINESCTPG_H
8 changes: 8 additions & 0 deletions exaudfclient/base/script_options_parser/ctpg/test/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cc_test(
name = "script-options-ctpg-parser-test",
srcs = ["script_option_lines_test.cpp"],
deps = [
"//base/script_options_parser/ctpg:scriptoptionlinesparser_ctpg",
"@googletest//:gtest_main",
],
)
Loading

0 comments on commit 1e817cd

Please sign in to comment.