From c97762ac162d682da46ce26c2f7b95b18e1a4526 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 23 Aug 2023 00:05:58 -0400 Subject: [PATCH] Added support for inline comments Also now doing comment serialization on def_comment directly as parse_comment doesn't need it. Essentially comment ast types serialize the same way s untyped and execution ASTs --- project/bootstrap.cpp | 2 +- project/components/ast.cpp | 134 +++++++++++--- project/components/ast.hpp | 10 +- project/components/ast_types.hpp | 34 ++-- project/components/gen/ast_inlines.hpp | 2 +- project/components/gen/ecode.hpp | 2 +- project/components/gen/eoperator.hpp | 2 +- project/components/gen/especifier.hpp | 2 +- project/components/gen/etoktype.cpp | 4 +- project/components/interface.parsing.cpp | 215 +++++++++++++++++------ project/components/interface.upfront.cpp | 32 +++- project/enums/ETokType.csv | 2 +- singleheader/singleheader.cpp | 2 +- test/test.cpp | 4 +- 14 files changed, 344 insertions(+), 103 deletions(-) diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 229b888..2efad37 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -17,7 +17,7 @@ GEN_NS_END using namespace gen; constexpr char const* generation_notice = -"// This file was generated automatially by gen.bootstrap.cpp " +"// This file was generated automatially by gencpp's bootstrap.cpp " "(See: https://github.com/Ed94/gencpp)\n\n"; constexpr bool DontSkipInitialDirectives = false; diff --git a/project/components/ast.cpp b/project/components/ast.cpp index be87278..9836fd3 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -43,6 +43,11 @@ String AST::to_string() case Comment: { + // TODO : Move this formmating process to def_comment, + // Were going to preserve as much of the original formatting as possible + // so that the parsed comments don't have any artifacts. + // Just doing what untyped and execution do +#if 0 if ( Prev && Prev->Type != Comment && Prev->Type != NewLine ) result.append( "\n" ); @@ -72,6 +77,9 @@ String AST::to_string() if ( result.back() != '\n' ) result.append( "\n" ); +#else + result.append( Content ); +#endif } break; @@ -140,9 +148,15 @@ String AST::to_string() result.append_fmt( "class %S %S", Attributes->to_string(), Name ); else result.append_fmt( "class %S", Name ); - + + // Check if it can have an end-statement if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) - result.append(";\n"); + { + if ( InlineCmt ) + result.append_fmt( "; // %S\n", InlineCmt->Content ); + else + result.append(";\n"); + } } break; @@ -169,7 +183,12 @@ String AST::to_string() if ( Params ) result.append_fmt( "( %S )", Params->to_string() ); else - result.append( "(void);\n" ); + { + if ( InlineCmt ) + result.append_fmt( "(void); // %S\n", InlineCmt->Content ); + else + result.append( "(void);\n" ); + } } break; @@ -203,10 +222,15 @@ String AST::to_string() result.append_fmt( "~%S()", Parent->Name ); if ( specs.has( ESpecifier::Pure ) ) - result.append( " = 0;\n" ); + result.append( " = 0;" ); } else - result.append_fmt( "~%S();\n", Parent->Name ); + result.append_fmt( "~%S();", Parent->Name ); + + if ( InlineCmt ) + result.append_fmt( " %S", InlineCmt->Content ); + else + result.append("\n"); } break; @@ -249,7 +273,12 @@ String AST::to_string() result.append_fmt( "enum %S : %S", Name, UnderlyingType->to_string() ); if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) - result.append(";\n"); + { + if ( InlineCmt ) + result.append_fmt("; %S", InlineCmt->Content ); + else + result.append(";\n"); + } } break; @@ -299,7 +328,12 @@ String AST::to_string() result.append_fmt( "%S : %S", Name, UnderlyingType->to_string() ); if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) - result.append(";\n"); + { + if ( InlineCmt ) + result.append_fmt("; %S", InlineCmt->Content ); + else + result.append(";\n"); + } } break; @@ -327,7 +361,14 @@ String AST::to_string() result.append_fmt( "friend %S", Declaration->to_string() ); if ( result[ result.length() -1 ] != ';' ) - result.append( ";\n" ); + { + result.append( ";" ); + } + + if ( InlineCmt ) + result.append_fmt(" %S", InlineCmt->Content ); + else + result.append("\n"); break; case Function: @@ -404,7 +445,10 @@ String AST::to_string() } } - result.append( ";\n" ); + if ( InlineCmt ) + result.append_fmt( "; %S", InlineCmt->Content ); + else + result.append( ";\n" ); } break; @@ -495,8 +539,11 @@ String AST::to_string() } } } - - result.append( ";\n" ); + + if ( InlineCmt ) + result.append_fmt( "; %S", InlineCmt->Content ); + else + result.append( ";\n" ); } break; @@ -504,6 +551,8 @@ String AST::to_string() { if ( Specs ) { + // TODO : Add support for specifies before the operator keyword + if ( Name && Name.length() ) result.append_fmt( "%Soperator %S()", Name, ValueType->to_string() ); else @@ -532,6 +581,8 @@ String AST::to_string() case Operator_Cast_Fwd: if ( Specs ) { + // TODO : Add support for specifies before the operator keyword + result.append_fmt( "operator %S()", ValueType->to_string() ); for ( SpecifierT spec : Specs->cast() ) @@ -543,11 +594,17 @@ String AST::to_string() } } - result.append( ";" ); + if ( InlineCmt ) + result.append_fmt( "; %S", InlineCmt->Content ); + else + result.append( ";\n" ); break; } - result.append_fmt("operator %S();\n", ValueType->to_string() ); + if ( InlineCmt ) + result.append_fmt("operator %S(); %S", ValueType->to_string() ); + else + result.append_fmt("operator %S();\n", ValueType->to_string() ); break; case Parameters: @@ -680,7 +737,12 @@ String AST::to_string() } if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) - result.append(";\n"); + { + if ( InlineCmt ) + result.append_fmt("; %S", InlineCmt->Content ); + else + result.append(";\n"); + } } break; @@ -695,7 +757,12 @@ String AST::to_string() else result.append_fmt( "struct %S", Name ); if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) - result.append(";\n"); + { + if ( InlineCmt ) + result.append_fmt("; %S", InlineCmt->Content ); + else + result.append(";\n"); + } } break; @@ -726,8 +793,13 @@ String AST::to_string() } else { - result.append( ";\n" ); + result.append( ";" ); } + + if ( InlineCmt ) + result.append_fmt(" %S", InlineCmt->Content); + else + result.append("\n"); } break; @@ -796,15 +868,23 @@ String AST::to_string() if ( UnderlyingType->ArrExpr ) result.append_fmt( "[%S]", UnderlyingType->ArrExpr->to_string() ); - result.append( ";\n" ); + result.append( ";" ); } else - result.append_fmt( "using %S;\n", Name ); + result.append_fmt( "using %S;", Name ); + + if ( InlineCmt ) + result.append_fmt(" %S\n", InlineCmt->Content ); + else + result.append("\n"); } break; case Using_Namespace: - result.append_fmt( "using namespace %s;\n", Name ); + if ( InlineCmt ) + result.append_fmt( "using namespace $S; %S", Name, InlineCmt->Content ); + else + result.append_fmt( "using namespace %s;\n", Name ); break; case Variable: @@ -831,19 +911,27 @@ String AST::to_string() if ( Value ) result.append_fmt( " = %S", Value->to_string() ); - result.append( ";\n" ); + if ( InlineCmt ) + result.append_fmt("; %S", InlineCmt->Content); + else + result.append( ";\n" ); break; } if ( BitfieldSize ) - result.append_fmt( "%S %S : %S;\n", ValueType->to_string(), Name, BitfieldSize->to_string() ); + result.append_fmt( "%S %S : %S;", ValueType->to_string(), Name, BitfieldSize->to_string() ); else if ( UnderlyingType->ArrExpr ) - result.append_fmt( "%S %S[%S];\n", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() ); + result.append_fmt( "%S %S[%S];", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() ); else - result.append_fmt( "%S %S;\n", UnderlyingType->to_string(), Name ); + result.append_fmt( "%S %S;", UnderlyingType->to_string(), Name ); + + if ( InlineCmt ) + result.append_fmt(" %S", InlineCmt->Content); + else + result.append("\n"); } break; diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 7292e25..07f669a 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -226,10 +226,11 @@ struct AST union { struct { + AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable - AST* Specs; // Function, Operator, Type symbol, Variable + AST* Specs; // Destructor, Function, Operator, Type symbol, Variable union { - AST* InitializerList; // Constructor, Destructor + AST* InitializerList; // Constructor AST* ParentType; // Class, Struct AST* ReturnType; // Function, Operator AST* UnderlyingType; // Enum, Typedef @@ -237,7 +238,7 @@ struct AST }; union { AST* BitfieldSize; // Varaiable (Class/Struct Data Member) - AST* Params; // Function, Operator, Template + AST* Params; // Constructor, Function, Operator, Template }; union { AST* ArrExpr; // Type Symbol @@ -275,10 +276,11 @@ struct AST_POD union { struct { + AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable AST* Attributes; // Class, Enum, Function, Struct, Typename, Union, Using, Variable AST* Specs; // Function, Operator, Type symbol, Variable union { - AST* InitializerList; // Constructor, Destructor + AST* InitializerList; // Constructor AST* ParentType; // Class, Struct AST* ReturnType; // Function, Operator AST* UnderlyingType; // Enum, Typedef diff --git a/project/components/ast_types.hpp b/project/components/ast_types.hpp index 9a215be..860feab 100644 --- a/project/components/ast_types.hpp +++ b/project/components/ast_types.hpp @@ -57,6 +57,7 @@ struct AST_Class char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; char _PAD_SPECS_ [ sizeof(AST*) ]; CodeType ParentType; @@ -80,10 +81,11 @@ struct AST_Constructor char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_ [ sizeof(AST*) * 3 ]; - Code InitializerList; - CodeParam Params; - Code Body; + CodeComment InlineCmt; + char _PAD_PROPERTIES_ [ sizeof(AST*) * 3 ]; + Code InitializerList; + CodeParam Params; + Code Body; }; }; Code Prev; @@ -116,6 +118,7 @@ struct AST_Destructor char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; char _PAD_PROPERTIES_ [ sizeof(AST*) * 1 ]; CodeSpecifiers Specs; char _PAD_PROPERTIES_2_ [ sizeof(AST*) * 2 ]; @@ -137,6 +140,7 @@ struct AST_Enum char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; char _PAD_SPEC_ [ sizeof(AST*) ]; CodeType UnderlyingType; @@ -175,7 +179,7 @@ struct AST_Extern char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; + char _PAD_PROPERTIES_[ sizeof(AST*) * 5 ]; CodeBody Body; }; }; @@ -209,8 +213,9 @@ struct AST_Friend char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; - Code Declaration; + CodeComment InlineCmt; + char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; + Code Declaration; }; }; Code Prev; @@ -228,6 +233,7 @@ struct AST_Fn char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; CodeSpecifiers Specs; CodeType ReturnType; @@ -263,7 +269,7 @@ struct AST_NS union { char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; + char _PAD_PROPERTIES_[ sizeof(AST*) * 5 ]; CodeBody Body; }; }; @@ -283,6 +289,7 @@ struct AST_Operator char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; CodeSpecifiers Specs; CodeType ReturnType; @@ -306,6 +313,7 @@ struct AST_OpCast char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; char _PAD_PROPERTIES_[ sizeof(AST*) ]; CodeSpecifiers Specs; CodeType ValueType; @@ -328,7 +336,7 @@ struct AST_Param char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_2_[ sizeof(AST*) * 2 ]; + char _PAD_PROPERTIES_2_[ sizeof(AST*) * 3 ]; CodeType ValueType; char _PAD_PROPERTIES_[ sizeof(AST*) ]; Code Value; @@ -393,6 +401,7 @@ struct AST_Struct char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; char _PAD_SPECS_ [ sizeof(AST*) ]; CodeType ParentType; @@ -416,7 +425,7 @@ struct AST_Template char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { - char _PAD_PROPERTIES_[ sizeof(AST*) * 3 ]; + char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; CodeParam Params; Code Declaration; }; @@ -437,6 +446,7 @@ struct AST_Type char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + char _PAD_CMT_[ sizeof(AST*) ]; CodeAttributes Attributes; CodeSpecifiers Specs; char _PAD_PROPERTIES_[ sizeof(AST*) * 2 ]; @@ -458,6 +468,7 @@ struct AST_Typedef char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; char _PAD_PROPERTIES_[ sizeof(AST*) * 2 ]; Code UnderlyingType; char _PAD_PROPERTIES_2_[ sizeof(AST*) * 2 ]; @@ -479,6 +490,7 @@ struct AST_Union char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + char _PAD_INLINE_CMT_[ sizeof(AST*) ]; CodeAttributes Attributes; char _PAD_PROPERTIES_[ sizeof(AST*) * 3 ]; CodeBody Body; @@ -500,6 +512,7 @@ struct AST_Using char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; char _PAD_SPECS_ [ sizeof(AST*) ]; CodeType UnderlyingType; @@ -522,6 +535,7 @@ struct AST_Var char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap ]; struct { + CodeComment InlineCmt; CodeAttributes Attributes; CodeSpecifiers Specs; CodeType ValueType; diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index aaed685..f4bb2ab 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -1,5 +1,5 @@ #pragma once -// This file was generated automatially by gen.bootstrap.cpp (See: https://github.com/Ed94/gencpp) +// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) #pragma region generated code inline implementation diff --git a/project/components/gen/ecode.hpp b/project/components/gen/ecode.hpp index a1701b9..a2e61dc 100644 --- a/project/components/gen/ecode.hpp +++ b/project/components/gen/ecode.hpp @@ -1,6 +1,6 @@ #pragma once -// This file was generated automatially by gen.bootstrap.cpp (See: https://github.com/Ed94/gencpp) +// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) namespace ECode { diff --git a/project/components/gen/eoperator.hpp b/project/components/gen/eoperator.hpp index dd40844..cea26b1 100644 --- a/project/components/gen/eoperator.hpp +++ b/project/components/gen/eoperator.hpp @@ -1,6 +1,6 @@ #pragma once -// This file was generated automatially by gen.bootstrap.cpp (See: https://github.com/Ed94/gencpp) +// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) namespace EOperator { diff --git a/project/components/gen/especifier.hpp b/project/components/gen/especifier.hpp index b5f9d5e..4dedcf7 100644 --- a/project/components/gen/especifier.hpp +++ b/project/components/gen/especifier.hpp @@ -1,6 +1,6 @@ #pragma once -// This file was generated automatially by gen.bootstrap.cpp (See: https://github.com/Ed94/gencpp) +// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) namespace ESpecifier { diff --git a/project/components/gen/etoktype.cpp b/project/components/gen/etoktype.cpp index 0ef4f7a..db84d4c 100644 --- a/project/components/gen/etoktype.cpp +++ b/project/components/gen/etoktype.cpp @@ -1,4 +1,4 @@ -// This file was generated automatially by gen.bootstrap.cpp (See: https://github.com/Ed94/gencpp) +// This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) #pragma once @@ -128,7 +128,7 @@ namespace Parser { sizeof( "]" ), "]" }, { sizeof( "(" ), "(" }, { sizeof( ")" ), ")" }, - { sizeof( "__comemnt__" ), "__comemnt__" }, + { sizeof( "__comment__" ), "__comment__" }, { sizeof( "__comment_end__" ), "__comment_end__" }, { sizeof( "__comment_start__" ), "__comment_start__" }, { sizeof( "__character__" ), "__character__" }, diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index f9eea83..3f2d357 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -67,22 +67,22 @@ namespace Parser bool __eat( TokType type ); - Token& current( bool skip_new_lines = true ) + Token& current( bool skip_formatting = true ) { - if ( skip_new_lines ) + if ( skip_formatting ) { - while ( Arr[Idx].Type == TokType::NewLine ) + while ( Arr[Idx].Type == TokType::NewLine || Arr[Idx].Type == TokType::Comment ) Idx++; } return Arr[Idx]; } - Token& previous( bool skip_new_lines = false ) + Token& previous( bool skip_formatting = false ) { s32 idx = this->Idx; - if ( skip_new_lines ) + if ( skip_formatting ) { while ( Arr[idx].Type == TokType::NewLine ) idx--; @@ -93,11 +93,11 @@ namespace Parser return Arr[idx - 1]; } - Token& next( bool skip_new_lines = false ) + Token& next( bool skip_formatting = false ) { s32 idx = this->Idx; - if ( skip_new_lines ) + if ( skip_formatting ) { while ( Arr[idx].Type == TokType::NewLine ) idx++; @@ -114,7 +114,7 @@ namespace Parser } }; - constexpr bool dont_skip_new_lines = false; + constexpr bool dont_skip_formatting = false; struct StackNode { @@ -200,7 +200,8 @@ namespace Parser return false; } - if ( Arr[Idx].Type == TokType::NewLine && type != TokType::NewLine ) + if ( Arr[ Idx ].Type == TokType::NewLine && type != TokType::NewLine + || Arr[ Idx ].Type == TokType::Comment && type != TokType::Comment ) { Idx++; } @@ -879,57 +880,53 @@ namespace Parser if ( current == '/' ) { - token.Type = TokType::Comment_Start; + token.Type = TokType::Comment; token.Length = 2; - Tokens.append( token ); move_forward(); - Token content = { scanner, 1, TokType::Comment, line, column, false }; + token.Length++; while ( left && current != '\n' && current != '\r' ) { move_forward(); - content.Length++; + token.Length++; } - Tokens.append( content ); if ( current == '\r' ) { move_forward(); + token.Length++; } if ( current == '\n' ) { move_forward(); + token.Length++; } + Tokens.append( token ); continue; } else if ( current == '*' ) { - token.Type = TokType::Comment_Start; + token.Type = TokType::Comment; token.Length = 2; - Tokens.append( token ); - - Token content = { token.Text, 0, TokType::Comment, line, column, false }; + move_forward(); - content.Length++; + token.Length++; - bool star = current == '*'; + bool star = current == '*'; bool slash = scanner[1] == '/'; bool at_end = star && slash; while ( left && ! at_end ) { move_forward(); - content.Length++; + token.Length++; - star = current == '*'; + star = current == '*'; slash = scanner[1] == '/'; at_end = star && slash; } - content.Length += 3; - Tokens.append( content ); - - Token end = { scanner, 2, TokType::Comment_End, line, column, false }; - Tokens.append( end ); + token.Length += 3; + Tokens.append( token ); move_forward(); move_forward(); @@ -1141,7 +1138,7 @@ if ( def.Ptr == nullptr ) \ return CodeInvalid; \ } -# define currtok_noskip Context.Tokens.current( dont_skip_new_lines ) +# define currtok_noskip Context.Tokens.current( dont_skip_formatting ) # define currtok Context.Tokens.current() # define prevtok Context.Tokens.previous() # define nexttok Context.Tokens.next() @@ -1185,20 +1182,16 @@ internal CodeComment parse_comment() { using namespace Parser; - push_scope(); - - eat( TokType::Comment_Start ); - + StackNode scope { nullptr, currtok_noskip, NullToken, txt( __func__ ) }; + Context.push( & scope ); + CodeComment result = (CodeComment) make_code(); result->Type = ECode::Comment; - result->Content = get_cached_string( currtok ); + result->Content = get_cached_string( currtok_noskip ); result->Name = result->Content; eat( TokType::Comment ); - if ( check( TokType::Comment_End ) ) - eat( TokType::Comment_End ); - Context.pop(); return result; } @@ -1804,7 +1797,8 @@ CodeFn parse_function_after_name( eat( currtok.Type ); } - CodeBody body = { nullptr }; + CodeBody body = NoCode; + CodeComment inline_cmt = NoCode; if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); @@ -1816,7 +1810,11 @@ CodeFn parse_function_after_name( } else { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type && TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); } using namespace ECode; @@ -1857,6 +1855,9 @@ CodeFn parse_function_after_name( if ( params ) result->Params = params; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -2126,7 +2127,8 @@ CodeOperator parse_operator_after_ret_type( } // Parse Body - CodeBody body = { nullptr }; + CodeBody body = { nullptr }; + CodeComment inline_cmt = NoCode; if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); @@ -2138,11 +2140,19 @@ CodeOperator parse_operator_after_ret_type( } else { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); } // OpValidateResult check_result = operator__validate( op, params, ret_type, specifiers ); CodeOperator result = def_operator( op, nspace, params, ret_type, body, specifiers, attributes, mflags ); + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; + Context.pop(); return result; } @@ -2230,8 +2240,18 @@ CodeVar parse_variable_after_name( expr_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_tok.Text; bitfield_expr = untyped_str( expr_tok ); } - + + Token stmt_end = currtok; eat( TokType::Statement_End ); + + // Check for inline comment : = ; // + CodeComment inline_cmt = NoCode; + if ( left + && ( currtok_noskip.Type == TokType::Comment ) + && currtok_noskip.Line == stmt_end.Line ) + { + inline_cmt = parse_comment(); + } using namespace ECode; @@ -2257,6 +2277,9 @@ CodeVar parse_variable_after_name( if ( expr ) result->Value = expr; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -2294,7 +2317,11 @@ Code parse_simple_preprocess( Parser::TokType which ) { if ( check( TokType::Statement_End )) { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + eat( TokType::Comment ); } } @@ -2306,7 +2333,11 @@ Code parse_simple_preprocess( Parser::TokType which ) { if ( check( TokType::Statement_End )) { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + eat( TokType::Comment ); } } @@ -2382,7 +2413,6 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes if ( check( TokType::Capture_Start) ) { // Dealing with a function - result = parse_function_after_name( ModuleFlag::None, attributes, specifiers, type, name ); } else @@ -2575,7 +2605,7 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa eat( TokType::NewLine ); break; - case TokType::Comment_Start: + case TokType::Comment: member = parse_comment(); break; @@ -2880,14 +2910,24 @@ Code parse_class_struct( Parser::TokType which, bool inplace_def = false ) body = parse_class_struct_body( which, name ); } + CodeComment inline_cmt = NoCode; if ( ! inplace_def ) + { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); + } if ( which == TokType::Decl_Class ) result = def_class( name, body, parent, access, attributes, mflags ); else result = def_struct( name, body, (CodeType)parent, access, attributes, mflags ); + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; interfaces.free(); return result; @@ -2972,7 +3012,7 @@ CodeBody parse_global_nspace( CodeT which ) eat( TokType::NewLine ); break; - case TokType::Comment_Start: + case TokType::Comment: member = parse_comment(); break; @@ -3232,10 +3272,11 @@ CodeConstructor parse_constructor() using namespace Parser; push_scope(); - Token identifier = parse_identifier(); - CodeParam params = parse_params(); - Code initializer_list = { nullptr }; - CodeBody body = { nullptr }; + Token identifier = parse_identifier(); + CodeParam params = parse_params(); + Code initializer_list = NoCode; + CodeBody body = NoCode; + CodeComment inline_cmt = NoCode; if ( check( TokType::Assign_Classifer ) ) { @@ -3263,6 +3304,14 @@ CodeConstructor parse_constructor() { body = parse_function_body(); } + else + { + Token stmt_end = currtok; + eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); + } CodeConstructor result = (CodeConstructor) make_code(); @@ -3279,6 +3328,9 @@ CodeConstructor parse_constructor() } else result->Type = ECode::Constructor_Fwd; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -3321,8 +3373,8 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) return CodeInvalid; } - Token identifier = parse_identifier(); - CodeBody body = { nullptr }; + Token identifier = parse_identifier(); + CodeBody body = { nullptr }; eat( TokType::Capture_Start ); eat( TokType::Capture_End ); @@ -3343,9 +3395,19 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) return CodeInvalid; } } + + CodeComment inline_cmt = NoCode; if ( check( TokType::BraceCurly_Open ) ) body = parse_function_body(); + else + { + Token stmt_end = currtok; + eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); + } CodeDestructor result = (CodeDestructor) make_code(); @@ -3359,6 +3421,9 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) } else result->Type = ECode::Destructor_Fwd; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -3452,7 +3517,7 @@ CodeEnum parse_enum( bool inplace_def ) eat( TokType::NewLine ); break; - case TokType::Comment_Start: + case TokType::Comment: member = parse_comment(); break; @@ -3528,8 +3593,16 @@ CodeEnum parse_enum( bool inplace_def ) eat( TokType::BraceCurly_Close ); } + CodeComment inline_cmt = NoCode; + if ( ! inplace_def ) + { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); + } using namespace ECode; @@ -3553,6 +3626,9 @@ CodeEnum parse_enum( bool inplace_def ) if ( type ) result->UnderlyingType = type; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -3690,8 +3766,13 @@ CodeFriend parse_friend() if ( params ) function->Params = params; } - + + Token stmt_end = currtok; eat( TokType::Statement_End ); + + CodeComment inline_cmt = NoCode; + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); CodeFriend result = (CodeFriend) make_code(); @@ -3702,6 +3783,9 @@ CodeFriend parse_friend() else result->Declaration = type; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -3949,7 +4033,7 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) using namespace Parser; push_scope(); - // Specifiers attributed to the cast + // TODO : Specifiers attributed to the cast // Operator's namespace if not within same class. Token name = NullToken; @@ -3987,7 +4071,8 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) eat( TokType::Spec_Const ); } - Code body = { nullptr }; + Code body = NoCode; + CodeComment inline_cmt = NoCode; if ( check( TokType::BraceCurly_Open) ) { @@ -4014,7 +4099,11 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) } else { + Token stmt_end = currtok; eat( TokType::Statement_End ); + + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); } CodeOpCast result = (CodeOpCast) make_code(); @@ -4542,8 +4631,13 @@ CodeTypedef parse_typedef() } array_expr = parse_array_decl(); - + + Token stmt_end = currtok; eat( TokType::Statement_End ); + + CodeComment inline_cmt = NoCode; + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + inline_cmt = parse_comment(); using namespace ECode; @@ -4567,6 +4661,9 @@ CodeTypedef parse_typedef() if ( type->Type == Typename && array_expr && array_expr->Type != Invalid ) type.cast()->ArrExpr = array_expr; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; Context.pop(); return result; @@ -4633,7 +4730,7 @@ CodeUnion parse_union( bool inplace_def ) eat( TokType::NewLine ); break; - case TokType::Comment_Start: + case TokType::Comment: member = parse_comment(); break; @@ -4778,7 +4875,14 @@ CodeUsing parse_using() array_expr = parse_array_decl(); + Token stmt_end = currtok; eat( TokType::Statement_End ); + + CodeComment inline_cmt = NoCode; + if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) + { + inline_cmt = parse_comment(); + } using namespace ECode; @@ -4803,6 +4907,9 @@ CodeUsing parse_using() if ( attributes ) result->Attributes = attributes; + + if ( inline_cmt ) + result->InlineCmt = inline_cmt; } Context.pop(); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index a69b7f7..764c92a 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -415,11 +415,41 @@ CodeComment def_comment( StrC content ) return CodeInvalid; } + static char line[ MaxCommentLineLength ]; + + String cmt_formatted = String::make_reserve( GlobalAllocator, kilobytes(1) ); + char const* end = content.Ptr + content.Len; + char const* scanner = content.Ptr; + s32 curr = 0; + do + { + char const* next = scanner; + s32 length = 0; + while ( next != end && scanner[ length ] != '\n' ) + { + next = scanner + length; + length++; + } + length++; + + str_copy( line, scanner, length ); + cmt_formatted.append_fmt( "//%.*s", length, line ); + mem_set( line, 0, MaxCommentLineLength ); + + scanner += length; + } + while ( scanner <= end ); + + if ( cmt_formatted.back() != '\n' ) + cmt_formatted.append( "\n" ); + Code result = make_code(); result->Type = ECode::Comment; - result->Name = get_cached_string( content ); + result->Name = get_cached_string( cmt_formatted ); result->Content = result->Name; + + cmt_formatted.free(); return (CodeComment) result; } diff --git a/project/enums/ETokType.csv b/project/enums/ETokType.csv index 49a2fe3..608d053 100644 --- a/project/enums/ETokType.csv +++ b/project/enums/ETokType.csv @@ -15,7 +15,7 @@ BraceSquare_Open, "[" BraceSquare_Close, "]" Capture_Start, "(" Capture_End, ")" -Comment, "__comemnt__" +Comment, "__comment__" Comment_End, "__comment_end__" Comment_Start, "__comment_start__" Char, "__character__" diff --git a/singleheader/singleheader.cpp b/singleheader/singleheader.cpp index a699613..f79c017 100644 --- a/singleheader/singleheader.cpp +++ b/singleheader/singleheader.cpp @@ -17,7 +17,7 @@ GEN_NS_END using namespace gen; constexpr char const* generation_notice = -"// This file was generated automatially by gen.bootstrap.cpp " +"// This file was generated automatially by gencpp's singleheader.cpp" "(See: https://github.com/Ed94/gencpp)\n\n"; constexpr StrC implementation_guard_start = txt(R"( diff --git a/test/test.cpp b/test/test.cpp index 47b36f5..405b160 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -7,7 +7,7 @@ #include "gen.builder.cpp" #include "sanity.cpp" #include "SOA.cpp" -#include "test.singleheader.cpp" +#include "validate.singleheader.cpp" int gen_main() { @@ -18,7 +18,7 @@ int gen_main() // check_SOA(); - // check_singleheader_ast(); + check_singleheader_ast(); return 0; } #endif