/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/db/include/RAT/DBTextLoader.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/db/include/RAT/DBTextLoader.hh Source File
Ratpac-two
DBTextLoader.hh
1 #ifndef __RAT_DBTextLoader__
2 #define __RAT_DBTextLoader__
3 
4 #include <RAT/DBExceptions.hh>
5 #include <iostream>
6 #include <sstream>
7 #include <stack>
8 #include <string>
9 #include <vector>
10 
11 namespace RAT {
12 
13 class DBTable;
14 
16 class Tokenizer {
17  public:
18  enum Type {
19  TYPE_IDENTIFIER,
20  TYPE_INTEGER,
21  TYPE_DOUBLE,
22  TYPE_STRING,
23  TYPE_SYMBOL,
24  TYPE_EOF,
25  TYPE_ERROR,
26  TYPE_EOF_ERROR
27  };
28 
29  Tokenizer(std::string doc, std::string _filename = "");
30  Tokenizer::Type Next();
31  std::string Token() { return token; };
32  int AsInt() { return intval; };
33  double AsDouble() { return doubleval; };
34 
35  void RaiseError(std::string message);
36  void RaiseProbablyJSONError(std::string message);
37 
38  std::string GetFilename() const { return filename; };
39 
40  inline bool Eof() { return eof; };
41 
42  static bool IsValType(Tokenizer::Type type) {
43  return type == TYPE_STRING || type == TYPE_INTEGER || type == TYPE_DOUBLE;
44  };
45 
46  protected:
47  enum State {
48  STATE_START,
49  STATE_COMMENT,
50  STATE_COMMENT_LONG,
51  STATE_COMMENT_LINE,
52  STATE_IDENTIFIER,
53  STATE_NUMBER,
54  STATE_HEX_NUMBER,
55  STATE_STRING,
56  STATE_STRING_ESC
57  };
58 
59  bool eof;
60  std::string doc;
61  std::string filename;
62 
63  int pos, len;
64  std::string token;
65  int intval;
66  double doubleval;
67 
68  int linenum;
69  int linestart;
70 
71  static const std::string number_char;
72  static const std::string symbol_char;
73  static const std::string quote_char;
74  static const std::string hex_char;
75 };
76 
79  public:
80  TokenizerStack(Tokenizer tokenizer) { stack.push(tokenizer); };
81 
82  void Push(Tokenizer tokenizer) { stack.push(tokenizer); };
83  Tokenizer &Top() { return stack.top(); };
84 
85  Tokenizer::Type Next();
86  std::string Token() { return stack.top().Token(); };
87  int AsInt() { return stack.top().AsInt(); };
88  double AsDouble() { return stack.top().AsDouble(); };
89 
90  void RaiseError(std::string message) { stack.top().RaiseError(message); };
91  void RaiseProbablyJSONError(std::string message) { stack.top().RaiseProbablyJSONError(message); };
92 
93  bool Eof() { return stack.top().Eof(); };
94 
95  std::string GetCurrentFilename() const { return stack.top().GetFilename(); };
96 
97  protected:
98  std::stack<Tokenizer> stack;
99 };
100 
102 class Parser {
103  public:
104  Parser(std::string str, std::string filename) : tokenizer(Tokenizer(str, filename)){};
105 
106  enum State {
107  STATE_START,
108  STATE_TABLE,
109  STATE_FIELD,
110  STATE_VAL_SEP,
111  STATE_VAL,
112  STATE_ARRAY,
113  STATE_ARRAY_VAL,
114  STATE_END
115  };
116 
117  DBTable *Next();
118 
119  protected:
120  TokenizerStack tokenizer;
121 };
122 
123 /* A $#%!! class with one static function to make rootcint happy */
125  public:
127  static std::vector<RAT::DBTable *> parse(std::string filename);
128 };
129 
130 } // namespace RAT
131 
132 #endif
Definition: DBTable.hh:25
Definition: DBTextLoader.hh:124
static std::vector< RAT::DBTable * > parse(std::string filename)
Definition: DBTextLoader.cc:529
Definition: DBTextLoader.hh:102
Definition: DBTextLoader.hh:78
Definition: DBTextLoader.hh:16
Definition: CCCrossSecMessenger.hh:29