| ... | ... | @@ -6,17 +6,17 @@ |
|
|
|
#include <tao/pegtl.hpp>
|
|
|
|
|
|
|
|
// includes
|
|
|
|
#include "double.hpp"
|
|
|
|
#include "inputs.hpp"
|
|
|
|
|
|
|
|
using namespace TAO_PEGTL_NAMESPACE;
|
|
|
|
using namespace tao::pegtl;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct parser_state {
|
|
|
|
std::string current_key;
|
|
|
|
std::vector<double> current_vec;
|
|
|
|
parser::InputValue current_value;
|
|
|
|
std::variant<std::vector<double>, std::vector<std::string>> current_value;
|
|
|
|
std::vector<double> current_num_vec;
|
|
|
|
std::vector<std::string> current_str_vec;
|
|
|
|
parser::InputInfo info;
|
|
|
|
};
|
|
|
|
|
| ... | ... | @@ -32,31 +32,39 @@ struct escaped_c |
|
|
|
|
|
|
|
struct escaped : sor<escaped_x, escaped_u, escaped_U, escaped_c> {};
|
|
|
|
|
|
|
|
struct key : plus<sor<plus<alnum>, one<'_'>, one<'-'>, one<'.'>>> {};
|
|
|
|
|
|
|
|
struct character
|
|
|
|
: if_must_else<one<'\\'>, escaped, utf8::range<0x20, 0x10FFFF>> {};
|
|
|
|
struct string_literal : if_must<one<'"'>, until<one<'"'>, character>> {};
|
|
|
|
struct implicit_string : if_must<alnum, until<eolf, character>> {};
|
|
|
|
|
|
|
|
struct whitespace_delimited_array : list<double_::grammar, plus<one<' '>>> {};
|
|
|
|
struct quoted_string : if_must<one<'"'>, until<one<'"'>, character>> {};
|
|
|
|
struct unquoted_string : key {};
|
|
|
|
struct string_literal : sor<quoted_string, unquoted_string> {};
|
|
|
|
|
|
|
|
struct L_FUN : pad<string<'('>, space> {};
|
|
|
|
struct R_FUN : pad<string<')'>, space> {};
|
|
|
|
struct L_ARR : pad<string<'['>, space> {};
|
|
|
|
struct R_ARR : pad<string<']'>, space> {};
|
|
|
|
struct L_BLK : pad<string<'{'>, space> {};
|
|
|
|
struct R_BLK : pad<string<'}'>, space> {};
|
|
|
|
struct S_CLN : pad<string<';'>, space> {};
|
|
|
|
struct plus_minus : opt<one<'+', '-'>> {};
|
|
|
|
struct dot : one<'.'> {};
|
|
|
|
|
|
|
|
struct name : identifier {};
|
|
|
|
struct decimal
|
|
|
|
: if_then_else<dot, plus<digit>, seq<plus<digit>, opt<dot, star<digit>>>> {
|
|
|
|
};
|
|
|
|
struct e : one<'e', 'E'> {};
|
|
|
|
struct p : one<'p', 'P'> {};
|
|
|
|
struct exponent : seq<plus_minus, plus<digit>> {};
|
|
|
|
struct double_ : seq<plus_minus, decimal, opt<e, exponent>> {};
|
|
|
|
|
|
|
|
struct value
|
|
|
|
: sor<whitespace_delimited_array, string_literal, implicit_string> {};
|
|
|
|
struct num_array : list<double_, plus<one<' '>>> {};
|
|
|
|
struct str_array : list<string_literal, plus<one<' '>>> {};
|
|
|
|
|
|
|
|
struct value : sor<num_array, str_array> {};
|
|
|
|
struct comment : if_must<one<'#'>, until<eolf>> {};
|
|
|
|
|
|
|
|
struct key : list<identifier, one<'.'>> {};
|
|
|
|
struct keyval : seq<pad<key, space>, string<'='>, pad<value, space>> {};
|
|
|
|
struct grammar : pad<sor<keyval, comment>, space> {};
|
|
|
|
struct value_item : pad<value, blank> {};
|
|
|
|
struct value_list : list_must<value_item, one<','>> {};
|
|
|
|
struct keyval_line
|
|
|
|
: if_must<pad<key, space>, string<'='>, value_list, sor<comment, eolf>> {};
|
|
|
|
struct comment_line : seq<one<'#'>, until<eolf>> {};
|
|
|
|
struct blank_line : seq<star<one<' '>>, until<eolf>> {};
|
|
|
|
struct line : sor<comment_line, keyval_line, blank_line> {};
|
|
|
|
struct grammar : until<eof, line> {};
|
|
|
|
|
|
|
|
template <typename Rule> struct action {};
|
|
|
|
|
| ... | ... | @@ -68,45 +76,56 @@ template <> struct action<key> { |
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct action<string_literal> {
|
|
|
|
template <> struct action<quoted_string> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
std::stringstream ss(in.string());
|
|
|
|
std::string v;
|
|
|
|
ss >> v;
|
|
|
|
st.current_value = v.substr(1, v.length() - 2); // strip quotes
|
|
|
|
auto str_val = v.substr(1, v.length() - 2); // strip quotes
|
|
|
|
st.current_str_vec.push_back(str_val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct action<implicit_string> {
|
|
|
|
template <> struct action<unquoted_string> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
std::stringstream ss(in.string());
|
|
|
|
std::string v;
|
|
|
|
ss >> v;
|
|
|
|
st.current_value = v;
|
|
|
|
st.current_str_vec.push_back(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct action<double_::grammar> {
|
|
|
|
template <> struct action<double_> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
std::stringstream ss(in.string());
|
|
|
|
double v;
|
|
|
|
ss >> v;
|
|
|
|
st.current_vec.push_back(v);
|
|
|
|
st.current_num_vec.push_back(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct action<num_array> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
std::stringstream ss(in.string());
|
|
|
|
st.current_value = st.current_num_vec;
|
|
|
|
st.current_num_vec.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <> struct action<whitespace_delimited_array> {
|
|
|
|
|
|
|
|
template <> struct action<str_array> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
std::stringstream ss(in.string());
|
|
|
|
st.current_value = st.current_vec;
|
|
|
|
st.current_vec.clear();
|
|
|
|
st.current_value = st.current_str_vec;
|
|
|
|
st.current_str_vec.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct action<keyval> {
|
|
|
|
template <> struct action<keyval_line> {
|
|
|
|
template <typename Input>
|
|
|
|
static void apply(const Input &in, parser_state &st) {
|
|
|
|
st.info[st.current_key] = st.current_value;
|
| ... | ... | |
| ... | ... | |