Spicy
driver.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <iostream>
6 #include <optional>
7 #include <string>
8 #include <utility>
9 
10 #include <hilti/rt/result.h>
11 
12 #include <spicy/rt/parser.h>
13 
14 namespace spicy::rt {
15 
16 class Driver;
17 
18 namespace driver {
19 
20 enum class ParsingType { Stream, Block };
21 
26 class ParsingState {
27 public:
41  ParsingState(ParsingType type, const Parser* parser = nullptr, std::optional<UnitContext> context = {})
42  : _type(type), _parser(parser), _context(std::move(context)) {}
43 
48  bool hasParser() const { return _parser != nullptr; }
49 
61  void setParser(const Parser* parser, std::optional<UnitContext> context = {}) {
62  _parser = parser;
63  _context = std::move(context);
64  }
65 
71  bool isFinished() const { return _done || _skip; }
72 
77  void skipRemaining() { _skip = true; }
78 
80  bool isSkipping() const { return _skip; }
81 
83  enum State {
84  Done,
85  Continue
86  };
87 
102  State process(size_t size, const char* data) { return _process(size, data, false); }
103 
111  std::optional<hilti::rt::stream::Offset> finish();
112 
118  void reset() {
119  _input.reset();
120  _resumable.reset();
121  _done = false;
122  _skip = false;
123  }
124 
125 protected:
131  virtual void debug(const std::string& msg) = 0;
132 
136  void debug(const std::string& msg, size_t size, const char* data);
137 
138 private:
139  State _process(size_t size, const char* data, bool eod = true);
140 
141  ParsingType _type;
142  const Parser* _parser;
143  bool _skip = false;
144  std::optional<UnitContext> _context;
146  // State for stream matching only
147  bool _done = false;
148  std::optional<hilti::rt::ValueReference<hilti::rt::Stream>> _input;
149  std::optional<hilti::rt::Resumable> _resumable;
150 };
151 
154 public:
171  ParsingStateForDriver(ParsingType type, const Parser* parser, std::string id, std::optional<std::string> cid,
172  std::optional<UnitContext> context, Driver* driver)
173  : ParsingState(type, parser, std::move(context)),
174  _id(std::move(std::move(id))),
175  _cid(std::move(std::move(cid))),
176  _driver(driver) {}
177 
179  const auto& id() const { return _id; }
180 
181 protected:
182  void debug(const std::string& msg) override;
183 
184 private:
185  std::string _id;
186  std::optional<std::string> _cid;
187  Driver* _driver;
188 };
189 
192  std::string orig_id;
193  std::string resp_id;
194  ParsingStateForDriver* orig_state = nullptr;
195  ParsingStateForDriver* resp_state = nullptr;
196 };
197 
198 } // namespace driver
199 
201 HILTI_EXCEPTION(InvalidUnitType, UsageError);
202 
210 class Driver {
211 public:
212  Driver() {}
220  hilti::rt::Result<hilti::rt::Nothing> listParsers(std::ostream& out);
221 
232  hilti::rt::Result<const spicy::rt::Parser*> lookupParser(const std::string& name = "");
233 
245  hilti::rt::Result<spicy::rt::ParsedUnit> processInput(const spicy::rt::Parser& parser, std::istream& in,
246  int increment = 0);
247 
256  hilti::rt::Result<hilti::rt::Nothing> processPreBatchedInput(std::istream& in);
257 
259  void debug(const std::string& msg);
260 
261 private:
262  void _debugStats(const hilti::rt::ValueReference<hilti::rt::Stream>& data);
263  void _debugStats(size_t current_flows, size_t current_connections);
264 
265  uint64_t _total_flows = 0;
266  uint64_t _total_connections = 0;
267 };
268 
269 } // namespace spicy::rt
void setParser(const Parser *parser, std::optional< UnitContext > context={})
Definition: driver.h:61
ParsingStateForDriver(ParsingType type, const Parser *parser, std::string id, std::optional< std::string > cid, std::optional< UnitContext > context, Driver *driver)
Definition: driver.h:171
Definition: optional.h:79
bool isFinished() const
Definition: driver.h:71
void skipRemaining()
Definition: driver.h:77
ParsingState(ParsingType type, const Parser *parser=nullptr, std::optional< UnitContext > context={})
Definition: driver.h:41
State
Definition: driver.h:83
State process(size_t size, const char *data)
Definition: driver.h:102
bool isSkipping() const
Definition: driver.h:80
const auto & id() const
Definition: driver.h:179
void reset()
Definition: driver.h:118
Definition: parser.h:140
Definition: driver.h:191
Definition: driver.h:210
Definition: result.h:67
Definition: driver.h:26
bool hasParser() const
Definition: driver.h:48