All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scanner.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <string.h>
3 
4 #include "scanner.h"
5 
7  start(NULL),
8  current(NULL)
9 {}
10 
11 void uscript::Scanner::SetSource(const char *_start) {
12  start = _start;
13  current = _start;
14 }
15 
17  while (1) {
18  char c = *current;
19  switch (c) {
20  case '/': // comments
21  if (current[1] == '/') {
22  while (*current != '\n' && !IsAtEnd()) Advance();
23  }
24  else return;
25  break;
26  case '\n':
27  case ' ':
28  case '\r':
29  case '\t':
30  Advance();
31  break;
32  default:
33  return;
34  }
35  }
36 }
37 
38 static bool isDigit(char c) {
39  return c >= '0' && c <= '9';
40 }
41 
42 static bool isAlpha(char c) {
43  return (c >= 'a' && c <= 'z') ||
44  (c >= 'A' && c <= 'Z') ||
45  c == '_';
46 }
47 
49  SkipWhitespace();
50 
51  start = current;
52 
53  if (IsAtEnd()) {
54  return MakeToken(uscript::TOKEN_EOF);
55  }
56 
57  char c = Advance();
58 
59  if (isDigit(c)) return Number();
60  if (isAlpha(c)) return Identifier();
61 
62  switch (c) {
63  case '(': return MakeToken(uscript::TOKEN_LEFT_PAREN);
64  case ')': return MakeToken(uscript::TOKEN_RIGHT_PAREN);
65  case '{': return MakeToken(uscript::TOKEN_LEFT_BRACE);
66  case '}': return MakeToken(uscript::TOKEN_RIGHT_BRACE);
67  case ']': return MakeToken(uscript::TOKEN_RIGHT_BRACKET);
68  case '[': return MakeToken(uscript::TOKEN_LEFT_BRACKET);
69  case ';': return MakeToken(uscript::TOKEN_SEMICOLON);
70  case ',': return MakeToken(uscript::TOKEN_COMMA);
71  case '.': return MakeToken(uscript::TOKEN_DOT);
72  case '-': return MakeToken(uscript::TOKEN_MINUS);
73  case '+': return MakeToken(uscript::TOKEN_PLUS);
74  case '/': return MakeToken(uscript::TOKEN_SLASH);
75  case '*': return MakeToken(uscript::TOKEN_STAR);
76  case '!':
77  return MakeToken(Match('=') ? TOKEN_BANG_EQUAL : TOKEN_BANG);
78  case '=':
79  return MakeToken(Match('=') ? TOKEN_EQUAL_EQUAL : TOKEN_EQUAL);
80  case '<':
81  return MakeToken(Match('=') ? TOKEN_LESS_EQUAL : TOKEN_LESS);
82  case '>':
83  return MakeToken(Match('=') ?
85  case '"': return String();
86  }
87 
88  return ErrorToken("Unexpected character.");
89 }
90 
92  if (IsAtEnd()) return false;
93  if (*current != c) return false;
94 
95  current ++;
96  return true;
97 }
98 
100  current++;
101  return current[-1];
102 }
103 
105  return *current == '\0';
106 }
107 
109  uscript::Token token;
110  token.type = type;
111  token.start = start;
112  token.length = current - start;
113  return token;
114 }
115 
117  while (*current != '"' && !IsAtEnd()) {
118  Advance();
119  }
120 
121  if (IsAtEnd()) return ErrorToken("Unterminated string.");
122 
123  Advance();
124  return MakeToken(uscript::TOKEN_STRING);
125 }
126 
128  while (isDigit(*current)) Advance();
129 
130  if (*current == '.') { // && isDigit(current[1])) {
131  // consume the '.'
132  Advance();
133 
134  while (isDigit(*current)) Advance();
135  }
136 
137  return MakeToken(uscript::TOKEN_NUMBER);
138 }
139 
141  static const std::vector<std::string> keywords {"and", "or", "if", "else", "for", "while", "return", "true", "false", "fun", "nil", "var", "print", "length", "fields"};
142  static const std::vector<uscript::TokenType> keyword_types {
158  };
159  std::string comp = std::string(start, current);
160  for (unsigned i = 0; i < keywords.size(); i++) {
161  if (strcmp(comp.c_str(), keywords[i].c_str()) == 0) {
162  return keyword_types[i];
163  }
164  }
165 
167 }
168 
170  while (isAlpha(*current) || isDigit(*current)) Advance();
171 
172  return MakeToken(IdentifierType());
173 }
174 
176  uscript::Token token;
177  token.type = uscript::TOKEN_ERROR;
178  token.start = message;
179  token.length = (int)strlen(message);
180  return token;
181 }
Token ErrorToken(const char *message) const
Definition: scanner.cc:175
Token String()
Definition: scanner.cc:116
void SetSource(const char *_start)
Definition: scanner.cc:11
bool IsAtEnd() const
Definition: scanner.cc:104
bool Match(char c)
Definition: scanner.cc:91
TokenType IdentifierType() const
Definition: scanner.cc:140
TokenType type
Definition: scanner.h:39
Token Identifier()
Definition: scanner.cc:169
static bool isAlpha(char c)
Definition: scanner.cc:42
Token MakeToken(TokenType type) const
Definition: scanner.cc:108
Token ScanToken()
Definition: scanner.cc:48
TokenType
Definition: scanner.h:8
void SkipWhitespace()
Definition: scanner.cc:16
static bool isDigit(char c)
Definition: scanner.cc:38
Token Number()
Definition: scanner.cc:127
const char * start
Definition: scanner.h:40
process_name showerreco Particles Coinciding wih the Vertex services ScanOptions nu_mu services ScanOptions Number
char Advance()
Definition: scanner.cc:99