#ifdef GEN_INTELLISENSE_DIRECTIVES #pragma once #include "gen/etoktype.cpp" #include "interface.upfront.cpp" #include "lexer.cpp" #endif GEN_NS_PARSER_BEGIN // TODO(Ed) : Rename ETok_Capture_Start, ETok_Capture_End to Open_Parenthesis adn Close_Parenthesis constexpr bool lex_dont_skip_formatting = false; constexpr bool lex_skip_formatting = true; struct StackNode { StackNode* Prev; Token Start; Token Name; // The name of the AST node (if parsed) Str ProcName; // The name of the procedure }; struct ParseContext { TokArray Tokens; StackNode* Scope; }; void parser_push( ParseContext* ctx, StackNode* node ) { node->Prev = ctx->Scope; ctx->Scope = node; #if 0 && GEN_BUILD_DEBUG log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); #endif } void parser_pop(ParseContext* ctx) { #if 0 && GEN_BUILD_DEBUG log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); #endif ctx->Scope = ctx->Scope->Prev; } StrBuilder parser_to_strbuilder(ParseContext ctx) { StrBuilder result = strbuilder_make_reserve( GlobalAllocator, kilobytes(4) ); Token scope_start = ctx.Scope->Start; Token last_valid = ctx.Tokens.Idx >= array_num(ctx.Tokens.Arr) ? ctx.Tokens.Arr[array_num(ctx.Tokens.Arr) -1] : (* lex_current(& ctx.Tokens, true)); sptr length = scope_start.Text.Len; char const* current = scope_start.Text.Ptr + length; while ( current <= array_back( ctx.Tokens.Arr)->Text.Ptr && (* current) != '\n' && length < 74 ) { current++; length++; } Str scope_str = { scope_start.Text.Ptr, length }; StrBuilder line = strbuilder_make_str( GlobalAllocator, scope_str ); strbuilder_append_fmt( & result, "\tScope : %s\n", line ); strbuilder_free(& line); sptr dist = (sptr)last_valid.Text.Ptr - (sptr)scope_start.Text.Ptr + 2; sptr length_from_err = dist; Str err_str = { last_valid.Text.Ptr, length_from_err }; StrBuilder line_from_err = strbuilder_make_str( GlobalAllocator, err_str ); if ( length_from_err < 100 ) strbuilder_append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); else strbuilder_append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); StackNode* curr_scope = ctx.Scope; s32 level = 0; do { if ( tok_is_valid(curr_scope->Name) ) { strbuilder_append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Text.Len, curr_scope->Name.Text.Ptr ); } else { strbuilder_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 lex__eat(TokArray* self, TokType type ) { if ( array_num(self->Arr) - self->Idx <= 0 ) { log_failure( "No tokens left.\n%s", parser_to_strbuilder(Context) ); return false; } Token at_idx = self->Arr[ self->Idx ]; if ( ( at_idx.Type == Tok_NewLine && type != Tok_NewLine ) || ( at_idx.Type == Tok_Comment && type != Tok_Comment ) ) { self->Idx ++; } if ( at_idx.Type != type ) { Token tok = * lex_current( self, lex_skip_formatting ); log_failure( "Parse Error, TokArray::eat, Expected: ' %s ' not ' %.*s ' (%d, %d)`\n%s" , toktype_to_str(type).Ptr , at_idx.Text.Len, at_idx.Text.Ptr , tok.Line , tok.Column , parser_to_strbuilder(Context) ); GEN_DEBUG_TRAP(); return false; } #if 0 && GEN_BUILD_DEBUG log_fmt("Ate: %SB\n", self->Arr[Idx].to_strbuilder() ); #endif self->Idx ++; return true; } internal void parser_init() { Lexer_Tokens = array_init_reserve(Token, arena_allocator_info( & LexArena) , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) ); fixed_arena_init(& Lexer_defines_map_arena); Lexer_defines = hashtable_init_reserve(Str, fixed_arena_allocator_info( & Lexer_defines_map_arena), 256 ); } internal void parser_deinit() { Array(Token) null_array = { nullptr }; Lexer_Tokens = null_array; } #pragma region Helper Macros #define check_parse_args( def ) _check_parse_args(def, stringize(_func_) ) bool _check_parse_args( Str def, char const* func_name ) { if ( def.Len <= 0 ) { log_failure( c_str_fmt_buf("gen::%s: length must greater than 0", func_name) ); parser_pop(& Context); return false; } if ( def.Ptr == nullptr ) { log_failure( c_str_fmt_buf("gen::%s: def was null", func_name) ); parser_pop(& Context); return false; } return true; } # define currtok_noskip (* lex_current( & Context.Tokens, lex_dont_skip_formatting )) # define currtok (* lex_current( & Context.Tokens, lex_skip_formatting )) # define peektok (* lex_peek(Context.Tokens, lex_skip_formatting)) # define prevtok (* lex_previous( Context.Tokens, lex_dont_skip_formatting)) # define nexttok (* lex_next( Context.Tokens, lex_skip_formatting )) # define nexttok_noskip (* lex_next( Context.Tokens, lex_dont_skip_formatting)) # define eat( Type_ ) lex__eat( & Context.Tokens, Type_ ) # define left ( array_num(Context.Tokens.Arr) - Context.Tokens.Idx ) #if GEN_COMPILER_CPP # define def_assign( ... ) { __VA_ARGS__ } #else # define def_assign( ... ) __VA_ARGS__ #endif #ifdef check #define CHECK_WAS_DEFINED #pragma push_macro("check") #undef check #endif # define check_noskip( Type_ ) ( left && currtok_noskip.Type == Type_ ) # define check( Type_ ) ( left && currtok.Type == Type_ ) # define push_scope() \ GEN_NS_PARSER StackNode scope = { nullptr, currtok_noskip, GEN_NS_PARSER NullToken, txt( __func__ ) }; \ parser_push( & GEN_NS_PARSER Context, & scope ) #pragma endregion Helper Macros // Procedure Forwards ( Entire parser internal parser interface ) internal Code parse_array_decl (); internal CodeAttributes parse_attributes (); internal CodeComment parse_comment (); internal Code parse_complicated_definition ( TokType which ); internal CodeBody parse_class_struct_body ( TokType which, Token name ); internal Code parse_class_struct ( TokType which, bool inplace_def ); internal CodeDefine parse_define (); internal Code parse_expression (); internal Code parse_forward_or_definition ( TokType which, bool is_inplace ); internal CodeFn parse_function_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type, Token name ); internal Code parse_function_body (); internal CodeBody parse_global_nspace ( CodeType which ); internal Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ); internal Token parse_identifier ( bool* possible_member_function ); internal CodeInclude parse_include (); internal CodeOperator parse_operator_after_ret_type ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type ); internal Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifiers specifiers ); internal CodePragma parse_pragma (); internal CodeParams parse_params ( bool use_template_capture ); internal CodePreprocessCond parse_preprocess_cond (); internal Code parse_simple_preprocess ( TokType which, bool dont_consume_braces ); internal Code parse_static_assert (); internal void parse_template_args ( Token* token ); internal CodeVar parse_variable_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename type, Str name ); internal CodeVar parse_variable_declaration_list (); internal CodeClass parser_parse_class ( bool inplace_def ); internal CodeConstructor parser_parse_constructor ( CodeSpecifiers specifiers ); internal CodeDestructor parser_parse_destructor ( CodeSpecifiers specifiers ); internal CodeEnum parser_parse_enum ( bool inplace_def ); internal CodeBody parser_parse_export_body (); internal CodeBody parser_parse_extern_link_body(); internal CodeExtern parser_parse_extern_link (); internal CodeFriend parser_parse_friend (); internal CodeFn parser_parse_function (); internal CodeNS parser_parse_namespace (); internal CodeOpCast parser_parse_operator_cast ( CodeSpecifiers specifiers ); internal CodeStruct parser_parse_struct ( bool inplace_def ); internal CodeVar parser_parse_variable (); internal CodeTemplate parser_parse_template (); internal CodeTypename parser_parse_type ( bool from_template, bool* is_function ); internal CodeTypedef parser_parse_typedef (); internal CodeUnion parser_parse_union ( bool inplace_def ); internal CodeUsing parser_parse_using (); constexpr bool parser_inplace_def = true; constexpr bool parser_not_inplace_def = false; constexpr bool parser_dont_consume_braces = true; constexpr bool parser_consume_braces = false; constexpr bool parser_not_from_template = false; constexpr bool parser_use_parenthesis = false; // Internal parsing functions constexpr bool parser_strip_formatting_dont_preserve_newlines = false; /* This function was an attempt at stripping formatting from any c++ code. It has edge case failures that prevent it from being used in function bodies. */ internal StrBuilder parser_strip_formatting( Str raw_text, bool preserve_newlines ) { StrBuilder content = strbuilder_make_reserve( GlobalAllocator, raw_text.Len ); if ( raw_text.Len == 0 ) return content; #define cut_length ( scanner - raw_text.Ptr - last_cut ) #define cut_ptr ( raw_text.Ptr + last_cut ) #define pos ( rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ) ) #define move_fwd() do { scanner++; tokleft--; } while(0) s32 tokleft = raw_text.Len; sptr last_cut = 0; char const* scanner = raw_text.Ptr; if ( scanner[0] == ' ' ) { move_fwd(); last_cut = 1; } bool within_string = false; bool within_char = false; bool must_keep_newline = false; while ( tokleft ) { // Skip over the content of string literals if ( scanner[0] == '"' ) { move_fwd(); while ( tokleft && ( scanner[0] != '"' || *( scanner - 1 ) == '\\' ) ) { if ( scanner[0] == '\\' && tokleft > 1 ) { scanner += 2; tokleft -= 2; } else { move_fwd(); } } // Skip the closing " if ( tokleft ) move_fwd(); strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast(sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Skip over the content of character literals if ( scanner[0] == '\'' ) { move_fwd(); while ( tokleft && ( scanner[0] != '\'' || ( *(scanner -1 ) == '\\' ) ) ) { move_fwd(); } // Skip the closing ' if ( tokleft ) move_fwd(); strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Block comments if ( tokleft > 1 && scanner[0] == '/' && scanner[1] == '*' ) { while ( tokleft > 1 && !(scanner[0] == '*' && scanner[1] == '/') ) move_fwd(); scanner += 2; tokleft -= 2; strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Line comments if ( tokleft > 1 && scanner[0] == '/' && scanner[1] == '/' ) { must_keep_newline = true; scanner += 2; tokleft -= 2; while ( tokleft && scanner[ 0 ] != '\n' ) move_fwd(); if (tokleft) move_fwd(); strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Tabs if (scanner[0] == '\t') { if (pos > last_cut) strbuilder_append_c_str_len( & content, cut_ptr, cut_length); if ( * strbuilder_back( content ) != ' ' ) strbuilder_append_char( & content, ' ' ); move_fwd(); last_cut = rcast( sptr, scanner) - rcast( sptr, raw_text.Ptr); continue; } if ( tokleft > 1 && scanner[0] == '\r' && scanner[1] == '\n' ) { if ( must_keep_newline || preserve_newlines ) { must_keep_newline = false; scanner += 2; tokleft -= 2; strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } if ( pos > last_cut ) strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); // Replace with a space if ( * strbuilder_back( content ) != ' ' ) strbuilder_append_char( & content, ' ' ); scanner += 2; tokleft -= 2; last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } if ( scanner[0] == '\n' ) { if ( must_keep_newline || preserve_newlines ) { must_keep_newline = false; move_fwd(); strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } if ( pos > last_cut ) strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); // Replace with a space if ( * strbuilder_back( content ) != ' ' ) strbuilder_append_char( & content, ' ' ); move_fwd(); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Escaped newlines if ( scanner[0] == '\\' ) { strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); s32 amount_to_skip = 1; if ( tokleft > 1 && scanner[1] == '\n' ) { amount_to_skip = 2; } else if ( tokleft > 2 && scanner[1] == '\r' && scanner[2] == '\n' ) { amount_to_skip = 3; } if ( amount_to_skip > 1 && pos == last_cut ) { scanner += amount_to_skip; tokleft -= amount_to_skip; } else move_fwd(); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); continue; } // Consectuive spaces if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) ) { strbuilder_append_c_str_len( & content, cut_ptr, cut_length ); do { move_fwd(); } while ( tokleft && char_is_space( scanner[0] ) ); last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ); // Preserve only 1 space of formattting char* last = strbuilder_back(content); if ( last == nullptr || * last != ' ' ) strbuilder_append_char( & content, ' ' ); continue; } move_fwd(); } if ( last_cut < raw_text.Len ) { strbuilder_append_c_str_len( & content, cut_ptr, raw_text.Len - last_cut ); } #undef cut_ptr #undef cut_length #undef pos #undef move_fwd return content; } internal Code parse_array_decl() { push_scope(); if ( check( Tok_Operator ) && currtok.Text.Ptr[0] == '[' && currtok.Text.Ptr[1] == ']' ) { Code array_expr = untyped_str( txt(" ") ); eat( Tok_Operator ); // [] parser_pop(& Context); return array_expr; } if ( check( Tok_BraceSquare_Open ) ) { eat( Tok_BraceSquare_Open ); // [ if ( left == 0 ) { log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } if ( currtok.Type == Tok_BraceSquare_Close ) { log_failure( "Error, empty array expression in definition\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } Token untyped_tok = currtok; while ( left && currtok.Type != Tok_BraceSquare_Close ) { eat( currtok.Type ); } untyped_tok.Text.Len = ( (sptr)prevtok.Text.Ptr + prevtok.Text.Len ) - (sptr)untyped_tok.Text.Ptr; Code array_expr = untyped_str( tok_to_str(untyped_tok) ); // [ if ( left == 0 ) { log_failure( "Error, unexpected end of array declaration, expected ]\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } if ( currtok.Type != Tok_BraceSquare_Close ) { log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } eat( Tok_BraceSquare_Close ); // [ ] // Its a multi-dimensional array if ( check( Tok_BraceSquare_Open )) { Code adjacent_arr_expr = parse_array_decl(); // [ ][ ]... array_expr->Next = adjacent_arr_expr; } parser_pop(& Context); return array_expr; } parser_pop(& Context); return NullCode; } internal inline CodeAttributes parse_attributes() { push_scope(); Token start = currtok; s32 len = 0; // There can be more than one attribute. If there is flatten them to a single string. // TODO(Ed): Support keeping an linked list of attributes similar to parameters while ( left && tok_is_attribute(currtok) ) { if ( check( Tok_Attribute_Open ) ) { eat( Tok_Attribute_Open ); // [[ while ( left && currtok.Type != Tok_Attribute_Close ) { eat( currtok.Type ); } // [[ eat( Tok_Attribute_Close ); // [[ ]] len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr; } else if ( check( Tok_Decl_GNU_Attribute ) ) { eat( Tok_Decl_GNU_Attribute ); eat( Tok_Capture_Start ); eat( Tok_Capture_Start ); // __attribute__(( while ( left && currtok.Type != Tok_Capture_End ) { eat( currtok.Type ); } // __attribute__(( eat( Tok_Capture_End ); eat( Tok_Capture_End ); // __attribute__(( )) len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr; } else if ( check( Tok_Decl_MSVC_Attribute ) ) { eat( Tok_Decl_MSVC_Attribute ); eat( Tok_Capture_Start ); // __declspec( while ( left && currtok.Type != Tok_Capture_End ) { eat( currtok.Type ); } // __declspec( eat( Tok_Capture_End ); // __declspec( ) len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr; } else if ( tok_is_attribute(currtok) ) { eat( currtok.Type ); // // If its a macro based attribute, this could be a functional macro such as Unreal's UE_DEPRECATED(...) if ( check( Tok_Capture_Start)) { eat( Tok_Capture_Start ); s32 level = 0; while (left && currtok.Type != Tok_Capture_End && level == 0) { if (currtok.Type == Tok_Capture_Start) ++ level; if (currtok.Type == Tok_Capture_End) --level; eat(currtok.Type); } eat(Tok_Capture_End); } len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr; // ( ... ) } } if ( len > 0 ) { Str attribute_txt = { start.Text.Ptr, len }; parser_pop(& Context); StrBuilder name_stripped = parser_strip_formatting( attribute_txt, parser_strip_formatting_dont_preserve_newlines ); Code result = make_code(); result->Type = CT_PlatformAttributes; result->Name = get_cached_string( strbuilder_to_str(name_stripped) ); result->Content = result->Name; // result->Token = return ( CodeAttributes )result; } parser_pop(& Context); return NullCode; } internal Code parse_class_struct( TokType which, bool inplace_def ) { if ( which != Tok_Decl_Class && which != Tok_Decl_Struct ) { log_failure( "Error, expected class or struct, not %s\n%s", toktype_to_str( which ), parser_to_strbuilder(Context) ); return InvalidCode; } Token name = { nullptr, 0, Tok_Invalid }; AccessSpec access = AccessSpec_Default; CodeTypename parent = { nullptr }; CodeBody body = { nullptr }; CodeAttributes attributes = { nullptr }; ModuleFlag mflags = ModuleFlag_None; Code result = InvalidCode; if ( check(Tok_Module_Export) ) { mflags = ModuleFlag_Export; eat( Tok_Module_Export ); } // eat( which ); // attributes = parse_attributes(); // if ( check( Tok_Identifier ) ) { name = parse_identifier(nullptr); Context.Scope->Name = name; } // local_persist char interface_arr_mem[ kilobytes(4) ] = {0}; Array(CodeTypename) interfaces; { Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); interfaces = array_init_reserve(CodeTypename, arena_allocator_info(& arena), 4 ); } // TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them. if ( check( Tok_Assign_Classifer ) ) { eat( Tok_Assign_Classifer ); // : if ( tok_is_access_specifier(currtok) ) { access = tok_to_access_specifier(currtok); // : eat( currtok.Type ); } Token parent_tok = parse_identifier(nullptr); parent = def_type( tok_to_str(parent_tok) ); // : while ( check(Tok_Comma) ) { eat( Tok_Comma ); // : , if ( tok_is_access_specifier(currtok) ) { eat(currtok.Type); } Token interface_tok = parse_identifier(nullptr); array_append( interfaces, def_type( tok_to_str(interface_tok) ) ); // : , ... } } if ( check( Tok_BraceCurly_Open ) ) { body = parse_class_struct_body( which, name ); } // : , ... { } CodeComment inline_cmt = NullCode; if ( ! inplace_def ) { Token stmt_end = currtok; eat( Tok_Statement_End ); // : , ... { }; if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line ) inline_cmt = parse_comment(); // : , ... { }; } if ( which == Tok_Decl_Class ) result = cast(Code, def_class( tok_to_str(name), def_assign( body, parent, access, attributes, interfaces, scast(s32, array_num(interfaces)), mflags ) )); else result = cast(Code, def_struct( tok_to_str(name), def_assign( body, (CodeTypename)parent, access, attributes, interfaces, scast(s32, array_num(interfaces)), mflags ) )); if ( inline_cmt ) result->InlineCmt = cast(Code, inline_cmt); array_free(interfaces); return result; } internal neverinline CodeBody parse_class_struct_body( TokType which, Token name ) { push_scope(); eat( Tok_BraceCurly_Open ); // { CodeBody result = (CodeBody) make_code(); if ( which == Tok_Decl_Class ) result->Type = CT_Class_Body; else result->Type = CT_Struct_Body; while ( left && currtok_noskip.Type != Tok_BraceCurly_Close ) { Code member = Code_Invalid; CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; bool expects_function = false; // Context.Scope->Start = currtok_noskip; if ( currtok_noskip.Type == Tok_Preprocess_Hash ) eat( Tok_Preprocess_Hash ); b32 macro_found = true; switch ( currtok_noskip.Type ) { case Tok_Statement_End: { // TODO(Ed): Convert this to a general warning procedure log_fmt("Dangling end statement found %SB\n", tok_to_strbuilder(currtok_noskip)); eat( Tok_Statement_End ); continue; } case Tok_NewLine: member = fmt_newline; eat( Tok_NewLine ); break; case Tok_Comment: member = cast(Code, parse_comment()); break; case Tok_Access_Public: member = access_public; eat( Tok_Access_Public ); eat( Tok_Assign_Classifer ); // public: break; case Tok_Access_Protected: member = access_protected; eat( Tok_Access_Protected ); eat( Tok_Assign_Classifer ); // protected: break; case Tok_Access_Private: member = access_private; eat( Tok_Access_Private ); eat( Tok_Assign_Classifer ); // private: break; case Tok_Decl_Class: member = parse_complicated_definition( Tok_Decl_Class ); // class break; case Tok_Decl_Enum: member = parse_complicated_definition( Tok_Decl_Enum ); // enum break; case Tok_Decl_Friend: member = cast(Code, parser_parse_friend()); // friend break; case Tok_Decl_Operator: member = cast(Code, parser_parse_operator_cast(NullCode)); // operator () break; case Tok_Decl_Struct: member = parse_complicated_definition( Tok_Decl_Struct ); // struct break; case Tok_Decl_Template: member = cast(Code, parser_parse_template()); // template< ... > break; case Tok_Decl_Typedef: member = cast(Code, parser_parse_typedef()); // typedef break; case Tok_Decl_Union: member = parse_complicated_definition( Tok_Decl_Union ); // union break; case Tok_Decl_Using: member = cast(Code, parser_parse_using()); // using break; case Tok_Operator: //if ( currtok.Text[0] != '~' ) //{ // log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_strbuilder(Context) ); // return InvalidCode; //} member = cast(Code, parser_parse_destructor(NullCode)); // ~() break; case Tok_Preprocess_Define: member = cast(Code, parse_define()); // #define break; case Tok_Preprocess_Include: member = cast(Code, parse_include()); // #include break; case Tok_Preprocess_If: case Tok_Preprocess_IfDef: case Tok_Preprocess_IfNotDef: case Tok_Preprocess_ElIf: member = cast(Code, parse_preprocess_cond()); // # break; case Tok_Preprocess_Else: member = cast(Code, preprocess_else); eat( Tok_Preprocess_Else ); // #else break; case Tok_Preprocess_EndIf: member = cast(Code, preprocess_endif); eat( Tok_Preprocess_EndIf ); // #endif break; case Tok_Preprocess_Macro: // macro_found = true; goto Preprocess_Macro_Bare_In_Body; break; case Tok_Preprocess_Pragma: member = cast(Code, parse_pragma()); // #pragma break; case Tok_Preprocess_Unsupported: member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces )); // # break; case Tok_StaticAssert: member = parse_static_assert(); // static_assert break; case Tok_Attribute_Open: case Tok_Decl_GNU_Attribute: case Tok_Decl_MSVC_Attribute: #define Entry( attribute, str ) case attribute: GEN_DEFINE_ATTRIBUTE_TOKENS #undef Entry { attributes = parse_attributes(); // } //! Fallthrough intended case Tok_Spec_Consteval: case Tok_Spec_Constexpr: case Tok_Spec_Constinit: case Tok_Spec_Explicit: case Tok_Spec_ForceInline: case Tok_Spec_Inline: case Tok_Spec_Mutable: case Tok_Spec_NeverInline: case Tok_Spec_Static: case Tok_Spec_Volatile: case Tok_Spec_Virtual: { Specifier specs_found[16] = { Spec_NumSpecifiers }; s32 NumSpecifiers = 0; while ( left && tok_is_specifier(currtok) ) { Specifier spec = str_to_specifier( tok_to_str(currtok) ); b32 ignore_spec = false; switch ( spec ) { case Spec_Constexpr: case Spec_Constinit: case Spec_Explicit: case Spec_Inline: case Spec_ForceInline: case Spec_Mutable: case Spec_NeverInline: case Spec_Static: case Spec_Volatile: case Spec_Virtual: break; case Spec_Consteval: expects_function = true; break; case Spec_Const : ignore_spec = true; break; default: log_failure( "Invalid specifier %S for variable\n%S", spec_to_str(spec), strbuilder_to_str( parser_to_strbuilder(Context)) ); parser_pop(& Context); return InvalidCode; } // Every specifier after would be considered part of the type type signature if (ignore_spec) break; specs_found[NumSpecifiers] = spec; NumSpecifiers++; eat( currtok.Type ); } if ( NumSpecifiers ) { specifiers = def_specifiers( NumSpecifiers, specs_found ); } // if ( tok_is_attribute(currtok) ) { // Unfortuantely Unreal has code where there is attirbutes before specifiers CodeAttributes more_attributes = parse_attributes(); if ( attributes ) { StrBuilder fused = strbuilder_make_reserve( GlobalAllocator, attributes->Content.Len + more_attributes->Content.Len ); strbuilder_append_fmt( & fused, "%SB %SB", attributes->Content, more_attributes->Content ); Str attrib_name = strbuilder_to_str(fused); attributes->Name = get_cached_string( attrib_name ); attributes->Content = attributes->Name; // } attributes = more_attributes; } if ( currtok.Type == Tok_Operator && currtok.Text.Ptr[0] == '~' ) { member = cast(Code, parser_parse_destructor( specifiers )); // ~() break; } if ( currtok.Type == Tok_Decl_Operator ) { member = cast(Code, parser_parse_operator_cast( specifiers )); // operator () break; } } //! Fallthrough intentional case Tok_Identifier: case Tok_Spec_Const: case Tok_Type_Unsigned: case Tok_Type_Signed: case Tok_Type_Short: case Tok_Type_Long: case Tok_Type_bool: case Tok_Type_char: case Tok_Type_int: case Tok_Type_double: { if ( nexttok.Type == Tok_Capture_Start && name.Text.Len && currtok.Type == Tok_Identifier ) { if ( c_str_compare_len( name.Text.Ptr, currtok.Text.Ptr, name.Text.Len ) == 0 ) { member = cast(Code, parser_parse_constructor( specifiers )); // () break; } } if (macro_found) { Preprocess_Macro_Bare_In_Body: b32 lone_macro = nexttok.Type == Tok_Statement_End || nexttok_noskip.Type == Tok_NewLine; if (lone_macro) { member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro, parser_consume_braces )); // ; if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } continue; } // We have a macro but its most likely behaving as a typename // operator ... // or // ... } break; default: Token untyped_tok = currtok; while ( left && currtok.Type != Tok_BraceCurly_Close ) { untyped_tok.Text.Len = ( (sptr)currtok.Text.Ptr + currtok.Text.Len ) - (sptr)untyped_tok.Text.Ptr; eat( currtok.Type ); } member = untyped_str( tok_to_str(untyped_tok) ); // Something unknown break; } if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } body_append(result, member ); } eat( Tok_BraceCurly_Close ); // { } parser_pop(& Context); return result; } internal CodeComment parse_comment() { push_scope(); CodeComment result = (CodeComment) make_code(); result->Type = CT_Comment; result->Content = get_cached_string( tok_to_str(currtok_noskip) ); result->Name = result->Content; // result->Token = currtok_noskip; eat( Tok_Comment ); parser_pop(& Context); return result; } internal Code parse_complicated_definition( TokType which ) { push_scope(); bool is_inplace = false; TokArray tokens = Context.Tokens; s32 idx = tokens.Idx; s32 level = 0; for ( ; idx < array_num(tokens.Arr); idx++ ) { if ( tokens.Arr[ idx ].Type == Tok_BraceCurly_Open ) level++; if ( tokens.Arr[ idx ].Type == Tok_BraceCurly_Close ) level--; if ( level == 0 && tokens.Arr[ idx ].Type == Tok_Statement_End ) break; } if ( ( idx - 2 ) == tokens.Idx ) { // Its a forward declaration only Code result = parse_forward_or_definition( which, is_inplace ); // ; parser_pop(& Context); return result; } Token tok = tokens.Arr[ idx - 1 ]; if ( tok_is_specifier(tok) && spec_is_trailing( str_to_specifier( tok_to_str(tok))) ) { // (...) ...; s32 spec_idx = idx - 1; Token spec = tokens.Arr[spec_idx]; while ( tok_is_specifier(spec) && spec_is_trailing( str_to_specifier( tok_to_str(spec))) ) { -- spec_idx; spec = tokens.Arr[spec_idx]; } if ( tokens.Arr[spec_idx].Type == Tok_Capture_End ) { // Forward declaration with trailing specifiers for a procedure tok = tokens.Arr[spec_idx]; Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // , or Name> ... parser_pop(& Context); return result; } log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } if ( tok.Type == Tok_Identifier ) { tok = tokens.Arr[ idx - 2 ]; bool is_indirection = tok.Type == Tok_Ampersand || tok.Type == Tok_Star; bool ok_to_parse = false; if ( tok.Type == Tok_BraceCurly_Close ) { // Its an inplace definition // { ... } ; ok_to_parse = true; is_inplace = true; } else if ( tok.Type == Tok_Identifier && tokens.Arr[ idx - 3 ].Type == which ) { // Its a variable with type ID using namespace. // ; ok_to_parse = true; } else if ( tok.Type == Tok_Assign_Classifer && ( ( tokens.Arr[idx - 5].Type == which && tokens.Arr[idx - 4].Type == Tok_Decl_Class ) || ( tokens.Arr[idx - 4].Type == which)) ) { // Its a forward declaration of an enum // : ; // : ; ok_to_parse = true; Code result = cast(Code, parser_parse_enum( ! parser_inplace_def)); parser_pop(& Context); return result; } else if ( is_indirection ) { // Its a indirection type with type ID using struct namespace. // * ; ok_to_parse = true; } if ( ! ok_to_parse ) { log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // , or Name> ... parser_pop(& Context); return result; } else if ( tok.Type >= Tok_Type_Unsigned && tok.Type <= Tok_Type_MS_W64 ) { tok = tokens.Arr[ idx - 2 ]; if ( tok.Type != Tok_Assign_Classifer || ( ( tokens.Arr[idx - 5].Type != which && tokens.Arr[idx - 4].Type != Tok_Decl_Class ) && ( tokens.Arr[idx - 4].Type != which)) ) { log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } // Its a forward declaration of an enum class // : ; // : ; Code result = cast(Code, parser_parse_enum( ! parser_inplace_def)); parser_pop(& Context); return result; } else if ( tok.Type == Tok_BraceCurly_Close ) { // Its a definition Code result = parse_forward_or_definition( which, is_inplace ); // { ... }; parser_pop(& Context); return result; } else if ( tok.Type == Tok_BraceSquare_Close ) { // Its an array definition Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // [ ... ]; parser_pop(& Context); return result; } else { log_failure( "Unsupported or bad member definition after %s declaration\n%SB", toktype_to_str(which).Ptr, parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } } internal inline CodeDefine parse_define() { push_scope(); eat( Tok_Preprocess_Define ); // #define CodeDefine define = (CodeDefine) make_code(); define->Type = CT_Preprocess_Define; if ( ! check( Tok_Identifier ) ) { log_failure( "Error, expected identifier after #define\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } Context.Scope->Name = currtok; define->Name = get_cached_string( tok_to_str(currtok) ); eat( Tok_Identifier ); // #define if ( ! check( Tok_Preprocess_Content )) { log_failure( "Error, expected content after #define %s\n%s", define->Name, parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } if ( currtok.Text.Len == 0 ) { define->Content = get_cached_string( tok_to_str(currtok) ); eat( Tok_Preprocess_Content ); // #define parser_pop(& Context); return define; } define->Content = get_cached_string( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) ); eat( Tok_Preprocess_Content ); // #define parser_pop(& Context); return define; } internal inline Code parse_assignment_expression() { Code expr = { nullptr }; eat( Tok_Operator ); // = Token expr_tok = currtok; if ( currtok.Type == Tok_Statement_End && currtok.Type != Tok_Comma ) { log_failure( "Expected expression after assignment operator\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } s32 level = 0; while ( left && currtok.Type != Tok_Statement_End && (currtok.Type != Tok_Comma || level > 0) ) { if (currtok.Type == Tok_BraceCurly_Open ) level++; if (currtok.Type == Tok_BraceCurly_Close ) level--; if (currtok.Type == Tok_Capture_Start) level++; else if (currtok.Type == Tok_Capture_End) level--; eat( currtok.Type ); } expr_tok.Text.Len = ( ( sptr )currtok.Text.Ptr + currtok.Text.Len ) - ( sptr )expr_tok.Text.Ptr - 1; expr = untyped_str( tok_to_str(expr_tok) ); // = return expr; } internal inline Code parse_forward_or_definition( TokType which, bool is_inplace ) { Code result = InvalidCode; switch ( which ) { case Tok_Decl_Class: result = cast(Code, parser_parse_class( is_inplace )); return result; case Tok_Decl_Enum: result = cast(Code, parser_parse_enum( is_inplace )); return result; case Tok_Decl_Struct: result = cast(Code, parser_parse_struct( is_inplace )); return result; case Tok_Decl_Union: result = cast(Code, parser_parse_union( is_inplace )); return result; default: log_failure( "Error, wrong token type given to parse_complicated_definition " "(only supports class, enum, struct, union) \n%s" , parser_to_strbuilder(Context) ); return InvalidCode; } } // Function parsing is handled in multiple places because its initial signature is shared with variable parsing internal inline CodeFn parse_function_after_name( ModuleFlag mflags , CodeAttributes attributes , CodeSpecifiers specifiers , CodeTypename ret_type , Token name ) { push_scope(); CodeParams params = parse_params(parser_use_parenthesis); // ( ) // TODO(Ed), Review old comment : These have to be kept separate from the return type's specifiers. while ( left && tok_is_specifier(currtok) ) { if ( specifiers == nullptr ) { specifiers = def_specifier( str_to_specifier( tok_to_str(currtok)) ); eat( currtok.Type ); continue; } specifiers_append(specifiers, str_to_specifier( tok_to_str(currtok)) ); eat( currtok.Type ); } // ( ) CodeBody body = NullCode; CodeComment inline_cmt = NullCode; if ( check( Tok_BraceCurly_Open ) ) { body = cast(CodeBody, parse_function_body()); if ( cast(Code, body) == Code_Invalid ) { parser_pop(& Context); return InvalidCode; } // ( ) { } } else if ( check(Tok_Operator) && currtok.Text.Ptr[0] == '=' ) { eat(Tok_Operator); specifiers_append(specifiers, Spec_Pure ); eat( Tok_Number); Token stmt_end = currtok; eat( Tok_Statement_End ); // ( ) = 0; if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line ) inline_cmt = parse_comment(); // ( ) ; } else { Token stmt_end = currtok; eat( Tok_Statement_End ); // ( ) ; if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line ) inline_cmt = parse_comment(); // ( ) ; } StrBuilder name_stripped = strbuilder_make_str( GlobalAllocator, tok_to_str(name) ); strip_space(name_stripped); CodeFn result = (CodeFn) make_code(); result->Name = get_cached_string( strbuilder_to_str(name_stripped) ); result->ModuleFlags = mflags; if ( body ) { switch ( body->Type ) { case CT_Function_Body: case CT_Untyped: break; default: { log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", code_debug_str(body), parser_to_strbuilder(Context)); parser_pop(& Context); return InvalidCode; } } result->Type = CT_Function; result->Body = body; } else { result->Type = CT_Function_Fwd; } if ( attributes ) result->Attributes = attributes; if ( specifiers ) result->Specs = specifiers; result->ReturnType = ret_type; if ( params ) result->Params = params; if ( inline_cmt ) result->InlineCmt = inline_cmt; parser_pop(& Context); return result; } internal Code parse_function_body() { push_scope(); eat( Tok_BraceCurly_Open ); CodeBody result = (CodeBody) make_code(); result->Type = CT_Function_Body; // TODO : Support actual parsing of function body Token start = currtok_noskip; s32 level = 0; while ( left && ( currtok_noskip.Type != Tok_BraceCurly_Close || level > 0 ) ) { if ( currtok_noskip.Type == Tok_BraceCurly_Open ) level++; else if ( currtok_noskip.Type == Tok_BraceCurly_Close && level > 0 ) level--; eat( currtok_noskip.Type ); } Token past = prevtok; s32 len = ( (sptr)prevtok.Text.Ptr + prevtok.Text.Len ) - (sptr)start.Text.Ptr; if ( len > 0 ) { Str str = { start.Text.Ptr, len }; body_append( result, cast(Code, def_execution( str )) ); } eat( Tok_BraceCurly_Close ); parser_pop(& Context); return cast(Code, result); } internal neverinline CodeBody parse_global_nspace( CodeType which ) { push_scope(); if ( which != CT_Namespace_Body && which != CT_Global_Body && which != CT_Export_Body && which != CT_Extern_Linkage_Body ) return InvalidCode; if ( which != CT_Global_Body ) eat( Tok_BraceCurly_Open ); // { CodeBody result = (CodeBody) make_code(); result->Type = which; while ( left && currtok_noskip.Type != Tok_BraceCurly_Close ) { Code member = Code_Invalid; CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; bool expects_function = false; // Context.Scope->Start = currtok_noskip; if ( currtok_noskip.Type == Tok_Preprocess_Hash ) eat( Tok_Preprocess_Hash ); b32 macro_found = false; switch ( currtok_noskip.Type ) { case Tok_Comma: { log_failure("Dangling comma found: %SB\nContext:\n%SB", tok_to_strbuilder(currtok), parser_to_strbuilder(Context)); parser_pop( & Context); return InvalidCode; } break; case Tok_Statement_End: { // TODO(Ed): Convert this to a general warning procedure log_fmt("Dangling end statement found %SB\n", tok_to_strbuilder(currtok_noskip)); eat( Tok_Statement_End ); continue; } case Tok_NewLine: // Empty lines are auto skipped by Tokens.current() member = fmt_newline; eat( Tok_NewLine ); break; case Tok_Comment: member = cast(Code, parse_comment()); break; case Tok_Decl_Class: member = parse_complicated_definition( Tok_Decl_Class ); // class break; case Tok_Decl_Enum: member = parse_complicated_definition( Tok_Decl_Enum ); // enum break; case Tok_Decl_Extern_Linkage: if ( which == CT_Extern_Linkage_Body ) log_failure( "Nested extern linkage\n%s", parser_to_strbuilder(Context) ); member = cast(Code, parser_parse_extern_link()); // extern "..." { ... } break; case Tok_Decl_Namespace: member = cast(Code, parser_parse_namespace()); // namespace { ... } break; case Tok_Decl_Struct: member = parse_complicated_definition( Tok_Decl_Struct ); // struct ... break; case Tok_Decl_Template: member = cast(Code, parser_parse_template()); // template<...> ... break; case Tok_Decl_Typedef: member = cast(Code, parser_parse_typedef()); // typedef ... break; case Tok_Decl_Union: member = parse_complicated_definition( Tok_Decl_Union ); // union ... break; case Tok_Decl_Using: member = cast(Code, parser_parse_using()); // using ... break; case Tok_Preprocess_Define: member = cast(Code, parse_define()); // #define ... break; case Tok_Preprocess_Include: member = cast(Code, parse_include()); // #include ... break; case Tok_Preprocess_If: case Tok_Preprocess_IfDef: case Tok_Preprocess_IfNotDef: case Tok_Preprocess_ElIf: member = cast(Code, parse_preprocess_cond()); // # ... break; case Tok_Preprocess_Else: member = cast(Code, preprocess_else); eat( Tok_Preprocess_Else ); // #else break; case Tok_Preprocess_EndIf: member = cast(Code, preprocess_endif); eat( Tok_Preprocess_EndIf ); // #endif break; case Tok_Preprocess_Macro: { // macro_found = true; goto Preprocess_Macro_Bare_In_Body; } break; case Tok_Preprocess_Pragma: { member = cast(Code, parse_pragma()); // #pragma ... } break; case Tok_Preprocess_Unsupported: { member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces )); // # ... } break; case Tok_StaticAssert: { member = cast(Code, parse_static_assert()); // static_assert( , ... ); } break; case Tok_Module_Export: { if ( which == CT_Export_Body ) log_failure( "Nested export declaration\n%s", parser_to_strbuilder(Context) ); member = cast(Code, parser_parse_export_body()); // export { ... } } break; case Tok_Module_Import: { // import ... log_failure( "gen::%s: This function is not implemented" ); return InvalidCode; } //! Fallthrough intentional case Tok_Attribute_Open: case Tok_Decl_GNU_Attribute: case Tok_Decl_MSVC_Attribute: #define Entry( attribute, str ) case attribute: GEN_DEFINE_ATTRIBUTE_TOKENS #undef Entry { attributes = parse_attributes(); // } //! Fallthrough intentional case Tok_Spec_Consteval: case Tok_Spec_Constexpr: case Tok_Spec_Constinit: case Tok_Spec_Extern: case Tok_Spec_ForceInline: case Tok_Spec_Global: case Tok_Spec_Inline: case Tok_Spec_Internal_Linkage: case Tok_Spec_NeverInline: case Tok_Spec_Static: { Specifier specs_found[16] = { Spec_NumSpecifiers }; s32 NumSpecifiers = 0; while ( left && tok_is_specifier(currtok) ) { Specifier spec = str_to_specifier( tok_to_str(currtok) ); bool ignore_spec = false; switch ( spec ) { case Spec_Constexpr: case Spec_Constinit: case Spec_ForceInline: case Spec_Global: case Spec_External_Linkage: case Spec_Internal_Linkage: case Spec_Inline: case Spec_Mutable: case Spec_NeverInline: case Spec_Static: case Spec_Volatile: break; case Spec_Consteval: expects_function = true; break; case Spec_Const: ignore_spec = true; break; default: Str spec_str = spec_to_str(spec); log_failure( "Invalid specifier %S for variable\n%S", spec_str, strbuilder_to_str( parser_to_strbuilder(Context)) ); parser_pop(& Context); return InvalidCode; } if (ignore_spec) break; specs_found[NumSpecifiers] = spec; NumSpecifiers++; eat( currtok.Type ); } if ( NumSpecifiers ) { specifiers = def_specifiers( NumSpecifiers, specs_found ); } // } //! Fallthrough intentional case Tok_Identifier: case Tok_Spec_Const: case Tok_Type_Long: case Tok_Type_Short: case Tok_Type_Signed: case Tok_Type_Unsigned: case Tok_Type_bool: case Tok_Type_char: case Tok_Type_double: case Tok_Type_int: { // This s only in a scope so that Preprocess_Macro_Bare_In_Body works without microsoft extension warnings { Code constructor_destructor = parse_global_nspace_constructor_destructor( specifiers ); // Possible constructor implemented at global file scope. if ( constructor_destructor ) { member = constructor_destructor; break; } bool found_operator_cast_outside_class_implmentation = false; s32 idx = Context.Tokens.Idx; for ( ; idx < array_num(Context.Tokens.Arr); idx++ ) { Token tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == Tok_Identifier ) { idx++; tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == Tok_Access_StaticSymbol ) continue; break; } if ( tok.Type == Tok_Decl_Operator ) found_operator_cast_outside_class_implmentation = true; break; } if ( found_operator_cast_outside_class_implmentation ) { member = cast(Code, parser_parse_operator_cast( specifiers )); // ::operator () { ... } break; } } if (macro_found) { Preprocess_Macro_Bare_In_Body: b32 lone_macro = nexttok.Type == Tok_Statement_End || nexttok_noskip.Type == Tok_NewLine; if (lone_macro) { member = parse_simple_preprocess( Tok_Preprocess_Macro, parser_consume_braces ); // ; if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } goto Member_Resolved_To_Lone_Macro; } // We have a macro but its most likely behaving as a typename // ... } } Member_Resolved_To_Lone_Macro: if ( member == Code_Invalid ) { log_failure( "Failed to parse member\nToken: %SB\nContext:\n%SB", tok_to_strbuilder(currtok_noskip), parser_to_strbuilder(Context) ); parser_pop(& Context); return InvalidCode; } // log_fmt("Global Body Member: %s", member->debug_str()); body_append(result, member ); } if ( which != CT_Global_Body ) eat( Tok_BraceCurly_Close ); // { } parser_pop(& Context); return result; } internal inline Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) { Code result = { nullptr }; /* To check if a definition is for a constructor we can go straight to the opening parenthesis for its parameters From There we work backwards to see if we come across two identifiers with the same name between an member access :: operator, there can be template parameters on the left of the :: so we ignore those. Whats important is that its back to back. This has multiple possible faults. What we parse using this method may not filter out if something has a "return type" This is bad since technically you could have a namespace nested into another namespace with the same name. If this awful pattern is done the only way to distiguish with this coarse parse is to know there is no return type defined. TODO(Ed): We could fix this by attempting to parse a type, but we would have to have a way to have it soft fail and rollback. */ TokArray tokens = Context.Tokens; s32 idx = tokens.Idx; Token nav = tokens.Arr[ idx ]; for ( ; idx < array_num(tokens.Arr); idx++, nav = tokens.Arr[ idx ] ) { if ( nav.Text.Ptr[0] == '<' ) { // Skip templated expressions as they mey have expressions with the () operators s32 capture_level = 0; s32 template_level = 0; for ( ; idx < array_num(tokens.Arr); idx++, nav = tokens.Arr[idx] ) { if (nav.Text.Ptr[ 0 ] == '<') ++ template_level; if (nav.Text.Ptr[ 0 ] == '>') -- template_level; if (nav.Type == Tok_Operator && nav.Text.Ptr[1] == '>') -- template_level; if ( nav.Type == Tok_Capture_Start) { if (template_level != 0 ) ++ capture_level; else break; } if ( template_level != 0 && nav.Type == Tok_Capture_End) -- capture_level; } } if ( nav.Type == Tok_Capture_Start ) break; } -- idx; Token tok_right = tokens.Arr[idx]; Token tok_left = NullToken; if (tok_right.Type != Tok_Identifier) { // We're not dealing with a constructor if there is no identifier right before the opening of a parameter's scope. return result; } -- idx; tok_left = tokens.Arr[idx]; // ... bool possible_destructor = false; if ( tok_left.Type == Tok_Operator && tok_left.Text.Ptr[0] == '~') { possible_destructor = true; -- idx; tok_left = tokens.Arr[idx]; } if ( tok_left.Type != Tok_Access_StaticSymbol ) return result; -- idx; tok_left = tokens.Arr[idx]; // ... :: // We search toward the left until we find the next valid identifier s32 capture_level = 0; s32 template_level = 0; while ( idx != tokens.Idx ) { if (tok_left.Text.Ptr[ 0 ] == '<') ++ template_level; if (tok_left.Text.Ptr[ 0 ] == '>') -- template_level; if (tok_left.Type == Tok_Operator && tok_left.Text.Ptr[1] == '>') -- template_level; if ( template_level != 0 && tok_left.Type == Tok_Capture_Start) ++ capture_level; if ( template_level != 0 && tok_left.Type == Tok_Capture_End) -- capture_level; if ( capture_level == 0 && template_level == 0 && tok_left.Type == Tok_Identifier ) break; -- idx; tok_left = tokens.Arr[idx]; } bool is_same = c_str_compare_len( tok_right.Text.Ptr, tok_left.Text.Ptr, tok_right.Text.Len ) == 0; if (tok_left.Type == Tok_Identifier && is_same) { // We have found the pattern we desired if (possible_destructor) { // :: ~ ( result = cast(Code, parser_parse_destructor( specifiers )); } else { // :: ( result = cast(Code, parser_parse_constructor( specifiers )); } } return result; } // TODO(Ed): I want to eventually change the identifier to its own AST type. // This would allow distinction of the qualifier for a symbol :: // This would also allow internal Token parse_identifier( bool* possible_member_function ) { push_scope(); Token name = currtok; Context.Scope->Name = name; eat( Tok_Identifier ); // parse_template_args( & name ); //