JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
Function_Actions.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 08/05/2019.
3 //
4 
5 #ifndef JODA_FUNCTION_ACTIONS_H
6 #define JODA_FUNCTION_ACTIONS_H
8 
9 template <>
11  template <typename Input>
12  static void apply(const Input &in, functionState &state) {
13  std::string pointer = in.string();
14  auto para = std::make_unique<joda::query::PointerProvider>(
15  pointer.substr(1, pointer.size() - 2));
16  if (state.atom == NO_ATOM) {
17  state.atom = ATOM_POINTER;
18  }
19  state.params.push_back(std::move(para));
20  }
21 };
22 template <>
24  template <typename Input>
25  static bool apply(const Input &in, functionState &state) {
26  std::string str = in.string();
27 
28  std::unique_ptr<joda::query::IValueProvider> ival;
29 
30  try {
31  double i = std::stod(str);
32  ival = std::make_unique<joda::query::DoubleProvider>(i);
33  } catch (std::exception &e) {
34  LOG(ERROR) << "Could not parse number, out of range?";
35  return false;
36  }
37 
38  if (ival == nullptr) return false;
39 
40  auto para = std::move(ival);
41  if (state.atom == NO_ATOM) {
42  state.atom = ATOM_NUMBER;
43  }
44  state.params.push_back(std::move(para));
45  return true;
46  }
47 };
48 
49 template <>
51  template <typename Input>
52  static bool apply(const Input &in, functionState &state) {
53  std::string str = in.string();
54 
55  std::unique_ptr<joda::query::IValueProvider> ival;
56 
57  try {
58  int64_t i = std::stol(str);
59  ival = std::make_unique<joda::query::Int64Provider>(i);
60  } catch (std::exception &e) {
61  }
62 
63  if (ival == nullptr) {
64  try {
65  u_int64_t i = std::stoul(str);
66  ival = std::make_unique<joda::query::UInt64Provider>(i);
67  } catch (std::exception &e) {
68  }
69  }
70  if (ival == nullptr) {
71  try {
72  double i = std::stod(str);
73  ival = std::make_unique<joda::query::DoubleProvider>(i);
74  } catch (std::exception &e) {
75  LOG(ERROR) << "Could not parse number, out of range?";
76  return false;
77  }
78  }
79 
80  if (ival == nullptr) return false;
81 
82  auto para = std::move(ival);
83  if (state.atom == NO_ATOM) {
84  state.atom = ATOM_NUMBER;
85  }
86  state.params.push_back(std::move(para));
87  return true;
88  }
89 };
90 
91 template <>
93  static inline std::string unescape(const std::string &s) {
94  std::string res;
95  std::string::const_iterator it = s.begin();
96  while (it != s.end()) {
97  char c = *it++;
98  if (c == '\\' && it != s.end()) {
99  switch (*it++) {
100  case '\\':
101  c = '\\';
102  break;
103  case '"':
104  c = '"';
105  break;
106  // all other escapes
107  default:
108  // invalid escape sequence - skip it. alternatively you can copy it
109  // as is, throw an exception...
110  continue;
111  }
112  }
113  res += c;
114  }
115 
116  return res;
117  }
118  template <typename Input>
119  static void apply(const Input &in, functionState &state) {
120  std::string str = in.string();
121  // Unescape string
122  auto unescaped = unescape(str.substr(1, str.size() - 2));
123  auto para = std::make_unique<joda::query::StringProvider>(unescaped);
124  if (state.atom == NO_ATOM) {
125  state.atom = ATOM_STRING;
126  }
127  state.params.push_back(std::move(para));
128  }
129 };
130 template <>
132  template <typename Input>
133  static void apply(const Input &in, functionState &state) {
134  std::string b = in.string();
135  bool bo{};
136  if (b == "true")
137  bo = true;
138  else if (b == "false")
139  bo = false;
140  else
141  assert(false && "Did not change bool parsing");
142  auto para = std::make_unique<joda::query::BoolProvider>(bo);
143  if (state.atom == NO_ATOM) {
144  state.atom = ATOM_BOOL;
145  }
146  state.params.push_back(std::move(para));
147  }
148 };
149 
150 template <>
152  template <typename Input>
153  static void apply(const Input &in, functionState &state) {
154  auto para = std::make_unique<joda::query::NullProvider>();
155  if (state.atom == NO_ATOM) {
156  state.atom = ATOM_NULL;
157  }
158  state.params.push_back(std::move(para));
159  }
160 };
161 
162 } // namespace joda::queryparsing::grammar
163 #endif // JODA_FUNCTION_ACTIONS_H
@ ATOM_BOOL
Definition: Function_State.h:16
@ ATOM_NUMBER
Definition: Function_State.h:15
@ ATOM_NULL
Definition: Function_State.h:18
@ NO_ATOM
Definition: Function_State.h:13
@ ATOM_POINTER
Definition: Function_State.h:17
@ ATOM_STRING
Definition: Function_State.h:14
Definition: Literals.h:65
static void apply(const Input &in, functionState &state)
Definition: Function_Actions.h:133
static bool apply(const Input &in, functionState &state)
Definition: Function_Actions.h:25
static bool apply(const Input &in, functionState &state)
Definition: Function_Actions.h:52
static void apply(const Input &in, functionState &state)
Definition: Function_Actions.h:153
static void apply(const Input &in, functionState &state)
Definition: Function_Actions.h:12
static void apply(const Input &in, functionState &state)
Definition: Function_Actions.h:119
static std::string unescape(const std::string &s)
Definition: Function_Actions.h:93
Definition: FunctionWrapper.h:12
Definition: Function_State.h:21
Atom_Value atom
Definition: Function_State.h:143
std::vector< std::unique_ptr< joda::query::IValueProvider > > params
Definition: Function_State.h:147
Definition: Literals.h:74
Definition: Literals.h:27