2020-12-21 12:32:27 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-21 12:32:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
|
2022-02-02 18:11:29 +01:00
|
|
|
namespace GUI::GML {
|
2020-12-21 12:32:27 +01:00
|
|
|
|
|
|
|
|
#define FOR_EACH_TOKEN_TYPE \
|
|
|
|
|
__TOKEN(Unknown) \
|
|
|
|
|
__TOKEN(Comment) \
|
|
|
|
|
__TOKEN(ClassMarker) \
|
|
|
|
|
__TOKEN(ClassName) \
|
|
|
|
|
__TOKEN(LeftCurly) \
|
|
|
|
|
__TOKEN(RightCurly) \
|
|
|
|
|
__TOKEN(Identifier) \
|
|
|
|
|
__TOKEN(Colon) \
|
|
|
|
|
__TOKEN(JsonValue)
|
|
|
|
|
|
2022-02-02 18:11:29 +01:00
|
|
|
struct Position {
|
2020-12-21 12:32:27 +01:00
|
|
|
size_t line;
|
|
|
|
|
size_t column;
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-02 18:11:29 +01:00
|
|
|
struct Token {
|
2020-12-21 12:32:27 +01:00
|
|
|
enum class Type {
|
|
|
|
|
#define __TOKEN(x) x,
|
|
|
|
|
FOR_EACH_TOKEN_TYPE
|
|
|
|
|
#undef __TOKEN
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-04 00:01:16 +02:00
|
|
|
char const* to_string() const
|
2020-12-21 12:32:27 +01:00
|
|
|
{
|
|
|
|
|
switch (m_type) {
|
|
|
|
|
#define __TOKEN(x) \
|
|
|
|
|
case Type::x: \
|
|
|
|
|
return #x;
|
|
|
|
|
FOR_EACH_TOKEN_TYPE
|
|
|
|
|
#undef __TOKEN
|
|
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-21 12:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Type m_type { Type::Unknown };
|
|
|
|
|
StringView m_view;
|
2022-02-02 18:11:29 +01:00
|
|
|
Position m_start;
|
|
|
|
|
Position m_end;
|
2020-12-21 12:32:27 +01:00
|
|
|
};
|
|
|
|
|
|
2022-02-02 18:11:29 +01:00
|
|
|
class Lexer {
|
2020-12-21 12:32:27 +01:00
|
|
|
public:
|
2022-02-02 18:11:29 +01:00
|
|
|
Lexer(StringView);
|
2020-12-21 12:32:27 +01:00
|
|
|
|
2022-02-02 18:11:29 +01:00
|
|
|
Vector<Token> lex();
|
2020-12-21 12:32:27 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
char peek(size_t offset = 0) const;
|
|
|
|
|
char consume();
|
|
|
|
|
|
|
|
|
|
StringView m_input;
|
|
|
|
|
size_t m_index { 0 };
|
2022-02-02 18:11:29 +01:00
|
|
|
Position m_position { 0, 0 };
|
2020-12-21 12:32:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2022-02-02 21:32:32 +01:00
|
|
|
|
|
|
|
|
#undef FOR_EACH_TOKEN_TYPE
|