11Lexer::Lexer(std::string source) : source(std::move(source))
15void Lexer::skipShebang()
17 if (position == 0 && peek() ==
'#' && position + 1 < source.length() && source[position + 1] ==
'!')
19 while (!isAtEnd() && peek() !=
'\n')
26std::vector<Phasor::Token> Lexer::tokenize()
28 std::vector<Phasor::Token> tokens;
37 tokens.push_back(scanToken());
49 return source[position];
54 char c = source[position++];
66 return position >= source.length();
69void Lexer::skipWhitespace()
74 if (std::isspace(
static_cast<unsigned char>(c)) != 0)
78 else if (c ==
'/' && position + 1 < source.length() && source[position + 1] ==
'/')
81 while (!isAtEnd() && peek() !=
'\n')
96 if (std::isalpha(
static_cast<unsigned char>(c)) != 0)
100 if (std::isdigit(
static_cast<unsigned char>(c)) != 0)
110 return complexString();
114 if (c ==
'+' && position + 1 < source.length() && source[position + 1] ==
'+')
120 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'-')
126 if (c ==
'=' && position + 1 < source.length() && source[position + 1] ==
'=')
132 if (c ==
'!' && position + 1 < source.length() && source[position + 1] ==
'=')
138 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'>')
144 if (c ==
'<' && position + 1 < source.length() && source[position + 1] ==
'=')
150 if (c ==
'>' && position + 1 < source.length() && source[position + 1] ==
'=')
156 if (c ==
'&' && position + 1 < source.length() && source[position + 1] ==
'&')
162 if (c ==
'|' && position + 1 < source.length() && source[position + 1] ==
'|')
170 if (std::string(
"()+-*/%<>=!&|.{}:;,[]").find(c) != std::string::npos)
182 size_t start = position;
183 while ((std::isalnum(
static_cast<unsigned char>(peek())) != 0) || peek() ==
'_')
187 std::string text = source.substr(start, position - start);
189 static const std::vector<std::string> keywords = {
"let",
"func",
"print",
"if",
"else",
"while"};
191 for (
const auto &kw : keywords)
204 size_t start = position;
205 while (std::isdigit(
static_cast<unsigned char>(peek())) != 0)
209 if (peek() ==
'.' && position + 1 < source.length() &&
210 (std::isdigit(
static_cast<unsigned char>(source[position + 1])) != 0))
213 while (std::isdigit(
static_cast<unsigned char>(peek())) != 0)
223 if (c >=
'0' && c <=
'9')
227 if (c >=
'a' && c <=
'f')
229 return 10 + (c -
'a');
231 if (c >=
'A' && c <=
'F')
233 return 10 + (c -
'A');
240 size_t tokenLine = line;
241 size_t tokenColumn = column;
242 std::ostringstream out;
257 char esc = advance();
260 case 'a': out <<
'\a';
break;
261 case 'b': out <<
'\b';
break;
262 case 'f': out <<
'\f';
break;
263 case 'n': out <<
'\n';
break;
264 case 'r': out <<
'\r';
break;
265 case 't': out <<
'\t';
break;
266 case 'v': out <<
'\v';
break;
267 case '\\': out <<
'\\';
break;
268 case '\'': out <<
'\'';
break;
269 case '"': out <<
'"';
break;
276 case '0':
case '1':
case '2':
case '3':
277 case '4':
case '5':
case '6':
case '7':
280 for (
int i = 1; i < 3 && !isAtEnd(); ++i)
283 if (d <
'0' || d >
'7')
break;
289 out << static_cast<char>(val);
295 if (isAtEnd() ||
hexValue(peek()) < 0)
298 if (!isAtEnd() &&
hexValue(peek()) >= 0)
299 val = (val << 4) |
hexValue(advance());
300 out << static_cast<char>(val);
307 int ndigits = (esc ==
'u') ? 4 : 8;
309 for (
int i = 0; i < ndigits; ++i)
311 if (isAtEnd() ||
hexValue(peek()) < 0)
313 cp = (cp << 4) | static_cast<Phasor::u32>(
hexValue(advance()));
315 if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF))
317 if (cp <= 0x7F) { out << static_cast<char>(cp); }
318 else if (cp <= 0x7FF) { out << static_cast<char>(0xC0 | (cp >> 6))
319 <<
static_cast<char>(0x80 | (cp & 0x3F)); }
320 else if (cp <= 0xFFFF) { out << static_cast<char>(0xE0 | (cp >> 12))
321 <<
static_cast<char>(0x80 | ((cp >> 6) & 0x3F))
322 <<
static_cast<char>(0x80 | (cp & 0x3F)); }
323 else { out << static_cast<char>(0xF0 | (cp >> 18))
324 <<
static_cast<char>(0x80 | ((cp >> 12) & 0x3F))
325 <<
static_cast<char>(0x80 | ((cp >> 6) & 0x3F))
326 <<
static_cast<char>(0x80 | (cp & 0x3F)); }
350 size_t tokenLine = line;
351 size_t tokenColumn = column;
352 std::ostringstream out;
Lexer(std::string source)
static int hexValue(char c)
The Pulsar Scripting Language.
static int hexValue(char c)