polserver/pol-core/bscript/compiler/file/SourceFile.h
turleypol 7627e3d353
"Preview/Initial"- Version sourcecode formatting feature (#626)
* Squash only prettify changes from escript_formating_try

* support of "format-off" / "format-on" comments to mark areas without
formatting

* fix typo

* added FormatterIdentLevel, FormatterMergeEmptyLines cfg entry
better line splits for dicts/arrays

* added several spacing options. the current setting list is now:
//LineWidth
FormatterLineWidth 80
// keep original keyword spelling
FormatterKeepKeywords 0
// number of spaces for ident
FormatterIdentLevel 2
// multiple newlines get merged to a single
FormatterMergeEmptyLines 1
// space between emtpy parenthesis eg foo() vs foo( )
FormatterEmptyParenthesisSpacing 0
// space between emtpy brackets eg struct{} vs struct{ }
FormatterEmptyBracketSpacing 0
// space after/before parenthesis in conditionals
// eg if ( true ) vs if (true)
FormatterConditionalParenthesisSpacing 1
// space after/before parenthesis
// eg foo( true ) vs foo(true)
FormatterParenthesisSpacing 1
// space after/before brackets
// eg array{ true } vs array{true}
FormatterBracketSpacing 1
// add space after delimiter comma or semi in for loops
// eg {1, 2, 3} vs {1,2,3}
FormatterDelimiterSpacing 1
// add space around assignment
// eg a := 1; vs a:=1;
FormatterAssignmentSpacing 1
// add space around comparison
// eg a == 1 vs a==1
FormatterComparisonSpacing 1
// add space around operations
// eg a + 1 vs a+1
FormatterOperatorSpacing 1
// use \r\n as newline instead of \n
FormatterWindowsLineEndings 0

* added tab support
// use tabs instead of spaces
FormatterUseTabs 0
// tab width
FormatterTabWidth 4

* extended example ecompile.cfg with formatter settings

* cleanup

* program args comma is optional...
fixed line comments in group splitting

* since tokenids are now correct no more guessing for linecomments needed

* comments use as start tokenid the whitespace token if its on the same
line

* splitted processing from linebuilding

* renamed TokenPart to FmtToken better matches the purpose.
Comments get directly stored as FmtToken
started with a context enum

* better(?) formatting for groups

* fix eof rawlines
added check that all comments/rawlines got parsed

* const correctness

* datatype adapted to antlr

* fixed warnings

* added InsertNewlineAtEOF

* add newline at the end if the original file had one
dont strip comment whitespace if its somejind of "header block"
use the defined lineending for /* */ comments

* docs

* \r\n is default on windows \n otherwise

* fixed warning

---------

Co-authored-by: Kevin Eady <8634912+KevinEady@users.noreply.github.com>
2024-02-26 21:21:44 +01:00

72 lines
2.3 KiB
C++

#ifndef POLSERVER_SOURCEFILE_H
#define POLSERVER_SOURCEFILE_H
#include <memory>
#include <mutex>
#include <string>
#include "bscript/compiler/file/ConformingCharStream.h"
#include "bscript/compiler/file/ErrorListener.h"
#include "bscript/compiler/file/SourceLocation.h"
#include <EscriptGrammar/EscriptLexer.h>
#include <EscriptGrammar/EscriptParser.h>
namespace antlr4
{
class ParserRuleContext;
}
namespace EscriptGrammar
{
class EscriptParserVisitor;
}
namespace Pol::Bscript::Compiler
{
class Profile;
class Report;
class SourceLocation;
class SourceFile
{
public:
SourceFile( const std::string& pathname, const std::string& contents, Profile& );
~SourceFile();
static bool enforced_case_sensitivity_mismatch( const SourceLocation& referencing_location,
const std::string& pathname, Report& report );
static std::shared_ptr<SourceFile> load( const SourceFileIdentifier&, Profile&, Report& );
void propagate_errors_to( Report&, const SourceFileIdentifier& );
EscriptGrammar::EscriptParser::CompilationUnitContext* get_compilation_unit(
Report&, const SourceFileIdentifier& );
EscriptGrammar::EscriptParser::ModuleUnitContext* get_module_unit( Report&,
const SourceFileIdentifier& );
EscriptGrammar::EscriptParser::EvaluateUnitContext* get_evaluate_unit( Report& );
antlr4::Token* get_token_at( const Position& position );
std::vector<antlr4::Token*> get_all_tokens();
std::vector<antlr4::Token*> get_hidden_tokens_before( const Position& position );
std::vector<antlr4::Token*> get_hidden_tokens_before( size_t tokenIndex );
const std::string pathname;
private:
antlr4::ANTLRInputStream input;
ConformingCharStream conformer;
EscriptGrammar::EscriptLexer lexer;
antlr4::CommonTokenStream token_stream;
EscriptGrammar::EscriptParser parser;
ErrorListener error_listener;
std::mutex mutex;
std::atomic<EscriptGrammar::EscriptParser::CompilationUnitContext*> compilation_unit;
std::atomic<EscriptGrammar::EscriptParser::ModuleUnitContext*> module_unit;
std::atomic<EscriptGrammar::EscriptParser::EvaluateUnitContext*> evaluate_unit;
std::atomic<unsigned> access_count;
};
} // namespace Pol::Bscript::Compiler
#endif // POLSERVER_SOURCEFILE_H