public
Last active

Parsing a number of named sets of other named sets

  • Download Gist
.gitignore
1 2 3 4
test
*.o
*~
*.sw?
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
all:test output.txt
 
CPPFLAGS+=-std=c++0x
CPPFLAGS+=-g -O0
CPPFLAGS+=-I ~/custom/boost/
 
CPPFLAGS+=-march=native
 
# CXX=/usr/lib/gcc-snapshot/bin/g++
# CC=/usr/lib/gcc-snapshot/bin/gcc
# CXX=~/Projects/CLANG/build/Debug+Asserts/bin/clang++
# CC=~/Projects/CLANG/build/Debug+Asserts/bin/clang
 
%:%.cpp
$(CXX) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)
 
output.txt: test
./test > $@
input.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
# comment
 
# other comment
 
set "Myset A"
{
figure "AF 1"
{
i 0 0 0
i 1 2 5
i 1 1 1
f 3.1 45.11 5.3
i 3 1 5
f 1.1 2.33 5.166
}
 
figure "AF 2"
{
i 25 5 1
i 3 1 3
}
}
 
# comment
 
set "Myset B"
{
figure "BF 1"
{
f 23.1 4.3 5.11
}
}
 
set "Myset C"
{
include "Myset A" # includes all figures from Myset A
 
figure "CF"
{
i 1 1 1
f 3.11 5.33 3
}
}
output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# figure sets exported automatically by karma
 
 
set "Myset A"
{
figure "AF 1"
{
i 0 0 0
i 1 2 5
i 1 1 1
f 3.1 45.11 5.3
i 3 1 5
f 1.1 2.33 5.166
}
figure "AF 2"
{
i 25 5 1
i 3 1 3
}
}
set "Myset B"
{
figure "BF 1"
{
f 23.1 4.3 5.11
}
}
set "Myset C"
{
include "Myset A"
figure "CF"
{
i 1 1 1
f 3.11 5.33 3.0
}
}
test.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
//#define BOOST_SPIRIT_DEBUG // before including Spirit
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <fstream>
 
namespace Format
{
struct int_point { int x, y, z; };
struct float_point { float x, y, z; };
 
typedef boost::variant<int_point, float_point> if_point;
 
struct figure
{
std::string name;
std::vector<if_point> if_points;
 
friend std::ostream& operator<<(std::ostream& os, figure const& o);
};
 
struct figure_set
{
std::string name;
std::set<std::string> includes;
std::vector<figure> figures;
 
friend std::ostream& operator<<(std::ostream& os, figure_set const& o);
};
 
typedef std::vector<figure_set> file_data;
}
 
BOOST_FUSION_ADAPT_STRUCT(Format::int_point,
(int, x)(int, y)(int, z))
BOOST_FUSION_ADAPT_STRUCT(Format::float_point,
(float, x)(float, y)(float, z))
BOOST_FUSION_ADAPT_STRUCT(Format::figure,
(std::string, name)
(std::vector<Format::if_point>, if_points))
BOOST_FUSION_ADAPT_STRUCT(Format::figure_set,
(std::string, name)
(std::set<std::string>, includes)
(std::vector<Format::figure>, figures))
 
namespace Format
{
std::ostream& operator<<(std::ostream& os, if_point const& o)
{
using namespace boost::spirit::karma;
struct _ : boost::static_visitor<std::ostream&> { _(std::ostream&os):_os(os){}
std::ostream& operator()(int_point const&o) const { return _os << format_delimited("\n i" << int_ << int_ << int_, ' ', o); }
std::ostream& operator()(float_point const&o) const { return _os << format_delimited("\n f" << float_ << float_ << float_, ' ', o); }
private: std::ostream& _os;
};
return boost::apply_visitor(_(os), o);
}
 
std::ostream& operator<<(std::ostream& os, figure const& o)
{
using namespace boost::spirit::karma;
return os << format_delimited("\n figure" << no_delimit [ '"' << string << '"' ]
<< "\n {" << *stream << "\n }"
, ' ', o);
}
 
std::ostream& operator<<(std::ostream& os, figure_set const& o)
{
using namespace boost::spirit::karma;
return os << format_delimited(
"\nset" << no_delimit [ '"' << string << '"' ] << "\n{"
<< *("\n include " << no_delimit [ '"' << string << '"' ])
<< *stream << "\n}"
, ' ', o);
}
}
 
namespace /*anon*/
{
namespace phx=boost::phoenix;
namespace qi =boost::spirit::qi;
 
template <typename Iterator> struct skipper
: public qi::grammar<Iterator>
{
skipper() : skipper::base_type(start, "skipper")
{
using namespace qi;
 
comment = '#' >> *(char_ - eol) >> (eol|eoi);
start = comment | qi::space;
 
BOOST_SPIRIT_DEBUG_NODE(start);
BOOST_SPIRIT_DEBUG_NODE(comment);
}
 
private:
qi::rule<Iterator> start, comment;
};
 
template <typename Iterator> struct parser
: public qi::grammar<Iterator, Format::file_data(), skipper<Iterator> >
{
parser() : parser::base_type(start, "parser")
{
using namespace qi;
using phx::push_back;
using phx::at_c;
 
name = eps >> lexeme [ '"' >> *~char_('"') >> '"' ];
 
include = eps >> "include" >> name;
ipoints = eps >> "i" >> int_ >> int_ >> int_;
fpoints = eps >> "f" >> float_ >> float_ >> float_;
 
figure = eps >> "figure" >> name >> '{' >> *(ipoints | fpoints) >> '}';
set = eps >> "set" >> name >> '{' >> *include >> *figure >> '}';
start = *set;
}
 
private:
qi::rule<Iterator, std::string() , skipper<Iterator> > name, include;
qi::rule<Iterator, Format::int_point() , skipper<Iterator> > ipoints;
qi::rule<Iterator, Format::float_point(), skipper<Iterator> > fpoints;
qi::rule<Iterator, Format::figure() , skipper<Iterator> > figure;
qi::rule<Iterator, Format::figure_set() , skipper<Iterator> > set;
qi::rule<Iterator, Format::file_data() , skipper<Iterator> > start;
};
}
 
namespace Parser {
 
bool parsefile(const std::string& spec, Format::file_data& data)
{
std::ifstream in(spec.c_str());
in.unsetf(std::ios::skipws);
 
std::string v;
v.reserve(4096);
v.insert(v.end(), std::istreambuf_iterator<char>(in.rdbuf()), std::istreambuf_iterator<char>());
 
if (!in)
return false;
 
typedef char const * iterator_type;
iterator_type first = &v[0];
iterator_type last = first+v.size();
 
try
{
parser<iterator_type> p;
skipper<iterator_type> s;
bool r = qi::phrase_parse(first, last, p, s, data);
 
r = r && (first == last);
 
if (!r)
std::cerr << spec << ": parsing failed at: \"" << std::string(first, last) << "\"\n";
return r;
}
catch (const qi::expectation_failure<char const *>& e)
{
std::cerr << "FIXME: expected " << e.what_ << ", got '" << std::string(e.first, e.last) << "'" << std::endl;
return false;
}
}
}
 
int main()
{
Format::file_data data;
bool ok = Parser::parsefile("input.txt", data);
 
std::cerr << "Parse " << (ok?"success":"failed") << std::endl;
std::cout << "# figure sets exported automatically by karma\n\n";
 
for (auto& set : data)
std::cout << set;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.