Xmipp  v3.23.11-Nereus
Public Member Functions | List of all members
ArgLexer Class Reference

#include <argsparser.h>

Public Member Functions

 ArgLexer ()
 
 ~ArgLexer ()
 
void addLine (const String &line)
 
bool nextToken ()
 
ArgTokencurrentToken () const
 
ArgTokenType lookahead () const
 

Detailed Description

This class will split the input stream into tokens. The tokens will be further used by the Parser to build the syntax tree.

Definition at line 94 of file argsparser.h.

Constructor & Destructor Documentation

◆ ArgLexer()

ArgLexer::ArgLexer ( )

Constructor

Definition at line 81 of file argsparser.cpp.

82 {
83  line = 0;
84  pos = 0;
85  pToken = new ArgToken();
86  //Initialize reserved words dictionary
87  reservedWords["WHERE"] = TOK_WHERE;
88  reservedWords["ALIAS"] = TOK_ALIAS;
89  reservedWords["OR"] = TOK_OR;
90  reservedWords["REQUIRES"] = TOK_REQUIRES;
91 }
Reserved words.
Definition: argsparser.h:62

◆ ~ArgLexer()

ArgLexer::~ArgLexer ( )

Destructor

Definition at line 93 of file argsparser.cpp.

94 {
95  delete pToken;
96 }

Member Function Documentation

◆ addLine()

void ArgLexer::addLine ( const String line)

Add input lines to the lexer

Definition at line 99 of file argsparser.cpp.

100 {
101  input.push_back(line + " ");
102 #ifdef DEBUG
103  std::cout << input.size() << ": " << line << std::endl;
104 #endif
105 }

◆ currentToken()

ArgToken * ArgLexer::currentToken ( ) const

Definition at line 337 of file argsparser.cpp.

338 {
339  return pToken;
340 }

◆ lookahead()

ArgTokenType ArgLexer::lookahead ( ) const

Definition at line 341 of file argsparser.cpp.

342 {
343  return pToken->type;
344 }
ArgTokenType type
Type of the token.
Definition: argsparser.h:75

◆ nextToken()

bool ArgLexer::nextToken ( )

Function to parse a new token. If the token is TOK_END will return false and true otherwise. The current token will be changed.

Definition at line 164 of file argsparser.cpp.

165 {
166  //make it visible by default
167  pToken->visibility = 0;
168  pToken->starred = false;
169 
170  if (line == input.size())
171  {
172  setupToken(TOK_END);
173  return false;
174  }
175 
176  char c = input[line][pos];
177 
178  //Skip all white spaces
179  while (line < input.size() && pos < input[line].length() && isspace(c
180  = input[line][pos]))
181  {
182  ++pos;
183  if (c == '\0' || c == '\n' || pos == input[line].length()) //Next line
184  {
185  nextLine();
186  }
187  }
188 
189  if (line == input.size())
190  setupToken(TOK_END);
191  else if (isalpha(c) || c == '_')
192  {
193  offset = 0;
194  while (isalnum(c) || c == '_')
195  {
196  ++offset;
197  c = input[line][pos + offset];
198  }
199  setupToken(TOK_ID);
200  }
201  else if (isalnum(c) || (c == '-' && isdigit(input[line][pos + 1])))
202  {
203  offset = 1;
204  ArgTokenType t = TOK_INT;
205  while (isdigit(input[line][pos + offset]))
206  ++offset;
207  if (input[line][pos + offset] == '.')
208  {
209  ++offset;
210  while (isdigit(input[line][pos + offset]))
211  ++offset;
212  t = TOK_FLOAT;
213  }
214  if (input[line][pos + offset] == 'e')
215  {
216  ++offset;
217  if (input[line][pos + offset] != '+' && input[line][pos + offset]
218  != '-')
219  {
220  std::cerr << "ERROR: expected '+' or '-' " << std::endl
221  << "at line: " << line + 1 << " pos: " << offset + 1
222  << std::endl;
223  exit(1);
224  }
225  ++offset;
226  while (isdigit(input[line][pos + offset]))
227  ++offset;
228  t = TOK_FLOAT;
229 
230  }
231  setupToken(t);
232  }
233  else
234  {
235  offset = 1;
236  bool empty;
237  switch (c)
238  {
239  case '<':
240  setupToken(TOK_LAN);
241  break;
242  case '>':
243  setupToken(TOK_RAN);
244  break;
245  case '=':
246  if (input[line][pos + offset] == '=')
247  {
248  pos = input[line].find_first_not_of('=', pos + 1);
249  checkVisibility();
250  offset = input[line].find_first_of('=', pos + 1);
251  offset -= pos;
252  setupToken(TOK_SECTION);
253  nextLine();
254  }
255  else
256  //simple equal sign '='
257  setupToken(TOK_EQ);
258  break;
259  case '[':
260  setupToken(TOK_LBRA);
261  break;
262  case ']':
263  setupToken(TOK_RBRA);
264  break;
265  case ';':
266  setupToken(TOK_SEMI);
267  break;
268  case ',':
269  setupToken(TOK_COMMA);
270  break;
271  case '+':
272  setupToken(TOK_PLUS);
273  break;
274  case '.':
275  if (input[line][pos + offset] == '.' &&
276  input[line][pos + offset + 1] == '.')
277  {
278  offset += 2;
279  setupToken(TOK_ETC);
280  }
281  break;
282  case ':':
283  ++pos;
284 
285  checkVisibility();
286 
287  offset = input[line].find_first_of("\n\r", pos);
288  offset -= pos;
289  setupToken(TOK_COMM);
290  nextLine();
291  break;
292  case '-':
293  if (input[line][pos + offset] == '-')
294  ++offset;
295  empty = true;
296  c = input[line][pos + offset];
297  if (isalpha(c) || c == '_') //should start with letter or _
298  {
299  while (isalnum(c) || c == '_')
300  {
301  empty = false;
302  ++offset;
303  c = input[line][pos + offset];
304  }
305  }
306  if (empty)
307  {
308  std::cerr << "ERROR: Params should be of the form -ID or --ID"
309  << std::endl;
310  exit(1);
311  }
312  setupToken(TOK_OPT);
313  checkVisibility();
314  checkIndependent();
315  break;
316  case '"':
317  offset = input[line].find_first_of('"', pos + 1);
318  ++pos;
319  offset -= pos;
320  setupToken(TOK_STR);
321  break;
322  default:
323  std::cerr << "ERROR: Unexpected character '" << c << "'"
324  << std::endl << "at line: " << line + 1 << " pos: " << pos
325  + 1 << std::endl;
326  std::cerr << "WRONG LINE: " << input[line] << std::endl;
327  exit(1);
328  }
329  }
330 
331  //ConsolePrinter * cp = new ConsolePrinter();
332  //cp->printToken(pToken);
333 
334  return true;
335 }
doublereal * c
int visibility
Definition: argsparser.h:83
bool starred
Some special mark to tokens.
Definition: argsparser.h:85
ArgTokenType
Definition: argsparser.h:43
__host__ __device__ float length(float2 v)

The documentation for this class was generated from the following files: