libpniio
parsers/parser.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 23, 2015
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 
25 #pragma once
26 
27 #include<pni/core/types.hpp>
28 #include <boost/spirit/include/qi.hpp>
29 
30 #include "../exceptions.hpp"
31 #include "get_rule_type.hpp"
32 #include "conversion_trait.hpp"
33 
34 namespace pni{
35 namespace io{
36 
37  //------------------------------------------------------------------------
58  template<
59  typename T,
60  typename ITERT = pni::core::string::const_iterator
61  >
62  class parser
63  {
64  public:
66  typedef ITERT iterator_type;
68  typedef T result_type;
70  typedef boost::spirit::qi::expectation_failure<iterator_type> expectation_error;
71  private:
75  typedef typename trait_type::read_type read_type;
78 
79  public:
90  result_type operator()(const pni::core::string &data) const
91  {
92  using namespace boost::spirit;
93  using namespace pni::core;
94 
95  read_type buffer;
96 
97  try
98  {
99  if(!qi::parse(data.begin(),data.end(),_rule>qi::eoi,buffer))
100  {
101  throw parser_error(EXCEPTION_RECORD,
102  "Failure parsing primitive type!");
103  }
104 
105  }
106  catch(const expectation_error &error)
107  {
108  throw parser_error(EXCEPTION_RECORD,
109  "Syntax error!");
110  }
111 
112  try
113  {
114  return trait_type::convert(std::move(buffer));
115  }
116  catch(...)
117  {
118  throw parser_error(EXCEPTION_RECORD,
119  "Failure during type conversion!");
120  }
121 
122  }
123  };
124 
125 
126 //end of namespace
127 }
128 }
default conversion trait
Definition: conversion_trait.hpp:46
boost::spirit::qi::expectation_failure< iterator_type > expectation_error
parser exception type
Definition: parsers/parser.hpp:70
general parser related error
Definition: exceptions.hpp:99
static result_type convert(read_type &&v)
conversion function
Definition: conversion_trait.hpp:61
trait_type::read_type read_type
type used for reading the data
Definition: parsers/parser.hpp:75
conversion_trait< result_type > trait_type
conversion trait type
Definition: parsers/parser.hpp:73
ITERT iterator_type
iterator type
Definition: parsers/parser.hpp:66
result_type operator()(const pni::core::string &data) const
parser primitive type
Definition: parsers/parser.hpp:90
Definition: spirit_container_traits.hpp:33
parser for primitive types
Definition: parsers/parser.hpp:62
Definition: cbf_reader.hpp:41
T read_type
the type to read
Definition: conversion_trait.hpp:51
get_rule_type< iterator_type, read_type >::type _rule
rule which will be used to read the data
Definition: parsers/parser.hpp:77
T result_type
result type of the parsing process
Definition: parsers/parser.hpp:68
obtain rule type
Definition: get_rule_type.hpp:48