Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
*  Formats files and adds a .clang-format file
  • Loading branch information
miker2 committed Nov 19, 2021
1 parent de3b478 commit 3864856
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 103 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: Google

IndentWidth: 2
ColumnLimit: 100
23 changes: 12 additions & 11 deletions config_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ namespace config {

template <typename Rule>
using selector = peg::parse_tree::selector<
Rule, peg::parse_tree::store_content::on<HEX, NUMBER, STRING, VAR, FLAT_KEY, KEY, COMMENT, filename::FILENAME>,
peg::parse_tree::remove_content::on<END, VARADD, VARREF, INCLUDE>,
/*
peg::parse_tree::remove_content::on<WS_, NL, SP, oSP, COMMENT, TAIL, COMMA,
SBo, SBc, CBo, CBc, KVs, HEXTAG, sign,
exp>,
*/
peg::parse_tree::fold_one::on<CONFIG, STRUCTc, STRUCT, PROTO, REFERENCE,
VALUE, PAIR, LIST>>;
Rule,
peg::parse_tree::store_content::on<HEX, NUMBER, STRING, VAR, FLAT_KEY, KEY, COMMENT,
filename::FILENAME>,
peg::parse_tree::remove_content::on<END, VARADD, VARREF, INCLUDE>,
/*
peg::parse_tree::remove_content::on<WS_, NL, SP, oSP, COMMENT, TAIL,
COMMA, SBo, SBc, CBo, CBc, KVs, HEXTAG, sign, exp>,
*/
peg::parse_tree::fold_one::on<CONFIG, STRUCTc, STRUCT, PROTO, REFERENCE, VALUE, PAIR, LIST>>;

template <typename Rule> struct action : peg::nothing<Rule> {};
template <typename Rule>
struct action : peg::nothing<Rule> {};

/*
template <> struct action<HEX> {
Expand All @@ -46,4 +47,4 @@ template <> struct action<NUMBER> {
}
};
*/
} // namespace config
} // namespace config
39 changes: 16 additions & 23 deletions config_grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace peg = TAO_PEGTL_NAMESPACE;
// The data should consist only of specific types and eliminate all other
// unecessary data (e.g. whitespace, etc).


// This grammar parses a filename (path + filename) corresponding to a cfg file.
namespace filename {

Expand All @@ -18,22 +17,23 @@ struct EXT : TAO_PEGTL_KEYWORD(".cfg") {};
struct SEP : peg::one<'/'> {};

// There may be other valid characters in a filename. What might they be?
struct ALPHAPLUS : peg::plus<peg::sor<peg::ranges<'A','Z','a','z','0','9','_'>, peg::one<'-'>>> {};
struct ALPHAPLUS
: peg::plus<peg::sor<peg::ranges<'A', 'Z', 'a', 'z', '0', '9', '_'>, peg::one<'-'>>> {};

struct FILEPART : peg::sor<DOTDOT, ALPHAPLUS> {};
struct FILENAME : peg::seq<peg::list<FILEPART, SEP>, EXT> {};

struct grammar : peg::must<FILENAME> {};

}

} // namespace filename

namespace config {

/*
TODO: Add validator to throw on duplicate keys
*/

// clang-format off
/*
# TODO: Support the following syntax:
# key = $(path.to.other.key)
Expand Down Expand Up @@ -80,6 +80,7 @@ grammar my_config
NL <- [\r\n]+ # (required) new line
_ <- [ \t\r\n]* # All whitespace
*/
// clang-format on

struct WS_ : peg::star<peg::space> {};
struct NL : peg::plus<peg::eol> {};
Expand All @@ -97,23 +98,17 @@ struct KVs : peg::pad<peg::one<'='>, peg::blank> {};
struct HEXTAG : peg::seq<peg::one<'0'>, peg::one<'x', 'X'>> {};
struct HEX : peg::seq<HEXTAG, peg::plus<peg::xdigit>> {};

struct VAR
: peg::seq<peg::one<'$'>, peg::plus<peg::ranges<'A', 'Z', '0', '9', '_'>>> {
};
struct VAR : peg::seq<peg::one<'$'>, peg::plus<peg::ranges<'A', 'Z', '0', '9', '_'>>> {};

struct sign : peg::one<'+', '-'> {};
struct exp
: peg::seq<peg::one<'e', 'E'>, peg::opt<sign>, peg::plus<peg::digit>> {};
struct exp : peg::seq<peg::one<'e', 'E'>, peg::opt<sign>, peg::plus<peg::digit>> {};
struct INTEGER
: peg::seq<peg::opt<sign>,
peg::sor<peg::one<'0'>, peg::seq<peg::range<'1', '9'>,
peg::star<peg::digit>>>> {};
struct FLOAT
: peg::seq<INTEGER, peg::one<'.'>, peg::star<peg::digit>, peg::opt<exp>> {};
peg::sor<peg::one<'0'>, peg::seq<peg::range<'1', '9'>, peg::star<peg::digit>>>> {};
struct FLOAT : peg::seq<INTEGER, peg::one<'.'>, peg::star<peg::digit>, peg::opt<exp>> {};
struct NUMBER : peg::sor<FLOAT, INTEGER> {};

struct STRING
: peg::seq<peg::one<'"'>, peg::plus<peg::not_one<'"'>>, peg::one<'"'>> {};
struct STRING : peg::seq<peg::one<'"'>, peg::plus<peg::not_one<'"'>>, peg::one<'"'>> {};

struct VALUE;
// Should the 'space' here be a 'blank'? Allow multi-line lists (w/o \)?
Expand All @@ -122,8 +117,7 @@ struct LIST : peg::seq<SBo, peg::list<VALUE, COMMA, peg::space>, SBc> {};
struct VALUE : peg::sor<LIST, HEX, NUMBER, STRING> {};

struct KEY
: peg::seq<peg::range<'a', 'z'>,
peg::star<peg::ranges<'a', 'z', 'A', 'Z', '0', '9', '_'>>> {};
: peg::seq<peg::range<'a', 'z'>, peg::star<peg::ranges<'a', 'z', 'A', 'Z', '0', '9', '_'>>> {};
struct FLAT_KEY : peg::list<KEY, peg::one<'.'>> {};
struct VARADD : peg::seq<peg::one<'+'>, KEY, KVs, VALUE, TAIL> {};
struct VARREF : peg::seq<VAR, KVs, VALUE, TAIL> {};
Expand All @@ -135,8 +129,8 @@ struct END : peg::seq<peg::keyword<'e', 'n', 'd'>, SP, KEY> {};

struct REFs : peg::seq<TAO_PEGTL_KEYWORD("reference"), SP> {};
struct REFc : peg::plus<peg::sor<VARREF, VARADD>> {};
struct REFERENCE : peg::seq<REFs, FLAT_KEY, WS_, peg::keyword<'a', 's'>, WS_,
KEY, TAIL, REFc, END, WS_> {};
struct REFERENCE
: peg::seq<REFs, FLAT_KEY, WS_, peg::keyword<'a', 's'>, WS_, KEY, TAIL, REFc, END, WS_> {};

struct STRUCTc;
struct PROTOs : peg::seq<TAO_PEGTL_KEYWORD("proto"), SP> {};
Expand All @@ -152,9 +146,8 @@ struct STRUCTc : peg::plus<peg::sor<STRUCT, PAIR, REFERENCE, PROTO>> {};
// 1. Optional list of include files
// 2. Elements of a config file: struct, proto, reference, pair
//
// How do we fit in flat keys? I think we want to support flat keys or structured
// keys in a single file. But not both.

// How do we fit in flat keys? I think we want to support flat keys or
// structured keys in a single file. But not both.

struct INCLUDE : peg::seq<TAO_PEGTL_KEYWORD("include"), SP, filename::grammar, TAIL> {};
struct include_list : peg::star<INCLUDE> {};
Expand All @@ -163,4 +156,4 @@ struct CONFIG : peg::seq<TAIL, include_list, peg::sor<STRUCTc, peg::plus<FULLPAI

struct grammar : peg::seq<CONFIG, peg::eolf> {};

}
} // namespace config
24 changes: 10 additions & 14 deletions config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>

#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/analyze.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include <tao/pegtl/contrib/parse_tree_to_dot.hpp>
#include <tao/pegtl/contrib/trace.hpp>
#include <vector>

#include "config_grammar.h"
#include "config_actions.h"
#include "config_grammar.h"

// TODO(rose@): Turn this into an actual test suite.

namespace peg = TAO_PEGTL_NAMESPACE;


template <typename GTYPE, typename SOURCE>
auto runTest(SOURCE& src, bool pdot = true) -> bool {
auto runTest(SOURCE &src, bool pdot = true) -> bool {
std::cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
std::cout << "Parsing: " << src.source() << "\n----- content -----\n";
std::cout << std::string_view(src.begin(), src.size()) << std::endl;
Expand Down Expand Up @@ -50,9 +48,7 @@ auto runTest(SOURCE& src, bool pdot = true) -> bool {
std::cout << "!!!\n";
std::cout << " Parser failure!\n";
const auto p = e.positions().front();
std::cout << e.what() << '\n'
<< src.line_at(p) << '\n'
<< std::setw(p.column) << '^' << '\n';
std::cout << e.what() << '\n' << src.line_at(p) << '\n' << std::setw(p.column) << '^' << '\n';
std::cout << "!!!\n";
}

Expand All @@ -61,16 +57,15 @@ auto runTest(SOURCE& src, bool pdot = true) -> bool {
}

template <typename GTYPE>
auto runTest(size_t idx, const std::string& test_str, bool pdot = true) -> bool {
auto runTest(size_t idx, const std::string &test_str, bool pdot = true) -> bool {
peg::memory_input in(test_str, "example " + std::to_string(idx));

return runTest<GTYPE>(in, pdot);
}

auto main() -> int {

const bool pdot = false;
bool ret{ true };
bool ret{true};

if (peg::analyze<config::grammar>() != 0) {
std::cout << "Something in the grammar is broken!" << std::endl;
Expand All @@ -95,7 +90,8 @@ auto main() -> int {
ret &= runTest<peg::must<config::VALUE, peg::eolf>>(test_num++, content, pdot);
}

std::vector config_strs = {"\n\
std::vector config_strs = {
"\n\
struct test1\n\
key1 = \"value\"\n\
key2 = 1.342 # test comment here\n\
Expand All @@ -115,8 +111,8 @@ end test2\n\
}

for (size_t i = 1; i <= 6; ++i) {
const auto cfg_file = std::filesystem::path(EXAMPLE_DIR) /
("config_example" + std::to_string(i) + ".cfg");
const auto cfg_file =
std::filesystem::path(EXAMPLE_DIR) / ("config_example" + std::to_string(i) + ".cfg");
peg::file_input in(cfg_file);
ret &= runTest<config::grammar>(in, true);
}
Expand Down
37 changes: 14 additions & 23 deletions include_file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/analyze.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include <tao/pegtl/contrib/parse_tree_to_dot.hpp>
#include <tao/pegtl/contrib/trace.hpp>
#include <vector>

namespace peg = TAO_PEGTL_NAMESPACE;

Expand All @@ -19,26 +18,26 @@ struct DOTDOT : peg::two<'.'> {};
struct EXT : TAO_PEGTL_KEYWORD(".cfg") {};
struct SEP : peg::one<'/'> {};

struct ALPHAPLUS : peg::plus<peg::sor<peg::ranges<'A','Z','a','z','0','9','_'>, peg::one<'-'>>> {};
struct ALPHAPLUS
: peg::plus<peg::sor<peg::ranges<'A', 'Z', 'a', 'z', '0', '9', '_'>, peg::one<'-'>>> {};

struct FILEPART : peg::sor<DOTDOT, ALPHAPLUS> {};
struct FILENAME : peg::seq<peg::list<FILEPART, SEP>, EXT> {};

struct grammar : peg::must<FILENAME> {};

template <typename Rule>
using selector = peg::parse_tree::selector<
Rule,
peg::parse_tree::store_content::on<FILEPART, FILENAME, EXT>,
peg::parse_tree::remove_content::on<ALPHAPLUS>>;
using selector =
peg::parse_tree::selector<Rule, peg::parse_tree::store_content::on<FILEPART, FILENAME, EXT>,
peg::parse_tree::remove_content::on<ALPHAPLUS>>;

}
} // namespace filename

namespace include_file {
struct INCLUDE : TAO_PEGTL_KEYWORD("#include") {};

struct grammar : peg::seq<INCLUDE, peg::plus<peg::blank>, filename::FILENAME> {};
}
} // namespace include_file

#if 0
template <typename GTYPE>
Expand Down Expand Up @@ -109,13 +108,11 @@ auto runTest(SOURCE& src, bool pdot = true) -> bool {
src.restart();
ret = peg::parse<GTYPE>(src);
std::cout << " Parse " << (ret ? "success" : "failure") << std::endl;
} catch (const peg::parse_error &e) {
} catch (const peg::parse_error& e) {
std::cout << "!!!\n";
std::cout << " Parser failure!\n";
const auto p = e.positions().front();
std::cout << e.what() << '\n'
<< src.line_at(p) << '\n'
<< std::setw(p.column) << '^' << '\n';
std::cout << e.what() << '\n' << src.line_at(p) << '\n' << std::setw(p.column) << '^' << '\n';
std::cout << "!!!\n";
}

Expand All @@ -130,27 +127,21 @@ auto runTest(size_t idx, const std::string& test_str, bool pdot = true) -> bool
return runTest<GTYPE>(in, pdot);
}


auto main() -> int {

const bool pdot = false;

if (peg::analyze<filename::grammar>() != 0) {
std::cout << "Something in the grammar is broken!" << std::endl;
return 1;
}

bool ret{ true };
bool ret{true};
size_t test_num = 1;

std::vector<std::string> test_filenames = {
"example1.cfg",
"../example2.cfg",
"foo/bar/baz.cfg"
};

std::vector<std::string> test_filenames = {"example1.cfg", "../example2.cfg", "foo/bar/baz.cfg"};

for (const auto& e : test_filenames) {
ret &= runTest<filename::FILENAME>(test_num++, e);
ret &= runTest<filename::FILENAME>(test_num++, e);
}

runTest<include_file::grammar>(999, "#include ../robot/default.cfg");
Expand Down
Loading

0 comments on commit 3864856

Please sign in to comment.