Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Preview/Initial"- Version sourcecode formatting feature #626

Merged
merged 23 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fdbc22c
Squash only prettify changes from escript_formating_try
KevinEady Feb 15, 2024
82f0cf0
support of "format-off" / "format-on" comments to mark areas without
turleypol Feb 16, 2024
aab1a9c
fix typo
turleypol Feb 16, 2024
a104412
added FormatterIdentLevel, FormatterMergeEmptyLines cfg entry
turleypol Feb 16, 2024
b475d71
added several spacing options. the current setting list is now:
turleypol Feb 17, 2024
1bac559
added tab support
turleypol Feb 17, 2024
393d41d
extended example ecompile.cfg with formatter settings
turleypol Feb 17, 2024
9ab36e9
cleanup
turleypol Feb 17, 2024
488d599
program args comma is optional...
turleypol Feb 17, 2024
9d6208c
since tokenids are now correct no more guessing for linecomments needed
turleypol Feb 18, 2024
8e0596b
comments use as start tokenid the whitespace token if its on the same
turleypol Feb 18, 2024
2dc991a
splitted processing from linebuilding
turleypol Feb 18, 2024
1e04111
renamed TokenPart to FmtToken better matches the purpose.
turleypol Feb 19, 2024
e8c7366
better(?) formatting for groups
turleypol Feb 21, 2024
3e4968b
fix eof rawlines
turleypol Feb 22, 2024
8849c36
const correctness
turleypol Feb 22, 2024
054faa5
datatype adapted to antlr
turleypol Feb 23, 2024
fbea8f2
fixed warnings
turleypol Feb 23, 2024
2ea2a86
added InsertNewlineAtEOF
turleypol Feb 23, 2024
23728a0
add newline at the end if the original file had one
turleypol Feb 24, 2024
097a4a5
docs
turleypol Feb 26, 2024
1aebf56
\r\n is default on windows \n otherwise
turleypol Feb 26, 2024
0f49e1b
fixed warning
turleypol Feb 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 29 additions & 4 deletions cmake/escripttest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ if(NOT DEFINED ecompile)
message(FATAL_ERROR "ecompile not defined")
endif()

# Uncomment to not delete failed formatted files (will be saved as {scriptname}.formatted.src)
# set(keepfailedformats TRUE)

function (cleanup scriptname)
foreach (ext .ecl .tst)
foreach (ext .ecl .tst .unformatted.src .formatted.src)
if(EXISTS "${scriptname}${ext}")
if ("${ext}" STREQUAL ".formatted.src" AND DEFINED keepfailedformats)
continue()
endif()
if("${ext}" STREQUAL ".unformatted.src")
configure_file(${scriptname}${ext} ${scriptname}.src COPYONLY)
endif()
file(REMOVE "${scriptname}${ext}")
endif()
endforeach()
Expand Down Expand Up @@ -85,15 +94,27 @@ function (checkprocfailure output errorfile result)
endfunction()

# start of test
function (testwithcompiler)
function (testwithcompiler formatoption)
file(GLOB scripts RELATIVE ${testdir} ${testdir}/${subtest}/*)
foreach(script ${scripts})
string(FIND "${script}" ".src" out)
if(${out} LESS 0)
continue()
endif()
message(${script})
string(REPLACE ".src" "" scriptname "${script}")

if (EXISTS "${testdir}/${scriptname}.out" AND NOT ${formatoption} STREQUAL "")
message("${script} [formatted]")
string(REPLACE ".src" ".unformatted.src" unformattedscript "${script}")
configure_file(${testdir}/${script} ${testdir}/${unformattedscript} COPYONLY)
execute_process( COMMAND ${ecompile} ${formatoption} -q -C ecompile.cfg ${script}
RESULT_VARIABLE ecompile_format_res
OUTPUT_VARIABLE ecompile_format_out
ERROR_VARIABLE ecompile_format_out)
else()
message(${script})
endif()

if (EXISTS "${testdir}/${scriptname}.out" OR EXISTS "${testdir}/${scriptname}.err")
execute_process( COMMAND ${ecompile} -q -C ecompile.cfg ${script}
RESULT_VARIABLE ecompile_res
Expand All @@ -112,6 +133,9 @@ function (testwithcompiler)
endif()
else()
if(NOT "${ecompile_res}" STREQUAL "0")
if(NOT ${formatoption} STREQUAL "" AND DEFINED keepfailedformats)
configure_file(${testdir}/${script} ${testdir}/${scriptname}.formatted.src)
endif()
cleanup(${scriptname})
message(SEND_ERROR "${scriptname}.src did not compile")
message(${ecompile_out})
Expand Down Expand Up @@ -148,4 +172,5 @@ function (testwithcompiler)
endforeach()
endfunction()

testwithcompiler()
testwithcompiler("")
testwithcompiler("-Fi")
20 changes: 19 additions & 1 deletion docs/docs.polserver.com/pol100/configfiles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<list>elipsis ... : repeat entries are allowed.</list>
<bottom>This means the punctuation in these cases should NOT be in the actual config files used by POL, they only appear here for information purposes. Exception: curly braces { } are used to define an element in a config file. These must be present in the actual file.</bottom>
</desc>
<datemodified>10/18/2023</datemodified>
<datemodified>02/26/2024</datemodified>
</fileheader>


Expand Down Expand Up @@ -1042,6 +1042,24 @@ PackageRoot (path to package root)
[EmParseTreeCacheSize (0-N {default 25})] //Cache parse trees for this number of *.em files
[IncParseTreeCacheSize (0-N {default 50})] //Cache parse trees for this number of *.inc files

// Formatter Options (-F -Fi)
[FormatterLineWidth (0-N {default 80})]
[FormatterKeepKeywords (0/1 {default 0})] // keep original keyword spelling
[FormatterIdentLevel (0-N {default 2})] // number of spaces for ident
[FormatterMergeEmptyLines (0/1 {default 1})] // multiple newlines get merged to a single
[FormatterEmptyParenthesisSpacing (0/1 {default 0})] // space between emtpy parenthesis eg foo() vs foo( )
[FormatterEmptyBracketSpacing (0/1 {default 0})] // space between emtpy brackets eg struct{} vs struct{ }
[FormatterConditionalParenthesisSpacing (0/1 {default 1})] // space after/before parenthesis in conditionals, eg if ( true ) vs if (true)
[FormatterParenthesisSpacing (0/1 {default 1})] // space after/before parenthesis,eg foo( true ) vs foo(true)
[FormatterBracketSpacing (0/1 {default 1})] // space after/before brackets,eg array{ true } vs array{true}
[FormatterDelimiterSpacing (0/1 {default 1})] // add space after delimiter comma or semi in for loops, eg {1, 2, 3} vs {1,2,3}
[FormatterAssignmentSpacing (0/1 {default 1})] // add space around assignment, eg a := 1; vs a:=1;
[FormatterComparisonSpacing (0/1 {default 1})] // add space around comparison, eg a == 1 vs a==1
[FormatterOperatorSpacing (0/1 {default 1})] // add space around operations, eg a + 1 vs a+1
[FormatterWindowsLineEndings (0/1 {default 0/1})] // use \r\n (windows default) as newline instead of \n (default otherwise)
[FormatterUseTabs (0/1 {default 0})] // use tabs instead of spaces
[FormatterTabWidth (0-N {default 4})]
[FormatterInsertNewlineAtEOF (0/1 {default 0})] // Insert a newline at end of file if missing
</structure>
<explain>The 'generate' properties allow default behavior for generating listing or debug files.</explain>
<explain>If not found, ecompile will try to locate module and include directories from the system environment variables ECOMPILE_PATH_EM and ECOMPILE_PATH_INC.</explain>
Expand Down
13 changes: 12 additions & 1 deletion docs/docs.polserver.com/pol100/corechanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
<ESCRIPT>
<header>
<topic>Latest Core Changes</topic>
<datemodified>02-08-2024</datemodified>
<datemodified>02-26-2024</datemodified>
</header>
<version name="POL100.2.0">
<entry>
<date>02-26-2024</date>
<author>Turley:</author>
<change type="Added">ecompile option -F and -Fi<br/>
EScript formatter for .src and .inc (, .em) files, can be combined with -r and -A<br/>
-F prints the formatted source without touching thr original files<br/>
-Fi (inplace) formatting, rewrites the source file(s)<br/>
ecompile.cfg.example in the distribution contains all the options currently available (or online configuration files docs)<br/>
comments containing &quot;format-off&quot; disables the formatting until a comment containing &quot;format-on&quot; is found (or eof is reached)<br/>
this is a very early stage of the formatter, so dont expect perfect results</change>
</entry>
<entry>
<date>02-08-2024</date>
<author>Kukkino:</author>
Expand Down
6 changes: 6 additions & 0 deletions pol-core/bscript/CMakeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ set (bscript_sources # sorted !
compiler/file/ConformingCharStream.h
compiler/file/ErrorListener.cpp
compiler/file/ErrorListener.h
compiler/file/PrettifyBuilder.cpp
compiler/file/PrettifyBuilder.h
compiler/file/PrettifyFileProcessor.cpp
compiler/file/PrettifyFileProcessor.h
compiler/file/PrettifyLineBuilder.cpp
compiler/file/PrettifyLineBuilder.h
compiler/file/SourceFile.cpp
compiler/file/SourceFile.h
compiler/file/SourceFileCache.cpp
Expand Down
20 changes: 20 additions & 0 deletions pol-core/bscript/compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bscript/compiler/analyzer/SemanticAnalyzer.h"
#include "bscript/compiler/astbuilder/CompilerWorkspaceBuilder.h"
#include "bscript/compiler/codegen/CodeGenerator.h"
#include "bscript/compiler/file/PrettifyBuilder.h"
#include "bscript/compiler/file/SourceFileCache.h"
#include "bscript/compiler/file/SourceFileIdentifier.h"
#include "bscript/compiler/format/CompiledScriptSerializer.h"
Expand Down Expand Up @@ -127,6 +128,25 @@ void Compiler::compile_file_steps( const std::string& pathname, Report& report )
output = generate( std::move( workspace ) );
}

bool Compiler::format_file( const std::string& filename, bool is_module, bool inplace )
{
Report report( false, true );
PrettifyBuilder prettify_builder( profile, report );
auto formatted = prettify_builder.build( filename, is_module );
if ( report.error_count() )
return false;
if ( inplace )
{
std::ofstream filestream;
filestream.open( filename, std::ios_base::out | std::ios_base::trunc );
filestream << formatted;
filestream.flush();
}
else
INFO_PRINTLN( formatted );
return true;
}

std::unique_ptr<CompilerWorkspace> Compiler::build_workspace( const std::string& pathname,
Report& report )
{
Expand Down
1 change: 1 addition & 0 deletions pol-core/bscript/compiler/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Compiler
void set_include_compile_mode();

void compile_file_steps( const std::string& pathname, Report& );
bool format_file( const std::string& filename, bool is_module, bool inplace );

private:
std::unique_ptr<CompilerWorkspace> build_workspace( const std::string&, Report& );
Expand Down
11 changes: 6 additions & 5 deletions pol-core/bscript/compiler/codegen/InstructionGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ namespace Pol::Bscript::Compiler
InstructionGenerator::InstructionGenerator(
InstructionEmitter& emitter, std::map<std::string, FlowControlLabel>& user_function_labels,
bool in_function )
: emitter( emitter ),
emit( emitter ),
user_function_labels( user_function_labels ),
in_function( in_function )
: emitter( emitter ),
emit( emitter ),
user_function_labels( user_function_labels ),
in_function( in_function )
{
}

Expand All @@ -93,7 +93,8 @@ void InstructionGenerator::update_debug_location( const Node& node )

void InstructionGenerator::update_debug_location( const SourceLocation& loc )
{
emit.debug_file_line( loc.source_file_identifier->index, loc.line_number );
emit.debug_file_line( loc.source_file_identifier->index,
static_cast<unsigned>( loc.range.start.line_number ) );
}

void InstructionGenerator::visit_array_initializer( ArrayInitializer& node )
Expand Down
41 changes: 41 additions & 0 deletions pol-core/bscript/compiler/file/PrettifyBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "PrettifyBuilder.h"

#include "bscript/compiler/Profile.h"
#include "bscript/compiler/Report.h"
#include "bscript/compiler/file/PrettifyFileProcessor.h"
#include "bscript/compiler/file/SourceFile.h"
#include "bscript/compiler/file/SourceFileIdentifier.h"
#include "bscript/compiler/file/SourceLocation.h"

namespace Pol::Bscript::Compiler
{
PrettifyBuilder::PrettifyBuilder( Profile& profile, Report& report )
: profile( profile ), report( report )
{
}

std::string PrettifyBuilder::build( const std::string& pathname, bool is_module )
{
auto ident = std::make_unique<SourceFileIdentifier>( 0, pathname );

SourceLocation source_location( ident.get(), 0, 0 );

auto sf = SourceFile::load( *ident, profile, report );

if ( !sf || report.error_count() )
{
report.error( *ident, "Unable to load '{}'.", pathname );
return {};
}
PrettifyFileProcessor prettify_processor( *ident, profile, report );
if ( is_module )
prettify_processor.process_module_unit( *sf );
else
prettify_processor.process_compilation_unit( *sf );

if ( report.error_count() )
return {};
return prettify_processor.prettify();
}

} // namespace Pol::Bscript::Compiler
21 changes: 21 additions & 0 deletions pol-core/bscript/compiler/file/PrettifyBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>

namespace Pol::Bscript::Compiler
{
class Profile;
class Report;

class PrettifyBuilder
{
public:
PrettifyBuilder( Profile&, Report& );

std::string build( const std::string& pathname, bool is_module );

Profile& profile;
Report& report;
};

} // namespace Pol::Bscript::Compiler