gencpp/project/components/interface.parsing.cpp

330 lines
6.0 KiB
C++
Raw Normal View History

#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "gen/etoktype.cpp"
#include "interface.upfront.cpp"
#include "lexer.cpp"
#include "parser.cpp"
#endif
// Publically Exposed Interface
CodeClass parse_class( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
2024-12-03 06:31:27 -08:00
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
2024-12-03 06:31:27 -08:00
Context.Tokens = toks;
push_scope();
2024-12-03 06:50:30 -08:00
CodeClass result = (CodeClass) parse_class_struct( Tok_Decl_Class );
parser_pop(& Context);
return result;
}
CodeConstructor parse_constructor( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
// TODO(Ed): Constructors can have prefix attributes
CodeSpecifiers specifiers;
Specifier specs_found[ 16 ] { Spec_NumSpecifiers };
s32 NumSpecifiers = 0;
while ( left && tok_is_specifier(currtok) )
{
Specifier spec = strc_to_specifier( tok_to_str(currtok) );
b32 ignore_spec = false;
switch ( spec )
{
case Spec_Constexpr :
case Spec_Explicit:
case Spec_Inline :
case Spec_ForceInline :
case Spec_NeverInline :
break;
case Spec_Const :
ignore_spec = true;
break;
default :
log_failure( "Invalid specifier %s for variable\n%s", spec_to_str( spec ), parser_to_string(Context) );
parser_pop(& Context);
2024-12-01 21:03:38 -08:00
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 );
// <specifiers> ...
}
Context.Tokens = toks;
CodeConstructor result = parse_constructor( specifiers );
return result;
}
CodeDestructor parse_destructor( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
// TODO(Ed): Destructors can have prefix attributes
// TODO(Ed): Destructors can have virtual
Context.Tokens = toks;
CodeDestructor result = parse_destructor();
return result;
}
CodeEnum parse_enum( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
{
parser_pop(& Context);
2024-12-01 21:03:38 -08:00
return InvalidCode;
}
2023-08-22 23:17:47 -07:00
Context.Tokens = toks;
return parse_enum();
}
CodeBody parse_export_body( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_export_body();
}
CodeExtern parse_extern_link( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
2023-08-01 11:02:54 -07:00
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
2023-08-01 11:02:54 -07:00
Context.Tokens = toks;
return parse_extern_link();
}
CodeFriend parse_friend( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_friend();
}
CodeFn parse_function( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return (CodeFn) parse_function();
}
CodeBody parse_global_body( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
push_scope();
2024-12-03 12:19:39 -08:00
CodeBody result = parse_global_nspace( CT_Global_Body );
parser_pop(& Context);
return result;
}
CodeNS parse_namespace( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_namespace();
}
CodeOperator parse_operator( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return (CodeOperator) parse_operator();
}
CodeOpCast parse_operator_cast( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_operator_cast();
}
CodeStruct parse_struct( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
push_scope();
2024-12-03 06:50:30 -08:00
CodeStruct result = (CodeStruct) parse_class_struct( Tok_Decl_Struct );
parser_pop(& Context);
return result;
}
CodeTemplate parse_template( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_template();
}
2024-12-03 12:19:39 -08:00
CodeTypename parse_type( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_type();
}
CodeTypedef parse_typedef( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_typedef();
}
CodeUnion parse_union( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_union();
}
CodeUsing parse_using( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_using();
}
CodeVar parse_variable( StrC def )
{
2024-12-03 06:31:27 -08:00
GEN_USING_NS_PARSER;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
2024-12-01 21:03:38 -08:00
return InvalidCode;
Context.Tokens = toks;
return parse_variable();
}
// Undef helper macros
# undef check_parse_args
# undef currtok
# undef prevtok
# undef nexttok
# undef eat
# undef left
# undef check
# undef push_scope