HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1#ifndef PARSER_H
2#define PARSER_H
3
4#ifdef WIN32
5
6// disable warnings about long names
7 #pragma warning( disable : 4786)
8
9#endif
10
11
12#include <string>
13#include <map>
14#include <stdexcept>
15#include <sstream>
16
17#include <cmath>
18#include <ctime>
19#include <cstdlib>
20
21namespace hipo {
22
23
24class Parser
25 {
26
27 public:
28
30 {
35 PLUS='+',
36 MINUS='-',
38 DIVIDE='/',
39 ASSIGN='=',
42 COMMA=',',
43 NOT='!',
44
45 // comparisons
46 LT='<',
47 GT='>',
48 LE, // <=
49 GE, // >=
50 EQ, // ==
51 NE, // !=
52 AND, // &&
53 OR, // ||
54
55 // special assignments
56
60 ASSIGN_DIV // +/
61
62 };
63
64 private:
65
66 std::string program_;
67
68 const char * pWord_;
69 const char * pWordStart_;
70 // last token parsed
71 TokenType type_;
72 std::string word_;
73 double value_;
74
75 public:
76
77 // ctor
78 Parser (const std::string & program)
79 : program_ (program)
80 {
81 // insert pre-defined names:
82 symbols_ ["pi"] = 3.1415926535897932385;
83 symbols_ ["e"] = 2.7182818284590452354;
84 }
85
86 const double Evaluate (); // get result
87 const double Evaluate (const std::string & program); // get result
88
89 // access symbols with operator []
90 double & operator[] (const std::string & key) { return symbols_ [key]; }
91
92 // symbol table - can be accessed directly (eg. to copy a batch in)
93 std::map<std::string, double> symbols_;
94
95 private:
96
97 const TokenType GetToken (const bool ignoreSign = false);
98 const double CommaList (const bool get);
99 const double Expression (const bool get);
100 const double Comparison (const bool get);
101 const double AddSubtract (const bool get);
102 const double Term (const bool get); // multiply and divide
103 const double Primary (const bool get); // primary (base) tokens
104
105 inline void CheckToken (const TokenType wanted)
106 {
107 if (type_ != wanted)
108 {
109 std::ostringstream s;
110 s << "'" << static_cast <char> (wanted) << "' expected.";
111 throw std::runtime_error (s.str ());
112 }
113 }
114 }; // end of Parser
115}
116
117#endif // PARSER_H
118
Definition parser.h:25
std::map< std::string, double > symbols_
Definition parser.h:93
TokenType
Definition parser.h:30
@ LHPAREN
Definition parser.h:40
@ NE
Definition parser.h:51
@ RHPAREN
Definition parser.h:41
@ NOT
Definition parser.h:43
@ EQ
Definition parser.h:50
@ ASSIGN_DIV
Definition parser.h:60
@ DIVIDE
Definition parser.h:38
@ MULTIPLY
Definition parser.h:37
@ NUMBER
Definition parser.h:33
@ END
Definition parser.h:34
@ OR
Definition parser.h:53
@ GE
Definition parser.h:49
@ AND
Definition parser.h:52
@ NAME
Definition parser.h:32
@ LT
Definition parser.h:46
@ ASSIGN_MUL
Definition parser.h:59
@ COMMA
Definition parser.h:42
@ ASSIGN_SUB
Definition parser.h:58
@ LE
Definition parser.h:48
@ NONE
Definition parser.h:31
@ GT
Definition parser.h:47
@ MINUS
Definition parser.h:36
@ PLUS
Definition parser.h:35
@ ASSIGN_ADD
Definition parser.h:57
@ ASSIGN
Definition parser.h:39
double & operator[](const std::string &key)
Definition parser.h:90
const double Evaluate()
Definition parser.cpp:649
Parser(const std::string &program)
Definition parser.h:78
HIPO namespace is used for the classes that read/write files and records.
Definition bank.cpp:45