From a7c9dad9fd6f14609c4c4a60840596faf20e9ea0 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 3 Dec 2024 09:31:27 -0500 Subject: [PATCH] cpp feature reduction usage in parser --- project/bootstrap.cpp | 7 +- project/components/gen/etoktype.cpp | 445 ++++++++++--------- project/components/interface.parsing.cpp | 55 +-- project/components/interface.upfront.cpp | 2 +- project/components/lexer.cpp | 2 +- project/components/parser.cpp | 519 ++++++++++++----------- project/dependencies/platform.hpp | 3 + project/helpers/helper.hpp | 11 +- 8 files changed, 531 insertions(+), 513 deletions(-) diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 4d9b86d..12ee7bb 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -243,7 +243,12 @@ int gen_main() Code untyped = scan_file( "components/interface.untyped.cpp" ); CodeBody etoktype = gen_etoktype( "enums/ETokType.csv", "enums/AttributeTokens.csv" ); - CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) ); + //CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) ); + CodeBody nspaced_etoktype = def_global_body( args( + untyped_str(txt("GEN_NS_PARSER_BEGIN\n")), + etoktype, + untyped_str(txt("GEN_NS_PARSER_END\n")) + )); Builder src = Builder::open( "gen/gen.cpp" ); diff --git a/project/components/gen/etoktype.cpp b/project/components/gen/etoktype.cpp index 8a5c490..b1ce2ef 100644 --- a/project/components/gen/etoktype.cpp +++ b/project/components/gen/etoktype.cpp @@ -5,237 +5,236 @@ // This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) -namespace parser +GEN_NS_PARSER_BEGIN + +namespace ETokType { - namespace ETokType - { #define GEN_DEFINE_ATTRIBUTE_TOKENS Entry( Attribute_API_Export, "GEN_API_Export_Code" ) Entry( Attribute_API_Import, "GEN_API_Import_Code" ) - enum Type : u32 - { - Invalid, - Access_Private, - Access_Protected, - Access_Public, - Access_MemberSymbol, - Access_StaticSymbol, - Ampersand, - Ampersand_DBL, - Assign_Classifer, - Attribute_Open, - Attribute_Close, - BraceCurly_Open, - BraceCurly_Close, - BraceSquare_Open, - BraceSquare_Close, - Capture_Start, - Capture_End, - Comment, - Comment_End, - Comment_Start, - Char, - Comma, - Decl_Class, - Decl_GNU_Attribute, - Decl_MSVC_Attribute, - Decl_Enum, - Decl_Extern_Linkage, - Decl_Friend, - Decl_Module, - Decl_Namespace, - Decl_Operator, - Decl_Struct, - Decl_Template, - Decl_Typedef, - Decl_Using, - Decl_Union, - Identifier, - Module_Import, - Module_Export, - NewLine, - Number, - Operator, - Preprocess_Hash, - Preprocess_Define, - Preprocess_If, - Preprocess_IfDef, - Preprocess_IfNotDef, - Preprocess_ElIf, - Preprocess_Else, - Preprocess_EndIf, - Preprocess_Include, - Preprocess_Pragma, - Preprocess_Content, - Preprocess_Macro, - Preprocess_Unsupported, - Spec_Alignas, - Spec_Const, - Spec_Consteval, - Spec_Constexpr, - Spec_Constinit, - Spec_Explicit, - Spec_Extern, - Spec_Final, - Spec_ForceInline, - Spec_Global, - Spec_Inline, - Spec_Internal_Linkage, - Spec_LocalPersist, - Spec_Mutable, - Spec_NeverInline, - Spec_Override, - Spec_Static, - Spec_ThreadLocal, - Spec_Volatile, - Spec_Virtual, - Star, - Statement_End, - StaticAssert, - String, - Type_Typename, - Type_Unsigned, - Type_Signed, - Type_Short, - Type_Long, - Type_bool, - Type_char, - Type_int, - Type_double, - Type_MS_int8, - Type_MS_int16, - Type_MS_int32, - Type_MS_int64, - Type_MS_W64, - Varadic_Argument, - __Attributes_Start, - Attribute_API_Export, - Attribute_API_Import, - NumTokens + enum Type : u32 + { + Invalid, + Access_Private, + Access_Protected, + Access_Public, + Access_MemberSymbol, + Access_StaticSymbol, + Ampersand, + Ampersand_DBL, + Assign_Classifer, + Attribute_Open, + Attribute_Close, + BraceCurly_Open, + BraceCurly_Close, + BraceSquare_Open, + BraceSquare_Close, + Capture_Start, + Capture_End, + Comment, + Comment_End, + Comment_Start, + Char, + Comma, + Decl_Class, + Decl_GNU_Attribute, + Decl_MSVC_Attribute, + Decl_Enum, + Decl_Extern_Linkage, + Decl_Friend, + Decl_Module, + Decl_Namespace, + Decl_Operator, + Decl_Struct, + Decl_Template, + Decl_Typedef, + Decl_Using, + Decl_Union, + Identifier, + Module_Import, + Module_Export, + NewLine, + Number, + Operator, + Preprocess_Hash, + Preprocess_Define, + Preprocess_If, + Preprocess_IfDef, + Preprocess_IfNotDef, + Preprocess_ElIf, + Preprocess_Else, + Preprocess_EndIf, + Preprocess_Include, + Preprocess_Pragma, + Preprocess_Content, + Preprocess_Macro, + Preprocess_Unsupported, + Spec_Alignas, + Spec_Const, + Spec_Consteval, + Spec_Constexpr, + Spec_Constinit, + Spec_Explicit, + Spec_Extern, + Spec_Final, + Spec_ForceInline, + Spec_Global, + Spec_Inline, + Spec_Internal_Linkage, + Spec_LocalPersist, + Spec_Mutable, + Spec_NeverInline, + Spec_Override, + Spec_Static, + Spec_ThreadLocal, + Spec_Volatile, + Spec_Virtual, + Star, + Statement_End, + StaticAssert, + String, + Type_Typename, + Type_Unsigned, + Type_Signed, + Type_Short, + Type_Long, + Type_bool, + Type_char, + Type_int, + Type_double, + Type_MS_int8, + Type_MS_int16, + Type_MS_int32, + Type_MS_int64, + Type_MS_W64, + Varadic_Argument, + __Attributes_Start, + Attribute_API_Export, + Attribute_API_Import, + NumTokens + }; + + inline StrC to_str( Type type ) + { + local_persist StrC lookup[] { + { sizeof( "__invalid__" ), "__invalid__" }, + { sizeof( "private" ), "private" }, + { sizeof( "protected" ), "protected" }, + { sizeof( "public" ), "public" }, + { sizeof( "." ), "." }, + { sizeof( "::" ), "::" }, + { sizeof( "&" ), "&" }, + { sizeof( "&&" ), "&&" }, + { sizeof( ":" ), ":" }, + { sizeof( "[[" ), "[[" }, + { sizeof( "]]" ), "]]" }, + { sizeof( "{" ), "{" }, + { sizeof( "}" ), "}" }, + { sizeof( "[" ), "[" }, + { sizeof( "]" ), "]" }, + { sizeof( "(" ), "(" }, + { sizeof( ")" ), ")" }, + { sizeof( "__comment__" ), "__comment__" }, + { sizeof( "__comment_end__" ), "__comment_end__" }, + { sizeof( "__comment_start__" ), "__comment_start__" }, + { sizeof( "__character__" ), "__character__" }, + { sizeof( "," ), "," }, + { sizeof( "class" ), "class" }, + { sizeof( "__attribute__" ), "__attribute__" }, + { sizeof( "__declspec" ), "__declspec" }, + { sizeof( "enum" ), "enum" }, + { sizeof( "extern" ), "extern" }, + { sizeof( "friend" ), "friend" }, + { sizeof( "module" ), "module" }, + { sizeof( "namespace" ), "namespace" }, + { sizeof( "operator" ), "operator" }, + { sizeof( "struct" ), "struct" }, + { sizeof( "template" ), "template" }, + { sizeof( "typedef" ), "typedef" }, + { sizeof( "using" ), "using" }, + { sizeof( "union" ), "union" }, + { sizeof( "__identifier__" ), "__identifier__" }, + { sizeof( "import" ), "import" }, + { sizeof( "export" ), "export" }, + { sizeof( "__new_line__" ), "__new_line__" }, + { sizeof( "__number__" ), "__number__" }, + { sizeof( "__operator__" ), "__operator__" }, + { sizeof( "#" ), "#" }, + { sizeof( "define" ), "define" }, + { sizeof( "if" ), "if" }, + { sizeof( "ifdef" ), "ifdef" }, + { sizeof( "ifndef" ), "ifndef" }, + { sizeof( "elif" ), "elif" }, + { sizeof( "else" ), "else" }, + { sizeof( "endif" ), "endif" }, + { sizeof( "include" ), "include" }, + { sizeof( "pragma" ), "pragma" }, + { sizeof( "__macro_content__" ), "__macro_content__" }, + { sizeof( "__macro__" ), "__macro__" }, + { sizeof( "__unsupported__" ), "__unsupported__" }, + { sizeof( "alignas" ), "alignas" }, + { sizeof( "const" ), "const" }, + { sizeof( "consteval" ), "consteval" }, + { sizeof( "constexpr" ), "constexpr" }, + { sizeof( "constinit" ), "constinit" }, + { sizeof( "explicit" ), "explicit" }, + { sizeof( "extern" ), "extern" }, + { sizeof( "final" ), "final" }, + { sizeof( "forceinline" ), "forceinline" }, + { sizeof( "global" ), "global" }, + { sizeof( "inline" ), "inline" }, + { sizeof( "internal" ), "internal" }, + { sizeof( "local_persist" ), "local_persist" }, + { sizeof( "mutable" ), "mutable" }, + { sizeof( "neverinline" ), "neverinline" }, + { sizeof( "override" ), "override" }, + { sizeof( "static" ), "static" }, + { sizeof( "thread_local" ), "thread_local" }, + { sizeof( "volatile" ), "volatile" }, + { sizeof( "virtual" ), "virtual" }, + { sizeof( "*" ), "*" }, + { sizeof( ";" ), ";" }, + { sizeof( "static_assert" ), "static_assert" }, + { sizeof( "__string__" ), "__string__" }, + { sizeof( "typename" ), "typename" }, + { sizeof( "unsigned" ), "unsigned" }, + { sizeof( "signed" ), "signed" }, + { sizeof( "short" ), "short" }, + { sizeof( "long" ), "long" }, + { sizeof( "bool" ), "bool" }, + { sizeof( "char" ), "char" }, + { sizeof( "int" ), "int" }, + { sizeof( "double" ), "double" }, + { sizeof( "__int8" ), "__int8" }, + { sizeof( "__int16" ), "__int16" }, + { sizeof( "__int32" ), "__int32" }, + { sizeof( "__int64" ), "__int64" }, + { sizeof( "_W64" ), "_W64" }, + { sizeof( "..." ), "..." }, + { sizeof( "__attrib_start__" ), "__attrib_start__" }, + { sizeof( "GEN_API_Export_Code" ), "GEN_API_Export_Code" }, + { sizeof( "GEN_API_Import_Code" ), "GEN_API_Import_Code" }, }; + return lookup[type]; + } - inline StrC to_str( Type type ) + inline Type to_type( StrC str ) + { + local_persist u32 keymap[NumTokens]; + do_once_start for ( u32 index = 0; index < NumTokens; index++ ) { - local_persist StrC lookup[] { - { sizeof( "__invalid__" ), "__invalid__" }, - { sizeof( "private" ), "private" }, - { sizeof( "protected" ), "protected" }, - { sizeof( "public" ), "public" }, - { sizeof( "." ), "." }, - { sizeof( "::" ), "::" }, - { sizeof( "&" ), "&" }, - { sizeof( "&&" ), "&&" }, - { sizeof( ":" ), ":" }, - { sizeof( "[[" ), "[[" }, - { sizeof( "]]" ), "]]" }, - { sizeof( "{" ), "{" }, - { sizeof( "}" ), "}" }, - { sizeof( "[" ), "[" }, - { sizeof( "]" ), "]" }, - { sizeof( "(" ), "(" }, - { sizeof( ")" ), ")" }, - { sizeof( "__comment__" ), "__comment__" }, - { sizeof( "__comment_end__" ), "__comment_end__" }, - { sizeof( "__comment_start__" ), "__comment_start__" }, - { sizeof( "__character__" ), "__character__" }, - { sizeof( "," ), "," }, - { sizeof( "class" ), "class" }, - { sizeof( "__attribute__" ), "__attribute__" }, - { sizeof( "__declspec" ), "__declspec" }, - { sizeof( "enum" ), "enum" }, - { sizeof( "extern" ), "extern" }, - { sizeof( "friend" ), "friend" }, - { sizeof( "module" ), "module" }, - { sizeof( "namespace" ), "namespace" }, - { sizeof( "operator" ), "operator" }, - { sizeof( "struct" ), "struct" }, - { sizeof( "template" ), "template" }, - { sizeof( "typedef" ), "typedef" }, - { sizeof( "using" ), "using" }, - { sizeof( "union" ), "union" }, - { sizeof( "__identifier__" ), "__identifier__" }, - { sizeof( "import" ), "import" }, - { sizeof( "export" ), "export" }, - { sizeof( "__new_line__" ), "__new_line__" }, - { sizeof( "__number__" ), "__number__" }, - { sizeof( "__operator__" ), "__operator__" }, - { sizeof( "#" ), "#" }, - { sizeof( "define" ), "define" }, - { sizeof( "if" ), "if" }, - { sizeof( "ifdef" ), "ifdef" }, - { sizeof( "ifndef" ), "ifndef" }, - { sizeof( "elif" ), "elif" }, - { sizeof( "else" ), "else" }, - { sizeof( "endif" ), "endif" }, - { sizeof( "include" ), "include" }, - { sizeof( "pragma" ), "pragma" }, - { sizeof( "__macro_content__" ), "__macro_content__" }, - { sizeof( "__macro__" ), "__macro__" }, - { sizeof( "__unsupported__" ), "__unsupported__" }, - { sizeof( "alignas" ), "alignas" }, - { sizeof( "const" ), "const" }, - { sizeof( "consteval" ), "consteval" }, - { sizeof( "constexpr" ), "constexpr" }, - { sizeof( "constinit" ), "constinit" }, - { sizeof( "explicit" ), "explicit" }, - { sizeof( "extern" ), "extern" }, - { sizeof( "final" ), "final" }, - { sizeof( "forceinline" ), "forceinline" }, - { sizeof( "global" ), "global" }, - { sizeof( "inline" ), "inline" }, - { sizeof( "internal" ), "internal" }, - { sizeof( "local_persist" ), "local_persist" }, - { sizeof( "mutable" ), "mutable" }, - { sizeof( "neverinline" ), "neverinline" }, - { sizeof( "override" ), "override" }, - { sizeof( "static" ), "static" }, - { sizeof( "thread_local" ), "thread_local" }, - { sizeof( "volatile" ), "volatile" }, - { sizeof( "virtual" ), "virtual" }, - { sizeof( "*" ), "*" }, - { sizeof( ";" ), ";" }, - { sizeof( "static_assert" ), "static_assert" }, - { sizeof( "__string__" ), "__string__" }, - { sizeof( "typename" ), "typename" }, - { sizeof( "unsigned" ), "unsigned" }, - { sizeof( "signed" ), "signed" }, - { sizeof( "short" ), "short" }, - { sizeof( "long" ), "long" }, - { sizeof( "bool" ), "bool" }, - { sizeof( "char" ), "char" }, - { sizeof( "int" ), "int" }, - { sizeof( "double" ), "double" }, - { sizeof( "__int8" ), "__int8" }, - { sizeof( "__int16" ), "__int16" }, - { sizeof( "__int32" ), "__int32" }, - { sizeof( "__int64" ), "__int64" }, - { sizeof( "_W64" ), "_W64" }, - { sizeof( "..." ), "..." }, - { sizeof( "__attrib_start__" ), "__attrib_start__" }, - { sizeof( "GEN_API_Export_Code" ), "GEN_API_Export_Code" }, - { sizeof( "GEN_API_Import_Code" ), "GEN_API_Import_Code" }, - }; - return lookup[type]; + StrC enum_str = to_str( (Type)index ); + keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 ); } - - inline Type to_type( StrC str ) + do_once_end u32 hash = crc32( str.Ptr, str.Len ); + for ( u32 index = 0; index < NumTokens; index++ ) { - local_persist u32 keymap[NumTokens]; - do_once_start for ( u32 index = 0; index < NumTokens; index++ ) - { - StrC enum_str = to_str( (Type)index ); - keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 ); - } - do_once_end u32 hash = crc32( str.Ptr, str.Len ); - for ( u32 index = 0; index < NumTokens; index++ ) - { - if ( keymap[index] == hash ) - return (Type)index; - } - return Invalid; + if ( keymap[index] == hash ) + return (Type)index; } + return Invalid; + } - } // namespace ETokType +} // namespace ETokType - using TokType = ETokType::Type; - -} // namespace parser +using TokType = ETokType::Type; +GEN_NS_PARSER_END diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index 4d18903..1abc9eb 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -10,23 +10,24 @@ CodeClass parse_class( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - parser::TokArray toks = parser::lex( def ); + TokArray toks = lex( def ); if ( toks.Arr == nullptr ) return InvalidCode; - parser::Context.Tokens = toks; + Context.Tokens = toks; push_scope(); - CodeClass result = (CodeClass) parser::parse_class_struct( parser::TokType::Decl_Class ); - parser::Context.pop(); + CodeClass result = (CodeClass) parse_class_struct( parser::TokType::Decl_Class ); + pop(& Context); return result; } CodeConstructor parse_constructor( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -58,8 +59,8 @@ CodeConstructor parse_constructor( StrC def ) break; default : - log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -85,8 +86,8 @@ CodeConstructor parse_constructor( StrC def ) CodeDestructor parse_destructor( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -102,13 +103,13 @@ CodeDestructor parse_destructor( StrC def ) CodeEnum parse_enum( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) { - Context.pop(); + pop(& Context); return InvalidCode; } @@ -118,8 +119,8 @@ CodeEnum parse_enum( StrC def ) CodeBody parse_export_body( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -131,8 +132,8 @@ CodeBody parse_export_body( StrC def ) CodeExtern parse_extern_link( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -144,8 +145,8 @@ CodeExtern parse_extern_link( StrC def ) CodeFriend parse_friend( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -157,8 +158,8 @@ CodeFriend parse_friend( StrC def ) CodeFn parse_function( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -170,8 +171,8 @@ CodeFn parse_function( StrC def ) CodeBody parse_global_body( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -180,14 +181,14 @@ CodeBody parse_global_body( StrC def ) Context.Tokens = toks; push_scope(); CodeBody result = parse_global_nspace( ECode::Global_Body ); - Context.pop(); + pop(& Context); return result; } CodeNS parse_namespace( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -199,8 +200,8 @@ CodeNS parse_namespace( StrC def ) CodeOperator parse_operator( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -212,8 +213,8 @@ CodeOperator parse_operator( StrC def ) CodeOpCast parse_operator_cast( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -225,8 +226,8 @@ CodeOpCast parse_operator_cast( StrC def ) CodeStruct parse_struct( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -235,14 +236,14 @@ CodeStruct parse_struct( StrC def ) Context.Tokens = toks; push_scope(); CodeStruct result = (CodeStruct) parse_class_struct( TokType::Decl_Struct ); - Context.pop(); + pop(& Context); return result; } CodeTemplate parse_template( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -254,8 +255,8 @@ CodeTemplate parse_template( StrC def ) CodeType parse_type( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -267,8 +268,8 @@ CodeType parse_type( StrC def ) CodeTypedef parse_typedef( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -280,8 +281,8 @@ CodeTypedef parse_typedef( StrC def ) CodeUnion parse_union( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -293,8 +294,8 @@ CodeUnion parse_union( StrC def ) CodeUsing parse_using( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -306,8 +307,8 @@ CodeUsing parse_using( StrC def ) CodeVar parse_variable( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 6aa0646..3eae336 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -499,7 +499,7 @@ CodeConstructor def_constructor( Opts_def_constructor p ) { CodeParam params = p.params; Code initializer_list = p.initializer_list; - Code body = p.body; + Code body = p.body; if ( params && params->Type != ECode::Parameters ) { diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 5d82209..ddf47da 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -450,7 +450,7 @@ s32 lex_preprocessor_directive( LexContext* ctx ) } forceinline -void lex_found_token( LexContext* ctx ) +void lex_found_token( LexContext* ctx ) { if ( ctx->token.Type != TokType::Invalid ) { diff --git a/project/components/parser.cpp b/project/components/parser.cpp index a9c2a2a..88b4032 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -25,81 +25,81 @@ struct ParseContext { TokArray Tokens; StackNode* Scope; - - void push( StackNode* node ) - { - node->Prev = Scope; - Scope = node; - - #if 0 && Build_Debug - log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); - #endif - } - - void pop() - { - #if 0 && Build_Debug - log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); - #endif - Scope = Scope->Prev; - } - - String to_string() - { - String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); - - Token scope_start = Scope->Start; - Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : (* current(& Tokens, true)); - - sptr length = scope_start.Length; - char const* current = scope_start.Text + length; - while ( current <= back( & Tokens.Arr)->Text && *current != '\n' && length < 74 ) - { - current++; - length++; - } - - String line = string_make( GlobalAllocator, { length, scope_start.Text } ); - append_fmt( & result, "\tScope : %s\n", line ); - free(& line); - - sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; - sptr length_from_err = dist; - String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); - - if ( length_from_err < 100 ) - append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); - else - append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); - - StackNode* curr_scope = Scope; - s32 level = 0; - do - { - if ( is_valid(curr_scope->Name) ) - { - append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); - } - else - { - append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); - } - - curr_scope = curr_scope->Prev; - level++; - } - while ( curr_scope ); - return result; - } }; +void push( ParseContext* ctx, StackNode* node ) +{ + node->Prev = ctx->Scope; + ctx->Scope = node; + +#if 0 && Build_Debug + log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); +#endif +} + +void pop(ParseContext* ctx) +{ +#if 0 && Build_Debug + log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); +#endif + ctx->Scope = ctx->Scope->Prev; +} + +String to_string(ParseContext ctx) +{ + String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); + + Token scope_start = ctx.Scope->Start; + Token last_valid = ctx.Tokens.Idx >= num(ctx.Tokens.Arr) ? ctx.Tokens.Arr[num(ctx.Tokens.Arr) -1] : (* current(& ctx.Tokens, true)); + + sptr length = scope_start.Length; + char const* current = scope_start.Text + length; + while ( current <= back( & ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 ) + { + current++; + length++; + } + + String line = string_make( GlobalAllocator, { length, scope_start.Text } ); + append_fmt( & result, "\tScope : %s\n", line ); + free(& line); + + sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; + sptr length_from_err = dist; + String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); + + if ( length_from_err < 100 ) + append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); + else + append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); + + StackNode* curr_scope = ctx.Scope; + s32 level = 0; + do + { + if ( is_valid(curr_scope->Name) ) + { + append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); + } + else + { + append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); + } + + curr_scope = curr_scope->Prev; + level++; + } + while ( curr_scope ); + return result; +} + global ParseContext Context; bool __eat(TokArray* self, TokType type ) { if ( num(self->Arr) - self->Idx <= 0 ) { - log_failure( "No tokens left.\n%s", Context.to_string() ); + log_failure( "No tokens left.\n%s", to_string(Context) ); return false; } @@ -119,7 +119,7 @@ bool __eat(TokArray* self, TokType type ) , at_idx.Length, at_idx.Text , tok.Line , tok.Column - , Context.to_string() + , to_string(Context) ); return false; @@ -152,22 +152,26 @@ void deinit() #pragma region Helper Macros -# define check_parse_args( def ) \ -if ( def.Len <= 0 ) \ -{ \ - log_failure( "gen::" stringize(__func__) ": length must greater than 0" ); \ - parser::Context.pop(); \ - return InvalidCode; \ -} \ -if ( def.Ptr == nullptr ) \ -{ \ - log_failure( "gen::" stringize(__func__) ": def was null" ); \ - parser::Context.pop(); \ - return InvalidCode; \ +#define check_parse_args( def ) _check_parse_args(def, stringize(_func_) ) +bool _check_parse_args( StrC def, char const* func_name ) +{ + if ( def.Len <= 0 ) + { + log_failure( str_fmt_buf("gen::%s: length must greater than 0", func_name) ); + pop(& Context); + return false; + } + if ( def.Ptr == nullptr ) + { + log_failure( str_fmt_buf("gen::%s: def was null", func_name) ); + pop(& Context); + return false; + } + return true; } -# define currtok_noskip (* parser::current( & parser::Context.Tokens, parser::dont_skip_formatting )) -# define currtok (* current( & Context.Tokens, skip_formatting )) +# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) +# define currtok (* current( & Context.Tokens, skip_formatting )) # define prevtok (* previous( Context.Tokens, dont_skip_formatting)) # define nexttok (* next( Context.Tokens, skip_formatting )) # define eat( Type_ ) __eat( & Context.Tokens, Type_ ) @@ -184,7 +188,7 @@ if ( def.Ptr == nullptr ) \ # define push_scope() \ parser::StackNode scope { nullptr, currtok_noskip, parser::NullToken, txt( __func__ ) }; \ - parser::Context.push( & scope ) + push( & parser::Context, & scope ) #pragma endregion Helper Macros @@ -496,7 +500,7 @@ Code parse_array_decl() eat( TokType::Operator ); // [] - Context.pop(); + pop(& Context); return array_expr; } @@ -507,15 +511,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } if ( currtok.Type == TokType::BraceSquare_Close ) { - log_failure( "Error, empty array expression in definition\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, empty array expression in definition\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -533,15 +537,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration, expected ]\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of array declaration, expected ]\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } if ( currtok.Type != TokType::BraceSquare_Close ) { - log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -557,11 +561,11 @@ Code parse_array_decl() array_expr->Next.ast = adjacent_arr_expr.ast; } - Context.pop(); + pop(& Context); return array_expr; } - Context.pop(); + pop(& Context); return { nullptr }; } @@ -659,7 +663,7 @@ CodeAttributes parse_attributes() if ( len > 0 ) { StrC attribute_txt = { len, start.Text }; - Context.pop(); + pop(& Context); String name_stripped = strip_formatting( attribute_txt, strip_formatting_dont_preserve_newlines ); @@ -672,7 +676,7 @@ CodeAttributes parse_attributes() return ( CodeAttributes )result; } - Context.pop(); + pop(& Context); return { nullptr }; } @@ -681,7 +685,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) { if ( which != TokType::Decl_Class && which != TokType::Decl_Struct ) { - log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), Context.to_string() ); + log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), to_string(Context) ); return InvalidCode; } @@ -904,7 +908,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) case TokType::Operator: if ( currtok.Text[0] != '~' ) { - log_failure( "Operator token found in global body but not destructor unary negation\n%s", Context.to_string() ); + log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); return InvalidCode; } @@ -1017,8 +1021,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; default: - log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1114,8 +1118,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Failed to parse member\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1124,15 +1128,14 @@ CodeBody parse_class_struct_body( TokType which, Token name ) eat( TokType::BraceCurly_Close ); // { } - Context.pop(); + pop(& Context); return result; } internal CodeComment parse_comment() { - StackNode scope { nullptr, currtok_noskip, NullToken, txt( __func__ ) }; - Context.push( & scope ); + push_scope(); CodeComment result = (CodeComment) make_code(); @@ -1142,7 +1145,7 @@ CodeComment parse_comment() // result->Token = currtok_noskip; eat( TokType::Comment ); - Context.pop(); + pop(& Context); return result; } @@ -1174,7 +1177,7 @@ Code parse_complicated_definition( TokType which ) // Its a forward declaration only Code result = parse_forward_or_definition( which, is_inplace ); // ; - Context.pop(); + pop(& Context); return result; } @@ -1198,12 +1201,12 @@ Code parse_complicated_definition( TokType which ) Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // , or Name> ... - Context.pop(); + pop(& Context); return result; } - log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } if ( tok.Type == TokType::Identifier ) @@ -1235,7 +1238,7 @@ Code parse_complicated_definition( TokType which ) // : ; ok_to_parse = true; Code result = parse_enum(); - Context.pop(); + pop(& Context); return result; } else if ( is_indirection ) @@ -1247,14 +1250,14 @@ Code parse_complicated_definition( TokType which ) if ( ! ok_to_parse ) { - log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // , or Name> ... - Context.pop(); + pop(& Context); return result; } else if ( tok.Type >= TokType::Type_Unsigned && tok.Type <= TokType::Type_MS_W64 ) @@ -1266,8 +1269,8 @@ Code parse_complicated_definition( TokType which ) && ( tokens.Arr[idx - 4].Type != which)) ) { - log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1275,7 +1278,7 @@ Code parse_complicated_definition( TokType which ) // : ; // : ; Code result = parse_enum(); - Context.pop(); + pop(& Context); return result; } else if ( tok.Type == TokType::BraceCurly_Close ) @@ -1283,7 +1286,7 @@ Code parse_complicated_definition( TokType which ) // Its a definition Code result = parse_forward_or_definition( which, is_inplace ); // { ... }; - Context.pop(); + pop(& Context); return result; } else if ( tok.Type == TokType::BraceSquare_Close ) @@ -1291,13 +1294,13 @@ Code parse_complicated_definition( TokType which ) // Its an array definition Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // [ ... ]; - Context.pop(); + pop(& Context); return result; } else { - log_failure( "Unsupported or bad member definition after %s declaration\n%S", to_str(which).Ptr, Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%S", to_str(which).Ptr, to_string(Context) ); + pop(& Context); return InvalidCode; } } @@ -1315,8 +1318,8 @@ CodeDefine parse_define() if ( ! check( TokType::Identifier ) ) { - log_failure( "Error, expected identifier after #define\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, expected identifier after #define\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1327,8 +1330,8 @@ CodeDefine parse_define() if ( ! check( TokType::Preprocess_Content )) { - log_failure( "Error, expected content after #define %s\n%s", define->Name, Context.to_string() ); - Context.pop(); + log_failure( "Error, expected content after #define %s\n%s", define->Name, to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1338,7 +1341,7 @@ CodeDefine parse_define() eat( TokType::Preprocess_Content ); // #define - Context.pop(); + pop(& Context); return define; } @@ -1346,7 +1349,7 @@ CodeDefine parse_define() eat( TokType::Preprocess_Content ); // #define - Context.pop(); + pop(& Context); return define; } @@ -1362,8 +1365,8 @@ Code parse_assignment_expression() if ( currtok.Type == TokType::Statement_End && currtok.Type != TokType::Comma ) { - log_failure( "Expected expression after assignment operator\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Expected expression after assignment operator\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1414,7 +1417,7 @@ Code parse_forward_or_definition( TokType which, bool is_inplace ) default: log_failure( "Error, wrong token type given to parse_complicated_definition " "(only supports class, enum, struct, union) \n%s" - , Context.to_string() ); + , to_string(Context) ); return InvalidCode; } @@ -1456,7 +1459,7 @@ CodeFn parse_function_after_name( body = parse_function_body(); if ( body == Code_Invalid ) { - Context.pop(); + pop(& Context); return InvalidCode; } // ( ) { } @@ -1507,8 +1510,8 @@ CodeFn parse_function_after_name( default: { - log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", debug_str(body), Context.to_string()); - Context.pop(); + log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", debug_str(body), to_string(Context)); + pop(& Context); return InvalidCode; } } @@ -1535,7 +1538,7 @@ CodeFn parse_function_after_name( if ( inline_cmt ) result->InlineCmt = inline_cmt; - Context.pop(); + pop(& Context); return result; } @@ -1577,7 +1580,7 @@ Code parse_function_body() eat( TokType::BraceCurly_Close ); - Context.pop(); + pop(& Context); return result; } @@ -1643,7 +1646,7 @@ CodeBody parse_global_nspace( CodeT which ) case TokType::Decl_Extern_Linkage: if ( which == Extern_Linkage_Body ) - log_failure( "Nested extern linkage\n%s", Context.to_string() ); + log_failure( "Nested extern linkage\n%s", to_string(Context) ); member = parse_extern_link(); // extern "..." { ... } @@ -1731,7 +1734,7 @@ CodeBody parse_global_nspace( CodeT which ) case TokType::Module_Export: if ( which == Export_Body ) - log_failure( "Nested export declaration\n%s", Context.to_string() ); + log_failure( "Nested export declaration\n%s", to_string(Context) ); member = parse_export_body(); // export { ... } @@ -1800,8 +1803,8 @@ CodeBody parse_global_nspace( CodeT which ) default: StrC spec_str = ESpecifier::to_str(spec); - log_failure( "Invalid specifier %.*s for variable\n%s", spec_str.Len, spec_str, Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %.*s for variable\n%s", spec_str.Len, spec_str, to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1876,8 +1879,8 @@ CodeBody parse_global_nspace( CodeT which ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Failed to parse member\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1889,7 +1892,7 @@ CodeBody parse_global_nspace( CodeT which ) eat( TokType::BraceCurly_Close ); // { } - Context.pop(); + pop(& Context); return result; } @@ -2044,8 +2047,8 @@ Token parse_identifier( bool* possible_member_function ) if ( left == 0 ) { - log_failure( "Error, unexpected end of static symbol identifier\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of static symbol identifier\n%s", to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2055,12 +2058,12 @@ Token parse_identifier( bool* possible_member_function ) if (is_destructor) { name.Length = ( ( sptr )prevtok.Text + prevtok.Length ) - ( sptr )name.Text; - Context.pop(); + pop(& Context); return name; } - log_failure( "Error, had a ~ operator after %S but not a destructor\n%s", ETokType::to_str( prevtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "Error, had a ~ operator after %S but not a destructor\n%s", ETokType::to_str( prevtok.Type ), to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2071,16 +2074,16 @@ Token parse_identifier( bool* possible_member_function ) else { - log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } } if ( currtok.Type != TokType::Identifier ) { - log_failure( "Error, expected static symbol identifier, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "Error, expected static symbol identifier, not %s\n%s", ETokType::to_str( currtok.Type ), to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2093,7 +2096,7 @@ Token parse_identifier( bool* possible_member_function ) } //