完成 Error定义和ErrorLog. 以及一些相关的东西

This commit is contained in:
2026-02-13 23:11:37 +08:00
parent cfcdfde170
commit 877253cbbc
22 changed files with 2200 additions and 0 deletions

87
src/Token/Token.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include <Token/Token.hpp>
namespace Fig
{
const HashMap<String, TokenType> Token::symbolMap = {
// 三字符
{String("..."), TokenType::TripleDot},
// 双字符
{String("=="), TokenType::Equal},
{String("!="), TokenType::NotEqual},
{String("<="), TokenType::LessEqual},
{String(">="), TokenType::GreaterEqual},
{String("<<"), TokenType::ShiftLeft},
{String(">>"), TokenType::ShiftRight},
{String("+="), TokenType::PlusEqual},
{String("-="), TokenType::MinusEqual},
{String("*="), TokenType::AsteriskEqual},
{String("/="), TokenType::SlashEqual},
{String("%="), TokenType::PercentEqual},
{String("^="), TokenType::CaretEqual},
{String("++"), TokenType::DoublePlus},
{String("--"), TokenType::DoubleMinus},
{String("&&"), TokenType::DoubleAmpersand},
{String("||"), TokenType::DoublePipe},
{String(":="), TokenType::Walrus},
{String("**"), TokenType::Power},
{String("->"), TokenType::RightArrow},
{String("=>"), TokenType::DoubleArrow},
// 单字符
{String("+"), TokenType::Plus},
{String("-"), TokenType::Minus},
{String("*"), TokenType::Asterisk},
{String("/"), TokenType::Slash},
{String("%"), TokenType::Percent},
{String("^"), TokenType::Caret},
{String("&"), TokenType::Ampersand},
{String("|"), TokenType::Pipe},
{String("~"), TokenType::Tilde},
{String("="), TokenType::Assign},
{String("<"), TokenType::Less},
{String(">"), TokenType::Greater},
{String("."), TokenType::Dot},
{String(","), TokenType::Comma},
{String(":"), TokenType::Colon},
{String(";"), TokenType::Semicolon},
{String("'"), TokenType::SingleQuote},
{String("\""), TokenType::DoubleQuote},
{String("("), TokenType::LeftParen},
{String(")"), TokenType::RightParen},
{String("["), TokenType::LeftBracket},
{String("]"), TokenType::RightBracket},
{String("{"), TokenType::LeftBrace},
{String("}"), TokenType::RightBrace},
{String("?"), TokenType::Question},
{String("!"), TokenType::Not},
};
const HashMap<String, TokenType> Token::keywordMap{
{String("and"), TokenType::And},
{String("or"), TokenType::Or},
{String("not"), TokenType::Not},
{String("import"), TokenType::Import},
{String("func"), TokenType::Function},
{String("var"), TokenType::Variable},
{String("const"), TokenType::Const},
// {String("final"), TokenType::Final},
{String("while"), TokenType::While},
{String("for"), TokenType::For},
{String("if"), TokenType::If},
{String("else"), TokenType::Else},
{String("new"), TokenType::New},
{String("struct"), TokenType::Struct},
{String("interface"), TokenType::Interface},
{String("impl"), TokenType::Implement},
{String("is"), TokenType::Is},
{String("public"), TokenType::Public},
{String("return"), TokenType::Return},
{String("break"), TokenType::Break},
{String("continue"), TokenType::Continue},
{String("try"), TokenType::Try},
{String("catch"), TokenType::Catch},
{String("throw"), TokenType::Throw},
{String("Finally"), TokenType::Finally},
{String("as"), TokenType::As},
};
};

145
src/Token/Token.hpp Normal file
View File

@@ -0,0 +1,145 @@
#pragma once
#include <cstdint>
#include <format>
#include <Utils/magic_enum/magic_enum.hpp>
#include <Deps/Deps.hpp>
#include <Core/SourceLocations.hpp>
namespace Fig
{
enum class TokenType : int8_t
{
Illegal = -1,
EndOfFile = 0,
Comments,
Identifier,
/* Keywords */
Package, // package
And, // and
Or, // or
Not, // not
Import, // import
Function, // func
Variable, // var
Const, // const
// Final, // final
While, // while
For, // for
If, // if
Else, // else
New, // new
Struct, // struct
Interface, // interface
Implement, // impl
Is, // is
Public, // public
Return, // return
Break, // break
Continue, // continue
Try, // try
Catch, // catch
Throw, // throw
Finally, // finally
As, // as
// TypeNull, // Null
// TypeInt, // Int
// TypeDeps::String, // Deps::String
// TypeBool, // Bool
// TypeDouble, // Double
/* Literal Types (not keyword)*/
LiteralNumber, // number (int,float...)
LiteralString, // string
LiteralBool, // bool (true/false)
LiteralNull, // null (Null unique instance)
/* Punct */
Plus, // +
Minus, // -
Asterisk, // *
Slash, // /
Percent, // %
Caret, // ^
Ampersand, // &
Pipe, // |
Tilde, // ~
ShiftLeft, // <<
ShiftRight, // >>
// Exclamation, // !
Question, // ?
Assign, // =
Less, // <
Greater, // >
Dot, // .
Comma, // ,
Colon, // :
Semicolon, // ;
SingleQuote, // '
DoubleQuote, // "
// Backtick, // `
// At, // @
// Hash, // #
// Dollar, // $
// Backslash, // '\'
// Underscore, // _
LeftParen, // (
RightParen, // )
LeftBracket, // [
RightBracket, // ]
LeftBrace, // {
RightBrace, // }
// LeftArrow, // <-
RightArrow, // ->
DoubleArrow, // =>
Equal, // ==
NotEqual, // !=
LessEqual, // <=
GreaterEqual, // >=
PlusEqual, // +=
MinusEqual, // -=
AsteriskEqual, // *=
SlashEqual, // /=
PercentEqual, // %=
CaretEqual, // ^=
DoublePlus, // ++
DoubleMinus, // --
DoubleAmpersand, // &&
DoublePipe, // ||
Walrus, // :=
Power, // **
TripleDot, // ... for variadic parameter
};
class Token final
{
public:
static const HashMap<String, TokenType> symbolMap;
static const HashMap<String, TokenType> keywordMap;
const size_t index, length;
// 源文件中的下标 Token长度
const TokenType type;
Token() : index(0), length(0), type(TokenType::Illegal) {};
Token(size_t _index, size_t _length, TokenType _type) : index(_index), length(_length), type(_type) {}
Deps::String toString() const
{
return Deps::String(std::format("Token'{}' at {}, len {}", magic_enum::enum_name(type), index, length));
}
bool isIdentifier() const { return type == TokenType::Identifier; }
bool isLiteral() const
{
return type == TokenType::LiteralNull || type == TokenType::LiteralBool || type == TokenType::LiteralNumber
|| type == TokenType::LiteralString;
}
};
} // namespace Fig