1 
2 #pragma once
3 
4 #include <string>
5 
6 namespace pdal
7 {
8 namespace expr
9 {
10 
11 enum class TokenType
12 {
13     Eof,
14     Error,
15 
16     Assign,
17 
18     Plus,
19     Dash,
20     Slash,
21     Asterisk,
22 
23     Lparen,
24     Rparen,
25 
26     Not,
27     Or,
28     And,
29     Greater,
30     Less,
31     Equal,
32     NotEqual,
33     LessEqual,
34     GreaterEqual,
35 
36     Number,
37     Identifier
38 };
39 
40 class Token
41 {
42     friend class Lexer;
43 
44     // Union with string requires a bunch of muck, so...
45     struct Value
46     {
47         double d;
48         std::string s;
49     };
50 
51 public:
Token(TokenType type,std::string::size_type start,std::string::size_type end,const std::string & s,double d=0)52     Token(TokenType type, std::string::size_type start,
53             std::string::size_type end, const std::string& s, double d = 0) :
54         m_type(type), m_start(start), m_end(end)
55     {
56         m_val.s = s;
57         m_val.d = d;
58     }
59 
60 
Token(TokenType type,std::string::size_type start,std::string::size_type end)61     Token(TokenType type, std::string::size_type start,
62             std::string::size_type end) :
63         m_type(type), m_start(start), m_end(end)
64     {}
Token(TokenType type)65     Token(TokenType type) : m_type(type), m_start(0), m_end(0)
66     {}
Token()67     Token() : m_type(TokenType::Error), m_start(0), m_end(0)
68     {}
69 
type() const70     TokenType type() const
71     { return m_type; }
72 
start() const73     std::string::size_type start() const
74     { return m_start; }
75 
end() const76     std::string::size_type end() const
77     { return m_end; }
78 
valid() const79     bool valid() const
80     { return m_type != TokenType::Error; }
81 
dval() const82     double dval() const
83     { return m_val.d; }
84 
sval() const85     std::string sval() const
86     { return m_val.s; }
87 
operator bool() const88     operator bool () const
89     { return valid() && m_type != TokenType::Eof; }
90 
operator ==(TokenType type) const91     bool operator == (TokenType type) const
92     { return m_type == type; }
93 
operator ==(const Token & other) const94     bool operator == (const Token& other) const
95     { return m_type == other.m_type; }
96 
operator !=(TokenType type) const97     bool operator != (TokenType type) const
98     { return m_type != type; }
99 
operator !=(const Token & other) const100     bool operator != (const Token& other) const
101     { return m_type != other.m_type; }
102 
103 private:
104     TokenType m_type;
105     std::string::size_type m_start;
106     std::string::size_type m_end;
107     Value m_val;
108 };
109 
110 } // namespace expr
111 } // namespace pdal
112