All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | List of all members
uscript::Scanner Class Reference

#include <scanner.h>

Public Member Functions

 Scanner ()
 
void SetSource (const char *_start)
 
Token ScanToken ()
 
Token MakeToken (TokenType type) const
 
Token ErrorToken (const char *message) const
 
bool IsAtEnd () const
 
char Advance ()
 
bool Match (char c)
 
void SkipWhitespace ()
 
Token String ()
 
Token Number ()
 
Token Identifier ()
 
TokenType IdentifierType () const
 

Private Attributes

const char * start
 
const char * current
 

Detailed Description

Definition at line 44 of file scanner.h.

Constructor & Destructor Documentation

uscript::Scanner::Scanner ( )

Definition at line 6 of file scanner.cc.

6  :
7  start(NULL),
8  current(NULL)
9 {}
const char * start
Definition: scanner.h:45
const char * current
Definition: scanner.h:46

Member Function Documentation

char uscript::Scanner::Advance ( )

Definition at line 99 of file scanner.cc.

99  {
100  current++;
101  return current[-1];
102 }
const char * current
Definition: scanner.h:46
uscript::Token uscript::Scanner::ErrorToken ( const char *  message) const

Definition at line 175 of file scanner.cc.

175  {
176  uscript::Token token;
177  token.type = uscript::TOKEN_ERROR;
178  token.start = message;
179  token.length = (int)strlen(message);
180  return token;
181 }
TokenType type
Definition: scanner.h:39
const char * start
Definition: scanner.h:40
uscript::Token uscript::Scanner::Identifier ( )

Definition at line 169 of file scanner.cc.

169  {
170  while (isAlpha(*current) || isDigit(*current)) Advance();
171 
172  return MakeToken(IdentifierType());
173 }
TokenType IdentifierType() const
Definition: scanner.cc:140
static bool isAlpha(char c)
Definition: scanner.cc:42
Token MakeToken(TokenType type) const
Definition: scanner.cc:108
static bool isDigit(char c)
Definition: scanner.cc:38
const char * current
Definition: scanner.h:46
char Advance()
Definition: scanner.cc:99
uscript::TokenType uscript::Scanner::IdentifierType ( ) const

Definition at line 140 of file scanner.cc.

140  {
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 }
const char * start
Definition: scanner.h:45
const char * current
Definition: scanner.h:46
bool uscript::Scanner::IsAtEnd ( ) const

Definition at line 104 of file scanner.cc.

104  {
105  return *current == '\0';
106 }
const char * current
Definition: scanner.h:46
uscript::Token uscript::Scanner::MakeToken ( uscript::TokenType  type) const

Definition at line 108 of file scanner.cc.

108  {
109  uscript::Token token;
110  token.type = type;
111  token.start = start;
112  token.length = current - start;
113  return token;
114 }
const char * start
Definition: scanner.h:45
TokenType type
Definition: scanner.h:39
const char * start
Definition: scanner.h:40
const char * current
Definition: scanner.h:46
bool uscript::Scanner::Match ( char  c)

Definition at line 91 of file scanner.cc.

91  {
92  if (IsAtEnd()) return false;
93  if (*current != c) return false;
94 
95  current ++;
96  return true;
97 }
bool IsAtEnd() const
Definition: scanner.cc:104
const char * current
Definition: scanner.h:46
uscript::Token uscript::Scanner::Number ( )

Definition at line 127 of file scanner.cc.

127  {
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 
138 }
Token MakeToken(TokenType type) const
Definition: scanner.cc:108
static bool isDigit(char c)
Definition: scanner.cc:38
const char * current
Definition: scanner.h:46
char Advance()
Definition: scanner.cc:99
uscript::Token uscript::Scanner::ScanToken ( )

Definition at line 48 of file scanner.cc.

48  {
50 
51  start = current;
52 
53  if (IsAtEnd()) {
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 '=':
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 }
Token ErrorToken(const char *message) const
Definition: scanner.cc:175
Token String()
Definition: scanner.cc:116
const char * start
Definition: scanner.h:45
bool IsAtEnd() const
Definition: scanner.cc:104
bool Match(char c)
Definition: scanner.cc:91
Token Identifier()
Definition: scanner.cc:169
static bool isAlpha(char c)
Definition: scanner.cc:42
Token MakeToken(TokenType type) const
Definition: scanner.cc:108
void SkipWhitespace()
Definition: scanner.cc:16
static bool isDigit(char c)
Definition: scanner.cc:38
Token Number()
Definition: scanner.cc:127
const char * current
Definition: scanner.h:46
char Advance()
Definition: scanner.cc:99
void uscript::Scanner::SetSource ( const char *  _start)

Definition at line 11 of file scanner.cc.

11  {
12  start = _start;
13  current = _start;
14 }
const char * start
Definition: scanner.h:45
const char * current
Definition: scanner.h:46
void uscript::Scanner::SkipWhitespace ( )

Definition at line 16 of file scanner.cc.

16  {
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 }
bool IsAtEnd() const
Definition: scanner.cc:104
const char * current
Definition: scanner.h:46
char Advance()
Definition: scanner.cc:99
uscript::Token uscript::Scanner::String ( )

Definition at line 116 of file scanner.cc.

116  {
117  while (*current != '"' && !IsAtEnd()) {
118  Advance();
119  }
120 
121  if (IsAtEnd()) return ErrorToken("Unterminated string.");
122 
123  Advance();
125 }
Token ErrorToken(const char *message) const
Definition: scanner.cc:175
bool IsAtEnd() const
Definition: scanner.cc:104
Token MakeToken(TokenType type) const
Definition: scanner.cc:108
const char * current
Definition: scanner.h:46
char Advance()
Definition: scanner.cc:99

Member Data Documentation

const char* uscript::Scanner::current
private

Definition at line 46 of file scanner.h.

const char* uscript::Scanner::start
private

Definition at line 45 of file scanner.h.


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