libpniio
complex_rule.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2015 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 //
20 // Created on: Jan 20, 2015
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 
25 #pragma once
26 
27 // the spirit_rules.hpp header file pulls everything in we need
28 #include "spirit_rules.hpp"
29 #include <boost/mpl/at.hpp>
30 #include <boost/spirit/include/qi.hpp>
31 #include <boost/spirit/include/phoenix.hpp>
32 
33 
34 namespace pni{
35 namespace io{
36 
49  template<
50  typename ITERT,
51  typename CTYPE
52  >
53  struct complex_rule: boost::spirit::qi::grammar<ITERT,
54  boost::spirit::qi::locals<
55  typename pni::core::type_info<CTYPE>::base_type,
56  typename pni::core::type_info<CTYPE>::base_type,
57  typename pni::core::type_info<CTYPE>::base_type
58  >,
59  CTYPE()>
60  {
62  typedef CTYPE result_type;
64  typedef typename pni::core::type_info<result_type>::base_type base_t;
65 
67  typename boost::mpl::at<spirit_rules,base_t>::type base_rule;
68 
70  boost::spirit::qi::rule<ITERT,base_t()> number_rule;
72  boost::spirit::qi::rule<ITERT,base_t()> sign_rule;
74  boost::spirit::qi::rule<ITERT> i_rule;
76  boost::spirit::qi::rule<ITERT,base_t()> imag_rule;
78  boost::spirit::qi::rule<ITERT,
79  boost::spirit::qi::locals<base_t,
80  base_t,
81  base_t>,
83 
86  {
87  using namespace boost::spirit::qi;
88  using namespace boost::phoenix;
89  using boost::spirit::qi::_1;
90 
91  number_rule = base_rule[_val = _1];
92  sign_rule = char_('+')[_val=1] | char_('-')[_val=-1.] ;
93  i_rule = (char_('i') | char_('j') | char_('I'))>!sign_rule;
94  imag_rule = i_rule>number_rule[_val = _1];
95 
96  complex_= eps[_a = 0,_b = 1, _c = 0] >>
97  (
98  (number_rule[_a = _1] || (sign_rule[_b = _1] > imag_rule[_c = _1]))
99  |
100  (sign_rule[_b = _1] || imag_rule[_c = _1])
101  )[_val = construct<result_type>(_a,_b*_c)] ;
102 
103  }
104  };
105 
106 //end of namespace
107 }
108 }
boost::mpl::at< spirit_rules, base_t >::type base_rule
rule to parse the base type
Definition: complex_rule.hpp:67
boost::spirit::qi::rule< ITERT, base_t()> imag_rule
rule determining the imaginary part
Definition: complex_rule.hpp:76
boost::spirit::qi::rule< ITERT > i_rule
rule matching the separator i,j,I
Definition: complex_rule.hpp:74
boost::spirit::qi::rule< ITERT, boost::spirit::qi::locals< base_t, base_t, base_t >, result_type()> complex_
rule defining the entire complex number
Definition: complex_rule.hpp:82
CTYPE result_type
result type of the rule
Definition: complex_rule.hpp:62
Definition: cbf_reader.hpp:41
complex_rule()
default constructor
Definition: complex_rule.hpp:85
pni::core::type_info< result_type >::base_type base_t
base type of the complex number type
Definition: complex_rule.hpp:64
boost::spirit::qi::rule< ITERT, base_t()> sign_rule
rule obtaining the sign
Definition: complex_rule.hpp:72
boost::spirit::qi::rule< ITERT, base_t()> number_rule
rule matching a single numeric value
Definition: complex_rule.hpp:70
complex number rule
Definition: complex_rule.hpp:53