From dedb2622b3676011ee5b2a8a1e379d5328d1bc7a Mon Sep 17 00:00:00 2001 From: DomingoGP Date: Fri, 24 Nov 2023 00:24:32 +0100 Subject: [PATCH] Jedi code format: issue #40612 Accept and "or xor class, ..." operators and improved formatting of operator overloads. --- components/jcf2/Parse/BuildParseTree.pas | 24 ++++++------------- components/jcf2/Parse/Tokens.pas | 7 +++--- .../jcf2/Process/Spacing/NoSpaceAfter.pas | 3 +++ .../jcf2/Process/Spacing/NoSpaceBefore.pas | 8 ++----- .../jcf2/Process/Spacing/SingleSpaceAfter.pas | 5 +++- .../Process/Spacing/SingleSpaceBefore.pas | 10 +++++--- 6 files changed, 26 insertions(+), 31 deletions(-) diff --git a/components/jcf2/Parse/BuildParseTree.pas b/components/jcf2/Parse/BuildParseTree.pas index 05b7945bd9..7cc5740d4f 100644 --- a/components/jcf2/Parse/BuildParseTree.pas +++ b/components/jcf2/Parse/BuildParseTree.pas @@ -115,7 +115,6 @@ type procedure RecogniseProcedureDeclSection; procedure RecogniseClassOperator(const pbHasBody: boolean); procedure RecogniseOperator(const pbHasBody: boolean); - procedure RecogniseOperatorSymbol; // set pbAnon = true if the proc has no name procedure RecogniseProcedureHeading(const pbAnon, pbCanInterfaceMap: boolean); @@ -2605,7 +2604,7 @@ begin Recognise(ttOperator); CheckEnumeratorToken(); - RecogniseOperatorSymbol(); + RecogniseMethodName(False); if fcTokenList.FirstSolidTokenType = ttOpenBracket then RecogniseFormalParameters; @@ -2635,16 +2634,6 @@ begin PopNode; end; -procedure TBuildParseTree.RecogniseOperatorSymbol; -const - OperatorTokens: TTokenTypeSet = [ttPlus, ttMinus, ttTimes, ttFloatDiv, ttExponent, - ttEquals, ttGreaterThan, ttLessThan, ttGreaterThanOrEqual, ttLessThanOrEqual, - ttAssign, ttPlusAssign, ttMinusAssign, ttTimesAssign, ttFloatDivAssign, ttXor, - ttAnd, ttOr, ttEnumerator, ttNot, ttDiv, ttMod, ttIn, ttShl, ttShr]; -begin - Recognise(OperatorTokens); -end; - procedure TBuildParseTree.RecogniseVarDecl(aVarType: TVarType=vtNormal); const VariableModifiers: TTokenTypeSet = [ttExternal, ttExport, ttPublic]; @@ -5443,11 +5432,12 @@ begin exit; end else - if IsSymbolOperator(fcTokenList.FirstSolidToken) then begin - PushNode(nIdentifier); - Recognise(Operators); - PopNode; - exit; + if fcTokenList.FirstSolidTokenType in operators then + begin + PushNode(nIdentifier); + Recognise(Operators); + PopNode; + exit; end; if not (IdentifierNext(idAllowDirectives)) then RaiseParseError('Expected identifier', fcTokenList.FirstSolidToken); diff --git a/components/jcf2/Parse/Tokens.pas b/components/jcf2/Parse/Tokens.pas index bc31da5226..aa2c21b531 100644 --- a/components/jcf2/Parse/Tokens.pas +++ b/components/jcf2/Parse/Tokens.pas @@ -314,16 +314,15 @@ type ttGreaterThanOrEqual, ttLessThanOrEqual, ttNotEqual, - ttSetSymDif, - ttBackSlash, { legal in char literals } - // FreePascal c-style operators ttPlusAssign, // += ttMinusAssign, // -= ttTimesAssign, // *= ttFloatDivAssign, // /= ttShl_ll, // << - ttShr_gg // >> + ttShr_gg, // >> + ttSetSymDif, + ttBackSlash { legal in char literals } ); TTokenTypeSet = set of TTokenType; diff --git a/components/jcf2/Process/Spacing/NoSpaceAfter.pas b/components/jcf2/Process/Spacing/NoSpaceAfter.pas index da1cc8c69a..27ba07851b 100644 --- a/components/jcf2/Process/Spacing/NoSpaceAfter.pas +++ b/components/jcf2/Process/Spacing/NoSpaceAfter.pas @@ -67,6 +67,9 @@ begin if pt = nil then exit; + if (pt.TokenType in operators) and pt.HasParentNode(nIdentifier,1) then //operator orverloading identifier; + Exit(True); + { if the next thing is a comment, leave well enough alone } if ptNext.TokenType = ttComment then exit; diff --git a/components/jcf2/Process/Spacing/NoSpaceBefore.pas b/components/jcf2/Process/Spacing/NoSpaceBefore.pas index c25467d50d..9fd5d19f09 100644 --- a/components/jcf2/Process/Spacing/NoSpaceBefore.pas +++ b/components/jcf2/Process/Spacing/NoSpaceBefore.pas @@ -132,12 +132,8 @@ begin if (FormattingSettings.Spaces.SpaceForOperator = eNever) then begin - if IsSymbolOperator(pt) then - begin - Result := True; - exit; - end; - + if (pt.TokenType in Operators) and (not pt.HasParentNode(nIdentifier,1)) and IsSymbolOperator(pt) then + exit(True); end; { '[' of array property definition } diff --git a/components/jcf2/Process/Spacing/SingleSpaceAfter.pas b/components/jcf2/Process/Spacing/SingleSpaceAfter.pas index cdc3fd551b..e2925c0f16 100644 --- a/components/jcf2/Process/Spacing/SingleSpaceAfter.pas +++ b/components/jcf2/Process/Spacing/SingleSpaceAfter.pas @@ -78,7 +78,10 @@ begin Result := False; - if (pt.TokenType=ttClass) and (pt.NextSolidTokenType in SingleSpaceAfterClass) then + if (pt.TokenType in operators) and pt.HasParentNode(nIdentifier,1) then //operator orverloading identifier; + Exit(False); + + if (pt.TokenType=ttClass) and (pt.NextSolidTokenType in SingleSpaceAfterClass) then Exit(True); if pt.HasParentNode(nLiteralString) then diff --git a/components/jcf2/Process/Spacing/SingleSpaceBefore.pas b/components/jcf2/Process/Spacing/SingleSpaceBefore.pas index ba17149ed4..84644bb4ea 100644 --- a/components/jcf2/Process/Spacing/SingleSpaceBefore.pas +++ b/components/jcf2/Process/Spacing/SingleSpaceBefore.pas @@ -81,6 +81,11 @@ begin if pt.HasParentNode(nGeneric, 1) then exit; + if (pt.TokenType in operators) and pt.HasParentNode(nIdentifier,1) then + begin + exit(pt.PriorSolidTokenType<>ttDot); + end; + if pt.TokenType = ttCloseBracket then begin if FormattingSettings.Spaces.SpaceBeforeCloseBrackets then @@ -147,7 +152,7 @@ begin begin if (pt.TokenType in SingleSpaceOperators) then begin - Result := True; + Result := not pt.HasParentNode(nIdentifier,1); //can be an overloaded operator identifier. end; { 'a := --3;' and 'lc := ptr^;' @@ -158,8 +163,7 @@ begin (IsUnaryOperator(pt) and IsUnaryOperator(pt.PriorSolidToken)) then Result := False else - Result := True; - + Result := not pt.HasParentNode(nIdentifier,1); //can be an overloaded operator identifier. exit; end;