mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
* 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>
129 lines
3.5 KiB
C++
129 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include "bscript/compiler/file/SourceLocation.h"
|
|
#include <EscriptGrammar/EscriptParserBaseVisitor.h>
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
// TODO: something like that to set in FileProcessor all needed meta info to define the actual style
|
|
enum class FmtContext
|
|
{
|
|
NONE = 0,
|
|
KEYWORD = 1, // space
|
|
KEYWORD_BREAKING,
|
|
LITERAL,
|
|
TERMINATOR, // terminatorStyle
|
|
OPENING_PARANTHESIS, //
|
|
OPENING_PARANTHESIS_ATTACHED, //
|
|
CLOSING_PARANTHESIS, // closing..
|
|
OPENING_BRACKET, //
|
|
OPENING_BRACKET_ATTACHED, //
|
|
CLOSING_BRACKET, //
|
|
DELIMITER, // delimiterStyle
|
|
DELIMITER_NONBREAKING, // really?
|
|
DELIMITER_SPACE_ATTACHED, // enum
|
|
ASSIGNMENT,
|
|
OPER_PREFIX,
|
|
OPER_PREFIX_SPACE,
|
|
OPER_POSTFIX,
|
|
BINARY_SPACE_ATTACHED,
|
|
BINARY_ATTACHED,
|
|
BINARY_SPACE_BREAK, // assignment
|
|
BINARY_ASSIGNMENT,
|
|
BINARY_SPACE,
|
|
BINARY_COMPARISON,
|
|
BINARY_OPERATOR,
|
|
BINARY_ELVIS,
|
|
BINARY_QUESTION,
|
|
SUFFIX_ATTACHED,
|
|
COLON,
|
|
FUNC_REF,
|
|
INTER_STRING,
|
|
INTER_STRING_PART,
|
|
INTER_STRING_END,
|
|
|
|
LINE_COMMENT,
|
|
COMMENT,
|
|
};
|
|
|
|
// structure to hold one token as string
|
|
struct FmtToken
|
|
{
|
|
enum Style
|
|
{
|
|
NONE = 0, // do nothing
|
|
ATTACHED = 1, // override SPACE of preceding token
|
|
SPACE = 2, // add a whitespace char after this token
|
|
BREAKPOINT = 4, // potential linebreak
|
|
FORCED_BREAK = 8, // force linebreak
|
|
};
|
|
std::string text = {};
|
|
Position pos = {};
|
|
Position pos_end = {};
|
|
int style = 0;
|
|
size_t group = 0;
|
|
size_t token_type = 0;
|
|
FmtContext context = FmtContext::NONE;
|
|
FmtToken() = default;
|
|
FmtToken( std::string&& text, const Position& pos, int style, size_t group, size_t token_type )
|
|
: text( std::move( text ) ),
|
|
pos( pos ),
|
|
pos_end( pos ),
|
|
style( style ),
|
|
group( group ),
|
|
token_type( token_type ){};
|
|
};
|
|
|
|
class PrettifyLineBuilder
|
|
{
|
|
public:
|
|
PrettifyLineBuilder() = default;
|
|
const std::vector<std::string>& formattedLines() const;
|
|
|
|
void setRawLines( std::vector<std::string> rawlines );
|
|
void setComments( std::vector<FmtToken> comments );
|
|
void setSkipLines( std::vector<Range> skiplines );
|
|
void addPart( FmtToken part );
|
|
void buildLine( size_t current_ident );
|
|
bool finalize();
|
|
const std::vector<FmtToken>& currentTokens() const;
|
|
|
|
int closingParenthesisStyle( size_t begin_size );
|
|
int closingBracketStyle( size_t begin_size );
|
|
int openingParenthesisStyle();
|
|
int openingBracketStyle();
|
|
int delimiterStyle();
|
|
int terminatorStyle();
|
|
int assignmentStyle();
|
|
int comparisonStyle();
|
|
int operatorStyle();
|
|
|
|
private:
|
|
std::vector<std::string> _rawlines = {};
|
|
std::vector<std::string> _lines = {};
|
|
std::vector<FmtToken> _line_parts = {};
|
|
std::vector<FmtToken> _comments = {};
|
|
std::vector<Range> _skiplines = {};
|
|
size_t _last_line = 0;
|
|
size_t _currident = 0;
|
|
size_t _currentgroup = 0;
|
|
void mergeRawContent( size_t nextlineno );
|
|
void mergeComments();
|
|
void mergeCommentsBefore( size_t nextlineno );
|
|
void addEmptyLines( size_t line_number );
|
|
void mergeEOFNonTokens();
|
|
std::string identSpacing();
|
|
std::string alignmentSpacing( size_t count );
|
|
};
|
|
} // namespace Pol::Bscript::Compiler
|
|
|
|
template <>
|
|
struct fmt::formatter<Pol::Bscript::Compiler::FmtToken> : fmt::formatter<std::string>
|
|
{
|
|
fmt::format_context::iterator format( const Pol::Bscript::Compiler::FmtToken& t,
|
|
fmt::format_context& ctx ) const;
|
|
};
|