2019-08-23 11:56:54 -07:00
# include "BfPrinter.h"
# include "BfParser.h"
# include "BfUtil.h"
2020-03-28 14:26:14 -07:00
# include "BeefySysLib/util/UTF8.h"
2019-08-23 11:56:54 -07:00
USING_NS_BF ;
2020-03-28 14:26:14 -07:00
// This is a really long line, I'm testing to see what's up. This is a really long line, I'm testing to see what's up. This is a really long line, I'm testing to see what's up.
// This is a really long line, I'm testing to see what's up.
BfPrinter : : BfPrinter ( BfRootNode * rootNode , BfRootNode * sidechannelRootNode , BfRootNode * errorRootNode )
{
2019-08-23 11:56:54 -07:00
mSource = rootNode - > GetSourceData ( ) ;
2020-03-28 14:26:14 -07:00
mParser = mSource - > ToParserData ( ) ;
2019-08-23 11:56:54 -07:00
if ( sidechannelRootNode ! = NULL )
mSidechannelItr = sidechannelRootNode - > begin ( ) ;
mSidechannelNextNode = mSidechannelItr . Get ( ) ;
2020-03-28 14:26:14 -07:00
2019-08-23 11:56:54 -07:00
if ( errorRootNode ! = NULL )
mErrorItr = errorRootNode - > begin ( ) ;
mErrorNextNode = mErrorItr . Get ( ) ;
mTriviaIdx = 0 ;
mCurSrcIdx = 0 ;
mCurIndentLevel = 0 ;
2020-12-28 09:10:49 -08:00
mCurBlockState = NULL ;
2019-08-23 11:56:54 -07:00
mQueuedSpaceCount = 0 ;
mLastSpaceOffset = 0 ;
mReformatting = false ;
mDocPrep = false ;
mIgnoreTrivia = false ;
mForceUseTrivia = false ;
mIsFirstStatementInBlock = false ;
mFormatStart = - 1 ;
mFormatEnd = - 1 ;
mInSideChannel = false ;
mCharMapping = NULL ;
mExpectingNewLine = false ;
mCurBlockMember = NULL ;
mStateModifyVirtualIndentLevel = 0 ;
mVirtualIndentLevel = 0 ;
mVirtualNewLineIdx = 0 ;
mHighestCharId = 0 ;
mCurTypeDecl = NULL ;
2020-03-28 14:26:14 -07:00
mCurCol = 0 ;
mMaxCol = 120 ;
2019-08-23 11:56:54 -07:00
}
2020-02-24 09:58:49 -08:00
void BfPrinter : : Write ( const StringView & str )
2019-08-23 11:56:54 -07:00
{
2020-03-28 14:26:14 -07:00
if ( str = = ' \n ' )
mCurCol = 0 ;
else if ( mMaxCol > 0 )
{
int startCol = mCurCol ;
for ( int i = 0 ; i < ( int ) str . mLength ; i + + )
{
char c = str [ i ] ;
if ( c = = ' \t ' )
mCurCol = ( ( mCurCol + 1 ) & ~ 3 ) + 4 ;
else
mCurCol + + ;
}
}
2019-08-23 11:56:54 -07:00
mOutString . Append ( str ) ;
if ( mCharMapping ! = NULL )
{
for ( int i = 0 ; i < ( int ) str . length ( ) ; i + + )
{
int prevSrcIdx = - 1 ;
if ( mCharMapping - > size ( ) > 0 )
prevSrcIdx = mCharMapping - > back ( ) ;
if ( prevSrcIdx ! = - 1 )
{
bool matched = false ;
int curSrcIdx = prevSrcIdx + 1 ;
// Search for char, skipping over whitespace
while ( curSrcIdx < mParser - > mSrcLength )
{
char c = mParser - > mSrc [ curSrcIdx ] ;
if ( c = = str [ i ] )
{
matched = true ;
break ;
}
else if ( ( c = = ' \t ' ) | | ( c = = ' ' ) )
curSrcIdx + + ;
else
break ;
}
if ( matched )
{
// It matches the source character, extend...
mCharMapping - > push_back ( curSrcIdx ) ;
}
else
{
// New char
mCharMapping - > push_back ( - 1 ) ;
}
}
else
{
mCharMapping - > push_back ( - 1 ) ;
}
}
}
}
void BfPrinter : : FlushIndent ( )
{
while ( mQueuedSpaceCount > = 4 )
{
Write ( " \t " ) ;
mQueuedSpaceCount - = 4 ;
}
while ( mQueuedSpaceCount > 0 )
{
Write ( " " ) ;
mQueuedSpaceCount - - ;
}
}
void BfPrinter : : Write ( BfAstNode * node , int start , int len )
{
FlushIndent ( ) ;
2020-03-28 14:26:14 -07:00
if ( mMaxCol > 0 )
{
int startCol = mCurCol ;
auto parserData = node - > GetParserData ( ) ;
for ( int i = 0 ; i < len ; i + + )
{
char c = parserData - > mSrc [ start + i ] ;
if ( c = = ' \t ' )
mCurCol = ( ( mCurCol + 1 ) & ~ 3 ) + 4 ;
else
mCurCol + + ;
}
if ( ( mCurCol > mMaxCol ) & & ( startCol ! = 0 ) )
{
NOP ;
}
}
2019-08-23 11:56:54 -07:00
mOutString . Append ( node - > GetSourceData ( ) - > mSrc + start , len ) ;
if ( mCharMapping ! = NULL )
{
if ( start < mParser - > mSrcLength )
{
for ( int i = 0 ; i < len ; i + + )
{
mCharMapping - > push_back ( start + i ) ;
}
}
else
{
for ( int i = 0 ; i < len ; i + + )
mCharMapping - > push_back ( - 1 ) ;
}
}
}
void BfPrinter : : WriteSourceString ( BfAstNode * node )
{
Write ( node , node - > GetSrcStart ( ) , node - > GetSrcLength ( ) ) ;
}
void BfPrinter : : QueueVisitChild ( BfAstNode * astNode )
{
if ( astNode ! = NULL )
{
mNextStateModify . mQueuedNode = astNode ;
mChildNodeQueue . push_back ( mNextStateModify ) ;
mNextStateModify . Clear ( ) ;
}
}
void BfPrinter : : QueueVisitErrorNodes ( BfRootNode * astNode )
{
for ( auto & childNodeRef : * astNode )
{
BfAstNode * childNode = childNodeRef ;
if ( childNode - > IsA < BfErrorNode > ( ) )
QueueVisitChild ( childNode ) ;
}
}
static bool CompareNodeStart ( const BfPrinter : : StateModify & node1 , const BfPrinter : : StateModify & node2 )
{
return node1 . mQueuedNode - > GetSrcStart ( ) < node2 . mQueuedNode - > GetSrcStart ( ) ;
}
void BfPrinter : : FlushVisitChild ( )
{
if ( mChildNodeQueue . size ( ) = = 0 )
return ;
auto nodeQueue = mChildNodeQueue ;
mChildNodeQueue . Clear ( ) ;
std : : stable_sort ( nodeQueue . begin ( ) , nodeQueue . end ( ) , CompareNodeStart ) ;
for ( auto & node : nodeQueue )
{
mNextStateModify = node ;
VisitChild ( node . mQueuedNode ) ;
if ( mVirtualNewLineIdx = = mNextStateModify . mWantNewLineIdx )
mVirtualNewLineIdx = node . mWantNewLineIdx ;
}
}
void BfPrinter : : VisitChildWithPrecedingSpace ( BfAstNode * bfAstNode )
{
if ( bfAstNode = = NULL )
return ;
ExpectSpace ( ) ;
VisitChild ( bfAstNode ) ;
}
void BfPrinter : : VisitChildWithProceedingSpace ( BfAstNode * bfAstNode )
{
if ( bfAstNode = = NULL )
return ;
VisitChild ( bfAstNode ) ;
ExpectSpace ( ) ;
}
void BfPrinter : : ExpectSpace ( )
{
mNextStateModify . mExpectingSpace = true ;
}
void BfPrinter : : ExpectNewLine ( )
{
mNextStateModify . mWantNewLineIdx + + ;
}
void BfPrinter : : ExpectIndent ( )
{
mNextStateModify . mWantVirtualIndent + + ;
ExpectNewLine ( ) ;
}
void BfPrinter : : ExpectUnindent ( )
{
mNextStateModify . mWantVirtualIndent - - ;
ExpectNewLine ( ) ;
}
void BfPrinter : : VisitChildNextLine ( BfAstNode * node )
{
if ( node = = NULL )
return ;
if ( node - > IsA < BfBlock > ( ) )
{
VisitChild ( node ) ;
}
else
{
ExpectNewLine ( ) ;
ExpectIndent ( ) ;
VisitChild ( node ) ;
ExpectUnindent ( ) ;
}
}
int BfPrinter : : CalcOrigLineSpacing ( BfAstNode * bfAstNode , int * lineStartIdx )
{
int origLineSpacing = 0 ;
int checkIdx = bfAstNode - > GetSrcStart ( ) - 1 ;
auto astNodeSrc = bfAstNode - > GetSourceData ( ) ;
for ( ; checkIdx > = 0 ; checkIdx - - )
{
char c = astNodeSrc - > mSrc [ checkIdx ] ;
if ( c = = ' \n ' )
break ;
if ( c = = ' \t ' )
origLineSpacing + = 4 ;
else if ( c = = ' ' )
origLineSpacing + = 1 ;
else
return - 1 ;
}
if ( lineStartIdx ! = NULL )
* lineStartIdx = checkIdx + 1 ;
return origLineSpacing ;
}
void BfPrinter : : WriteIgnoredNode ( BfAstNode * node )
{
2020-03-28 14:26:14 -07:00
bool wasExpectingNewLine = mExpectingNewLine ;
2019-08-23 11:56:54 -07:00
mTriviaIdx = std : : max ( mTriviaIdx , node - > GetTriviaStart ( ) ) ;
int endIdx = mTriviaIdx ;
int crCount = 0 ;
auto astNodeSrc = node - > GetSourceData ( ) ;
int srcEnd = node - > GetSrcEnd ( ) ;
for ( int i = mTriviaIdx ; i < srcEnd ; i + + )
{
char c = astNodeSrc - > mSrc [ i ] ;
if ( ( c = = ' \n ' ) & & ( i < node - > GetSrcStart ( ) ) )
crCount + + ;
if ( ( ( c ! = ' ' ) & & ( c ! = ' \t ' ) ) | | ( ! mReformatting ) )
endIdx = i + 1 ;
}
bool expectingNewLine = mNextStateModify . mWantNewLineIdx ! = mVirtualNewLineIdx ;
int startIdx = mTriviaIdx ;
if ( ( expectingNewLine ) & & ( mReformatting ) )
{
// Try to adjust the tabbing of this comment by the same amount that the previous line was adjusted
int lineStart = - 1 ;
int origLineSpacing = CalcOrigLineSpacing ( node , & lineStart ) ;
if ( ( origLineSpacing ! = - 1 ) & & ( lineStart ! = - 1 ) )
{
for ( int i = 0 ; i < crCount ; i + + )
Write ( " \n " ) ;
startIdx = node - > GetSrcStart ( ) ;
// Leave left-aligned preprocessor nodes
if ( ( node - > GetSourceData ( ) - > mSrc [ node - > GetSrcStart ( ) ] ! = ' # ' ) | | ( origLineSpacing > 0 ) )
mQueuedSpaceCount = origLineSpacing + mLastSpaceOffset ;
}
}
2020-03-28 14:26:14 -07:00
auto commentNode = BfNodeDynCast < BfCommentNode > ( node ) ;
bool isBlockComment = false ;
bool isStarredBlockComment = false ;
if ( commentNode ! = NULL )
{
if ( ( commentNode - > mCommentKind = = BfCommentKind_Block ) | |
( commentNode - > mCommentKind = = BfCommentKind_Documentation_Block_Pre ) | |
( commentNode - > mCommentKind = = BfCommentKind_Documentation_Block_Post ) )
{
isBlockComment = true ;
int lineCount = 0 ;
bool onNewLine = false ;
for ( int srcIdx = startIdx ; srcIdx < endIdx ; srcIdx + + )
{
char checkC = astNodeSrc - > mSrc [ srcIdx ] ;
if ( checkC = = ' \n ' )
{
onNewLine = true ;
lineCount + + ;
if ( lineCount > = 2 )
break ;
}
else if ( ( checkC = = ' * ' ) & & ( onNewLine ) )
isStarredBlockComment = true ;
else if ( ( checkC ! = ' ' ) & & ( checkC ! = ' \t ' ) )
onNewLine = false ;
}
}
}
bool doWrap = ( commentNode ! = NULL ) & & ( mMaxCol > 0 ) ;
bool prevHadWrap = false ;
if ( commentNode ! = NULL )
{
Visit ( ( BfAstNode * ) node ) ;
startIdx = node - > mSrcStart ;
2021-02-25 10:14:22 -08:00
if ( doWrap )
{
bool wantWrap = false ;
int spacedWordCount = 0 ;
bool inQuotes = false ;
auto src = astNodeSrc - > mSrc ;
bool isDefinitelyCode = false ;
bool hadNonSlash = false ;
for ( int i = node - > mSrcStart + 1 ; i < node - > mSrcEnd - 1 ; i + + )
{
char c = src [ i ] ;
if ( c ! = ' / ' )
hadNonSlash = true ;
if ( inQuotes )
{
if ( c = = ' \\ ' )
{
i + + ;
}
else if ( c = = ' \" ' )
{
inQuotes = false ;
}
}
else if ( c = = ' " ' )
{
inQuotes = true ;
}
else if ( c = = ' ' )
{
if ( ( isalpha ( ( uint8 ) src [ i - 1 ] ) ) & & ( isalpha ( ( uint8 ) src [ i + 1 ] ) ) )
spacedWordCount + + ;
}
else if ( ( c = = ' / ' ) & & ( src [ i - 1 ] = = ' / ' ) & & ( hadNonSlash ) )
isDefinitelyCode = true ;
}
// If this doesn't look like a sentence then don't try to word wrap
if ( ( isDefinitelyCode ) | | ( spacedWordCount < 4 ) )
doWrap = false ;
}
2020-03-28 14:26:14 -07:00
}
2020-12-28 09:10:49 -08:00
int lineEmittedChars = 0 ;
2019-08-23 11:56:54 -07:00
// This handles tab adjustment within multiline comments
FlushIndent ( ) ;
bool isNewLine = false ;
for ( int srcIdx = startIdx ; srcIdx < endIdx ; srcIdx + + )
{
# ifdef A
int zap = 999 ;
# endif
bool emitChar = true ;
2020-03-28 14:26:14 -07:00
2019-08-23 11:56:54 -07:00
char c = astNodeSrc - > mSrc [ srcIdx ] ;
2020-03-28 14:26:14 -07:00
if ( c = = ' \n ' )
{
isNewLine = true ;
if ( prevHadWrap )
{
bool merging = false ;
int blockContentStart = - 1 ;
bool foundStar = false ;
int startIdx = srcIdx ;
for ( int checkIdx = srcIdx + 1 ; checkIdx < endIdx + 1 ; checkIdx + + )
{
char checkC = astNodeSrc - > mSrc [ checkIdx ] ;
if ( isBlockComment )
{
if ( checkC = = ' \n ' )
break ;
if ( ( isStarredBlockComment ) & & ( ! foundStar ) & & ( checkC = = ' * ' ) )
{
foundStar = true ;
continue ;
}
if ( ( checkC ! = ' ' ) & & ( checkC ! = ' \t ' ) )
{
if ( blockContentStart = = - 1 )
{
blockContentStart = checkIdx ;
}
// Only do merge if we have content on this line
if ( isalnum ( checkC ) )
{
srcIdx = blockContentStart ;
merging = true ;
break ;
}
}
}
else
{
if ( ( checkC = = ' / ' ) & & ( astNodeSrc - > mSrc [ checkIdx + 1 ] = = ' / ' ) )
{
srcIdx = checkIdx ;
while ( srcIdx < endIdx )
{
char checkC = astNodeSrc - > mSrc [ srcIdx ] ;
if ( checkC ! = ' / ' )
break ;
srcIdx + + ;
}
merging = true ;
break ;
}
if ( ( checkC ! = ' ' ) & & ( checkC ! = ' \t ' ) )
break ;
}
}
if ( merging )
{
srcIdx - - ;
while ( srcIdx < endIdx - 1 )
{
char checkC = astNodeSrc - > mSrc [ srcIdx + 1 ] ;
if ( ( checkC ! = ' ' ) & & ( checkC ! = ' \t ' ) )
break ;
srcIdx + + ;
}
Write ( " " ) ;
continue ;
}
}
mCurCol = 0 ;
prevHadWrap = false ;
}
2019-08-23 11:56:54 -07:00
else if ( isNewLine )
{
if ( c = = ' ' )
{
mQueuedSpaceCount + + ;
emitChar = false ;
}
else if ( c = = ' \t ' )
{
mQueuedSpaceCount + = 4 ;
emitChar = false ;
}
else
{
// Leave left-aligned preprocessor nodes that are commented out
if ( ( c ! = ' # ' ) | | ( mQueuedSpaceCount > 0 ) )
2020-03-28 14:26:14 -07:00
mQueuedSpaceCount = std : : max ( 0 , mQueuedSpaceCount + mLastSpaceOffset ) ;
else
mQueuedSpaceCount = mCurIndentLevel * 4 ; // Do default indent
2019-08-23 11:56:54 -07:00
isNewLine = false ;
}
}
if ( emitChar )
2020-03-28 14:26:14 -07:00
{
int startIdx = srcIdx ;
if ( c ! = ' \n ' )
2019-08-23 11:56:54 -07:00
{
2020-03-28 14:26:14 -07:00
while ( srcIdx < endIdx )
{
char c = astNodeSrc - > mSrc [ srcIdx + 1 ] ;
if ( isspace ( ( uint8 ) c ) )
break ;
srcIdx + + ;
}
}
if ( doWrap )
{
int len = 0 ;
for ( int idx = startIdx ; idx < = srcIdx ; idx + + )
{
char c = astNodeSrc - > mSrc [ idx ] ;
if ( isutf ( c ) )
len + + ;
}
2020-12-28 09:10:49 -08:00
if ( ( mCurCol + len > mMaxCol ) & & ( lineEmittedChars > = 8 ) )
2020-03-28 14:26:14 -07:00
{
Write ( " \n " ) ;
mQueuedSpaceCount = mCurIndentLevel * 4 ;
FlushIndent ( ) ;
if ( isStarredBlockComment )
Write ( " * " ) ;
else if ( ! isBlockComment )
Write ( " // " ) ;
prevHadWrap = true ;
while ( startIdx < endIdx )
{
char c = astNodeSrc - > mSrc [ startIdx ] ;
if ( ! isspace ( ( uint8 ) c ) )
break ;
startIdx + + ;
}
2020-12-28 09:10:49 -08:00
lineEmittedChars = 0 ;
2020-03-28 14:26:14 -07:00
}
}
FlushIndent ( ) ;
2020-07-01 13:35:33 -07:00
for ( int idx = startIdx ; idx < = BF_MIN ( srcIdx , endIdx - 1 ) ; idx + + )
2020-03-28 14:26:14 -07:00
{
char c = astNodeSrc - > mSrc [ idx ] ;
mOutString . Append ( c ) ;
if ( mCharMapping ! = NULL )
{
if ( idx < mParser - > mSrcLength )
mCharMapping - > push_back ( idx ) ;
else
mCharMapping - > push_back ( - 1 ) ;
}
if ( c = = ' \n ' )
2020-12-28 09:10:49 -08:00
{
mCurCol = 0 ;
lineEmittedChars = 0 ;
}
2020-03-28 14:26:14 -07:00
else if ( isutf ( c ) )
2020-12-28 09:10:49 -08:00
{
2020-03-28 14:26:14 -07:00
mCurCol + + ;
2020-12-28 09:10:49 -08:00
lineEmittedChars + + ;
}
2019-08-23 11:56:54 -07:00
}
}
}
FlushIndent ( ) ;
mTriviaIdx = endIdx ;
mIsFirstStatementInBlock = false ;
2020-03-28 14:26:14 -07:00
mExpectingNewLine = wasExpectingNewLine ;
2019-08-23 11:56:54 -07:00
}
2020-12-28 09:10:49 -08:00
void BfPrinter : : CheckRawNode ( BfAstNode * node )
{
if ( node = = NULL )
return ;
if ( ( ! BfNodeIsExact < BfTokenNode > ( node ) ) & &
( ! BfNodeIsExact < BfIdentifierNode > ( node ) ) & &
( ! BfNodeIsExact < BfLiteralExpression > ( node ) ) )
return ;
//mForceUseTrivia = true;
// Usually 'raw' nodes get merged into larger nodes like expressions/statements, but if they don't then this tries to help formatting
mExpectingNewLine = false ;
mVirtualNewLineIdx = mNextStateModify . mWantNewLineIdx ;
mNextStateModify . mExpectingSpace = false ;
bool inLineStart = false ;
int spaceCount = 0 ;
auto parserData = node - > GetParserData ( ) ;
for ( int i = node - > mTriviaStart ; i < node - > mSrcStart ; i + + )
{
char c = parserData - > mSrc [ i ] ;
if ( c = = ' \n ' )
{
ExpectNewLine ( ) ;
inLineStart = true ;
spaceCount = 0 ;
}
else if ( c = = ' \t ' )
{
ExpectSpace ( ) ;
if ( inLineStart )
spaceCount + = 4 ;
}
else if ( c = = ' ' )
{
ExpectSpace ( ) ;
if ( inLineStart )
spaceCount + + ;
}
else
{
inLineStart = false ;
}
}
if ( ( spaceCount > 0 ) & & ( mCurBlockState ! = NULL ) )
{
int indentCount = spaceCount / 4 ;
mNextStateModify . mWantVirtualIndent = BF_MAX ( indentCount , mCurBlockState - > mIndentStart + 1 ) ;
}
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfAstNode * bfAstNode )
{
SetAndRestoreValue < bool > prevForceTrivia ( mForceUseTrivia ) ;
bool expectingNewLine = mNextStateModify . mWantNewLineIdx ! = mVirtualNewLineIdx ;
if ( expectingNewLine )
mExpectingNewLine = true ;
int indentOffset = 0 ;
if ( bfAstNode - > GetTriviaStart ( ) ! = - 1 )
{
if ( expectingNewLine )
{
indentOffset = mNextStateModify . mWantVirtualIndent - mVirtualIndentLevel ;
mVirtualIndentLevel + = indentOffset ;
mCurIndentLevel + = indentOffset ;
}
}
while ( true )
{
int sidechannelDist = - 1 ;
if ( mSidechannelNextNode ! = NULL )
sidechannelDist = bfAstNode - > GetSrcStart ( ) - mSidechannelNextNode - > GetSrcStart ( ) ;
int errorDist = - 1 ;
if ( mErrorNextNode ! = NULL )
errorDist = bfAstNode - > GetSrcStart ( ) - mErrorNextNode - > GetSrcStart ( ) ;
if ( ( sidechannelDist > 0 ) & & ( sidechannelDist > = errorDist ) )
{
BF_ASSERT ( ! mInSideChannel ) ;
mInSideChannel = true ;
VisitChild ( mSidechannelNextNode ) ;
mInSideChannel = false ;
+ + mSidechannelItr ;
mSidechannelNextNode = mSidechannelItr . Get ( ) ;
}
else if ( errorDist > 0 )
{
auto curErrorNode = mErrorNextNode ;
+ + mErrorItr ;
mErrorNextNode = mErrorItr . Get ( ) ;
mForceUseTrivia = true ;
VisitChild ( curErrorNode ) ;
}
else
break ;
}
// This won't be true if we move nodes around during refactoring
if ( bfAstNode - > GetSrcStart ( ) > = mCurSrcIdx )
{
mCurSrcIdx = bfAstNode - > GetSrcStart ( ) ;
}
if ( ( ! mReformatting ) & & ( mFormatStart ! = - 1 ) & & ( mCurSrcIdx > = mFormatStart ) & & ( ( mCurSrcIdx < mFormatEnd ) | | ( mFormatEnd = = - 1 ) ) )
{
mReformatting = true ;
// We entered the format region, figure our what our current indent level is
int backIdx = mCurSrcIdx - 1 ;
int prevSpaceCount = 0 ;
auto astNodeSrc = bfAstNode - > GetSourceData ( ) ;
while ( backIdx > = 0 )
{
char c = astNodeSrc - > mSrc [ backIdx ] ;
if ( c = = ' ' )
prevSpaceCount + + ;
else if ( c = = ' \t ' )
prevSpaceCount + = 4 ;
else if ( c = = ' \n ' )
{
// Found previous line
mCurIndentLevel = prevSpaceCount / 4 ;
break ;
}
else
{
prevSpaceCount = 0 ;
}
backIdx - - ;
}
}
if ( ( mCurSrcIdx > = mFormatEnd ) & & ( mFormatEnd ! = - 1 ) )
mReformatting = false ;
// When triviaStart == -1, that indicates it's a combined node where the text we want to process is on the inside
if ( bfAstNode - > GetTriviaStart ( ) ! = - 1 )
{
mTriviaIdx = std : : max ( mTriviaIdx , bfAstNode - > GetTriviaStart ( ) ) ;
bool usedTrivia = false ;
if ( ( ! mIgnoreTrivia ) & & ( mTriviaIdx < bfAstNode - > GetSrcStart ( ) ) )
{
if ( ( ! mReformatting ) | | ( mForceUseTrivia ) )
{
Write ( bfAstNode , mTriviaIdx , bfAstNode - > GetSrcStart ( ) - mTriviaIdx ) ;
usedTrivia = true ;
}
}
if ( ( mReformatting ) & & ( ! mInSideChannel ) )
{
// Check to see if our trivia contained a newline and duplicate whatever the
// indent increase was from the previous line
if ( ( ! usedTrivia ) & & ( mTriviaIdx < bfAstNode - > GetSrcStart ( ) ) & & ( ! mIsFirstStatementInBlock ) & & ( ! mNextStateModify . mDoingBlockOpen ) & & ( ! mNextStateModify . mDoingBlockClose ) )
{
bool hadNewline = true ;
bool hadPrevLineSpacing = false ;
int prevSpaceCount = - 1 ;
int spaceCount = 0 ;
auto astNodeSrc = bfAstNode - > GetSourceData ( ) ;
for ( int i = mTriviaIdx ; i < bfAstNode - > GetSrcStart ( ) ; i + + )
{
if ( mIgnoreTrivia )
break ;
char c = astNodeSrc - > mSrc [ i ] ;
if ( c = = ' ' )
spaceCount + + ;
else if ( c = = ' \t ' )
spaceCount + = 4 ;
if ( ( ( c = = ' \n ' ) | | ( i = = bfAstNode - > GetSrcStart ( ) - 1 ) ) & & ( hadPrevLineSpacing ) & & ( prevSpaceCount > 0 ) )
{
mQueuedSpaceCount + = std : : max ( 0 , spaceCount - prevSpaceCount ) - std : : max ( 0 , indentOffset * 4 ) ;
prevSpaceCount = - 1 ;
hadPrevLineSpacing = false ;
}
if ( c = = ' \n ' )
{
hadNewline = true ;
int backIdx = i - 1 ;
prevSpaceCount = 0 ;
while ( backIdx > = 0 )
{
char c = astNodeSrc - > mSrc [ backIdx ] ;
if ( c = = ' ' )
prevSpaceCount + + ;
else if ( c = = ' \t ' )
prevSpaceCount + = 4 ;
if ( ( c = = ' \n ' ) | | ( backIdx = = 0 ) )
{
// Found previous line
usedTrivia = true ;
Write ( " \n " ) ;
2020-03-28 14:26:14 -07:00
mQueuedSpaceCount = mCurIndentLevel * 4 ;
2019-08-23 11:56:54 -07:00
// Indents extra if we have a statement split over multiple lines
if ( ! mExpectingNewLine )
mQueuedSpaceCount + = 4 ;
break ;
}
else
{
hadPrevLineSpacing = true ;
prevSpaceCount = 0 ;
}
backIdx - - ;
}
spaceCount = 0 ;
}
else if ( ! isspace ( ( uint8 ) c ) )
spaceCount = 0 ;
}
}
if ( usedTrivia )
{
// Already did whitespace
}
else if ( ( mNextStateModify . mDoingBlockOpen ) | | ( mNextStateModify . mDoingBlockClose ) | | ( mIsFirstStatementInBlock ) )
{
int wasOnNewLine = true ;
int idx = ( int ) mOutString . length ( ) - 1 ;
while ( idx > = 0 )
{
char c = mOutString [ idx ] ;
if ( c = = ' \n ' )
break ;
if ( ! isspace ( ( uint8 ) c ) )
{
Write ( " \n " ) ;
2020-09-30 07:39:39 -07:00
mQueuedSpaceCount = mCurIndentLevel * 4 ;
if ( ! mNextStateModify . mDoingBlockClose )
{
int origLineSpacing = CalcOrigLineSpacing ( bfAstNode , NULL ) ;
if ( origLineSpacing ! = - 1 )
mLastSpaceOffset = mQueuedSpaceCount - origLineSpacing ;
}
2019-08-23 11:56:54 -07:00
break ;
}
idx - - ;
}
}
else if ( ( mNextStateModify . mExpectingSpace ) | | ( expectingNewLine ) )
{
FlushIndent ( ) ;
char startChar = bfAstNode - > GetSourceData ( ) - > mSrc [ bfAstNode - > GetSrcStart ( ) ] ;
bool isEnding = ( startChar = = ' ; ' ) | | ( startChar = = ' ) ' ) ;
if ( ( ! isEnding ) & & ( mOutString . length ( ) > 0 ) & & ( ! isspace ( ( uint8 ) mOutString [ mOutString . length ( ) - 1 ] ) ) )
Write ( " " ) ;
}
}
if ( ! mInSideChannel )
{
if ( mNextStateModify . mDoingBlockOpen )
mIsFirstStatementInBlock = true ;
else
mIsFirstStatementInBlock = false ;
mNextStateModify . mExpectingSpace = false ;
mNextStateModify . mDoingBlockOpen = false ;
mNextStateModify . mDoingBlockClose = false ;
mNextStateModify . Clear ( ) ;
mVirtualNewLineIdx = mNextStateModify . mWantNewLineIdx ;
}
mForceUseTrivia = false ;
if ( bfAstNode - > GetSrcStart ( ) > = mTriviaIdx )
mTriviaIdx = bfAstNode - > GetSrcStart ( ) ;
// We either used this mExpectingNewLine to do an extra indent for a statement split over multiple lines or we didn't
mExpectingNewLine = false ;
}
}
bool BfPrinter : : CheckReplace ( BfAstNode * astNode )
{
return false ;
}
void BfPrinter : : Visit ( BfErrorNode * errorNode )
{
SetAndRestoreValue < bool > prevForceUseTrivia ( mForceUseTrivia , true ) ;
SetAndRestoreValue < bool > prevFormatting ( mReformatting , false ) ;
SetAndRestoreValue < int > prevFormatEndIdx ( mFormatEnd , 0 ) ;
VisitChild ( errorNode - > mRefNode ) ;
}
2019-11-26 13:11:17 -08:00
void BfPrinter : : Visit ( BfScopeNode * scopeNode )
{
Visit ( scopeNode - > ToBase ( ) ) ;
VisitChild ( scopeNode - > mScopeToken ) ;
VisitChild ( scopeNode - > mColonToken ) ;
VisitChild ( scopeNode - > mTargetNode ) ;
if ( scopeNode - > mAttributes ! = NULL )
{
ExpectSpace ( ) ;
VisitChild ( scopeNode - > mAttributes ) ;
}
}
void BfPrinter : : Visit ( BfNewNode * newNode )
{
Visit ( newNode - > ToBase ( ) ) ;
VisitChild ( newNode - > mNewToken ) ;
VisitChild ( newNode - > mColonToken ) ;
VisitChild ( newNode - > mAllocNode ) ;
if ( newNode - > mAttributes ! = NULL )
{
ExpectSpace ( ) ;
VisitChild ( newNode - > mAttributes ) ;
}
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfExpression * expr )
{
Visit ( expr - > ToBase ( ) ) ;
}
void BfPrinter : : Visit ( BfExpressionStatement * exprStmt )
{
//Visit((BfAstNode*)exprStmt);
Visit ( exprStmt - > ToBase ( ) ) ;
VisitChild ( exprStmt - > mExpression ) ;
VisitChild ( exprStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfAttributedExpression * attribExpr )
{
Visit ( attribExpr - > ToBase ( ) ) ;
if ( auto indexerExpr = BfNodeDynCast < BfIndexerExpression > ( attribExpr - > mExpression ) )
{
VisitChild ( indexerExpr - > mTarget ) ;
VisitChild ( indexerExpr - > mOpenBracket ) ;
VisitChild ( attribExpr - > mAttributes ) ;
for ( int i = 0 ; i < ( int ) indexerExpr - > mArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( indexerExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( indexerExpr - > mArguments [ i ] ) ;
}
VisitChild ( indexerExpr - > mCloseBracket ) ;
}
else
{
VisitChild ( attribExpr - > mAttributes ) ;
VisitChild ( attribExpr - > mExpression ) ;
}
}
void BfPrinter : : Visit ( BfStatement * stmt )
{
// Trailing semicolon must come after, so every derived type is responsible for printing it
# ifdef BF_AST_HAS_PARENT_MEMBER
// if (stmt->mParent != NULL)
// {
// if (auto block = BfNodeDynCast<BfBlock>(stmt->mParent))
// {
// if (block->mOpenBrace != NULL)
// {
// if (mReformatting)
// BF_ASSERT(stmt == mCurBlockMember);
// }
// }
// }
# endif
if ( stmt = = mCurBlockMember )
ExpectNewLine ( ) ;
//Visit(stmt->ToBase());
}
void BfPrinter : : Visit ( BfLabelableStatement * labelableStmt )
{
Visit ( labelableStmt - > ToBase ( ) ) ;
if ( labelableStmt - > mLabelNode ! = NULL )
{
ExpectNewLine ( ) ;
VisitChild ( labelableStmt - > mLabelNode - > mLabel ) ;
VisitChild ( labelableStmt - > mLabelNode - > mColonToken ) ;
}
}
void BfPrinter : : Visit ( BfCommentNode * commentNode )
{
WriteIgnoredNode ( commentNode ) ;
}
void BfPrinter : : Visit ( BfPreprocesorIgnoredSectionNode * preprocesorIgnoredSection )
{
WriteIgnoredNode ( preprocesorIgnoredSection ) ;
}
void BfPrinter : : Visit ( BfPreprocessorNode * preprocessorNode )
{
WriteIgnoredNode ( preprocessorNode ) ;
}
void BfPrinter : : Visit ( BfAttributeDirective * attributeDirective )
{
Visit ( attributeDirective - > ToBase ( ) ) ;
if ( attributeDirective - > mAttrOpenToken ! = NULL )
{
if ( attributeDirective - > mAttrOpenToken - > mToken = = BfToken_Comma )
{
VisitChild ( attributeDirective - > mAttrOpenToken ) ;
ExpectSpace ( ) ;
}
else
{
SetAndRestoreValue < bool > prevExpectNewLine ( mExpectingNewLine , true ) ;
VisitChild ( attributeDirective - > mAttrOpenToken ) ;
}
}
2019-11-26 13:11:17 -08:00
2019-08-23 11:56:54 -07:00
if ( attributeDirective - > mAttributeTargetSpecifier ! = NULL )
{
2019-11-26 13:11:17 -08:00
if ( auto attributeTargetSpecifier = BfNodeDynCast < BfAttributeTargetSpecifier > ( attributeDirective - > mAttributeTargetSpecifier ) )
{
VisitChild ( attributeTargetSpecifier - > mTargetToken ) ;
VisitChild ( attributeTargetSpecifier - > mColonToken ) ;
ExpectSpace ( ) ;
}
else
{
VisitChild ( attributeDirective - > mAttributeTargetSpecifier ) ;
}
2019-08-23 11:56:54 -07:00
}
VisitChild ( attributeDirective - > mAttributeTypeRef ) ;
VisitChild ( attributeDirective - > mCtorOpenParen ) ;
for ( int i = 0 ; i < ( int ) attributeDirective - > mArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( attributeDirective - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( attributeDirective - > mArguments [ i ] ) ;
}
VisitChild ( attributeDirective - > mCtorCloseParen ) ;
VisitChild ( attributeDirective - > mAttrCloseToken ) ;
VisitChild ( attributeDirective - > mNextAttribute ) ;
}
void BfPrinter : : Visit ( BfGenericParamsDeclaration * genericParams )
{
Visit ( genericParams - > ToBase ( ) ) ;
VisitChild ( genericParams - > mOpenChevron ) ;
for ( int i = 0 ; i < ( int ) genericParams - > mGenericParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( genericParams - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( genericParams - > mGenericParams [ i ] ) ;
}
VisitChild ( genericParams - > mCloseChevron ) ;
}
2019-11-22 12:28:24 -08:00
void BfPrinter : : Visit ( BfGenericOperatorConstraint * genericConstraints )
{
Visit ( genericConstraints - > ToBase ( ) ) ;
VisitChild ( genericConstraints - > mOperatorToken ) ;
ExpectSpace ( ) ;
VisitChild ( genericConstraints - > mLeftType ) ;
ExpectSpace ( ) ;
VisitChild ( genericConstraints - > mOpToken ) ;
ExpectSpace ( ) ;
VisitChild ( genericConstraints - > mRightType ) ;
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfGenericConstraintsDeclaration * genericConstraints )
{
Visit ( genericConstraints - > ToBase ( ) ) ;
2019-11-17 09:28:39 -08:00
for ( auto genericConstraintNode : genericConstraints - > mGenericConstraints )
2019-08-23 11:56:54 -07:00
{
2019-11-17 09:28:39 -08:00
auto genericConstraint = BfNodeDynCast < BfGenericConstraint > ( genericConstraintNode ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
VisitChild ( genericConstraint - > mWhereToken ) ;
ExpectSpace ( ) ;
2019-11-17 09:28:39 -08:00
VisitChild ( genericConstraint - > mTypeRef ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
VisitChild ( genericConstraint - > mColonToken ) ;
ExpectSpace ( ) ;
for ( int i = 0 ; i < ( int ) genericConstraint - > mConstraintTypes . size ( ) ; i + + )
{
if ( i > 0 )
{
if ( ! genericConstraint - > mCommas . IsEmpty ( ) )
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( genericConstraint - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( genericConstraint - > mConstraintTypes [ i ] ) ;
}
}
}
void BfPrinter : : Visit ( BfGenericArgumentsNode * genericArgumentsNode )
{
Visit ( genericArgumentsNode - > ToBase ( ) ) ;
VisitChild ( genericArgumentsNode - > mOpenChevron ) ;
for ( int i = 0 ; i < ( int ) genericArgumentsNode - > mGenericArgs . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( genericArgumentsNode - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( genericArgumentsNode - > mGenericArgs [ i ] ) ;
}
VisitChild ( genericArgumentsNode - > mCloseChevron ) ;
}
void BfPrinter : : Visit ( BfEmptyStatement * emptyStmt )
{
Visit ( emptyStmt - > ToBase ( ) ) ;
VisitChild ( emptyStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfTokenNode * tokenNode )
{
Visit ( tokenNode - > ToBase ( ) ) ;
if ( mDocPrep )
{
if ( tokenNode - > mToken = = BfToken_Mut )
return ;
}
if ( tokenNode - > GetSrcEnd ( ) = = 0 )
{
String tokenStr = BfTokenToString ( tokenNode - > GetToken ( ) ) ;
Write ( tokenStr ) ;
}
else
{
BF_ASSERT ( tokenNode - > GetSrcEnd ( ) > tokenNode - > GetSrcStart ( ) ) ;
WriteSourceString ( tokenNode ) ;
2020-12-28 09:10:49 -08:00
}
2019-08-23 11:56:54 -07:00
}
2020-12-07 07:53:12 -08:00
void BfPrinter : : Visit ( BfTokenPairNode * tokenPairNode )
{
Visit ( tokenPairNode - > ToBase ( ) ) ;
VisitChild ( tokenPairNode - > mLeft ) ;
if ( ( tokenPairNode - > mRight ! = NULL ) & & ( tokenPairNode - > mRight - > mToken ! = BfToken_Star ) )
ExpectSpace ( ) ;
VisitChild ( tokenPairNode - > mRight ) ;
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfLiteralExpression * literalExpr )
{
Visit ( literalExpr - > ToBase ( ) ) ;
2020-02-24 09:58:49 -08:00
if ( literalExpr - > mValue . mTypeCode = = BfTypeCode_CharPtr )
{
bool isMultiLine = false ;
auto sourceData = literalExpr - > GetSourceData ( ) ;
for ( int i = literalExpr - > GetSrcStart ( ) ; i < ( int ) literalExpr - > GetSrcEnd ( ) ; i + + )
{
char c = sourceData - > mSrc [ i ] ;
if ( c = = ' \n ' )
{
isMultiLine = true ;
break ;
}
}
if ( isMultiLine )
{
int srcLineStart = 0 ;
int checkIdx = literalExpr - > GetSrcStart ( ) - 1 ;
while ( checkIdx > = 0 )
{
char c = sourceData - > mSrc [ checkIdx ] ;
if ( c = = ' \n ' )
{
srcLineStart = checkIdx + 1 ;
break ;
}
checkIdx - - ;
}
int queuedSpaceCount = mQueuedSpaceCount ;
FlushIndent ( ) ;
for ( int i = literalExpr - > GetSrcStart ( ) ; i < ( int ) literalExpr - > GetSrcEnd ( ) ; i + + )
{
char c = sourceData - > mSrc [ i ] ;
Write ( c ) ;
if ( c = = ' \n ' )
{
i + + ;
int srcIdx = srcLineStart ;
while ( true )
{
char srcC = sourceData - > mSrc [ srcIdx + + ] ;
char litC = sourceData - > mSrc [ i ] ;
if ( srcC ! = litC )
break ;
if ( ( srcC ! = ' ' ) & & ( srcC ! = ' \t ' ) )
break ;
i + + ;
}
mQueuedSpaceCount = queuedSpaceCount ;
FlushIndent ( ) ;
i - - ;
}
}
return ;
}
}
2019-08-23 11:56:54 -07:00
WriteSourceString ( literalExpr ) ;
}
2020-11-11 05:46:52 -08:00
void BfPrinter : : Visit ( BfStringInterpolationExpression * stringInterpolationExpression )
{
Visit ( stringInterpolationExpression - > ToBase ( ) ) ;
String str ;
stringInterpolationExpression - > ToString ( str ) ;
Write ( str ) ;
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfIdentifierNode * identifierNode )
{
Visit ( identifierNode - > ToBase ( ) ) ;
if ( ! CheckReplace ( identifierNode ) )
WriteSourceString ( identifierNode ) ;
}
void BfPrinter : : Visit ( BfQualifiedNameNode * nameNode )
{
Visit ( ( BfAstNode * ) nameNode ) ;
VisitChild ( nameNode - > mLeft ) ;
VisitChild ( nameNode - > mDot ) ;
VisitChild ( nameNode - > mRight ) ;
}
void BfPrinter : : Visit ( BfThisExpression * thisExpr )
{
Visit ( ( BfAstNode * ) thisExpr ) ;
WriteSourceString ( thisExpr ) ;
}
void BfPrinter : : Visit ( BfBaseExpression * baseExpr )
{
Visit ( ( BfAstNode * ) baseExpr ) ;
WriteSourceString ( baseExpr ) ;
}
void BfPrinter : : Visit ( BfMixinExpression * mixinExpr )
{
Visit ( ( BfAstNode * ) mixinExpr ) ;
WriteSourceString ( mixinExpr ) ;
}
void BfPrinter : : Visit ( BfSizedArrayCreateExpression * createExpr )
{
Visit ( createExpr - > ToBase ( ) ) ;
VisitChild ( createExpr - > mTypeRef ) ;
VisitChildWithPrecedingSpace ( createExpr - > mInitializer ) ;
}
2020-06-18 06:12:14 -07:00
void BfPrinter : : Visit ( BfInitializerExpression * initExpr )
{
Visit ( initExpr - > ToBase ( ) ) ;
VisitChild ( initExpr - > mTarget ) ;
2020-09-30 07:39:39 -07:00
BlockState blockState ;
DoBlockOpen ( initExpr - > mTarget , initExpr - > mOpenBrace , initExpr - > mCloseBrace , false , blockState ) ;
2020-06-18 06:12:14 -07:00
for ( int i = 0 ; i < ( int ) initExpr - > mValues . size ( ) ; i + + )
{
if ( i > 0 )
2020-09-30 07:39:39 -07:00
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( initExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2020-09-30 07:39:39 -07:00
if ( blockState . mDoInlineBlock )
ExpectSpace ( ) ;
else
ExpectNewLine ( ) ;
2020-06-18 06:12:14 -07:00
}
2020-09-30 07:39:39 -07:00
2020-06-18 06:12:14 -07:00
VisitChild ( initExpr - > mValues [ i ] ) ;
}
2020-09-30 07:39:39 -07:00
DoBlockClose ( initExpr - > mTarget , initExpr - > mOpenBrace , initExpr - > mCloseBrace , false , blockState ) ;
// Visit(initExpr->ToBase());
//
// VisitChild(initExpr->mTarget);
// ExpectSpace();
// VisitChild(initExpr->mOpenBrace);
// ExpectIndent();
// for (int i = 0; i < (int)initExpr->mValues.size(); i++)
// {
// if (i > 0)
// {
// VisitChildNoRef(initExpr->mCommas.GetSafe(i - 1));
// ExpectSpace();
// }
// VisitChild(initExpr->mValues[i]);
// }
// ExpectUnindent();
// VisitChild(initExpr->mCloseBrace);
2020-06-18 06:12:14 -07:00
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfCollectionInitializerExpression * initExpr )
{
Visit ( initExpr - > ToBase ( ) ) ;
VisitChild ( initExpr - > mOpenBrace ) ;
2020-09-30 07:39:39 -07:00
for ( int i = 0 ; i < ( int ) initExpr - > mValues . size ( ) ; i + + )
2019-08-23 11:56:54 -07:00
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( initExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( initExpr - > mValues [ i ] ) ;
}
VisitChild ( initExpr - > mCloseBrace ) ;
}
void BfPrinter : : Visit ( BfTypeReference * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
}
void BfPrinter : : Visit ( BfNamedTypeReference * typeRef )
{
Visit ( ( BfAstNode * ) typeRef ) ;
VisitChild ( typeRef - > mNameNode ) ;
}
void BfPrinter : : Visit ( BfQualifiedTypeReference * qualifiedTypeRef )
{
Visit ( qualifiedTypeRef - > ToBase ( ) ) ;
VisitChild ( qualifiedTypeRef - > mLeft ) ;
VisitChild ( qualifiedTypeRef - > mDot ) ;
VisitChild ( qualifiedTypeRef - > mRight ) ;
}
void BfPrinter : : Visit ( BfVarTypeReference * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
VisitChild ( typeRef - > mVarToken ) ;
}
void BfPrinter : : Visit ( BfLetTypeReference * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
VisitChild ( typeRef - > mLetToken ) ;
}
void BfPrinter : : Visit ( BfConstTypeRef * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
VisitChild ( typeRef - > mConstToken ) ;
ExpectSpace ( ) ;
VisitChild ( typeRef - > mElementType ) ;
}
void BfPrinter : : Visit ( BfConstExprTypeRef * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
if ( typeRef - > mConstToken ! = NULL )
{
VisitChild ( typeRef - > mConstToken ) ;
ExpectSpace ( ) ;
}
VisitChild ( typeRef - > mConstExpr ) ;
}
void BfPrinter : : Visit ( BfRefTypeRef * typeRef )
{
Visit ( typeRef - > ToBase ( ) ) ;
VisitChild ( typeRef - > mRefToken ) ;
ExpectSpace ( ) ;
VisitChild ( typeRef - > mElementType ) ;
}
void BfPrinter : : Visit ( BfArrayTypeRef * arrayTypeRef )
{
Visit ( ( BfAstNode * ) arrayTypeRef ) ;
std : : function < void ( BfTypeReference * ) > _VisitElements = [ & ] ( BfTypeReference * elementRef )
{
auto arrType = BfNodeDynCast < BfArrayTypeRef > ( elementRef ) ;
if ( arrType ! = NULL )
{
_VisitElements ( arrType - > mElementType ) ;
}
else
{
VisitChild ( elementRef ) ;
}
} ;
std : : function < void ( BfArrayTypeRef * ) > _VisitBrackets = [ & ] ( BfArrayTypeRef * arrayTypeRef )
{
VisitChild ( arrayTypeRef - > mOpenBracket ) ;
for ( int paramIdx = 0 ; paramIdx < ( int ) arrayTypeRef - > mParams . size ( ) ; paramIdx + + )
{
auto param = arrayTypeRef - > mParams [ paramIdx ] ;
if ( paramIdx > 0 )
{
if ( auto tokenNode = BfNodeDynCast < BfTokenNode > ( param ) )
{
}
else
{
ExpectSpace ( ) ;
}
}
VisitChild ( param ) ;
}
VisitChild ( arrayTypeRef - > mCloseBracket ) ;
if ( auto innerArrayType = BfNodeDynCast < BfArrayTypeRef > ( arrayTypeRef - > mElementType ) )
_VisitBrackets ( innerArrayType ) ;
} ;
_VisitElements ( arrayTypeRef ) ;
_VisitBrackets ( arrayTypeRef ) ;
}
void BfPrinter : : Visit ( BfGenericInstanceTypeRef * genericInstTypeRef )
{
Visit ( ( BfAstNode * ) genericInstTypeRef ) ;
VisitChild ( genericInstTypeRef - > mElementType ) ;
VisitChild ( genericInstTypeRef - > mOpenChevron ) ;
if ( genericInstTypeRef - > mGenericArguments . size ( ) > 0 )
{
for ( int i = 0 ; i < ( int ) genericInstTypeRef - > mGenericArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( genericInstTypeRef - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( genericInstTypeRef - > mGenericArguments [ i ] ) ;
}
}
else
{
for ( auto comma : genericInstTypeRef - > mCommas )
VisitChild ( comma ) ;
}
VisitChild ( genericInstTypeRef - > mCloseChevron ) ;
}
void BfPrinter : : Visit ( BfTupleTypeRef * typeRef )
{
Visit ( ( BfAstNode * ) typeRef ) ;
VisitChild ( typeRef - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) typeRef - > mFieldTypes . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( typeRef - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( typeRef - > mFieldTypes [ i ] ) ;
if ( i < ( int ) typeRef - > mFieldNames . size ( ) )
{
ExpectSpace ( ) ;
auto fieldNameNode = typeRef - > mFieldNames [ i ] ;
if ( fieldNameNode ! = NULL )
VisitChild ( fieldNameNode ) ;
}
2020-01-31 16:10:23 -08:00
}
VisitChild ( typeRef - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfDelegateTypeRef * typeRef )
{
Visit ( ( BfAstNode * ) typeRef ) ;
VisitChild ( typeRef - > mTypeToken ) ;
ExpectSpace ( ) ;
VisitChild ( typeRef - > mReturnType ) ;
VisitChild ( typeRef - > mAttributes ) ;
VisitChild ( typeRef - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) typeRef - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( typeRef - > mCommas . GetSafe ( i - 1 ) ) ;
2020-01-31 16:10:23 -08:00
ExpectSpace ( ) ;
}
VisitChild ( typeRef - > mParams [ i ] ) ;
2019-08-23 11:56:54 -07:00
}
VisitChild ( typeRef - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfPointerTypeRef * ptrType )
{
2020-01-31 16:10:23 -08:00
Visit ( ( BfAstNode * ) ptrType ) ;
2019-08-23 11:56:54 -07:00
VisitChild ( ptrType - > mElementType ) ;
VisitChild ( ptrType - > mStarNode ) ;
}
void BfPrinter : : Visit ( BfNullableTypeRef * ptrType )
{
Visit ( ( BfAstNode * ) ptrType ) ;
VisitChild ( ptrType - > mElementType ) ;
VisitChild ( ptrType - > mQuestionToken ) ;
}
void BfPrinter : : Visit ( BfVariableDeclaration * varDecl )
{
//Visit(varDecl->ToBase());
2021-06-19 07:48:28 -07:00
VisitChild ( varDecl - > mAttributes ) ;
2019-08-23 11:56:54 -07:00
VisitChildWithProceedingSpace ( varDecl - > mModSpecifier ) ;
if ( varDecl - > mPrecedingComma ! = NULL )
{
mNextStateModify . mWantNewLineIdx - - ;
VisitChild ( varDecl - > mPrecedingComma ) ;
}
else
VisitChild ( varDecl - > mTypeRef ) ;
VisitChildWithPrecedingSpace ( varDecl - > mNameNode ) ;
VisitChildWithPrecedingSpace ( varDecl - > mEqualsNode ) ;
if ( varDecl - > mInitializer ! = NULL )
{
if ( varDecl - > mNameNode ! = NULL )
ExpectSpace ( ) ;
VisitChild ( varDecl - > mInitializer ) ;
}
}
void BfPrinter : : Visit ( BfParameterDeclaration * paramDecl )
{
if ( paramDecl - > mModToken ! = NULL )
{
VisitChild ( paramDecl - > mModToken ) ;
ExpectSpace ( ) ;
}
Visit ( paramDecl - > ToBase ( ) ) ;
}
void BfPrinter : : Visit ( BfTypeOfExpression * typeOfExpr )
{
Visit ( typeOfExpr - > ToBase ( ) ) ;
VisitChild ( typeOfExpr - > mToken ) ;
VisitChild ( typeOfExpr - > mOpenParen ) ;
VisitChild ( typeOfExpr - > mTypeRef ) ;
VisitChild ( typeOfExpr - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfSizeOfExpression * sizeOfExpr )
{
Visit ( sizeOfExpr - > ToBase ( ) ) ;
VisitChild ( sizeOfExpr - > mToken ) ;
VisitChild ( sizeOfExpr - > mOpenParen ) ;
VisitChild ( sizeOfExpr - > mTypeRef ) ;
VisitChild ( sizeOfExpr - > mCloseParen ) ;
}
2021-09-10 14:21:25 -07:00
void BfPrinter : : Visit ( BfOffsetOfExpression * offsetOfExpr )
{
VisitChild ( offsetOfExpr - > mToken ) ;
VisitChild ( offsetOfExpr - > mOpenParen ) ;
VisitChild ( offsetOfExpr - > mTypeRef ) ;
VisitChild ( offsetOfExpr - > mCommaToken ) ;
ExpectSpace ( ) ;
VisitChild ( offsetOfExpr - > mMemberName ) ;
VisitChild ( offsetOfExpr - > mCloseParen ) ;
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfDefaultExpression * defaultExpr )
{
Visit ( defaultExpr - > ToBase ( ) ) ;
VisitChild ( defaultExpr - > mDefaultToken ) ;
VisitChild ( defaultExpr - > mOpenParen ) ;
VisitChild ( defaultExpr - > mTypeRef ) ;
VisitChild ( defaultExpr - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfCheckTypeExpression * checkTypeExpr )
{
Visit ( checkTypeExpr - > ToBase ( ) ) ;
VisitChild ( checkTypeExpr - > mTarget ) ;
ExpectSpace ( ) ;
VisitChild ( checkTypeExpr - > mIsToken ) ;
ExpectSpace ( ) ;
VisitChild ( checkTypeExpr - > mTypeRef ) ;
}
void BfPrinter : : Visit ( BfDynamicCastExpression * dynCastExpr )
{
Visit ( dynCastExpr - > ToBase ( ) ) ;
VisitChild ( dynCastExpr - > mTarget ) ;
ExpectSpace ( ) ;
VisitChild ( dynCastExpr - > mAsToken ) ;
ExpectSpace ( ) ;
VisitChild ( dynCastExpr - > mTypeRef ) ;
}
void BfPrinter : : Visit ( BfCastExpression * castExpr )
{
Visit ( ( BfAstNode * ) castExpr ) ;
VisitChild ( castExpr - > mOpenParen ) ;
VisitChild ( castExpr - > mTypeRef ) ;
VisitChild ( castExpr - > mCloseParen ) ;
bool surroundWithParen = false ;
if ( surroundWithParen )
Write ( " ( " ) ;
VisitChild ( castExpr - > mExpression ) ;
if ( surroundWithParen )
Write ( " ) " ) ;
}
void BfPrinter : : Visit ( BfDelegateBindExpression * bindExpr )
{
Visit ( bindExpr - > ToBase ( ) ) ;
VisitChild ( bindExpr - > mNewToken ) ;
ExpectSpace ( ) ;
VisitChild ( bindExpr - > mFatArrowToken ) ;
ExpectSpace ( ) ;
VisitChild ( bindExpr - > mTarget ) ;
VisitChild ( bindExpr - > mGenericArgs ) ;
}
void BfPrinter : : Visit ( BfLambdaBindExpression * lambdaBindExpr )
{
Visit ( lambdaBindExpr - > ToBase ( ) ) ;
2021-01-20 09:41:44 -08:00
if ( lambdaBindExpr - > mNewToken ! = NULL )
{
VisitChild ( lambdaBindExpr - > mNewToken ) ;
ExpectSpace ( ) ;
}
2019-08-23 11:56:54 -07:00
VisitChild ( lambdaBindExpr - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) lambdaBindExpr - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( lambdaBindExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( lambdaBindExpr - > mParams [ i ] ) ;
}
VisitChild ( lambdaBindExpr - > mCloseParen ) ;
ExpectSpace ( ) ;
VisitChild ( lambdaBindExpr - > mFatArrowToken ) ;
if ( lambdaBindExpr - > mBody ! = NULL )
{
if ( lambdaBindExpr - > mBody - > IsA < BfBlock > ( ) )
{
ExpectNewLine ( ) ;
ExpectIndent ( ) ;
VisitChild ( lambdaBindExpr - > mBody ) ;
ExpectUnindent ( ) ;
}
else
{
ExpectSpace ( ) ;
VisitChild ( lambdaBindExpr - > mBody ) ;
}
}
}
void BfPrinter : : Visit ( BfObjectCreateExpression * newExpr )
{
Visit ( newExpr - > ToBase ( ) ) ;
VisitChild ( newExpr - > mNewNode ) ;
ExpectSpace ( ) ;
2020-01-31 16:10:23 -08:00
2019-08-23 11:56:54 -07:00
VisitChild ( newExpr - > mTypeRef ) ;
2020-05-28 08:47:09 -07:00
if ( newExpr - > mStarToken ! = NULL )
{
VisitChild ( newExpr - > mStarToken ) ;
ExpectSpace ( ) ;
}
2020-08-29 11:29:13 -07:00
auto _WriteToken = [ & ] ( BfAstNode * node , BfToken token )
{
if ( node = = NULL )
return ;
2020-10-11 07:59:03 -07:00
Visit ( ( BfAstNode * ) node ) ;
Write ( node , node - > GetSrcStart ( ) , 0 ) ;
2020-08-29 11:29:13 -07:00
Write ( BfTokenToString ( token ) ) ;
} ;
_WriteToken ( newExpr - > mOpenToken , BfToken_LParen ) ;
2019-08-23 11:56:54 -07:00
for ( int i = 0 ; i < ( int ) newExpr - > mArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( newExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( newExpr - > mArguments [ i ] ) ;
}
2020-08-29 11:29:13 -07:00
_WriteToken ( newExpr - > mCloseToken , BfToken_RParen ) ;
2019-08-23 11:56:54 -07:00
}
void BfPrinter : : Visit ( BfBoxExpression * boxExpr )
{
Visit ( boxExpr - > ToBase ( ) ) ;
VisitChild ( boxExpr - > mAllocNode ) ;
ExpectSpace ( ) ;
VisitChild ( boxExpr - > mBoxToken ) ;
ExpectSpace ( ) ;
VisitChild ( boxExpr - > mExpression ) ;
}
void BfPrinter : : Visit ( BfThrowStatement * throwStmt )
{
Visit ( throwStmt - > ToBase ( ) ) ;
VisitChild ( throwStmt - > mThrowToken ) ;
ExpectSpace ( ) ;
VisitChild ( throwStmt - > mExpression ) ;
VisitChild ( throwStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfDeleteStatement * deleteStmt )
{
Visit ( deleteStmt - > ToBase ( ) ) ;
VisitChild ( deleteStmt - > mDeleteToken ) ;
2021-06-06 20:24:46 +02:00
VisitChild ( deleteStmt - > mTargetTypeToken ) ;
VisitChild ( deleteStmt - > mAllocExpr ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
VisitChild ( deleteStmt - > mExpression ) ;
2021-06-06 20:24:46 +02:00
2019-08-23 11:56:54 -07:00
VisitChild ( deleteStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfInvocationExpression * invocationExpr )
{
Visit ( invocationExpr - > ToBase ( ) ) ;
VisitChild ( invocationExpr - > mTarget ) ;
VisitChild ( invocationExpr - > mGenericArgs ) ;
VisitChild ( invocationExpr - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) invocationExpr - > mArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( invocationExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( invocationExpr - > mArguments [ i ] ) ;
}
VisitChild ( invocationExpr - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfDeferStatement * deferStmt )
{
Visit ( deferStmt - > ToBase ( ) ) ;
VisitChild ( deferStmt - > mDeferToken ) ;
VisitChild ( deferStmt - > mColonToken ) ;
2021-02-25 10:14:22 -08:00
VisitChild ( deferStmt - > mScopeName ) ;
2019-08-23 11:56:54 -07:00
if ( deferStmt - > mBind ! = NULL )
{
auto bind = deferStmt - > mBind ;
VisitChild ( bind - > mOpenBracket ) ;
for ( int i = 0 ; i < bind - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( bind - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( bind - > mParams [ i ] ) ;
}
VisitChild ( bind - > mCloseBracket ) ;
}
VisitChild ( deferStmt - > mOpenParen ) ;
VisitChild ( deferStmt - > mScopeToken ) ;
VisitChild ( deferStmt - > mCloseParen ) ;
ExpectSpace ( ) ;
VisitChild ( deferStmt - > mTargetNode ) ;
VisitChild ( deferStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfEnumCaseBindExpression * caseBindExpr )
{
Visit ( caseBindExpr - > ToBase ( ) ) ;
VisitChild ( caseBindExpr - > mBindToken ) ;
VisitChild ( caseBindExpr - > mEnumMemberExpr ) ;
VisitChild ( caseBindExpr - > mBindNames ) ;
}
void BfPrinter : : Visit ( BfCaseExpression * caseExpr )
{
Visit ( caseExpr - > ToBase ( ) ) ;
if ( ( caseExpr - > mValueExpression = = NULL ) | | ( caseExpr - > mCaseToken - > GetSrcStart ( ) < caseExpr - > mValueExpression - > GetSrcStart ( ) ) )
{
// Old version
VisitChild ( caseExpr - > mCaseToken ) ;
ExpectSpace ( ) ;
VisitChild ( caseExpr - > mCaseExpression ) ;
ExpectSpace ( ) ;
VisitChild ( caseExpr - > mEqualsNode ) ;
ExpectSpace ( ) ;
VisitChild ( caseExpr - > mValueExpression ) ;
}
else
{
VisitChild ( caseExpr - > mValueExpression ) ;
ExpectSpace ( ) ;
VisitChild ( caseExpr - > mCaseToken ) ;
BF_ASSERT ( caseExpr - > mEqualsNode = = NULL ) ;
ExpectSpace ( ) ;
VisitChild ( caseExpr - > mCaseExpression ) ;
}
}
void BfPrinter : : Visit ( BfSwitchCase * switchCase )
{
VisitChild ( switchCase - > mCaseToken ) ;
for ( int caseIdx = 0 ; caseIdx < ( int ) switchCase - > mCaseExpressions . size ( ) ; caseIdx + + )
{
if ( ( caseIdx = = 0 ) | | ( caseIdx > ( int ) switchCase - > mCaseCommas . size ( ) ) )
ExpectSpace ( ) ;
else
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( switchCase - > mCaseCommas . GetSafe ( caseIdx - 1 ) ) ;
2019-08-23 11:56:54 -07:00
VisitChild ( switchCase - > mCaseExpressions [ caseIdx ] ) ;
}
VisitChild ( switchCase - > mColonToken ) ;
ExpectNewLine ( ) ;
ExpectIndent ( ) ;
VisitChild ( switchCase - > mCodeBlock ) ;
ExpectUnindent ( ) ;
if ( switchCase - > mEndingToken ! = NULL )
{
ExpectNewLine ( ) ;
VisitChild ( switchCase - > mEndingToken ) ;
VisitChild ( switchCase - > mEndingSemicolonToken ) ;
ExpectNewLine ( ) ;
}
}
void BfPrinter : : Visit ( BfWhenExpression * whenExpr )
{
VisitChild ( whenExpr - > mWhenToken ) ;
ExpectSpace ( ) ;
VisitChild ( whenExpr - > mExpression ) ;
}
void BfPrinter : : Visit ( BfSwitchStatement * switchStmt )
{
Visit ( switchStmt - > ToBase ( ) ) ;
VisitChild ( switchStmt - > mSwitchToken ) ;
ExpectSpace ( ) ;
VisitChild ( switchStmt - > mOpenParen ) ;
VisitChild ( switchStmt - > mSwitchValue ) ;
VisitChild ( switchStmt - > mCloseParen ) ;
ExpectNewLine ( ) ;
VisitChild ( switchStmt - > mOpenBrace ) ;
ExpectNewLine ( ) ;
for ( auto switchCase : switchStmt - > mSwitchCases )
Visit ( switchCase ) ;
if ( switchStmt - > mDefaultCase ! = NULL )
Visit ( switchStmt - > mDefaultCase ) ;
ExpectNewLine ( ) ;
VisitChild ( switchStmt - > mCloseBrace ) ;
VisitChild ( switchStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfTryStatement * tryStmt )
{
Visit ( tryStmt - > ToBase ( ) ) ;
VisitChild ( tryStmt - > mTryToken ) ;
VisitChild ( tryStmt - > mStatement ) ;
}
void BfPrinter : : Visit ( BfCatchStatement * catchStmt )
{
Visit ( catchStmt - > ToBase ( ) ) ;
WriteSourceString ( catchStmt ) ;
VisitChild ( catchStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfFinallyStatement * finallyStmt )
{
Visit ( finallyStmt - > ToBase ( ) ) ;
VisitChild ( finallyStmt - > mFinallyToken ) ;
VisitChild ( finallyStmt - > mStatement ) ;
}
void BfPrinter : : Visit ( BfCheckedStatement * checkedStmt )
{
Visit ( checkedStmt - > ToBase ( ) ) ;
VisitChild ( checkedStmt - > mCheckedToken ) ;
VisitChild ( checkedStmt - > mStatement ) ;
}
void BfPrinter : : Visit ( BfUncheckedStatement * uncheckedStmt )
{
Visit ( uncheckedStmt - > ToBase ( ) ) ;
VisitChild ( uncheckedStmt - > mUncheckedToken ) ;
VisitChild ( uncheckedStmt - > mStatement ) ;
}
void BfPrinter : : Visit ( BfIfStatement * ifStmt )
{
Visit ( ifStmt - > ToBase ( ) ) ;
VisitChild ( ifStmt - > mIfToken ) ;
ExpectSpace ( ) ;
VisitChild ( ifStmt - > mOpenParen ) ;
VisitChild ( ifStmt - > mCondition ) ;
VisitChild ( ifStmt - > mCloseParen ) ;
ExpectSpace ( ) ;
VisitChildNextLine ( ifStmt - > mTrueStatement ) ;
VisitChild ( ifStmt - > mElseToken ) ;
if ( ifStmt - > mFalseStatement ! = NULL )
{
ExpectSpace ( ) ;
if ( ifStmt - > mFalseStatement - > IsA < BfIfStatement > ( ) )
VisitChild ( ifStmt - > mFalseStatement ) ;
else
VisitChildNextLine ( ifStmt - > mFalseStatement ) ;
}
VisitChild ( ifStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfReturnStatement * returnStmt )
{
Visit ( returnStmt - > ToBase ( ) ) ;
VisitChild ( returnStmt - > mReturnToken ) ;
if ( returnStmt - > mExpression ! = NULL )
{
ExpectSpace ( ) ;
VisitChild ( returnStmt - > mExpression ) ;
}
VisitChild ( returnStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfUsingStatement * doStmt )
{
Visit ( doStmt - > ToBase ( ) ) ;
VisitChild ( doStmt - > mUsingToken ) ;
ExpectSpace ( ) ;
VisitChild ( doStmt - > mOpenParen ) ;
VisitChild ( doStmt - > mVariableDeclaration ) ;
VisitChild ( doStmt - > mCloseParen ) ;
VisitChildNextLine ( doStmt - > mEmbeddedStatement ) ;
VisitChild ( doStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfDoStatement * doStmt )
{
Visit ( doStmt - > ToBase ( ) ) ;
VisitChild ( doStmt - > mDoToken ) ;
VisitChildNextLine ( doStmt - > mEmbeddedStatement ) ;
}
void BfPrinter : : Visit ( BfRepeatStatement * repeatStmt )
{
Visit ( repeatStmt - > ToBase ( ) ) ;
VisitChild ( repeatStmt - > mRepeatToken ) ;
VisitChildNextLine ( repeatStmt - > mEmbeddedStatement ) ;
VisitChild ( repeatStmt - > mWhileToken ) ;
ExpectSpace ( ) ;
VisitChild ( repeatStmt - > mOpenParen ) ;
VisitChild ( repeatStmt - > mCondition ) ;
VisitChild ( repeatStmt - > mCloseParen ) ;
VisitChild ( repeatStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfWhileStatement * whileStmt )
{
Visit ( whileStmt - > ToBase ( ) ) ;
VisitChild ( whileStmt - > mWhileToken ) ;
ExpectSpace ( ) ;
VisitChild ( whileStmt - > mOpenParen ) ;
VisitChild ( whileStmt - > mCondition ) ;
VisitChild ( whileStmt - > mCloseParen ) ;
VisitChildNextLine ( whileStmt - > mEmbeddedStatement ) ;
VisitChild ( whileStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfBreakStatement * breakStmt )
{
Visit ( breakStmt - > ToBase ( ) ) ;
VisitChild ( breakStmt - > mBreakNode ) ;
if ( breakStmt - > mLabel ! = NULL )
{
ExpectSpace ( ) ;
VisitChild ( breakStmt - > mLabel ) ;
}
VisitChild ( breakStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfContinueStatement * continueStmt )
{
Visit ( continueStmt - > ToBase ( ) ) ;
VisitChild ( continueStmt - > mContinueNode ) ;
if ( continueStmt - > mLabel ! = NULL )
{
ExpectSpace ( ) ;
VisitChild ( continueStmt - > mLabel ) ;
}
VisitChild ( continueStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfFallthroughStatement * fallthroughStmt )
{
Visit ( fallthroughStmt - > ToBase ( ) ) ;
VisitChild ( fallthroughStmt - > mFallthroughToken ) ;
VisitChild ( fallthroughStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfForStatement * forStmt )
{
Visit ( forStmt - > ToBase ( ) ) ;
VisitChild ( forStmt - > mForToken ) ;
ExpectSpace ( ) ;
VisitChild ( forStmt - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) forStmt - > mInitializers . size ( ) ; i + + )
{
if ( i > 0 )
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( forStmt - > mInitializerCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
VisitChild ( forStmt - > mInitializers [ i ] ) ;
}
VisitChild ( forStmt - > mInitializerSemicolon ) ;
ExpectSpace ( ) ;
VisitChild ( forStmt - > mCondition ) ;
VisitChild ( forStmt - > mConditionSemicolon ) ;
ExpectSpace ( ) ;
for ( int i = 0 ; i < ( int ) forStmt - > mIterators . size ( ) ; i + + )
{
if ( i > 0 )
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( forStmt - > mIteratorCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
VisitChild ( forStmt - > mIterators [ i ] ) ;
}
VisitChild ( forStmt - > mCloseParen ) ;
VisitChildNextLine ( forStmt - > mEmbeddedStatement ) ;
}
void BfPrinter : : Visit ( BfForEachStatement * forEachStmt )
{
Visit ( forEachStmt - > ToBase ( ) ) ;
VisitChild ( forEachStmt - > mForToken ) ;
ExpectSpace ( ) ;
VisitChild ( forEachStmt - > mOpenParen ) ;
if ( forEachStmt - > mReadOnlyToken ! = NULL )
{
VisitChild ( forEachStmt - > mReadOnlyToken ) ;
ExpectSpace ( ) ;
}
VisitChild ( forEachStmt - > mVariableTypeRef ) ;
ExpectSpace ( ) ;
VisitChild ( forEachStmt - > mVariableName ) ;
ExpectSpace ( ) ;
VisitChild ( forEachStmt - > mInToken ) ;
ExpectSpace ( ) ;
VisitChild ( forEachStmt - > mCollectionExpression ) ;
VisitChild ( forEachStmt - > mCloseParen ) ;
ExpectNewLine ( ) ;
VisitChildNextLine ( forEachStmt - > mEmbeddedStatement ) ;
VisitChild ( forEachStmt - > mTrailingSemicolon ) ;
}
void BfPrinter : : Visit ( BfConditionalExpression * condExpr )
{
Visit ( condExpr - > ToBase ( ) ) ;
VisitChild ( condExpr - > mConditionExpression ) ;
ExpectSpace ( ) ;
VisitChild ( condExpr - > mQuestionToken ) ;
ExpectSpace ( ) ;
VisitChild ( condExpr - > mTrueExpression ) ;
ExpectSpace ( ) ;
VisitChild ( condExpr - > mColonToken ) ;
ExpectSpace ( ) ;
VisitChild ( condExpr - > mFalseExpression ) ;
}
void BfPrinter : : Visit ( BfAssignmentExpression * assignExpr )
{
Visit ( assignExpr - > ToBase ( ) ) ;
VisitChild ( assignExpr - > mLeft ) ;
ExpectSpace ( ) ;
VisitChild ( assignExpr - > mOpToken ) ;
ExpectSpace ( ) ;
VisitChild ( assignExpr - > mRight ) ;
}
void BfPrinter : : Visit ( BfParenthesizedExpression * parenExpr )
{
Visit ( parenExpr - > ToBase ( ) ) ;
VisitChild ( parenExpr - > mOpenParen ) ;
VisitChild ( parenExpr - > mExpression ) ;
VisitChild ( parenExpr - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfTupleExpression * tupleExpr )
{
Visit ( tupleExpr - > ToBase ( ) ) ;
VisitChild ( tupleExpr - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) tupleExpr - > mValues . size ( ) ; i + + )
{
2021-01-20 09:41:44 -08:00
if ( i > 0 )
{
VisitChildNoRef ( tupleExpr - > mCommas . GetSafe ( i - 1 ) ) ;
ExpectSpace ( ) ;
}
2019-08-23 11:56:54 -07:00
if ( i < ( int ) tupleExpr - > mNames . size ( ) )
{
auto nameNode = tupleExpr - > mNames [ i ] ;
if ( nameNode ! = NULL )
{
VisitChild ( nameNode - > mNameNode ) ;
VisitChild ( nameNode - > mColonToken ) ;
ExpectSpace ( ) ;
}
}
2021-01-20 09:41:44 -08:00
2019-08-23 11:56:54 -07:00
VisitChild ( tupleExpr - > mValues [ i ] ) ;
}
VisitChild ( tupleExpr - > mCloseParen ) ;
}
void BfPrinter : : Visit ( BfMemberReferenceExpression * memberRefExpr )
{
Visit ( memberRefExpr - > ToBase ( ) ) ;
VisitChild ( memberRefExpr - > mTarget ) ;
VisitChild ( memberRefExpr - > mDotToken ) ;
VisitChild ( memberRefExpr - > mMemberName ) ;
}
void BfPrinter : : Visit ( BfIndexerExpression * indexerExpr )
{
Visit ( indexerExpr - > ToBase ( ) ) ;
VisitChild ( indexerExpr - > mTarget ) ;
VisitChild ( indexerExpr - > mOpenBracket ) ;
for ( int i = 0 ; i < ( int ) indexerExpr - > mArguments . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( indexerExpr - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( indexerExpr - > mArguments [ i ] ) ;
}
VisitChild ( indexerExpr - > mCloseBracket ) ;
}
void BfPrinter : : Visit ( BfUnaryOperatorExpression * unaryOpExpr )
{
Visit ( unaryOpExpr - > ToBase ( ) ) ;
2021-07-21 07:48:37 -07:00
bool postOp = ( unaryOpExpr - > mOp = = BfUnaryOp_PostIncrement ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_PostDecrement ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_PartialRangeFrom ) ;
2019-08-23 11:56:54 -07:00
if ( ! postOp )
VisitChild ( unaryOpExpr - > mOpToken ) ;
2021-01-20 09:41:44 -08:00
if ( ( unaryOpExpr - > mOp = = BfUnaryOp_Ref ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_Mut ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_Out ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_Params ) | | ( unaryOpExpr - > mOp = = BfUnaryOp_Cascade ) )
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
VisitChild ( unaryOpExpr - > mExpression ) ;
if ( postOp )
VisitChild ( unaryOpExpr - > mOpToken ) ;
}
void BfPrinter : : Visit ( BfBinaryOperatorExpression * binOpExpr )
{
//Visit(binOpExpr->ToBase());
VisitChild ( binOpExpr - > mLeft ) ;
ExpectSpace ( ) ;
VisitChild ( binOpExpr - > mOpToken ) ;
ExpectSpace ( ) ;
VisitChild ( binOpExpr - > mRight ) ;
}
void BfPrinter : : Visit ( BfConstructorDeclaration * ctorDeclaration )
{
//Visit((BfAstNode*)ctorDeclaration);
QueueVisitChild ( ctorDeclaration - > mAttributes ) ;
ExpectNewLine ( ) ;
2020-08-29 05:52:51 -07:00
ExpectSpace ( ) ;
2020-03-09 06:34:16 -07:00
QueueVisitChild ( ctorDeclaration - > mProtectionSpecifier ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
QueueVisitChild ( ctorDeclaration - > mNewSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( ctorDeclaration - > mStaticSpecifier ) ;
ExpectSpace ( ) ;
if ( mDocPrep )
{
FlushVisitChild ( ) ;
Write ( " " ) ;
VisitChild ( mCurTypeDecl - > mNameNode ) ;
}
else
{
QueueVisitChild ( ctorDeclaration - > mThisToken ) ;
}
QueueVisitChild ( ctorDeclaration - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) ctorDeclaration - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
QueueVisitChild ( ctorDeclaration - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
QueueVisitChild ( ctorDeclaration - > mParams [ i ] ) ;
}
QueueVisitChild ( ctorDeclaration - > mCloseParen ) ;
ExpectSpace ( ) ;
QueueVisitChild ( ctorDeclaration - > mInitializerColonToken ) ;
ExpectSpace ( ) ;
QueueVisitChild ( ctorDeclaration - > mInitializer ) ;
QueueVisitChild ( ctorDeclaration - > mFatArrowToken ) ;
QueueVisitChild ( ctorDeclaration - > mBody ) ;
FlushVisitChild ( ) ;
}
void BfPrinter : : Visit ( BfDestructorDeclaration * dtorDeclaration )
{
2020-03-25 06:28:10 -07:00
//Visit((BfAstNode*)dtorDeclaration);
2019-08-23 11:56:54 -07:00
QueueVisitChild ( dtorDeclaration - > mAttributes ) ;
ExpectNewLine ( ) ;
2020-08-29 05:52:51 -07:00
ExpectSpace ( ) ;
2020-03-09 06:34:16 -07:00
QueueVisitChild ( dtorDeclaration - > mProtectionSpecifier ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
QueueVisitChild ( dtorDeclaration - > mNewSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( dtorDeclaration - > mStaticSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( dtorDeclaration - > mTildeToken ) ;
if ( mDocPrep )
{
FlushVisitChild ( ) ;
VisitChild ( mCurTypeDecl - > mNameNode ) ;
}
else
{
QueueVisitChild ( dtorDeclaration - > mThisToken ) ;
}
QueueVisitChild ( dtorDeclaration - > mOpenParen ) ;
for ( int i = 0 ; i < ( int ) dtorDeclaration - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
QueueVisitChild ( dtorDeclaration - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
QueueVisitChild ( dtorDeclaration - > mParams [ i ] ) ;
}
QueueVisitChild ( dtorDeclaration - > mCloseParen ) ;
QueueVisitChild ( dtorDeclaration - > mFatArrowToken ) ;
QueueVisitChild ( dtorDeclaration - > mBody ) ;
FlushVisitChild ( ) ;
}
void BfPrinter : : QueueMethodDeclaration ( BfMethodDeclaration * methodDeclaration )
{
if ( methodDeclaration - > mAttributes ! = NULL )
{
QueueVisitChild ( methodDeclaration - > mAttributes ) ;
ExpectNewLine ( ) ;
}
if ( methodDeclaration - > mExternSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mExternSpecifier ) ;
}
if ( methodDeclaration - > mProtectionSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mProtectionSpecifier ) ;
}
2020-03-09 06:34:16 -07:00
2019-08-23 11:56:54 -07:00
if ( methodDeclaration - > mNewSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mNewSpecifier ) ;
}
if ( methodDeclaration - > mVirtualSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mVirtualSpecifier ) ;
}
if ( methodDeclaration - > mStaticSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mStaticSpecifier ) ;
}
if ( methodDeclaration - > mMixinSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mMixinSpecifier ) ;
}
if ( methodDeclaration - > mPartialSpecifier ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mPartialSpecifier ) ;
}
if ( ( methodDeclaration - > mNameNode ! = NULL ) | | ( methodDeclaration - > mExplicitInterface ! = NULL ) )
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mExplicitInterface ) ;
QueueVisitChild ( methodDeclaration - > mExplicitInterfaceDotToken ) ;
QueueVisitChild ( methodDeclaration - > mNameNode ) ;
2019-11-22 12:28:24 -08:00
if ( auto operatorDecl = BfNodeDynCast < BfOperatorDeclaration > ( methodDeclaration ) )
{
if ( ( operatorDecl - > mOpTypeToken ! = NULL ) & & ( operatorDecl - > mOpTypeToken - > mToken = = BfToken_LChevron ) )
ExpectSpace ( ) ;
}
2019-08-23 11:56:54 -07:00
QueueVisitChild ( methodDeclaration - > mGenericParams ) ;
if ( auto operatorDecl = BfNodeDynCast < BfOperatorDeclaration > ( methodDeclaration ) )
{
ExpectSpace ( ) ;
QueueVisitChild ( operatorDecl - > mExplicitToken ) ;
ExpectSpace ( ) ;
2019-11-22 12:28:24 -08:00
QueueVisitChild ( operatorDecl - > mOperatorToken ) ;
2019-08-23 11:56:54 -07:00
QueueVisitChild ( operatorDecl - > mOpTypeToken ) ;
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mReturnType ) ;
}
else if ( methodDeclaration - > mReturnType ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mReturnType ) ;
}
2020-09-24 09:06:27 -07:00
2019-08-23 11:56:54 -07:00
QueueVisitChild ( methodDeclaration - > mOpenParen ) ;
2020-09-24 09:06:27 -07:00
if ( methodDeclaration - > mThisToken ! = NULL )
{
QueueVisitChild ( methodDeclaration - > mThisToken ) ;
ExpectSpace ( ) ;
}
2019-08-23 11:56:54 -07:00
for ( int i = 0 ; i < ( int ) methodDeclaration - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
QueueVisitChild ( methodDeclaration - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
QueueVisitChild ( methodDeclaration - > mParams [ i ] ) ;
}
QueueVisitChild ( methodDeclaration - > mCloseParen ) ;
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mMutSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mGenericConstraintsDeclaration ) ;
if ( methodDeclaration - > mFatArrowToken ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( methodDeclaration - > mFatArrowToken ) ;
ExpectSpace ( ) ;
}
QueueVisitChild ( methodDeclaration - > mBody ) ;
QueueVisitChild ( methodDeclaration - > mEndSemicolon ) ;
}
void BfPrinter : : Visit ( BfMethodDeclaration * methodDeclaration )
{
//Visit(methodDeclaration->ToBase());
ExpectNewLine ( ) ;
QueueMethodDeclaration ( methodDeclaration ) ;
//?? QueueVisitErrorNodes(methodDeclaration);
FlushVisitChild ( ) ;
ExpectNewLine ( ) ;
}
void BfPrinter : : Visit ( BfOperatorDeclaration * opreratorDeclaration )
{
Visit ( opreratorDeclaration - > ToBase ( ) ) ;
}
void BfPrinter : : Visit ( BfPropertyMethodDeclaration * propertyMethodDeclaration )
{
ExpectNewLine ( ) ;
QueueVisitChild ( propertyMethodDeclaration - > mAttributes ) ;
ExpectNewLine ( ) ;
QueueVisitChild ( propertyMethodDeclaration - > mProtectionSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyMethodDeclaration - > mNameNode ) ;
ExpectSpace ( ) ;
2021-11-27 09:05:23 -08:00
QueueVisitChild ( propertyMethodDeclaration - > mSetRefSpecifier ) ;
ExpectSpace ( ) ;
2019-08-23 11:56:54 -07:00
QueueVisitChild ( propertyMethodDeclaration - > mMutSpecifier ) ;
ExpectSpace ( ) ;
2020-05-08 11:11:01 -07:00
QueueVisitChild ( propertyMethodDeclaration - > mFatArrowToken ) ;
ExpectSpace ( ) ;
2019-08-23 11:56:54 -07:00
if ( auto block = BfNodeDynCast < BfBlock > ( propertyMethodDeclaration - > mBody ) )
ExpectNewLine ( ) ;
QueueVisitChild ( propertyMethodDeclaration - > mBody ) ;
2020-05-08 11:11:01 -07:00
QueueVisitChild ( propertyMethodDeclaration - > mEndSemicolon ) ;
2019-08-23 11:56:54 -07:00
}
void BfPrinter : : Visit ( BfPropertyDeclaration * propertyDeclaration )
2020-09-25 09:17:33 -07:00
{
2019-08-23 11:56:54 -07:00
auto indexerDeclaration = BfNodeDynCast < BfIndexerDeclaration > ( propertyDeclaration ) ;
ExpectNewLine ( ) ;
QueueVisitChild ( propertyDeclaration - > mAttributes ) ;
2020-03-09 06:34:16 -07:00
ExpectNewLine ( ) ;
2020-09-14 11:55:18 -07:00
ExpectSpace ( ) ;
2019-08-23 11:56:54 -07:00
QueueVisitChild ( propertyDeclaration - > mProtectionSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mConstSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mVolatileSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mVirtualSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mExternSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mStaticSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mTypeRef ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mExplicitInterface ) ;
QueueVisitChild ( propertyDeclaration - > mExplicitInterfaceDotToken ) ;
QueueVisitChild ( propertyDeclaration - > mNameNode ) ;
ExpectSpace ( ) ;
if ( indexerDeclaration ! = NULL )
{
2021-11-27 10:31:32 -08:00
QueueVisitChild ( indexerDeclaration - > mThisToken ) ;
QueueVisitChild ( indexerDeclaration - > mOpenBracket ) ;
2019-08-23 11:56:54 -07:00
for ( int i = 0 ; i < ( int ) indexerDeclaration - > mParams . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
QueueVisitChild ( indexerDeclaration - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
QueueVisitChild ( indexerDeclaration - > mParams [ i ] ) ;
}
QueueVisitChild ( indexerDeclaration - > mCloseBracket ) ;
ExpectSpace ( ) ;
2021-11-27 10:31:32 -08:00
}
2019-08-23 11:56:54 -07:00
2019-09-30 12:24:02 -07:00
if ( auto block = BfNodeDynCast < BfBlock > ( propertyDeclaration - > mDefinitionBlock ) )
2019-08-23 11:56:54 -07:00
{
2020-09-30 07:39:39 -07:00
BlockState blockState ;
DoBlockOpen ( NULL , block - > mOpenBrace , block - > mCloseBrace , true , blockState ) ;
2019-09-30 12:24:02 -07:00
for ( auto method : propertyDeclaration - > mMethods )
{
Visit ( method ) ;
2020-03-25 06:28:10 -07:00
}
FlushVisitChild ( ) ;
2020-09-30 07:39:39 -07:00
DoBlockClose ( NULL , block - > mOpenBrace , block - > mCloseBrace , true , blockState ) ;
2019-09-30 12:24:02 -07:00
}
else
{
2020-09-14 11:55:18 -07:00
ExpectSpace ( ) ;
2019-09-30 12:24:02 -07:00
QueueVisitChild ( propertyDeclaration - > mDefinitionBlock ) ;
ExpectSpace ( ) ;
for ( auto method : propertyDeclaration - > mMethods )
{
QueueVisitChild ( method - > mBody ) ;
}
2019-08-23 11:56:54 -07:00
}
2021-11-27 10:31:32 -08:00
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mEqualsNode ) ;
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mInitializer ) ;
2021-12-07 10:33:43 -08:00
ExpectSpace ( ) ;
QueueVisitChild ( propertyDeclaration - > mFieldDtor ) ;
2021-11-27 10:31:32 -08:00
FlushVisitChild ( ) ;
2019-08-23 11:56:54 -07:00
//QueueVisitChild(propertyDeclaration->mTrailingSemicolon);
// ??? QueueVisitErrorNodes(propertyDeclaration);
FlushVisitChild ( ) ;
}
2020-09-25 09:17:33 -07:00
void BfPrinter : : Visit ( BfIndexerDeclaration * indexerDeclaration )
{
Visit ( ( BfPropertyDeclaration * ) indexerDeclaration ) ;
}
2019-08-23 11:56:54 -07:00
void BfPrinter : : Visit ( BfFieldDeclaration * fieldDeclaration )
2020-11-09 08:54:40 -08:00
{
2020-02-06 16:45:53 -08:00
bool isEnumDecl = false ;
if ( auto enumEntry = BfNodeDynCast < BfEnumEntryDeclaration > ( fieldDeclaration ) )
{
isEnumDecl = true ;
}
2020-11-09 08:54:40 -08:00
if ( fieldDeclaration - > mPrecedingComma ! = NULL )
{
mVirtualNewLineIdx = mNextStateModify . mWantNewLineIdx ;
2020-02-06 16:45:53 -08:00
2020-11-09 08:54:40 -08:00
QueueVisitChild ( fieldDeclaration - > mPrecedingComma ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mNameNode ) ;
}
else
2019-08-23 11:56:54 -07:00
{
2020-11-09 08:54:40 -08:00
if ( ! isEnumDecl )
ExpectNewLine ( ) ;
if ( fieldDeclaration - > mAttributes ! = NULL )
{
QueueVisitChild ( fieldDeclaration - > mAttributes ) ;
ExpectNewLine ( ) ;
}
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mProtectionSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mConstSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mReadOnlySpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mVolatileSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mNewSpecifier ) ;
ExpectSpace ( ) ;
2021-01-02 08:16:51 -08:00
QueueVisitChild ( fieldDeclaration - > mExternSpecifier ) ;
ExpectSpace ( ) ;
2020-11-09 08:54:40 -08:00
QueueVisitChild ( fieldDeclaration - > mStaticSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mPrecedingComma ) ;
ExpectSpace ( ) ;
if ( isEnumDecl )
mNextStateModify . mExpectingSpace = false ;
QueueVisitChild ( fieldDeclaration - > mTypeRef ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mNameNode ) ;
}
2020-02-06 16:45:53 -08:00
if ( fieldDeclaration - > mEqualsNode ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mEqualsNode ) ;
}
if ( fieldDeclaration - > mInitializer ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( fieldDeclaration - > mInitializer ) ;
}
2019-08-23 11:56:54 -07:00
auto fieldDtor = fieldDeclaration - > mFieldDtor ;
while ( fieldDtor ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( fieldDtor - > mTildeToken ) ;
ExpectSpace ( ) ;
QueueVisitChild ( fieldDtor - > mBody ) ;
fieldDtor = fieldDtor - > mNextFieldDtor ;
}
2020-11-09 08:54:40 -08:00
2019-08-23 11:56:54 -07:00
mNextStateModify . mExpectingSpace = false ;
FlushVisitChild ( ) ;
}
void BfPrinter : : Visit ( BfEnumCaseDeclaration * enumCaseDeclaration )
{
2020-02-06 16:45:53 -08:00
ExpectNewLine ( ) ;
Visit ( enumCaseDeclaration - > ToBase ( ) ) ;
2019-08-23 11:56:54 -07:00
if ( mDocPrep )
{
for ( int i = 0 ; i < ( int ) enumCaseDeclaration - > mEntries . size ( ) ; i + + )
{
auto fieldDecl = enumCaseDeclaration - > mEntries [ i ] ;
Visit ( ( BfAstNode * ) fieldDecl ) ;
Write ( " public static " ) ;
Visit ( mCurTypeDecl - > mNameNode ) ;
Write ( " " ) ;
VisitChild ( fieldDecl ) ;
Write ( " ; " ) ;
}
return ;
}
VisitChild ( enumCaseDeclaration - > mCaseToken ) ;
ExpectSpace ( ) ;
for ( int i = 0 ; i < ( int ) enumCaseDeclaration - > mEntries . size ( ) ; i + + )
{
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
VisitChildNoRef ( enumCaseDeclaration - > mCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
}
VisitChild ( enumCaseDeclaration - > mEntries [ i ] ) ;
}
}
void BfPrinter : : Visit ( BfTypeAliasDeclaration * typeDeclaration )
{
Visit ( typeDeclaration - > ToBase ( ) ) ;
ExpectSpace ( ) ;
VisitChild ( typeDeclaration - > mEqualsToken ) ;
ExpectSpace ( ) ;
VisitChild ( typeDeclaration - > mAliasToType ) ;
VisitChild ( typeDeclaration - > mEndSemicolon ) ;
}
void BfPrinter : : Visit ( BfTypeDeclaration * typeDeclaration )
{
SetAndRestoreValue < BfTypeDeclaration * > prevTypeDecl ( mCurTypeDecl , typeDeclaration ) ;
//Visit(typeDeclaration->ToBase());
bool isOneLine = true ;
const char * src = typeDeclaration - > GetSourceData ( ) - > mSrc ;
for ( int i = typeDeclaration - > GetSrcStart ( ) ; i < typeDeclaration - > GetSrcEnd ( ) ; i + + )
{
if ( src [ i ] = = ' \n ' )
{
isOneLine = false ;
break ;
}
}
ExpectNewLine ( ) ;
QueueVisitChild ( typeDeclaration - > mAttributes ) ;
if ( ! isOneLine )
2020-01-27 17:14:38 -08:00
ExpectNewLine ( ) ;
ExpectSpace ( ) ;
2019-08-23 11:56:54 -07:00
QueueVisitChild ( typeDeclaration - > mAbstractSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mSealedSpecifier ) ;
2020-03-09 06:34:16 -07:00
ExpectSpace ( ) ;
2019-08-23 11:56:54 -07:00
QueueVisitChild ( typeDeclaration - > mProtectionSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mStaticSpecifier ) ;
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mPartialSpecifier ) ;
ExpectSpace ( ) ;
bool isEnumDoc = false ;
if ( ( mDocPrep ) & & ( typeDeclaration - > mTypeNode ! = NULL ) & & ( typeDeclaration - > mTypeNode - > mToken = = BfToken_Enum ) )
{
if ( auto defineBlock = BfNodeDynCast < BfBlock > ( typeDeclaration - > mDefineNode ) )
{
if ( auto enumEntryDecl = BfNodeDynCast < BfEnumEntryDeclaration > ( defineBlock - > GetFirst ( ) ) )
{
}
else
{
isEnumDoc = true ;
}
}
}
if ( isEnumDoc )
{
FlushVisitChild ( ) ;
Write ( " struct " ) ;
}
else
QueueVisitChild ( typeDeclaration - > mTypeNode ) ;
bool queueChildren = ( typeDeclaration - > mTypeNode ! = NULL ) & &
( ( typeDeclaration - > mTypeNode - > mToken = = BfToken_Delegate ) | | ( typeDeclaration - > mTypeNode - > mToken = = BfToken_Function ) ) ;
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mNameNode ) ;
QueueVisitChild ( typeDeclaration - > mGenericParams ) ;
2021-01-02 12:54:05 -08:00
QueueVisitChild ( typeDeclaration - > mAutoCtor ) ;
2019-08-23 11:56:54 -07:00
if ( typeDeclaration - > mColonToken ! = NULL )
{
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mColonToken ) ;
for ( int i = 0 ; i < ( int ) typeDeclaration - > mBaseClasses . size ( ) ; i + + )
{
ExpectSpace ( ) ;
QueueVisitChild ( typeDeclaration - > mBaseClasses [ i ] ) ;
if ( i > 0 )
{
2020-09-06 05:28:19 -07:00
QueueVisitChild ( typeDeclaration - > mBaseClassCommas . GetSafe ( i - 1 ) ) ;
2019-08-23 11:56:54 -07:00
}
}
ExpectSpace ( ) ;
}
QueueVisitChild ( typeDeclaration - > mGenericConstraintsDeclaration ) ;
if ( queueChildren )
{
if ( auto defineBlock = BfNodeDynCast < BfBlock > ( typeDeclaration - > mDefineNode ) )
{
for ( auto member : defineBlock - > mChildArr )
{
SetAndRestoreValue < BfAstNode * > prevBlockMember ( mCurBlockMember , member ) ;
if ( auto methodDecl = BfNodeDynCast < BfMethodDeclaration > ( member ) )
{
QueueMethodDeclaration ( methodDecl ) ;
}
else
BF_FATAL ( " Error " ) ;
}
FlushVisitChild ( ) ;
}
else
{
FlushVisitChild ( ) ;
VisitChild ( typeDeclaration - > mDefineNode ) ;
}
}
else
{
FlushVisitChild ( ) ;
if ( auto defineBlock = BfNodeDynCast < BfBlock > ( typeDeclaration - > mDefineNode ) )
{
if ( ! isOneLine )
{
ExpectNewLine ( ) ;
mNextStateModify . mDoingBlockOpen = true ;
}
else
ExpectSpace ( ) ;
VisitChild ( defineBlock - > mOpenBrace ) ;
ExpectIndent ( ) ;
for ( auto member : defineBlock - > mChildArr )
{
SetAndRestoreValue < BfAstNode * > prevBlockMember ( mCurBlockMember , member ) ;
2020-02-06 16:45:53 -08:00
if ( auto fieldDecl = BfNodeDynCast < BfFieldDeclaration > ( member ) )
ExpectNewLine ( ) ;
2019-08-23 11:56:54 -07:00
VisitChild ( member ) ;
}
ExpectUnindent ( ) ;
VisitChild ( defineBlock - > mCloseBrace ) ;
}
else
{
FlushVisitChild ( ) ;
VisitChild ( typeDeclaration - > mDefineNode ) ;
}
}
}
void BfPrinter : : Visit ( BfUsingDirective * usingDirective )
{
//Visit(usingDirective->ToBase());
ExpectNewLine ( ) ;
VisitChild ( usingDirective - > mUsingToken ) ;
ExpectSpace ( ) ;
VisitChild ( usingDirective - > mNamespace ) ;
VisitChild ( usingDirective - > mTrailingSemicolon ) ;
ExpectNewLine ( ) ;
}
2020-10-14 11:33:41 -07:00
void BfPrinter : : Visit ( BfUsingModDirective * usingDirective )
2019-08-23 11:56:54 -07:00
{
ExpectNewLine ( ) ;
VisitChild ( usingDirective - > mUsingToken ) ;
ExpectSpace ( ) ;
2020-10-14 11:33:41 -07:00
VisitChild ( usingDirective - > mModToken ) ;
2019-08-23 11:56:54 -07:00
ExpectSpace ( ) ;
VisitChild ( usingDirective - > mTypeRef ) ;
VisitChild ( usingDirective - > mTrailingSemicolon ) ;
ExpectNewLine ( ) ;
}
void BfPrinter : : Visit ( BfNamespaceDeclaration * namespaceDeclaration )
{
//Visit(namespaceDeclaration->ToBase());
ExpectNewLine ( ) ;
VisitChild ( namespaceDeclaration - > mNamespaceNode ) ;
ExpectSpace ( ) ;
VisitChild ( namespaceDeclaration - > mNameNode ) ;
VisitChild ( namespaceDeclaration - > mBlock ) ;
}
2020-09-30 07:39:39 -07:00
void BfPrinter : : DoBlockOpen ( BfAstNode * prevNode , BfTokenNode * blockOpen , BfTokenNode * blockClose , bool queue , BlockState & blockState )
{
blockState . mLastSpaceOffset = mLastSpaceOffset ;
blockState . mIndentStart = mNextStateModify . mWantVirtualIndent ;
2020-09-25 09:17:33 -07:00
2019-08-23 11:56:54 -07:00
bool doInlineBlock = true ;
2020-09-30 07:39:39 -07:00
if ( blockClose ! = NULL )
2019-08-23 11:56:54 -07:00
{
2020-09-30 07:39:39 -07:00
auto blockSrc = blockOpen - > GetSourceData ( ) ;
int srcEnd = blockClose - > GetSrcEnd ( ) ;
int srcStart = blockOpen - > GetSrcStart ( ) ;
if ( prevNode ! = NULL )
srcStart = prevNode - > GetSrcEnd ( ) ;
for ( int i = srcStart ; i < srcEnd ; i + + )
2019-08-23 11:56:54 -07:00
{
if ( blockSrc - > mSrc [ i ] = = ' \n ' )
doInlineBlock = false ;
}
}
if ( ! doInlineBlock )
{
ExpectNewLine ( ) ;
mNextStateModify . mDoingBlockOpen = true ;
2020-09-30 07:39:39 -07:00
if ( prevNode ! = NULL )
ExpectIndent ( ) ;
2019-08-23 11:56:54 -07:00
}
else
ExpectSpace ( ) ;
if ( queue )
2020-09-30 07:39:39 -07:00
QueueVisitChild ( blockOpen ) ;
2019-08-23 11:56:54 -07:00
else
2020-09-30 07:39:39 -07:00
VisitChild ( blockOpen ) ;
2019-08-23 11:56:54 -07:00
if ( ! doInlineBlock )
ExpectIndent ( ) ;
else
ExpectSpace ( ) ;
2020-09-30 07:39:39 -07:00
blockState . mDoInlineBlock = doInlineBlock ;
2019-08-23 11:56:54 -07:00
}
2020-09-30 07:39:39 -07:00
void BfPrinter : : DoBlockClose ( BfAstNode * prevNode , BfTokenNode * blockOpen , BfTokenNode * blockClose , bool queue , BlockState & blockState )
2019-08-23 11:56:54 -07:00
{
2020-09-30 07:39:39 -07:00
if ( ! blockState . mDoInlineBlock )
2019-08-23 11:56:54 -07:00
{
2020-09-30 07:39:39 -07:00
ExpectUnindent ( ) ;
2019-08-23 11:56:54 -07:00
mNextStateModify . mDoingBlockClose = true ;
}
else
ExpectSpace ( ) ;
if ( queue )
2020-09-30 07:39:39 -07:00
QueueVisitChild ( blockClose ) ;
2019-08-23 11:56:54 -07:00
else
2020-09-30 07:39:39 -07:00
VisitChild ( blockClose ) ;
if ( ! blockState . mDoInlineBlock )
{
mNextStateModify . mWantVirtualIndent = blockState . mIndentStart ;
mLastSpaceOffset = blockState . mLastSpaceOffset ;
}
2019-08-23 11:56:54 -07:00
}
void BfPrinter : : Visit ( BfBlock * block )
{
2020-09-30 07:39:39 -07:00
BlockState blockState ;
2020-12-28 09:10:49 -08:00
SetAndRestoreValue < BlockState * > prevBlockState ( mCurBlockState , & blockState ) ;
2020-09-30 07:39:39 -07:00
DoBlockOpen ( NULL , block - > mOpenBrace , block - > mCloseBrace , false , blockState ) ;
2019-08-23 11:56:54 -07:00
for ( auto & childNodeRef : * block )
{
BfAstNode * child = childNodeRef ;
2020-12-28 09:10:49 -08:00
SetAndRestoreValue < bool > prevForceTrivia ( mForceUseTrivia ) ;
SetAndRestoreValue < int > prevVirtualIndent ( mNextStateModify . mWantVirtualIndent ) ;
2019-08-23 11:56:54 -07:00
SetAndRestoreValue < BfAstNode * > prevBlockMember ( mCurBlockMember , child ) ;
2020-12-28 09:10:49 -08:00
CheckRawNode ( child ) ;
2019-08-23 11:56:54 -07:00
child - > Accept ( this ) ;
}
2020-09-30 07:39:39 -07:00
DoBlockClose ( NULL , block - > mOpenBrace , block - > mCloseBrace , false , blockState ) ;
2019-08-23 11:56:54 -07:00
ExpectNewLine ( ) ;
}
void BfPrinter : : Visit ( BfRootNode * rootNode )
{
for ( auto child : rootNode - > mChildArr )
{
SetAndRestoreValue < BfAstNode * > prevBlockMember ( mCurBlockMember , child ) ;
child - > Accept ( this ) ;
}
2020-05-26 06:10:51 -07:00
// Flush whitespace at the end of the document
2019-08-23 11:56:54 -07:00
BfParserData * bfParser = rootNode - > GetSourceData ( ) - > ToParserData ( ) ;
if ( bfParser ! = NULL )
2020-06-05 10:24:48 -07:00
{
BfAstNode * endNode = mSource - > mAlloc . Alloc < BfAstNode > ( ) ;
endNode - > Init ( rootNode - > GetSrcEnd ( ) , bfParser - > mSrcLength , bfParser - > mSrcLength ) ;
Visit ( endNode ) ;
2020-05-26 06:10:51 -07:00
}
2019-08-23 11:56:54 -07:00
if ( mCharMapping ! = NULL )
{
BF_ASSERT ( mCharMapping - > size ( ) = = mOutString . length ( ) ) ;
}
}
void BfPrinter : : Visit ( BfInlineAsmStatement * asmStmt )
{
}