Skip to content

Instantly share code, notes, and snippets.

@eskil
Created May 13, 2016 21:23
Show Gist options
  • Save eskil/33f6a007a82e4cf182ed24835f4d4426 to your computer and use it in GitHub Desktop.
Save eskil/33f6a007a82e4cf182ed24835f4d4426 to your computer and use it in GitHub Desktop.
partial dive plan parser
struct dive_table_grammar : boost::spirit::grammar<dive_table_grammar> {
dive_table_grammar (TablesCollection &t) : tablescollection (t) { }
template<typename Scanner>
struct definition {
definition (const dive_table_grammar &dt)
: si1 (minutes (0)), tablescollection (dt.tablescollection)
{
using namespace boost::spirit;
m_depth_rule
= int_p[assign_a (raw_depth)] >> str_p ("'")[assign_unit_a (raw_unit)]
| real_p[assign_a (raw_depth)] >> str_p ("m")[assign_unit_a (raw_unit)];
//| real_p[assign_a (raw_depth)] >> str_p ("fsw")[assign_unit_a (raw_unit)];
m_group_rule
= (range_p ('A', 'Z') | ch_p ('x'));
m_decos_rule
= (ch_p ('(') >> '@' >> m_depth_rule[compose_depth_a (deco_depth, raw_depth, raw_unit)]
>> int_p[assign_deco_a (deco_depth, deco)][push_back_a (decos, deco)][decrement_depth_a (deco_depth)]
>> *(int_p[assign_deco_a (deco_depth, deco)][push_back_a (decos, deco)][decrement_depth_a (deco_depth)]) >> ')');
m_time_and_deco_stops_rule
= (int_p[assign_time_a (bottom_time)] >> m_decos_rule)
| int_p[assign_time_a (bottom_time)]
| str_p ("*")[assign_time_a (bottom_time)];
m_row_rule
= m_group_rule[assign_group_a (group)]
>> *(m_time_and_deco_stops_rule)
[assign_row_a (row, bottom_time, group, decos)]
[push_back_a (table, row)]
[clear_a (decos)]
[increment_a (group)];
m_deco_table_rule
= m_depth_rule[compose_depth_a (table_depth, raw_depth, raw_unit)]
>> ('{' >> *(m_row_rule) >> '}')[insert_at_a (tables, table_depth, table)][clear_a (table)];
m_time_rule
= (
int_p[assign_a (hours)]
>> ':' >>
int_p[assign_a (mins)]
)[compose_time_a (si2, hours, mins)];
m_si_rule
= m_group_rule[assign_group_a (group) ]
>> '[' >> *(m_time_rule[compose_interval_a (si, si1, si2)][push_back_a (intervals, si)])
>> ']'
>> m_time_rule
[compose_interval_a (si, si1, si2)]
[push_back_a (intervals, si)]
[insert_at_a (intervalmap, group, intervals)]
[clear_a (intervals)];
m_rnt_rule
= m_depth_rule[compose_depth_a (rnt_depth, raw_depth, raw_unit)]
>> '['
>> *(m_depth_rule[compose_depth_a (tmp_depth, raw_depth, raw_unit)][compose_rnt_a (rnt, tmp_depth)]
| int_p[compose_rnt_a (rnt, tmp_depth)]
| str_p("-")[compose_rnt_a (rnt, tmp_depth)]
)[push_back_a (rnts, rnt)] >> ch_p (']')
[insert_at_a (rntmap, rnt_depth, rnts)][clear_a (rnts)];
m_rule
= *comment_p ("//")
>> (str_p ("table") >> confix_p ("\"", (*anychar_p)[assign_a (table_name)], "\"")
>> *(comment_p ("//") | m_si_rule)
>> *(comment_p ("//") | m_rnt_rule)
>> *(comment_p ("//") | m_deco_table_rule)
>> *comment_p ("//")
)
[compose_dive_tables_a (divetables, table_name, intervalmap, rntmap, tables)]
[insert_at_a (tablescollection, table_name, divetables)]
[clear_a (intervalmap)]
[clear_a (rntmap)]
[clear_a (tables)];
BOOST_SPIRIT_DEBUG_NODE (m_rule);
BOOST_SPIRIT_DEBUG_NODE (m_time_rule);
BOOST_SPIRIT_DEBUG_NODE (m_si_rule);
BOOST_SPIRIT_DEBUG_NODE (m_rnt_rule);
BOOST_SPIRIT_DEBUG_NODE (m_depth_rule);
BOOST_SPIRIT_DEBUG_NODE (m_deco_table_rule);
BOOST_SPIRIT_DEBUG_NODE (m_decos_rule);
BOOST_SPIRIT_DEBUG_NODE (m_row_rule);
BOOST_SPIRIT_DEBUG_NODE (m_group_rule);
BOOST_SPIRIT_DEBUG_NODE (m_time_and_deco_stops_rule);
}
const boost::spirit::rule<Scanner>& start () const { return m_rule; }
boost::spirit::rule<Scanner> m_rule;
boost::spirit::rule<Scanner> m_depth_rule,
m_deco_table_rule,
m_decos_rule,
m_si_rule,
m_time_rule,
m_rnt_rule,
m_row_rule,
m_group_rule,
m_time_and_deco_stops_rule;
Depth::depth_t raw_depth;
Depth::unit_t raw_unit;
int hours, mins;
duration_t si1, si2;
Interval si;
Intervals intervals;
IntervalMap intervalmap;
Depth rnt_depth;
Depth tmp_depth;
RNT rnt;
RNTs rnts;
RNTMap rntmap;
Depth deco_depth;
DecoStops decos;
DecoStop deco;
duration_t bottom_time;
Group group;
TableRow row;
Depth table_depth;
Table table;
std::string table_name;
Tables tables;
DiveTables divetables;
TablesCollection &tablescollection;
};
TablesCollection &tablescollection;
};
/*
140' { C 5 7 10
G 15(@10' 2) I 20(@10' 6) 25(@20' 2 14) 30(@20' 5 21)
N 40(@30' 2 16 26) 50(@30' 6 24 44) Z 60(@30' 16 23 56)
Z 70(@40' 4 19 32 68) Z 80(@40' 10 23 41 79) x 90(@50' 2 14 18 42 88)
120(@50' 12 14 36 56 120) 180(@60' 10 26 32 54 94 168)
240(@70' 8 28 34 50 78 124 187) 360(@80' 9 32 42 64 84 122 142 187)
480(@80' 31 44 59 100 114 122 142 187)
720(@90' 16 56 88 97 100 114 122 142 187)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment