2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
#include "Lexer.h"
|
2021-06-04 00:35:50 +03:00
|
|
|
#include <AK/CharacterTypes.h>
|
2021-08-21 16:36:40 +03:00
|
|
|
#include <AK/Function.h>
|
2019-10-25 22:58:40 +03:00
|
|
|
#include <AK/HashTable.h>
|
2020-08-16 12:04:00 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-10-25 22:58:40 +03:00
|
|
|
#include <AK/String.h>
|
2019-10-25 20:52:44 +03:00
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
namespace Cpp {
|
2020-02-07 22:07:15 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
Lexer::Lexer(StringView input, size_t start_line)
|
2019-10-25 20:52:44 +03:00
|
|
|
: m_input(input)
|
2021-08-06 10:16:53 +03:00
|
|
|
, m_previous_position { start_line, 0 }
|
|
|
|
, m_position { start_line, 0 }
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
char Lexer::peek(size_t offset) const
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
|
|
|
if ((m_index + offset) >= m_input.length())
|
|
|
|
return 0;
|
|
|
|
return m_input[m_index + offset];
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
char Lexer::consume()
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_index < m_input.length());
|
2019-10-25 22:07:45 +03:00
|
|
|
char ch = m_input[m_index++];
|
|
|
|
m_previous_position = m_position;
|
|
|
|
if (ch == '\n') {
|
|
|
|
m_position.line++;
|
|
|
|
m_position.column = 0;
|
|
|
|
} else {
|
|
|
|
m_position.column++;
|
|
|
|
}
|
|
|
|
return ch;
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 00:35:50 +03:00
|
|
|
constexpr bool is_valid_first_character_of_identifier(char ch)
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
2021-06-04 00:35:50 +03:00
|
|
|
return is_ascii_alpha(ch) || ch == '_' || ch == '$';
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 00:35:50 +03:00
|
|
|
constexpr bool is_valid_nonfirst_character_of_identifier(char ch)
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
2021-06-04 00:35:50 +03:00
|
|
|
return is_valid_first_character_of_identifier(ch) || is_ascii_digit(ch);
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 00:17:34 +03:00
|
|
|
constexpr char const* s_known_keywords[] = {
|
2020-08-16 12:04:00 +03:00
|
|
|
"alignas",
|
|
|
|
"alignof",
|
|
|
|
"and",
|
|
|
|
"and_eq",
|
|
|
|
"asm",
|
|
|
|
"bitand",
|
|
|
|
"bitor",
|
|
|
|
"break",
|
|
|
|
"case",
|
|
|
|
"catch",
|
|
|
|
"class",
|
|
|
|
"compl",
|
|
|
|
"const",
|
|
|
|
"const_cast",
|
|
|
|
"constexpr",
|
|
|
|
"continue",
|
|
|
|
"decltype",
|
|
|
|
"default",
|
|
|
|
"delete",
|
|
|
|
"do",
|
|
|
|
"dynamic_cast",
|
|
|
|
"else",
|
|
|
|
"enum",
|
|
|
|
"explicit",
|
|
|
|
"export",
|
|
|
|
"extern",
|
|
|
|
"false",
|
|
|
|
"final",
|
|
|
|
"for",
|
|
|
|
"friend",
|
|
|
|
"goto",
|
|
|
|
"if",
|
|
|
|
"inline",
|
|
|
|
"mutable",
|
|
|
|
"namespace",
|
|
|
|
"new",
|
|
|
|
"noexcept",
|
|
|
|
"not",
|
|
|
|
"not_eq",
|
|
|
|
"nullptr",
|
|
|
|
"operator",
|
|
|
|
"or",
|
|
|
|
"or_eq",
|
|
|
|
"override",
|
|
|
|
"private",
|
|
|
|
"protected",
|
|
|
|
"public",
|
|
|
|
"register",
|
|
|
|
"reinterpret_cast",
|
|
|
|
"return",
|
|
|
|
"signed",
|
|
|
|
"sizeof",
|
|
|
|
"static",
|
|
|
|
"static_assert",
|
|
|
|
"static_cast",
|
|
|
|
"struct",
|
|
|
|
"switch",
|
|
|
|
"template",
|
|
|
|
"this",
|
|
|
|
"thread_local",
|
|
|
|
"throw",
|
|
|
|
"true",
|
|
|
|
"try",
|
|
|
|
"typedef",
|
|
|
|
"typeid",
|
|
|
|
"typename",
|
|
|
|
"union",
|
|
|
|
"using",
|
|
|
|
"virtual",
|
|
|
|
"volatile",
|
|
|
|
"while",
|
|
|
|
"xor",
|
|
|
|
"xor_eq"
|
|
|
|
};
|
|
|
|
|
2021-06-04 00:17:34 +03:00
|
|
|
constexpr char const* s_known_types[] = {
|
2020-08-16 12:04:00 +03:00
|
|
|
"ByteBuffer",
|
|
|
|
"CircularDeque",
|
|
|
|
"CircularQueue",
|
|
|
|
"Deque",
|
|
|
|
"DoublyLinkedList",
|
|
|
|
"FileSystemPath",
|
2020-09-07 14:17:12 +03:00
|
|
|
"Array",
|
2020-08-16 12:04:00 +03:00
|
|
|
"Function",
|
|
|
|
"HashMap",
|
|
|
|
"HashTable",
|
|
|
|
"IPv4Address",
|
|
|
|
"IntrusiveList",
|
|
|
|
"JsonArray",
|
|
|
|
"JsonObject",
|
|
|
|
"JsonValue",
|
|
|
|
"MappedFile",
|
|
|
|
"NetworkOrdered",
|
|
|
|
"NonnullOwnPtr",
|
|
|
|
"NonnullOwnPtrVector",
|
|
|
|
"NonnullRefPtr",
|
|
|
|
"NonnullRefPtrVector",
|
|
|
|
"Optional",
|
|
|
|
"OwnPtr",
|
|
|
|
"RefPtr",
|
|
|
|
"Result",
|
|
|
|
"ScopeGuard",
|
|
|
|
"SinglyLinkedList",
|
|
|
|
"String",
|
|
|
|
"StringBuilder",
|
|
|
|
"StringImpl",
|
|
|
|
"StringView",
|
|
|
|
"Utf8View",
|
|
|
|
"Vector",
|
|
|
|
"WeakPtr",
|
|
|
|
"auto",
|
2021-06-05 17:53:19 +03:00
|
|
|
"bool",
|
2020-08-16 12:04:00 +03:00
|
|
|
"char",
|
|
|
|
"char16_t",
|
|
|
|
"char32_t",
|
|
|
|
"char8_t",
|
|
|
|
"double",
|
|
|
|
"float",
|
|
|
|
"i16",
|
|
|
|
"i32",
|
|
|
|
"i64",
|
|
|
|
"i8",
|
|
|
|
"int",
|
|
|
|
"int",
|
|
|
|
"long",
|
|
|
|
"short",
|
|
|
|
"signed",
|
|
|
|
"u16",
|
|
|
|
"u32",
|
|
|
|
"u64",
|
|
|
|
"u8",
|
|
|
|
"unsigned",
|
|
|
|
"void",
|
|
|
|
"wchar_t"
|
|
|
|
};
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static bool is_keyword(StringView string)
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
2020-08-16 12:04:00 +03:00
|
|
|
static HashTable<String> keywords(array_size(s_known_keywords));
|
2019-10-25 22:58:40 +03:00
|
|
|
if (keywords.is_empty()) {
|
2020-08-16 12:04:00 +03:00
|
|
|
keywords.set_from(s_known_keywords);
|
2019-10-25 22:58:40 +03:00
|
|
|
}
|
|
|
|
return keywords.contains(string);
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static bool is_known_type(StringView string)
|
2019-10-26 11:32:12 +03:00
|
|
|
{
|
2020-08-16 12:04:00 +03:00
|
|
|
static HashTable<String> types(array_size(s_known_types));
|
2019-10-26 11:32:12 +03:00
|
|
|
if (types.is_empty()) {
|
2020-08-16 12:04:00 +03:00
|
|
|
types.set_from(s_known_types);
|
2019-10-26 11:32:12 +03:00
|
|
|
}
|
|
|
|
return types.contains(string);
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:36:40 +03:00
|
|
|
void Lexer::lex_impl(Function<void(Token)> callback)
|
2019-10-25 20:52:44 +03:00
|
|
|
{
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t token_start_index = 0;
|
2020-09-28 16:21:25 +03:00
|
|
|
Position token_start_position;
|
2019-10-25 22:07:45 +03:00
|
|
|
|
2021-03-12 14:11:41 +03:00
|
|
|
auto emit_single_char_token = [&](auto type) {
|
2021-08-21 16:36:40 +03:00
|
|
|
callback(Token(type, m_position, m_position, m_input.substring_view(m_index, 1)));
|
2019-10-25 22:07:45 +03:00
|
|
|
consume();
|
2019-10-25 20:52:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto begin_token = [&] {
|
|
|
|
token_start_index = m_index;
|
2019-10-25 22:07:45 +03:00
|
|
|
token_start_position = m_position;
|
2019-10-25 20:52:44 +03:00
|
|
|
};
|
|
|
|
auto commit_token = [&](auto type) {
|
2021-08-13 12:11:26 +03:00
|
|
|
if (m_options.ignore_whitespace && type == Token::Type::Whitespace)
|
|
|
|
return;
|
2021-08-21 16:36:40 +03:00
|
|
|
callback(Token(type, token_start_position, m_previous_position, m_input.substring_view(token_start_index, m_index - token_start_index)));
|
2019-10-25 20:52:44 +03:00
|
|
|
};
|
|
|
|
|
2020-07-26 20:35:47 +03:00
|
|
|
auto emit_token_equals = [&](auto type, auto equals_type) {
|
|
|
|
if (peek(1) == '=') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
consume();
|
|
|
|
commit_token(equals_type);
|
|
|
|
return;
|
|
|
|
}
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(type);
|
2020-07-26 20:35:47 +03:00
|
|
|
};
|
|
|
|
|
2020-03-10 23:26:11 +03:00
|
|
|
auto match_escape_sequence = [&]() -> size_t {
|
|
|
|
switch (peek(1)) {
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '?':
|
|
|
|
case '\\':
|
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'f':
|
|
|
|
case 'n':
|
|
|
|
case 'r':
|
|
|
|
case 't':
|
|
|
|
case 'v':
|
|
|
|
return 2;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7': {
|
|
|
|
size_t octal_digits = 1;
|
|
|
|
for (size_t i = 0; i < 2; ++i) {
|
|
|
|
char next = peek(2 + i);
|
|
|
|
if (next < '0' || next > '7')
|
|
|
|
break;
|
|
|
|
++octal_digits;
|
|
|
|
}
|
|
|
|
return 1 + octal_digits;
|
|
|
|
}
|
|
|
|
case 'x': {
|
|
|
|
size_t hex_digits = 0;
|
2021-06-04 00:35:50 +03:00
|
|
|
while (is_ascii_hex_digit(peek(2 + hex_digits)))
|
2020-03-10 23:26:11 +03:00
|
|
|
++hex_digits;
|
|
|
|
return 2 + hex_digits;
|
|
|
|
}
|
2020-07-27 04:06:44 +03:00
|
|
|
case 'u':
|
|
|
|
case 'U': {
|
2020-03-10 23:26:11 +03:00
|
|
|
bool is_unicode = true;
|
2020-07-27 04:06:44 +03:00
|
|
|
size_t number_of_digits = peek(1) == 'u' ? 4 : 8;
|
|
|
|
for (size_t i = 0; i < number_of_digits; ++i) {
|
2021-06-04 00:35:50 +03:00
|
|
|
if (!is_ascii_hex_digit(peek(2 + i))) {
|
2020-03-10 23:26:11 +03:00
|
|
|
is_unicode = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 04:06:44 +03:00
|
|
|
return is_unicode ? 2 + number_of_digits : 0;
|
2020-03-10 23:26:11 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-27 03:16:55 +03:00
|
|
|
auto match_string_prefix = [&](char quote) -> size_t {
|
|
|
|
if (peek() == quote)
|
|
|
|
return 1;
|
|
|
|
if (peek() == 'L' && peek(1) == quote)
|
|
|
|
return 2;
|
|
|
|
if (peek() == 'u') {
|
|
|
|
if (peek(1) == quote)
|
|
|
|
return 2;
|
|
|
|
if (peek(1) == '8' && peek(2) == quote)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (peek() == 'U' && peek(1) == quote)
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2019-10-25 20:52:44 +03:00
|
|
|
while (m_index < m_input.length()) {
|
|
|
|
auto ch = peek();
|
2021-06-04 00:35:50 +03:00
|
|
|
if (is_ascii_space(ch)) {
|
2019-10-25 20:52:44 +03:00
|
|
|
begin_token();
|
2021-06-04 00:35:50 +03:00
|
|
|
while (is_ascii_space(peek()))
|
2019-10-25 20:52:44 +03:00
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Whitespace);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '(') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::LeftParen);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == ')') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::RightParen);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '{') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::LeftCurly);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '}') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::RightCurly);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '[') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::LeftBracket);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == ']') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::RightBracket);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 23:37:23 +03:00
|
|
|
if (ch == '<') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '<') {
|
|
|
|
consume();
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::LessLessEquals);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::LessLess);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::LessEquals);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '>') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::LessGreater);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Less);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 23:37:40 +03:00
|
|
|
if (ch == '>') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '>') {
|
|
|
|
consume();
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::GreaterGreaterEquals);
|
2020-07-26 23:37:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::GreaterGreater);
|
2020-07-26 23:37:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::GreaterEquals);
|
2020-07-26 23:37:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Greater);
|
2020-07-26 23:37:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 20:52:44 +03:00
|
|
|
if (ch == ',') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::Comma);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 20:39:43 +03:00
|
|
|
if (ch == '+') {
|
2020-07-27 01:13:13 +03:00
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '+') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::PlusPlus);
|
2020-07-27 01:13:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::PlusEquals);
|
2020-07-27 01:13:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Plus);
|
2020-07-26 20:39:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '-') {
|
2020-07-27 01:13:13 +03:00
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '-') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::MinusMinus);
|
2020-07-27 01:13:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::MinusEquals);
|
2020-07-27 01:13:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 01:26:04 +03:00
|
|
|
if (peek() == '>') {
|
|
|
|
consume();
|
2020-07-27 01:35:17 +03:00
|
|
|
if (peek() == '*') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::ArrowAsterisk);
|
2020-07-27 01:35:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Arrow);
|
2020-07-27 01:26:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Minus);
|
2020-07-26 20:39:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 20:52:44 +03:00
|
|
|
if (ch == '*') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::Asterisk, Token::Type::AsteriskEquals);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 23:37:23 +03:00
|
|
|
if (ch == '%') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::Percent, Token::Type::PercentEquals);
|
2020-07-26 23:37:23 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 01:16:22 +03:00
|
|
|
if (ch == '^') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::Caret, Token::Type::CaretEquals);
|
2020-07-27 01:16:22 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 01:20:50 +03:00
|
|
|
if (ch == '!') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::ExclamationMark, Token::Type::ExclamationMarkEquals);
|
2020-07-27 01:20:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 20:39:43 +03:00
|
|
|
if (ch == '=') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::Equals, Token::Type::EqualsEquals);
|
2020-07-26 20:39:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 23:40:57 +03:00
|
|
|
if (ch == '&') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '&') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::AndAnd);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::AndEquals);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::And);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '|') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '|') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::PipePipe);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peek() == '=') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::PipeEquals);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Pipe);
|
2020-07-26 23:40:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 01:20:50 +03:00
|
|
|
if (ch == '~') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::Tilde);
|
2020-07-27 01:20:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '?') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::QuestionMark);
|
2020-07-27 01:20:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == ':') {
|
2020-07-27 01:35:17 +03:00
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == ':') {
|
|
|
|
consume();
|
|
|
|
if (peek() == '*') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::ColonColonAsterisk);
|
2020-07-27 01:35:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::ColonColon);
|
2020-07-27 01:35:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Colon);
|
2020-07-27 01:20:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 20:52:44 +03:00
|
|
|
if (ch == ';') {
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::Semicolon);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 01:26:04 +03:00
|
|
|
if (ch == '.') {
|
2020-07-27 01:35:17 +03:00
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
if (peek() == '*') {
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::DotAsterisk);
|
2020-07-27 01:35:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Dot);
|
2020-07-27 01:26:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 20:52:44 +03:00
|
|
|
if (ch == '#') {
|
|
|
|
begin_token();
|
2020-03-12 01:17:29 +03:00
|
|
|
consume();
|
2021-08-20 17:00:49 +03:00
|
|
|
while (AK::is_ascii_space(peek()))
|
|
|
|
consume();
|
2020-03-12 01:17:29 +03:00
|
|
|
|
2021-08-20 17:00:49 +03:00
|
|
|
size_t directive_start = m_index;
|
2020-03-12 01:17:29 +03:00
|
|
|
if (is_valid_first_character_of_identifier(peek()))
|
|
|
|
while (peek() && is_valid_nonfirst_character_of_identifier(peek()))
|
|
|
|
consume();
|
|
|
|
|
2021-08-20 17:00:49 +03:00
|
|
|
auto directive = StringView(m_input.characters_without_null_termination() + directive_start, m_index - directive_start);
|
|
|
|
if (directive == "include"sv) {
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::IncludeStatement);
|
2020-03-12 01:17:29 +03:00
|
|
|
|
2021-06-04 22:00:17 +03:00
|
|
|
if (is_ascii_space(peek())) {
|
|
|
|
begin_token();
|
|
|
|
do {
|
|
|
|
consume();
|
|
|
|
} while (is_ascii_space(peek()));
|
|
|
|
commit_token(Token::Type::Whitespace);
|
|
|
|
}
|
2020-03-12 01:17:29 +03:00
|
|
|
|
|
|
|
begin_token();
|
|
|
|
if (peek() == '<' || peek() == '"') {
|
|
|
|
char closing = consume() == '<' ? '>' : '"';
|
2020-04-04 12:00:14 +03:00
|
|
|
while (peek() && peek() != closing && peek() != '\n')
|
2020-03-12 01:17:29 +03:00
|
|
|
consume();
|
|
|
|
|
2020-04-04 12:00:14 +03:00
|
|
|
if (peek() && consume() == '\n') {
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::IncludePath);
|
2020-03-12 01:17:29 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-04 12:00:14 +03:00
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::IncludePath);
|
2020-04-04 12:00:14 +03:00
|
|
|
begin_token();
|
2020-03-12 01:17:29 +03:00
|
|
|
}
|
2021-01-23 17:47:20 +03:00
|
|
|
} else {
|
2021-07-28 03:34:39 +03:00
|
|
|
while (peek()) {
|
|
|
|
if (peek() == '\\' && peek(1) == '\n') {
|
|
|
|
consume();
|
|
|
|
consume();
|
|
|
|
} else if (peek() == '\n') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
consume();
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 01:17:29 +03:00
|
|
|
|
2021-01-23 17:47:20 +03:00
|
|
|
commit_token(Token::Type::PreprocessorStatement);
|
|
|
|
}
|
2020-03-12 01:17:29 +03:00
|
|
|
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '/' && peek(1) == '/') {
|
|
|
|
begin_token();
|
|
|
|
while (peek() && peek() != '\n')
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Comment);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '/' && peek(1) == '*') {
|
|
|
|
begin_token();
|
|
|
|
consume();
|
|
|
|
consume();
|
2019-12-02 09:47:09 +03:00
|
|
|
bool comment_block_ends = false;
|
2019-10-25 20:52:44 +03:00
|
|
|
while (peek()) {
|
2019-12-02 09:47:09 +03:00
|
|
|
if (peek() == '*' && peek(1) == '/') {
|
|
|
|
comment_block_ends = true;
|
2019-10-25 20:52:44 +03:00
|
|
|
break;
|
2019-12-02 09:47:09 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 20:52:44 +03:00
|
|
|
consume();
|
|
|
|
}
|
2019-12-02 09:47:09 +03:00
|
|
|
|
|
|
|
if (comment_block_ends) {
|
|
|
|
consume();
|
|
|
|
consume();
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Comment);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-26 20:39:43 +03:00
|
|
|
if (ch == '/') {
|
2020-09-28 16:21:25 +03:00
|
|
|
emit_token_equals(Token::Type::Slash, Token::Type::SlashEquals);
|
2020-07-26 20:39:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 03:16:55 +03:00
|
|
|
if (size_t prefix = match_string_prefix('"'); prefix > 0) {
|
2019-10-25 20:52:44 +03:00
|
|
|
begin_token();
|
2020-07-27 03:16:55 +03:00
|
|
|
for (size_t i = 0; i < prefix; ++i)
|
|
|
|
consume();
|
2019-10-25 20:52:44 +03:00
|
|
|
while (peek()) {
|
2020-03-10 23:26:11 +03:00
|
|
|
if (peek() == '\\') {
|
2020-07-27 03:16:55 +03:00
|
|
|
if (size_t escape = match_escape_sequence(); escape > 0) {
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::DoubleQuotedString);
|
2020-03-10 23:26:11 +03:00
|
|
|
begin_token();
|
|
|
|
for (size_t i = 0; i < escape; ++i)
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::EscapeSequence);
|
2020-03-10 23:26:11 +03:00
|
|
|
begin_token();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 23:15:38 +03:00
|
|
|
// If string is not terminated - stop before EOF
|
|
|
|
if (!peek(1))
|
|
|
|
break;
|
|
|
|
|
2019-10-25 20:52:44 +03:00
|
|
|
if (consume() == '"')
|
|
|
|
break;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::DoubleQuotedString);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 03:49:17 +03:00
|
|
|
if (size_t prefix = match_string_prefix('R'); prefix > 0 && peek(prefix) == '"') {
|
|
|
|
begin_token();
|
|
|
|
for (size_t i = 0; i < prefix + 1; ++i)
|
|
|
|
consume();
|
|
|
|
size_t prefix_start = m_index;
|
|
|
|
while (peek() && peek() != '(')
|
|
|
|
consume();
|
|
|
|
StringView prefix_string = m_input.substring_view(prefix_start, m_index - prefix_start);
|
|
|
|
while (peek()) {
|
|
|
|
if (consume() == '"') {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_index >= prefix_string.length() + 2);
|
|
|
|
VERIFY(m_input[m_index - 1] == '"');
|
2020-07-27 03:49:17 +03:00
|
|
|
if (m_input[m_index - 1 - prefix_string.length() - 1] == ')') {
|
|
|
|
StringView suffix_string = m_input.substring_view(m_index - 1 - prefix_string.length(), prefix_string.length());
|
|
|
|
if (prefix_string == suffix_string)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::RawString);
|
2020-07-27 03:49:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-27 03:16:55 +03:00
|
|
|
if (size_t prefix = match_string_prefix('\''); prefix > 0) {
|
2019-10-25 20:52:44 +03:00
|
|
|
begin_token();
|
2020-07-27 03:16:55 +03:00
|
|
|
for (size_t i = 0; i < prefix; ++i)
|
|
|
|
consume();
|
2019-10-25 20:52:44 +03:00
|
|
|
while (peek()) {
|
2020-03-10 23:26:11 +03:00
|
|
|
if (peek() == '\\') {
|
2020-07-27 03:16:55 +03:00
|
|
|
if (size_t escape = match_escape_sequence(); escape > 0) {
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::SingleQuotedString);
|
2020-03-10 23:26:11 +03:00
|
|
|
begin_token();
|
|
|
|
for (size_t i = 0; i < escape; ++i)
|
|
|
|
consume();
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::EscapeSequence);
|
2020-03-10 23:26:11 +03:00
|
|
|
begin_token();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:52:44 +03:00
|
|
|
if (consume() == '\'')
|
|
|
|
break;
|
|
|
|
}
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::SingleQuotedString);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-06-04 00:35:50 +03:00
|
|
|
if (is_ascii_digit(ch) || (ch == '.' && is_ascii_digit(peek(1)))) {
|
2019-10-25 20:52:44 +03:00
|
|
|
begin_token();
|
2020-03-11 21:31:25 +03:00
|
|
|
consume();
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
auto type = ch == '.' ? Token::Type::Float : Token::Type::Integer;
|
2020-03-11 21:31:25 +03:00
|
|
|
bool is_hex = false;
|
|
|
|
bool is_binary = false;
|
|
|
|
|
|
|
|
auto match_exponent = [&]() -> size_t {
|
|
|
|
char ch = peek();
|
|
|
|
if (ch != 'e' && ch != 'E' && ch != 'p' && ch != 'P')
|
|
|
|
return 0;
|
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
type = Token::Type::Float;
|
2020-03-11 21:31:25 +03:00
|
|
|
size_t length = 1;
|
|
|
|
ch = peek(length);
|
|
|
|
if (ch == '+' || ch == '-') {
|
|
|
|
++length;
|
|
|
|
}
|
2021-06-04 00:35:50 +03:00
|
|
|
for (ch = peek(length); is_ascii_digit(ch); ch = peek(length)) {
|
2020-03-11 21:31:25 +03:00
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto match_type_literal = [&]() -> size_t {
|
|
|
|
size_t length = 0;
|
|
|
|
for (;;) {
|
|
|
|
char ch = peek(length);
|
2020-09-28 16:21:25 +03:00
|
|
|
if ((ch == 'u' || ch == 'U') && type == Token::Type::Integer) {
|
2020-03-11 21:31:25 +03:00
|
|
|
++length;
|
|
|
|
} else if ((ch == 'f' || ch == 'F') && !is_binary) {
|
2020-09-28 16:21:25 +03:00
|
|
|
type = Token::Type::Float;
|
2020-03-11 21:31:25 +03:00
|
|
|
++length;
|
|
|
|
} else if (ch == 'l' || ch == 'L') {
|
|
|
|
++length;
|
|
|
|
} else
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (peek() == 'b' || peek() == 'B') {
|
2019-10-25 20:52:44 +03:00
|
|
|
consume();
|
2020-03-11 21:31:25 +03:00
|
|
|
is_binary = true;
|
|
|
|
for (char ch = peek(); ch == '0' || ch == '1' || (ch == '\'' && peek(1) != '\''); ch = peek()) {
|
|
|
|
consume();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (peek() == 'x' || peek() == 'X') {
|
|
|
|
consume();
|
|
|
|
is_hex = true;
|
|
|
|
}
|
|
|
|
|
2021-06-04 00:35:50 +03:00
|
|
|
for (char ch = peek(); (is_hex ? is_ascii_hex_digit(ch) : is_ascii_digit(ch)) || (ch == '\'' && peek(1) != '\'') || ch == '.'; ch = peek()) {
|
2020-03-11 21:31:25 +03:00
|
|
|
if (ch == '.') {
|
2020-09-28 16:21:25 +03:00
|
|
|
if (type == Token::Type::Integer) {
|
|
|
|
type = Token::Type::Float;
|
2020-03-11 21:31:25 +03:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
consume();
|
|
|
|
}
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
2020-03-11 21:31:25 +03:00
|
|
|
|
|
|
|
if (!is_binary) {
|
|
|
|
size_t length = match_exponent();
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
consume();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = match_type_literal();
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
consume();
|
|
|
|
|
|
|
|
commit_token(type);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_valid_first_character_of_identifier(ch)) {
|
|
|
|
begin_token();
|
|
|
|
while (peek() && is_valid_nonfirst_character_of_identifier(peek()))
|
|
|
|
consume();
|
|
|
|
auto token_view = StringView(m_input.characters_without_null_termination() + token_start_index, m_index - token_start_index);
|
|
|
|
if (is_keyword(token_view))
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Keyword);
|
2019-10-26 11:32:12 +03:00
|
|
|
else if (is_known_type(token_view))
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::KnownType);
|
2019-10-25 20:52:44 +03:00
|
|
|
else
|
2020-09-28 16:21:25 +03:00
|
|
|
commit_token(Token::Type::Identifier);
|
2019-10-25 20:52:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-07 15:44:34 +03:00
|
|
|
|
|
|
|
if (ch == '\\' && peek(1) == '\n') {
|
|
|
|
consume();
|
|
|
|
consume();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:04:22 +03:00
|
|
|
dbgln("Unimplemented token character: {}", ch);
|
2021-03-12 14:11:41 +03:00
|
|
|
emit_single_char_token(Token::Type::Unknown);
|
2019-10-25 20:52:44 +03:00
|
|
|
}
|
2021-08-21 16:36:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Token> Lexer::lex()
|
|
|
|
{
|
|
|
|
Vector<Token> tokens;
|
|
|
|
lex_impl([&](auto token) {
|
|
|
|
tokens.append(move(token));
|
|
|
|
});
|
2019-10-25 20:52:44 +03:00
|
|
|
return tokens;
|
|
|
|
}
|
2020-02-07 22:07:15 +03:00
|
|
|
|
|
|
|
}
|