2023-07-26 23:51:36 -07:00
|
|
|
namespace Parser
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is a simple lexer that focuses on tokenizing only tokens relevant to the library.
|
|
|
|
It will not be capable of lexing C++ code with unsupported features.
|
|
|
|
|
|
|
|
For the sake of scanning files, it can scan preprocessor directives
|
|
|
|
|
2023-07-31 21:42:08 -07:00
|
|
|
__Attributes_Start is only used to indicate the start of the user_defined attribute list.
|
2023-07-26 23:51:36 -07:00
|
|
|
*/
|
|
|
|
|
2023-07-29 02:52:06 -07:00
|
|
|
#ifndef GEN_DEFINE_ATTRIBUTE_TOKENS
|
|
|
|
# define GEN_DEFINE_ATTRIBUTE_TOKENS \
|
2023-07-28 18:44:31 -07:00
|
|
|
Entry( API_Export, "GEN_API_Export_Code" ) \
|
|
|
|
Entry( API_Import, "GEN_API_Import_Code" )
|
|
|
|
#endif
|
|
|
|
|
2023-07-29 22:21:04 -07:00
|
|
|
# define Define_TokType \
|
2023-07-27 14:12:58 -07:00
|
|
|
Entry( Invalid, "INVALID" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Access_Private, "private" ) \
|
|
|
|
Entry( Access_Protected, "protected" ) \
|
|
|
|
Entry( Access_Public, "public" ) \
|
|
|
|
Entry( Access_MemberSymbol, "." ) \
|
|
|
|
Entry( Access_StaticSymbol, "::") \
|
|
|
|
Entry( Ampersand, "&" ) \
|
|
|
|
Entry( Ampersand_DBL, "&&" ) \
|
|
|
|
Entry( Assign_Classifer, ":" ) \
|
|
|
|
Entry( Attribute_Open, "[[" ) \
|
|
|
|
Entry( Attribute_Close, "]]" ) \
|
|
|
|
Entry( BraceCurly_Open, "{" ) \
|
|
|
|
Entry( BraceCurly_Close, "}" ) \
|
|
|
|
Entry( BraceSquare_Open, "[" ) \
|
|
|
|
Entry( BraceSquare_Close, "]" ) \
|
|
|
|
Entry( Capture_Start, "(" ) \
|
|
|
|
Entry( Capture_End, ")" ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( Comment, "__comment__" ) \
|
|
|
|
Entry( Char, "__character__" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Comma, "," ) \
|
|
|
|
Entry( Decl_Class, "class" ) \
|
2023-07-29 22:21:04 -07:00
|
|
|
Entry( Decl_GNU_Attribute, "__attribute__" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Decl_MSVC_Attribute, "__declspec" ) \
|
|
|
|
Entry( Decl_Enum, "enum" ) \
|
|
|
|
Entry( Decl_Extern_Linkage, "extern" ) \
|
|
|
|
Entry( Decl_Friend, "friend" ) \
|
|
|
|
Entry( Decl_Module, "module" ) \
|
|
|
|
Entry( Decl_Namespace, "namespace" ) \
|
|
|
|
Entry( Decl_Operator, "operator" ) \
|
|
|
|
Entry( Decl_Struct, "struct" ) \
|
|
|
|
Entry( Decl_Template, "template" ) \
|
|
|
|
Entry( Decl_Typedef, "typedef" ) \
|
|
|
|
Entry( Decl_Using, "using" ) \
|
|
|
|
Entry( Decl_Union, "union" ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( Identifier, "__identifier__" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Module_Import, "import" ) \
|
|
|
|
Entry( Module_Export, "export" ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( Number, "__number__" ) \
|
|
|
|
Entry( Operator, "__operator__" ) \
|
2023-07-30 15:55:57 -07:00
|
|
|
Entry( Preprocess_Define, "define") \
|
|
|
|
Entry( Preprocess_If, "if") \
|
|
|
|
Entry( Preprocess_IfDef, "ifdef") \
|
|
|
|
Entry( Preprocess_IfNotDef, "ifndef") \
|
|
|
|
Entry( Preprocess_ElIf, "elif") \
|
|
|
|
Entry( Preprocess_Else, "else") \
|
|
|
|
Entry( Preprocess_EndIf, "endif") \
|
2023-07-29 22:21:04 -07:00
|
|
|
Entry( Preprocess_Include, "include" ) \
|
2023-07-30 15:55:57 -07:00
|
|
|
Entry( Preprocess_Pragma, "pragma") \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( Preprocess_Content, "__macro_content__") \
|
|
|
|
Entry( Preprocess_Macro, "__macro__") \
|
|
|
|
Entry( Preprocess_Unsupported, "__unsupported__" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Spec_Alignas, "alignas" ) \
|
|
|
|
Entry( Spec_Const, "const" ) \
|
|
|
|
Entry( Spec_Consteval, "consteval" ) \
|
|
|
|
Entry( Spec_Constexpr, "constexpr" ) \
|
|
|
|
Entry( Spec_Constinit, "constinit" ) \
|
|
|
|
Entry( Spec_Explicit, "explicit" ) \
|
|
|
|
Entry( Spec_Extern, "extern" ) \
|
|
|
|
Entry( Spec_Final, "final" ) \
|
|
|
|
Entry( Spec_Global, "global" ) \
|
|
|
|
Entry( Spec_Inline, "inline" ) \
|
|
|
|
Entry( Spec_Internal_Linkage, "internal" ) \
|
|
|
|
Entry( Spec_LocalPersist, "local_persist" ) \
|
|
|
|
Entry( Spec_Mutable, "mutable" ) \
|
|
|
|
Entry( Spec_Override, "override" ) \
|
|
|
|
Entry( Spec_Static, "static" ) \
|
|
|
|
Entry( Spec_ThreadLocal, "thread_local" ) \
|
|
|
|
Entry( Spec_Volatile, "volatile") \
|
|
|
|
Entry( Star, "*" ) \
|
|
|
|
Entry( Statement_End, ";" ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( StaticAssert, "static_assert" ) \
|
|
|
|
Entry( String, "__string__" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Type_Unsigned, "unsigned" ) \
|
|
|
|
Entry( Type_Signed, "signed" ) \
|
|
|
|
Entry( Type_Short, "short" ) \
|
|
|
|
Entry( Type_Long, "long" ) \
|
|
|
|
Entry( Type_char, "char" ) \
|
|
|
|
Entry( Type_int, "int" ) \
|
|
|
|
Entry( Type_double, "double" ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( Type_MS_int8, "__int8" ) \
|
|
|
|
Entry( Type_MS_int16, "__int16" ) \
|
|
|
|
Entry( Type_MS_int32, "__int32" ) \
|
|
|
|
Entry( Type_MS_int64, "__int64" ) \
|
|
|
|
Entry( Type_MS_W64, "_W64" ) \
|
2023-07-26 23:51:36 -07:00
|
|
|
Entry( Varadic_Argument, "..." ) \
|
2023-07-31 21:42:08 -07:00
|
|
|
Entry( __Attributes_Start, "__attrib_start__" )
|
2023-07-26 23:51:36 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
namespace ETokType
|
2023-07-26 23:51:36 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
enum Type : u32
|
2023-07-26 23:51:36 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
# define Entry( Name_, Str_ ) Name_,
|
2023-07-26 23:51:36 -07:00
|
|
|
Define_TokType
|
2023-07-29 02:52:06 -07:00
|
|
|
GEN_DEFINE_ATTRIBUTE_TOKENS
|
2023-07-26 23:51:36 -07:00
|
|
|
# undef Entry
|
2023-07-27 14:12:58 -07:00
|
|
|
NumTokens,
|
2023-07-26 23:51:36 -07:00
|
|
|
};
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
internal inline
|
|
|
|
Type to_type( StrC str_tok )
|
2023-07-26 23:51:36 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
local_persist
|
|
|
|
StrC lookup[(u32)NumTokens] =
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) { sizeof(Str_), Str_ },
|
|
|
|
Define_TokType
|
2023-07-29 02:52:06 -07:00
|
|
|
GEN_DEFINE_ATTRIBUTE_TOKENS
|
2023-07-27 14:12:58 -07:00
|
|
|
# undef Entry
|
|
|
|
};
|
|
|
|
|
|
|
|
for ( u32 index = 0; index < (u32)NumTokens; index++ )
|
|
|
|
{
|
|
|
|
s32 lookup_len = lookup[index].Len - 1;
|
|
|
|
char const* lookup_str = lookup[index].Ptr;
|
|
|
|
|
|
|
|
if ( lookup_len != str_tok.Len )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( str_compare( str_tok.Ptr, lookup_str, lookup_len ) == 0 )
|
|
|
|
return scast(Type, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
2023-07-26 23:51:36 -07:00
|
|
|
}
|
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
internal inline
|
|
|
|
char const* to_str( Type type )
|
2023-07-26 23:51:36 -07:00
|
|
|
{
|
2023-07-27 14:12:58 -07:00
|
|
|
local_persist
|
|
|
|
char const* lookup[(u32)NumTokens] =
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Str_,
|
|
|
|
Define_TokType
|
2023-07-29 02:52:06 -07:00
|
|
|
GEN_DEFINE_ATTRIBUTE_TOKENS
|
2023-07-27 14:12:58 -07:00
|
|
|
# undef Entry
|
|
|
|
};
|
|
|
|
|
|
|
|
return lookup[(u32)type];
|
|
|
|
}
|
|
|
|
# undef Define_TokType
|
|
|
|
};
|
2023-07-26 23:51:36 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
using TokType = ETokType::Type;
|
2023-07-26 23:51:36 -07:00
|
|
|
|
2023-07-27 14:12:58 -07:00
|
|
|
} // Parser
|