10Lexer::Lexer(std::string source) : source(std::move(source))
14void Lexer::skipShebang()
16 if (position == 0 && peek() ==
'#' && position + 1 < source.length() && source[position + 1] ==
'!')
18 while (!isAtEnd() && peek() !=
'\n')
25std::vector<Phasor::Token> Lexer::tokenize()
27 std::vector<Phasor::Token> tokens;
36 tokens.push_back(scanToken());
48 return source[position];
53 char c = source[position++];
65 return position >= source.length();
68void Lexer::skipWhitespace()
73 if (std::isspace(
static_cast<unsigned char>(c)) != 0)
77 else if (c ==
'/' && position + 1 < source.length() && source[position + 1] ==
'/')
80 while (!isAtEnd() && peek() !=
'\n')
95 if (std::isalpha(
static_cast<unsigned char>(c)) != 0)
99 if (std::isdigit(
static_cast<unsigned char>(c)) != 0)
109 return complexString();
113 if (c ==
'+' && position + 1 < source.length() && source[position + 1] ==
'+')
119 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'-')
125 if (c ==
'=' && position + 1 < source.length() && source[position + 1] ==
'=')
131 if (c ==
'!' && position + 1 < source.length() && source[position + 1] ==
'=')
137 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'>')
143 if (c ==
'<' && position + 1 < source.length() && source[position + 1] ==
'=')
149 if (c ==
'>' && position + 1 < source.length() && source[position + 1] ==
'=')
155 if (c ==
'&' && position + 1 < source.length() && source[position + 1] ==
'&')
161 if (c ==
'|' && position + 1 < source.length() && source[position + 1] ==
'|')
169 if (std::string(
"()+-*/%<>=!&|.{}:;,[]").find(c) != std::string::npos)
181 size_t start = position;
182 while ((std::isalnum(
static_cast<unsigned char>(peek())) != 0) || peek() ==
'_')
186 std::string text = source.substr(start, position - start);
188 static const std::vector<std::string> keywords = {
"let",
"func",
"print",
"if",
"else",
"while"};
190 for (
const auto &kw : keywords)
203 size_t start = position;
204 while (std::isdigit(
static_cast<unsigned char>(peek())) != 0)
208 if (peek() ==
'.' && position + 1 < source.length() &&
209 (std::isdigit(
static_cast<unsigned char>(source[position + 1])) != 0))
212 while (std::isdigit(
static_cast<unsigned char>(peek())) != 0)
222 if (c >=
'0' && c <=
'9')
226 if (c >=
'a' && c <=
'f')
228 return 10 + (c -
'a');
230 if (c >=
'A' && c <=
'F')
232 return 10 + (c -
'A');
239 size_t tokenLine = line;
240 size_t tokenColumn = column;
241 std::ostringstream out;
262 char esc = advance();
309 if (v1 < 0 || v2 < 0)
313 char value =
static_cast<char>((v1 << 4) | v2);
340 size_t tokenLine = line;
341 size_t tokenColumn = column;
342 std::ostringstream out;
Lexer(std::string source)
static int hexValue(char c)
The Pulsar Scripting Language.
static int hexValue(char c)