Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
parser.hpp
1#pragma once
2
3#include "ast.hpp"
4#include <boost/fusion/include/std_pair.hpp>
5#include <boost/fusion/include/vector.hpp>
6#include <boost/spirit/home/x3.hpp>
7
8namespace quetzal
9{
10using boost::spirit::x3::parse;
11namespace format::newick::parser
12{
13namespace x3 = boost::spirit::x3;
14
15static x3::rule<class branch, ast::node> const node{"node"};
16
17static auto const name = x3::lexeme[x3::alpha >> *x3::alnum];
18static auto const length = ':' >> x3::double_ | x3::attr(0.0);
19static auto const leaf = name | x3::attr(std::string{});
20static auto const children = '(' >> (node % ',') >> ')' | x3::attr(ast::nodes{});
21static auto const node_def = children >> leaf >> -length;
22static auto const tree = x3::skip(x3::blank)[node >> ';' >> x3::eoi];
23
24BOOST_SPIRIT_DEFINE(node);
25
26template <typename ExpectedAttributeType> void compatible(auto p)
27{
28 static_assert(
29 std::is_same_v<ExpectedAttributeType,
30 typename x3::traits::attribute_of<decltype(x3::rule<struct _, ExpectedAttributeType>{} = p),
31 x3::unused_type>::type>);
32};
33
34void checks()
35{
36 compatible<double>(length);
37 compatible<std::string>(leaf);
38 compatible<std::string>(name);
39 compatible<ast::nodes>(children);
40 compatible<ast::node>(tree);
41}
42} // namespace format::newick::parser
43
44} // namespace quetzal
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21