42 |
|
#ifndef SELECTION_TOKEN_HPP |
43 |
|
#define SELECTION_TOKEN_HPP |
44 |
|
|
45 |
+ |
#include <map> |
46 |
+ |
#include <string> |
47 |
+ |
#include <utility> |
48 |
+ |
#include "utils/any.hpp" |
49 |
|
|
50 |
|
namespace oopse { |
51 |
|
|
53 |
|
/** |
54 |
|
* @class Token |
55 |
|
* @todo document |
56 |
+ |
* @note translate from jmol |
57 |
|
*/ |
58 |
|
class Token { |
59 |
|
|
60 |
|
public: |
61 |
|
|
62 |
+ |
|
63 |
|
int tok; |
64 |
+ |
boost::any value; |
65 |
|
int intValue; |
66 |
+ |
|
67 |
+ |
Token() { tok = unknown;} |
68 |
+ |
|
69 |
+ |
Token(const Token& token) { |
70 |
+ |
*this = token; |
71 |
+ |
} |
72 |
|
|
73 |
+ |
Token(int tok, int intValue, const boost::any& value) { |
74 |
+ |
this->tok = tok; |
75 |
+ |
this->intValue = intValue; |
76 |
+ |
this->value = value; |
77 |
+ |
} |
78 |
+ |
|
79 |
+ |
Token(int tok, int intValue) { |
80 |
+ |
this->tok = tok; |
81 |
+ |
this->intValue = intValue; |
82 |
+ |
} |
83 |
+ |
|
84 |
+ |
Token(int tok) { |
85 |
+ |
this->tok = tok; |
86 |
+ |
} |
87 |
+ |
|
88 |
+ |
Token(int tok, const boost::any& value) { |
89 |
+ |
this->tok = tok; |
90 |
+ |
this->value = value; |
91 |
+ |
} |
92 |
+ |
|
93 |
|
const static int nada = 0; |
94 |
|
const static int identifier = 1; |
95 |
|
const static int integer = 2; |
96 |
|
const static int decimal = 3; |
97 |
|
const static int string = 4; |
98 |
< |
const static int seqcode = 5; |
99 |
< |
const static int unknown = 6; |
100 |
< |
const static int keyword = 7; |
101 |
< |
const static int whitespace = 8; |
102 |
< |
const static int comment = 9; |
103 |
< |
const static int endofline = 10; |
71 |
< |
const static int endofstatement = 11; |
98 |
> |
const static int unknown = 5; |
99 |
> |
const static int keyword = 6; |
100 |
> |
const static int whitespace = 7; |
101 |
> |
const static int comment = 8; |
102 |
> |
const static int endofline = 9; |
103 |
> |
const static int endofstatement = 10; |
104 |
|
|
105 |
|
const static int command = (1 << 8); |
106 |
|
const static int expressionCommand = (1 << 9); // expression command |
107 |
< |
const static int expression = (1 << 15); /// expression term |
107 |
> |
const static int expression = (1 << 10); /// expression term |
108 |
|
|
77 |
– |
// every property is also valid in an expression context |
78 |
– |
const static int atomproperty = (1 << 16) | expression; |
79 |
– |
// every predefined is also valid in an expression context |
80 |
– |
const static int comparator = (1 << 17) | expression; |
81 |
– |
const static int predefinedset = (1 << 18) | expression; |
109 |
|
// generally, the minus sign is used to denote atom ranges |
110 |
|
// this property is used for the few commands which allow negative integers |
111 |
< |
const static int negnums = (1 << 21); |
85 |
< |
// for some commands the 'set' is optional |
111 |
> |
const static int negnums = (1 << 11); |
112 |
|
|
113 |
< |
// These are unrelated |
114 |
< |
const static int varArgCount = (1 << 4); |
115 |
< |
const static int onDefault1 = (1 << 5) | 1; |
116 |
< |
|
113 |
> |
// every property is also valid in an expression context |
114 |
> |
const static int atomproperty = (1 << 12) | expression | negnums; |
115 |
> |
// every predefined is also valid in an expression context |
116 |
> |
const static int comparator = (1 << 13) | expression; |
117 |
> |
const static int predefinedset = (1 << 14) | expression; |
118 |
> |
const static int embeddedExpression= (1 << 15); // embedded expression |
119 |
> |
|
120 |
|
// rasmol commands |
121 |
|
const static int define = command | expressionCommand |1; |
122 |
|
const static int select = command |expressionCommand |2 ; |
94 |
– |
const static int all = expression | 11 ; |
123 |
|
|
124 |
+ |
//predefine |
125 |
+ |
//const static int selected = predefinedset |0; |
126 |
+ |
|
127 |
|
// atom expression operators |
128 |
|
const static int leftparen = expression | 0; |
129 |
|
const static int rightparen = expression | 1; |
132 |
|
const static int opOr = expression | 4; |
133 |
|
const static int opNot = expression | 5; |
134 |
|
const static int within = expression | 6; |
135 |
< |
const static int plus = expression | 7; |
136 |
< |
const static int pick = expression | 8; |
137 |
< |
const static int asterisk = expression | 9; |
138 |
< |
const static int dot = expression | 11; |
139 |
< |
const static int leftsquare = expression | 12; |
109 |
< |
const static int rightsquare = expression | 13; |
110 |
< |
const static int colon = expression | 14; |
111 |
< |
const static int slash = expression | 15; |
135 |
> |
const static int asterisk = expression | 7; |
136 |
> |
const static int dot = expression | 8; |
137 |
> |
const static int all = expression | 9 ; |
138 |
> |
const static int none = expression | 10; |
139 |
> |
const static int name = expression |11; |
140 |
|
|
141 |
|
// miguel 2005 01 01 |
142 |
|
// these are used to demark the beginning and end of expressions |
145 |
|
const static int expressionBegin = expression | 100; |
146 |
|
const static int expressionEnd = expression | 101; |
147 |
|
|
148 |
< |
const static int atomno = atomproperty | 0; |
149 |
< |
const static int elemno = atomproperty | 1; |
150 |
< |
const static int resno = atomproperty | 2; |
151 |
< |
const static int radius = atomproperty | 3 ; |
124 |
< |
const static int _bondedcount = atomproperty | 6; |
125 |
< |
const static int _groupID = atomproperty | 7; |
126 |
< |
const static int _atomID = atomproperty | 8; |
127 |
< |
const static int _structure = atomproperty | 9; |
128 |
< |
const static int occupancy = atomproperty | 10; |
129 |
< |
const static int polymerLength= atomproperty | 11; |
130 |
< |
|
148 |
> |
const static int mass = atomproperty | 0; |
149 |
> |
const static int charge = atomproperty | 1; |
150 |
> |
const static int dipole = atomproperty | 2; |
151 |
> |
|
152 |
|
const static int opGT = comparator | 0; |
153 |
|
const static int opGE = comparator | 1; |
154 |
|
const static int opLE = comparator | 2; |
156 |
|
const static int opEQ = comparator | 4; |
157 |
|
const static int opNE = comparator | 5; |
158 |
|
|
159 |
< |
const static int x = expression |2; |
160 |
< |
const static int y = expression | 3; |
140 |
< |
const static int z = expression | 4; |
141 |
< |
const static int none = expression | 5; |
142 |
< |
|
143 |
< |
|
144 |
< |
const static Token tokenAll(all, "all"); |
145 |
< |
const static Token tokenAnd(opAnd, "and"); |
146 |
< |
const static Token tokenElemno(elemno, "elemno"); |
147 |
< |
const static Token tokenExpressionBegin(expressionBegin, "expressionBegin"); |
148 |
< |
const static Token tokenExpressionEnd(expressionEnd, "expressionEnd"); |
159 |
> |
static Token tokenExpressionBegin; |
160 |
> |
static Token tokenExpressionEnd; |
161 |
|
|
150 |
– |
|
151 |
– |
const static String[] comparatorNames = {">", ">=", "<=", "<", "=", "!="}; |
152 |
– |
const static String[] atomPropertyNames = { |
153 |
– |
"atomno", "elemno", "resno", "radius", "temperature", "model", |
154 |
– |
"_bondedcount", "_groupID", "_atomID", "_structure"}; |
155 |
– |
|
156 |
– |
/* |
157 |
– |
Note that the RasMol scripting language is case-insensitive. |
158 |
– |
So, the compiler turns all identifiers to lower-case before |
159 |
– |
looking up in the hash table. |
160 |
– |
Therefore, the left column of this array *must* be lower-case |
161 |
– |
*/ |
162 |
– |
|
163 |
– |
const static Object[] arrayPairs = { |
164 |
– |
// commands |
165 |
– |
"define", new Token(define, varArgCount, "define"), |
166 |
– |
"select", new Token(select, varArgCount, "select"), |
167 |
– |
// atom expressions |
168 |
– |
"(", new Token(leftparen, "("), |
169 |
– |
")", new Token(rightparen, ")"), |
170 |
– |
"-", new Token(hyphen, "-"), |
171 |
– |
"and", tokenAnd, |
172 |
– |
"&", null, |
173 |
– |
"&&", null, |
174 |
– |
"or", new Token(opOr, "or"), |
175 |
– |
",", null, |
176 |
– |
"|", null, |
177 |
– |
"||", null, |
178 |
– |
"not", new Token(opNot, "not"), |
179 |
– |
"!", null, |
180 |
– |
"<", new Token(opLT, "<"), |
181 |
– |
"<=", new Token(opLE, "<="), |
182 |
– |
">=", new Token(opGE, ">="), |
183 |
– |
">", new Token(opGT, ">="), |
184 |
– |
"==", new Token(opEQ, "=="), |
185 |
– |
"=", null, |
186 |
– |
"!=", new Token(opNE, "!="), |
187 |
– |
"<>", null, |
188 |
– |
"/=", null, |
189 |
– |
"within", new Token(within, "within"), |
190 |
– |
"+", new Token(plus, "+"), |
191 |
– |
"pick", new Token(pick, "pick"), |
192 |
– |
".", new Token(dot, "."), |
193 |
– |
"[", new Token(leftsquare, "["), |
194 |
– |
"]", new Token(rightsquare, "]"), |
195 |
– |
":", new Token(colon, ":"), |
196 |
– |
"/", new Token(slash, "/"), |
197 |
– |
|
198 |
– |
"atomno", new Token(atomno, "atomno"), |
199 |
– |
"elemno", tokenElemno, |
200 |
– |
"_e", tokenElemno, |
201 |
– |
"resno", new Token(resno, "resno"), |
202 |
– |
"temperature", new Token(temperature, "temperature"), |
203 |
– |
"relativetemperature", null, |
204 |
– |
"_bondedcount", new Token(_bondedcount, "_bondedcount"), |
205 |
– |
"_groupID", new Token(_groupID, "_groupID"), |
206 |
– |
"_g", null, |
207 |
– |
"_atomID", new Token(_atomID, "_atomID"), |
208 |
– |
"_a", null, |
209 |
– |
"_structure", new Token(_structure, "_structure"), |
210 |
– |
"occupancy", new Token(occupancy, "occupancy"), |
211 |
– |
"polymerlength",new Token(polymerLength, "polymerlength"), |
212 |
– |
|
213 |
– |
"x", new Token(x, "x"), |
214 |
– |
"y", new Token(y, "y"), |
215 |
– |
"z", new Token(z, "z"), |
216 |
– |
"*", new Token(asterisk, "*"), |
217 |
– |
"all", tokenAll, |
218 |
– |
"none", new Token(none, "none"), |
219 |
– |
|
220 |
– |
}; |
221 |
– |
|
222 |
– |
|
223 |
– |
static Hashtable map = new Hashtable(); |
224 |
– |
static { |
225 |
– |
Token tokenLast = null; |
226 |
– |
String stringThis; |
227 |
– |
Token tokenThis; |
228 |
– |
for (int i = 0; i + 1 < arrayPairs.length; i += 2) { |
229 |
– |
stringThis = (String) arrayPairs[i]; |
230 |
– |
tokenThis = (Token) arrayPairs[i + 1]; |
231 |
– |
if (tokenThis == null) |
232 |
– |
tokenThis = tokenLast; |
233 |
– |
if (map.get(stringThis) != null) |
234 |
– |
System.out.println("duplicate token definition:" + stringThis); |
235 |
– |
map.put(stringThis, tokenThis); |
236 |
– |
tokenLast = tokenThis; |
237 |
– |
} |
238 |
– |
} |
239 |
– |
|
240 |
– |
|
162 |
|
}; |
163 |
|
|
164 |
+ |
|
165 |
|
} |
166 |
|
|
167 |
< |
#endif |
167 |
> |
#endif |