Debian Codegraph @

py/ymport/foamfile/Token.hpp

raw
1 /*************************************************************************
2 * 2022 Tóth János *
3 * *
4 * This program is free software; it is licensed under the terms of the *
5 * GNU General Public License v2 or later. See file LICENSE for details. *
6 *************************************************************************/
7
8 // Token is a lexical unit that the lexer outputs for the parser from the input characters.
9 // E.g.: "format ascii;" -> WORD WORD ';'
10 // characters tokens
11 //
12 // Valid tokens are:
13 // + EMPTY (default)
14 // + WORD (one or more characters)
15 // + CHAR (one character)
16 // + INT (integer number)
17 // + DOUBLE (double precision floating point number)
18 // + END (last token of the file)
19
20 #pragma once
21
22 #include "VerifyMacros.hpp"
23
24 #include <string>
25
26 namespace yade {
27 namespace ymport {
28 namespace foamfile {
29
30 class Token {
31 public:
32 enum Type { EMPTY = 0, WORD, CHAR, INT, DOUBLE, END };
33
34 // NOTE: It wolud be nice to include the "string (word)" value of the token as well
35 // in form of a "const char * s", create it with strdup() and release it with free().
36 // I tried to do that, but not sure when or where to use "free()".
37 union Value {
38 double d;
39 int i;
40 char c;
41 };
42
43 Token(Type type = Type::EMPTY)
44 {
45 if (type == Type::END) {
46 m_type = Type::END;
47 } else {
48 m_type = Type::EMPTY;
49 }
50 }
51
52 Token(const std::string& s)
53 : m_type(Type::WORD)
54 , m_str_value(s)
55 {
56 }
57
58 Token(char c)
59 : m_type(Type::CHAR)
60 {
61 m_value.c = c;
62 }
63
64 Token(int i)
65 : m_type(Type::INT)
66 {
67 m_value.i = i;
68 }
69
70 Token(double d)
71 : m_type(Type::DOUBLE)
72 {
73 m_value.d = d;
74 }
75
76 Type type() const { return m_type; }
77
78 bool isEmpty() const { return m_type == Type::EMPTY; }
79
80 bool isEnd() const { return m_type == Type::END; }
81
82 bool isWord() const { return m_type == Type::WORD; }
83
84 bool isChar() const { return m_type == Type::CHAR; }
85
86 bool isInt() const { return m_type == Type::INT; }
87
88 bool isDouble() const { return m_type == Type::DOUBLE; }
89
90 bool isNumber() const { return isInt() || isDouble(); }
91
92 const std::string& asWord() const
93 {
94 VERIFY(isWord());
95 return m_str_value;
96 };
97
98 char asChar() const
99 {
100 VERIFY(isChar());
101 return m_value.c;
102 };
103
104 int asInt() const
105 {
106 VERIFY(isInt());
107 return m_value.i;
108 };
109
110 double asDouble() const
111 {
112 VERIFY(isDouble());
113 return m_value.d;
114 };
115
116 double asNumber() const
117 {
118 VERIFY(isNumber());
119 if (isInt()) {
120 return (double)asInt();
121 } else {
122 return asDouble();
123 }
124 };
125
126 bool isEqual(const Token& t)
127 {
128 if (t.isWord()) {
129 return asWordEquals(t.asWord());
130 } else if (t.isInt()) {
131 return asIntEquals(t.asInt());
132 } else if (t.isChar()) {
133 return asCharEquals(t.asChar());
134 } else if (t.isDouble()) {
135 return asDoubleEquals(t.asDouble());
136 } else if (t.type() == Type::EMPTY || t.type() == Type::END) {
137 return type() == t.type();
138 } else {
139 UNREACHABLE();
140 return false;
141 }
142 }
143
144 bool asWordEquals(const std::string& s) { return isWord() && asWord() == s; }
145
146 bool asCharEquals(char c) { return isChar() && asChar() == c; }
147
148 bool asIntEquals(int i) { return isInt() && asInt() == i; }
149
150 bool asDoubleEquals(double d) { return isDouble() && asDouble() == d; }
151
152 bool asNumberEquals(double d) { return isNumber() && asNumber() == d; }
153
154 std::string debugString() const
155 {
156 switch (m_type) {
157 case Type::EMPTY: return "EMPTY";
158
159 case Type::WORD: return "WORD(" + asWord() + ")";
160
161 case Type::CHAR: return "CHAR(" + std::string(1, asChar()) + ")";
162
163 case Type::INT: return "INT(" + std::to_string(asInt()) + ")";
164
165 case Type::DOUBLE: return "DOUBLE(" + std::to_string(asDouble()) + ")";
166
167 case Type::END: return "END";
168
169 default: UNREACHABLE(); return "";
170 }
171 }
172
173 private:
174 Type m_type { EMPTY };
175 Value m_value {};
176 std::string m_str_value;
177 };
178
179 } // namespace foamfile
180 } // namespace ymport
181 } // namespace yade