Skip to content

Commit

Permalink
Fixes and improvements to serialization.
Browse files Browse the repository at this point in the history
There were multiple issues with comment and newline lexing.

Extended printing functions to support Strings with %S flag (captial 'S').
Allows for length detection. Also made it so that precision for strings is the string length.
  • Loading branch information
Ed94 committed Aug 9, 2023
1 parent bb35444 commit b5fa864
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 224 deletions.
363 changes: 169 additions & 194 deletions project/components/ast.cpp

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions project/components/interface.parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,13 @@ namespace Parser

if ( current == '\n' )
{
token.Type = TokType::NewLine;
token.Length ++;
move_forward();

token.Type = TokType::NewLine;
token.Length++;

Tokens.append( token );
// log_fmt( "NewLine: %d\n", token.Line );
continue;
}
}
Expand Down Expand Up @@ -370,14 +372,14 @@ namespace Parser

if ( current == '\r' )
{
// move_forward();
// token.Length++;
move_forward();
token.Length++;
}

if ( current == '\n' )
{
// move_forward();
// token.Length++;
move_forward();
token.Length++;
break;
}

Expand Down Expand Up @@ -502,13 +504,13 @@ namespace Parser

if ( current == '\r' )
{
// move_forward();
move_forward();
// content.Length++;
}

if ( current == '\n' )
{
// move_forward();
move_forward();
// content.Length++;
break;
}
Expand Down Expand Up @@ -880,7 +882,8 @@ namespace Parser
Tokens.append( token );

move_forward();
Token content = { scanner, 1, TokType::Comment, line, column, false };
// move_forward();
Token content = { scanner, 0, TokType::Comment, line, column, false };

bool star = current == '*';
bool slash = scanner[1] == '/';
Expand Down Expand Up @@ -1044,15 +1047,13 @@ namespace Parser
token.Length++;
}

if ( current == '\r' )
if ( current == '\r' && scanner[1] == '\n' )
{
move_forward();
// token.Length++;
}
if ( current == '\n' )
else if ( current == '\n' )
{
move_forward();
// token.Length++;
}
}
else
Expand Down Expand Up @@ -1326,10 +1327,9 @@ Code parse_static_assert()

content.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)content.Text;

// content.Text = str_fmt_buf( "%.*s\n", content.Length, content.Text );
// content.Length++;
char const* result = str_fmt_buf( "%.*s\n", content.Length, content.Text );

assert->Content = get_cached_string( content );
assert->Content = get_cached_string( to_StrC( result ) );
assert->Name = assert->Content;

Context.pop();
Expand Down Expand Up @@ -2197,7 +2197,7 @@ CodeVar parse_variable_after_name(
eat( currtok.Type );
}

expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
expr_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_tok.Text;
bitfield_expr = untyped_str( expr_tok );
}

Expand Down Expand Up @@ -2262,7 +2262,9 @@ Code parse_simple_preprocess( Parser::TokType which )
tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text;
}

Code result = untyped_str( tok );
char const* content = str_fmt_buf( "%.*s\n", tok.Length, tok.Text );

Code result = untyped_str( to_StrC( content ) );
Context.Scope->Name = tok;

if ( str_compare( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 )
Expand Down Expand Up @@ -2520,7 +2522,7 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa

Context.Scope->Start = currtok_noskip;

if ( currtok.Type == TokType::Preprocess_Hash )
if ( currtok_noskip.Type == TokType::Preprocess_Hash )
eat( TokType::Preprocess_Hash );

switch ( currtok_noskip.Type )
Expand Down Expand Up @@ -2906,7 +2908,7 @@ CodeBody parse_global_nspace( CodeT which )

Context.Scope->Start = currtok_noskip;

if ( currtok.Type == TokType::Preprocess_Hash )
if ( currtok_noskip.Type == TokType::Preprocess_Hash )
eat( TokType::Preprocess_Hash );

switch ( currtok_noskip.Type )
Expand Down
6 changes: 5 additions & 1 deletion project/components/interface.upfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,17 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr )
switch (type)
{
case EPreprocessCond::If:
result->Type = ECode::Preprocess_If;
result->Type = Preprocess_If;
break;
case EPreprocessCond::IfDef:
result->Type = Preprocess_IfDef;
break;
case EPreprocessCond::IfNotDef:
result->Type = Preprocess_IfNotDef;
break;
case EPreprocessCond::ElIf:
result->Type = Preprocess_ElIf;
break;
}

return result;
Expand Down
12 changes: 11 additions & 1 deletion project/dependencies/printing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
}

if ( info && info->precision >= 0 )
len = str_len( str, info->precision );
// Made the design decision for this library that precision is the length of the string.
len = info->precision;
else
len = str_len( str );

Expand Down Expand Up @@ -410,6 +411,15 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
len = _print_string( text, remaining, &info, va_arg( va, char* ) );
break;

case 'S':
{
String gen_str = String { va_arg( va, char*) };

info.precision = gen_str.length();
len = _print_string( text, remaining, &info, gen_str );
}
break;

case 'r' :
len = _print_repeated_char( text, remaining, &info, va_arg( va, int ) );
break;
Expand Down
5 changes: 5 additions & 0 deletions project/dependencies/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ struct String
return header.Capacity - header.Length;
}

char& back()
{
return Data[ length() - 1 ];
}

sw capacity() const
{
Header const&
Expand Down
8 changes: 4 additions & 4 deletions project/helpers/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ CodeBody gen_ecode( char const* path )
)));
#pragma pop_macro( "local_persist" )

CodeNS nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) );
CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) );
CodeNS nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) );
CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) );

return def_global_body( args( nspace, code_t ) );
}
Expand Down Expand Up @@ -324,8 +324,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
#pragma pop_macro( "do_once_start" )
#pragma pop_macro( "do_once_end" )

CodeNS nspace = def_namespace( name(ETokType), def_namespace_body( args( attribute_entires_def, enum_code, to_str, to_type ) ) );
CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) );
CodeNS nspace = def_namespace( name(ETokType), def_namespace_body( args( attribute_entires_def, enum_code, to_str, to_type ) ) );
CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) );

return def_global_body( args( nspace, td_toktype ) );
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/test.gen_run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ $path_gen = Join-Path $path_test gen
$path_test_build = Join-Path $path_test build
$path_gen_build = Join-Path $path_gen build

# Invoke-Expression "& $(Join-Path $PSScriptRoot 'bootstrap.ci.ps1')"
# Invoke-Expression "& $(Join-Path $PSScriptRoot 'singleheader.ci.ps1')"

write-host "`n`nBuilding Test`n"

if ( -not( Test-Path $path_gen_build ) )
Expand Down
4 changes: 2 additions & 2 deletions test/SOA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void check_SOA()
{
log_fmt("\ncheck_SOA:");
gen::init();

Builder soa_test = Builder::open( "SOA.gen.hpp" );

soa_test.print( parse_using( code(
using u16 = unsigned short;
)));
Expand Down
4 changes: 2 additions & 2 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ int gen_main()

// check_sanity();

check_SOA();
// check_SOA();

// check_singleheader_ast();
check_singleheader_ast();

return 0;
}
Expand Down

0 comments on commit b5fa864

Please sign in to comment.