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)), _id(id), _cid(cid), _driver(driver) {}
174 
176  const auto& id() const { return _id; }
177 
178 protected:
179  void debug(const std::string& msg) override;
180 
181 private:
182  std::string _id;
183  std::optional<std::string> _cid;
184  Driver* _driver;
185 };
186 
189  std::string orig_id;
190  std::string resp_id;
191  ParsingStateForDriver* orig_state = nullptr;
192  ParsingStateForDriver* resp_state = nullptr;
193 };
194 
195 } // namespace driver
196 
198 HILTI_EXCEPTION(InvalidUnitType, UserException);
199 
207 class Driver {
208 public:
209  Driver() {}
217  hilti::rt::Result<hilti::rt::Nothing> listParsers(std::ostream& out);
218 
229  hilti::rt::Result<const spicy::rt::Parser*> lookupParser(const std::string& name = "");
230 
242  hilti::rt::Result<spicy::rt::ParsedUnit> processInput(const spicy::rt::Parser& parser, std::istream& in,
243  int increment = 0);
244 
253  hilti::rt::Result<hilti::rt::Nothing> processPreBatchedInput(std::istream& in);
254 
256  void debug(const std::string& msg);
257 
258 private:
259  void _debugStats(const hilti::rt::ValueReference<hilti::rt::Stream>& data);
260  void _debugStats(size_t current_flows, size_t current_connections);
261 
262  uint64_t _total_flows = 0;
263  uint64_t _total_connections = 0;
264 };
265 
266 } // 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:176
void reset()
Definition: driver.h:118
Definition: parser.h:139
Definition: driver.h:188
Definition: driver.h:207
Definition: result.h:67
Definition: driver.h:26
bool hasParser() const
Definition: driver.h:48