CommandScannerbase.h
1 // Generated by Flexc++ V2.01.00 on Fri, 20 May 2016 16:54:44 -0400
2 
3 #ifndef pfp_cpCommandScannerBASE_H_INCLUDED
4 #define pfp_cpCommandScannerBASE_H_INCLUDED
5 
6 #include <limits>
7 #include <iostream>
8 #include <deque>
9 #include <string>
10 #include <vector>
11 #include <memory>
12 
13 
14 // $insert namespace-open
15 namespace pfp_cp
16 {
17 
18 class CommandScannerBase
19 {
20  // idx: rule, value: tail length (NO_INCREMENTS if no tail)
21  typedef std::vector<int> VectorInt;
22 
23  static size_t const s_unavailable = std::numeric_limits<size_t>::max();
24 
25  enum
26  {
27  AT_EOF = -1
28  };
29 
30 protected:
31  enum Leave__
32  {};
33 
34  enum class ActionType__
35  {
36  CONTINUE, // transition succeeded, go on
37  ECHO_CH, // echo ch itself (d_matched empty)
38  ECHO_FIRST, // echo d_matched[0], push back the rest
39  MATCH, // matched a rule
40  RETURN, // no further continuation, lex returns 0.
41  };
42 
43  enum class PostEnum__
44  {
45  END, // postCode called when lex__() ends
46  POP, // postCode called after switching files
47  RETURN, // postCode called when lex__() returns
48  WIP // postCode called when a non-returning rule
49  // was matched
50  };
51 
52 public:
53  enum class StartCondition__ {
54  // $insert startCondNames
55  INITIAL,
56  };
57 
58 private:
59  struct FinalData
60  {
61  size_t rule;
62  size_t length;
63  };
64  struct Final
65  {
66  FinalData std;
67  FinalData bol;
68  };
69 
70  // class Input encapsulates all input operations.
71  // Its member get() returns the next input character
72 // $insert inputInterface
73 
74  class Input
75  {
76  std::deque<unsigned char> d_deque; // pending input chars
77  std::istream *d_in; // ptr for easy streamswitching
78  size_t d_lineNr; // line count
79 
80  public:
81  Input();
82  // iStream: dynamically allocated
83  Input(std::istream *iStream, size_t lineNr = 1);
84  size_t get(); // the next range
85  void reRead(size_t ch); // push back 'ch' (if < 0x100)
86  // push back str from idx 'fmIdx'
87  void reRead(std::string const &str, size_t fmIdx);
88  size_t lineNr() const
89  {
90  return d_lineNr;
91  }
92  size_t nPending() const
93  {
94  return d_deque.size();
95  }
96  void setPending(size_t size)
97  {
98  d_deque.erase(d_deque.begin(), d_deque.end() - size);
99  }
100  void close() // force closing the stream
101  {
102  delete d_in;
103  d_in = 0; // switchStreams also closes
104  }
105 
106  private:
107  size_t next(); // obtain the next character
108  };
109 
110 protected:
111  struct StreamStruct
112  {
113  std::string pushedName;
114  Input pushedInput;
115  };
116 
117 private:
118  std::vector<StreamStruct> d_streamStack;
119 
120  std::string d_filename; // name of the currently processed
121  static size_t s_istreamNr; // file. With istreams it receives
122  // the name "<istream #>", where
123  // # is the sequence number of the
124  // istream (starting at 1)
125  int d_startCondition = 0;
126  int d_lopSC = 0;
127 
128  size_t d_state = 0;
129  int d_nextState;
130  std::shared_ptr<std::ostream> d_out;
131  bool d_atBOL = true; // the matched text starts at BOL
132  Final d_final;
133 
134  // only used interactively:
135  std::istream *d_in; // points to the input stream
136  std::shared_ptr<std::istringstream> d_line; // holds line fm d_in
137 
138  Input d_input;
139  std::string d_matched; // matched characters
140  std::string d_lopMatched; // matched lop-rule characters
141  std::string::iterator d_lopIter;
142  std::string::iterator d_lopTail;
143  std::string::iterator d_lopEnd;
144 
145  size_t d_lopPending; // # pending input chars at lop1__
146  bool d_return; // return after a rule's action
147  bool d_more = false; // set to true by more()
148 
149  size_t (CommandScannerBase::*d_get)() = &CommandScannerBase::getInput;
150 
151 protected:
152  std::istream *d_in__;
153  int d_token__; // returned by lex__
154 
155 
156 
157  int const (*d_dfaBase__)[38];
158 
159  static int const s_dfa__[][38];
160  static int const (*s_dfaBase__[])[38];
161  enum: bool { s_interactive__ = false };
162  enum: size_t {
163  s_rangeOfEOF__ = 35,
164  s_finIdx__ = 36,
165  s_nRules__ = 10,
166  s_maxSizeofStreamStack__ = 10
167  };
168  static size_t const s_ranges__[];
169  static size_t const s_rf__[][2];
170 
171 public:
172  CommandScannerBase(CommandScannerBase const &other) = delete;
173  CommandScannerBase &operator=(CommandScannerBase const &rhs) = delete;
174 
175  bool debug() const;
176  std::string const &filename() const;
177  std::string const &matched() const;
178 
179  size_t length() const;
180  size_t lineNr() const;
181 
182  void setDebug(bool onOff);
183 
184  void switchOstream(std::ostream &out);
185  void switchOstream(std::string const &outfilename);
186 
187 
188  void switchStreams(std::istream &in,
189  std::ostream &out = std::cout);
190 
191  void switchIstream(std::string const &infilename);
192  void switchStreams(std::string const &infilename,
193  std::string const &outfilename);
194 
195 
196 // $insert interactiveDecl
197 
198 protected:
199  CommandScannerBase(std::istream &in, std::ostream &out);
200  CommandScannerBase(std::string const &infilename, std::string const &outfilename);
201 
202  StartCondition__ startCondition() const; // current start condition
203  bool popStream();
204  std::ostream &out();
205  void begin(StartCondition__ startCondition);
206  void echo() const;
207  void leave(int retValue) const;
208 
209 // `accept(n)' returns all but the first `n' characters of the current
210 // token back to the input stream, where they will be rescanned when the
211 // scanner looks for the next match.
212 // So, it matches n of the characters in the input buffer, and so it accepts
213 // n characters, rescanning the rest.
214  void accept(size_t nChars = 0); // former: less
215  void redo(size_t nChars = 0); // rescan the last nChar
216  // characters, reducing
217  // length() by nChars
218  void more();
219  void push(size_t ch); // push char to Input
220  void push(std::string const &txt); // same: chars
221 
222 
223  std::vector<StreamStruct> const &streamStack() const;
224 
225  void pushStream(std::istream &curStream);
226  void pushStream(std::string const &curName);
227 
228 
229  void setFilename(std::string const &name);
230  void setMatched(std::string const &text);
231 
232  static std::string istreamName__();
233 
234  // members used by lex__(): they end in __ and should not be used
235  // otherwise.
236 
237  ActionType__ actionType__(size_t range); // next action
238  bool return__(); // 'return' from codeblock
239  size_t matched__(size_t ch); // handles a matched rule
240  size_t getRange__(int ch); // convert char to range
241  size_t get__(); // next character
242  size_t state__() const; // current state
243  void continue__(int ch); // handles a transition
244  void echoCh__(size_t ch); // echoes ch, sets d_atBOL
245  void echoFirst__(size_t ch); // handles unknown input
246  void updateFinals__(); // update a state's Final info
247  void noReturn__(); // d_return to false
248  void print__() const; // optionally print token
249  void pushFront__(size_t ch); // return char to Input
250  void reset__(); // prepare for new cycle
251  // next input stream:
252  void switchStream__(std::istream &in, size_t lineNr);
253  void lopf__(size_t tail); // matched fixed size tail
254  void lop1__(int lopSC); // matched ab for a/b
255  void lop2__(); // matches the LOP's b tail
256  void lop3__(); // catch-all while matching b
257  void lop4__(); // matches the LOP's a head
258 
259 private:
260  size_t getInput();
261  size_t getLOP();
262  void p_pushStream(std::string const &name, std::istream *streamPtr);
263  void setMatchedSize(size_t length);
264  bool knownFinalState();
265 
266  template <typename ReturnType, typename ArgType>
267  static ReturnType constexpr as(ArgType value);
268  static bool constexpr available(size_t value);
269  static StartCondition__ constexpr SC(int sc);
270  static int constexpr SC(StartCondition__ sc);
271 };
272 
273 template <typename ReturnType, typename ArgType>
274 inline ReturnType constexpr CommandScannerBase::as(ArgType value)
275 {
276  return static_cast<ReturnType>(value);
277 }
278 
279 inline bool CommandScannerBase::knownFinalState()
280 {
281  return (d_atBOL && available(d_final.bol.rule)) ||
282  available(d_final.std.rule);
283 }
284 
285 inline bool constexpr CommandScannerBase::available(size_t value)
286 {
287  return value != std::numeric_limits<size_t>::max();
288 }
289 
290 inline CommandScannerBase::StartCondition__ constexpr CommandScannerBase::SC(int sc)
291 {
292  return as<StartCondition__>(sc);
293 }
294 
295 inline int constexpr CommandScannerBase::SC(StartCondition__ sc)
296 {
297  return as<int>(sc);
298 }
299 
300 inline std::ostream &CommandScannerBase::out()
301 {
302  return *d_out;
303 }
304 
305 inline void CommandScannerBase::push(size_t ch)
306 {
307  d_input.reRead(ch);
308 }
309 
310 inline void CommandScannerBase::push(std::string const &str)
311 {
312  d_input.reRead(str, 0);
313 }
314 
315 inline void CommandScannerBase::setFilename(std::string const &name)
316 {
317  d_filename = name;
318 }
319 
320 inline void CommandScannerBase::setMatched(std::string const &text)
321 {
322  d_matched = text;
323 }
324 
325 inline std::string const &CommandScannerBase::matched() const
326 {
327  return d_matched;
328 }
329 
330 inline CommandScannerBase::StartCondition__ CommandScannerBase::startCondition() const
331 {
332  return SC(d_startCondition);
333 }
334 
335 inline std::string const &CommandScannerBase::filename() const
336 {
337  return d_filename;
338 }
339 
340 inline void CommandScannerBase::echo() const
341 {
342  *d_out << d_matched;
343 }
344 
345 inline size_t CommandScannerBase::length() const
346 {
347  return d_matched.size();
348 }
349 
350 inline void CommandScannerBase::leave(int retValue) const
351 {
352  throw as<Leave__>(retValue);
353 }
354 
355 inline size_t CommandScannerBase::lineNr() const
356 {
357  return d_input.lineNr();
358 }
359 
360 inline void CommandScannerBase::more()
361 {
362  d_more = true;
363 }
364 
365 inline void CommandScannerBase::begin(StartCondition__ startCondition)
366 {
367  // d_state is reset to 0 by reset__()
368  d_dfaBase__ = s_dfaBase__[d_startCondition = SC(startCondition)];
369 }
370 
371 inline size_t CommandScannerBase::state__() const
372 {
373  return d_state;
374 }
375 
376 inline size_t CommandScannerBase::get__()
377 {
378  return (this->*d_get)();
379 }
380 
381 inline size_t CommandScannerBase::getInput()
382 {
383  return d_input.get();
384 }
385 
386 inline bool CommandScannerBase::return__()
387 {
388  return d_return;
389 }
390 
391 inline void CommandScannerBase::noReturn__()
392 {
393  d_return = false;
394 }
395 
396 // $insert namespace-close
397 }
398 
399 #endif // CommandScannerBASE_H_INCLUDED
Definition: json.hpp:8681
Definition: CommandParser.h:50