Project

General

Profile

fhiclcpp-v2.patch

Gennadiy Lukhanin, 08/16/2017 02:39 PM

View differences:

fhiclcpp/extended_value.h
11 11
#include "fhiclcpp/Protection.h"
12 12
#include "fhiclcpp/fwd.h"
13 13
#include "stdmap_shims.h"
14
#include "fhiclcpp/exception.h"
14 15

  
15 16
#include <map>
16 17
#include <string>
17 18
#include <vector>
19
#include <utility>
18 20

  
19 21
// ----------------------------------------------------------------------
20 22

  
......
24 26
  };
25 27
}
26 28

  
29
namespace fhicl {
30
namespace detail {
31
using fhicl::exception;
32
using fhicl::error;
33

  
34
auto unwrap_value = [](auto value) {
35
  try {
36
    return boost::any_cast<std::pair<boost::any, std::string>>(value).first;
37
  } catch (const boost::bad_any_cast &) {
38
    return value;
39
  }
40
};
41

  
42
auto unwrap_raw_value = [](auto value) {
43
  try {
44
    return boost::any_cast<std::pair<boost::any, std::string>>(value).second;
45
  } catch (const boost::bad_any_cast &) {
46
    return std::string{};
47
  }
48
};
49

  
50
auto rtrim_whitespace = [](auto const &str) {
51
  constexpr auto double_quote = '\"';
52
  constexpr auto double_slashes = "//";
53
  constexpr auto space_char=' ';
54
  constexpr auto skip_chars = "# \n\r\t";
55

  
56
  if(str.empty())
57
    return str;
58
   
59
  if (str[0] == double_quote) {
60
    std::size_t pos = str.find(double_quote, 1);
61

  
62
    if (pos == std::string::npos)
63
      throw exception(cant_find, "missing a double quote in str=" + str);
64

  
65
    return str.substr(0, pos + 1);
66
  }
67

  
68
  std::size_t pos = str.find(double_slashes);
69

  
70
  if (pos == std::string::npos) {
71
    pos = str.find_first_of(skip_chars);
72

  
73
    if (pos == std::string::npos)
74
      return  str;
75
  }
76

  
77
  while(str[--pos]==space_char && pos!=0);
78

  
79
  return str.substr(0, pos+1);
80
};
81

  
82
auto wrap_value = [](auto value, auto raw_value) {
83
  return std::make_pair(value, rtrim_whitespace(raw_value));
84
};
85
} // namespace detail
86
} // namespace fhicl
87
  
27 88
// ----------------------------------------------------------------------
28

  
29 89
class fhicl::extended_value
30 90
{
31 91
public:
......
38 98
    : in_prolog( false )
39 99
    , tag      ( UNKNOWN )
40 100
    , value    ()
101
    , raw_value()    
41 102
    , src_info ()
42 103
    , protection (Protection::NONE)
43 104
  { }
......
49 110
                 std::string const& src = {})
50 111
    : in_prolog( in_prolog )
51 112
    , tag      ( tag )
52
    , value    ( value )
113
    , value    ( fhicl::detail::unwrap_value(value))
114
    , raw_value( fhicl::detail::unwrap_raw_value(value))
53 115
    , src_info ( src )
54
    , protection (protection)
116
    , protection (protection)    
55 117
  { }
56 118

  
57 119
  extended_value(bool       in_prolog,
......
60 122
                 std::string const& src = {})
61 123
    : in_prolog( in_prolog )
62 124
    , tag      ( tag )
63
    , value    ( value )
125
    , value    ( fhicl::detail::unwrap_value(value))
126
    , raw_value( fhicl::detail::unwrap_raw_value(value))
64 127
    , src_info ( src )
65 128
    , protection (Protection::NONE)
66 129
  { }
......
97 160
  bool       in_prolog;
98 161
  value_tag  tag;
99 162
  boost::any value;
163
  std::string raw_value;  
100 164
  std::string src_info;
101 165
  Protection protection;
102 166

  
fhiclcpp/parse.cc
194 194
  xvalue_dp(bool b,
195 195
            value_tag t,
196 196
            boost::any v,
197
            FwdIter pos,
197
            FwdIter first,
198
	    FwdIter last,
198 199
            cet::includer const & s)
199 200
  {
200
    std::string const src_info = s.src_whereis(pos);
201
    return extended_value(b, t, v, src_info);
201
    std::string const src_info = s.src_whereis(first);
202
    boost::any wv= fhicl::detail::wrap_value(v,std::string(first,last));
203
    return extended_value(b, t,wv,src_info);
202 204
  }
203 205

  
204 206
  complex_t
......
536 538
  value_parser();
537 539

  
538 540
  // data member:
539
  extended_value  v;
541
  extended_value    v;
540 542

  
541 543
  // parser rules:
542 544
  atom_token      nil, boolean;
......
731 733
#pragma clang diagnostic ignored "-Wunsequenced"
732 734
#endif
733 735
  value =
734
    ((iter_pos >> vp.nil    ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), NIL     , qi::_2, qi::_1, ref(s)) ] |
735
     (iter_pos >> vp.boolean) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), BOOL    , qi::_2, qi::_1, ref(s)) ] |
736
     (iter_pos >> vp.number ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), NUMBER  , qi::_2, qi::_1, ref(s)) ] |
737
     (iter_pos >> vp.complex) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), COMPLEX , qi::_2, qi::_1, ref(s)) ] |
738
     (iter_pos >> vp.string ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), STRING  , qi::_2, qi::_1, ref(s)) ] |
736
    ((iter_pos >> vp.nil >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), NIL     , qi::_2, qi::_1, qi::_3, ref(s)) ] |
737
     (iter_pos >> vp.boolean >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), BOOL    , qi::_2, qi::_1, qi::_3, ref(s)) ] |
738
     (iter_pos >> vp.number >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), NUMBER  , qi::_2, qi::_1, qi::_3, ref(s)) ] |
739
     (iter_pos >> vp.complex >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), COMPLEX , qi::_2, qi::_1, qi::_3, ref(s)) ] |
740
     (iter_pos >> vp.string >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), STRING  , qi::_2, qi::_1, qi::_3, ref(s)) ] |
739 741
     (iter_pos >> localref)
740
     [ _val = phx::bind(&local_lookup<iter_t>,
741
                        qi::_2, ref(tbl), ref(in_prolog),
742
                        qi::_1, ref(s)) ] |
743
     (iter_pos >> dbref)
744
     [ _val = phx::bind(&database_lookup<iter_t>,
745
                        qi::_2, ref(tbl), ref(in_prolog),
746
                        qi::_1, ref(s)) ] |
747
     (iter_pos >> vp.id    ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), TABLEID , qi::_2, qi::_1, ref(s)) ] |
748
     (iter_pos >> sequence ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), SEQUENCE, qi::_2, qi::_1, ref(s)) ] |
749
     (iter_pos >> table    ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), TABLE   , qi::_2, qi::_1, ref(s)) ] |
750
     (iter_pos >> vp.catchall ) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), STRING  , qi::_2, qi::_1, ref(s)) ]
742
	[ _val = phx::bind(&local_lookup<iter_t>,
743
			    qi::_2, ref(tbl), ref(in_prolog),
744
			    qi::_1, ref(s)) ] |
745
     (iter_pos >> dbref )
746
	[ _val = phx::bind(&database_lookup<iter_t>,
747
			    qi::_2, ref(tbl), ref(in_prolog),
748
			    qi::_1, ref(s)) ] |
749
     (iter_pos >> vp.id  >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), TABLEID , qi::_2, qi::_1, qi::_3, ref(s)) ] |
750
     (iter_pos >> sequence >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), SEQUENCE, qi::_2, qi::_1, qi::_3, ref(s)) ] |
751
     (iter_pos >> table  >>iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), TABLE   , qi::_2, qi::_1, qi::_3, ref(s)) ] |
752
     (iter_pos >> vp.catchall >> iter_pos) [ _val = phx::bind(&xvalue_dp<iter_t>, ref(in_prolog), STRING  , qi::_2, qi::_1, qi::_3, ref(s)) ]
751 753
    );
752 754
#ifdef __clang__
753 755
#pragma clang diagnostic pop