@@ -3613,19 +3613,166 @@ IReadOnlyList<Diagnostic> Diagnostics
36133613 return Constant ( Advance ( ) , PythonConstantKind . EllipsisLiteral ) ;
36143614 }
36153615
3616- if ( Current . Kind == SyntaxTokenKind . FormattedStringLiteral )
3616+ if (
3617+ Current . Kind
3618+ is SyntaxTokenKind . StringLiteral
3619+ or SyntaxTokenKind . BytesLiteral
3620+ or SyntaxTokenKind . FormattedStringLiteral
3621+ or SyntaxTokenKind . TemplateStringLiteral
3622+ )
36173623 {
3618- return ParseFormattedString ( Advance ( ) ) ;
3624+ return ParseAdjacentStrings ( ) ;
36193625 }
36203626
3621- if ( Current . Kind == SyntaxTokenKind . TemplateStringLiteral )
3627+ return constantKind is null ? null : Constant ( Advance ( ) , constantKind . Value ) ;
3628+ }
3629+
3630+ /// <summary>
3631+ /// A literal, with any adjacent literals folded into it: `"a" "b"` is one string,
3632+ /// `"a" f"{x}"` is one f-string, and bytes never mix with anything that is not
3633+ /// bytes, nor a template string with anything that is not a template string.
3634+ /// </summary>
3635+ private PythonExpression ParseAdjacentStrings ( )
3636+ {
3637+ var parts = new List < PythonExpression > { ParseStringAtom ( ) } ;
3638+ while (
3639+ Current . Kind
3640+ is SyntaxTokenKind . StringLiteral
3641+ or SyntaxTokenKind . BytesLiteral
3642+ or SyntaxTokenKind . FormattedStringLiteral
3643+ or SyntaxTokenKind . TemplateStringLiteral
3644+ )
36223645 {
3623- return ParseFormattedString ( Advance ( ) , isTemplate : true ) ;
3646+ parts . Add ( ParseStringAtom ( ) ) ;
36243647 }
36253648
3626- return constantKind is null ? null : Constant ( Advance ( ) , constantKind . Value ) ;
3649+ return parts . Count == 1 ? parts [ 0 ] : FoldStrings ( parts ) ;
3650+ }
3651+
3652+ private PythonExpression ParseStringAtom ( ) =>
3653+ Current . Kind switch
3654+ {
3655+ SyntaxTokenKind . StringLiteral => Constant (
3656+ Advance ( ) ,
3657+ PythonConstantKind . StringLiteral
3658+ ) ,
3659+ SyntaxTokenKind . BytesLiteral => Constant (
3660+ Advance ( ) ,
3661+ PythonConstantKind . BytesLiteral
3662+ ) ,
3663+ SyntaxTokenKind . FormattedStringLiteral => ParseFormattedString ( Advance ( ) ) ,
3664+ _ => ParseFormattedString ( Advance ( ) , isTemplate : true ) ,
3665+ } ;
3666+
3667+ private PythonExpression FoldStrings ( List < PythonExpression > parts )
3668+ {
3669+ var span = TextSpan . FromBounds ( parts [ 0 ] . Span . Start , parts [ ^ 1 ] . Span . End ) ;
3670+ var hasBytes = parts . Exists ( part =>
3671+ part is PythonConstantExpression { ConstantKind : PythonConstantKind . BytesLiteral }
3672+ ) ;
3673+ var hasString = parts . Exists ( part =>
3674+ part is PythonConstantExpression { ConstantKind : PythonConstantKind . StringLiteral }
3675+ ) ;
3676+ var hasFormatted = parts . Exists ( part => part is PythonFormattedStringExpression ) ;
3677+ var hasTemplate = parts . Exists ( part => part is PythonTemplateStringExpression ) ;
3678+ // A template string joins only other template strings, and bytes join only
3679+ // bytes; anything else is the mix CPython refuses.
3680+ if ( hasTemplate && ( hasBytes || hasString || hasFormatted ) )
3681+ {
3682+ Report ( "DPY2001" , TemplateMixMessage , span ) ;
3683+ return parts [ 0 ] ;
3684+ }
3685+ if ( hasBytes && ( hasString || hasFormatted ) )
3686+ {
3687+ Report ( "DPY2001" , BytesMixMessage , span ) ;
3688+ return parts [ 0 ] ;
3689+ }
3690+ if ( hasTemplate )
3691+ return FoldInterpolatedStrings ( parts , span , isTemplate : true ) ;
3692+ if ( hasFormatted )
3693+ return FoldInterpolatedStrings ( parts , span , isTemplate : false ) ;
3694+
3695+ return new PythonConstantExpression (
3696+ hasBytes ? PythonConstantKind . BytesLiteral : PythonConstantKind . StringLiteral ,
3697+ ( ( PythonConstantExpression ) parts [ 0 ] ) . TokenText ,
3698+ span ,
3699+ [ .. parts . Cast < PythonConstantExpression > ( ) ]
3700+ ) ;
36273701 }
36283702
3703+ /// <summary>
3704+ /// An f-string or template string joined with its neighbours: the interpolated
3705+ /// strings contribute their parts, and each plain literal contributes itself,
3706+ /// whole, for the string reader to decode.
3707+ /// </summary>
3708+ private static PythonExpression FoldInterpolatedStrings (
3709+ List < PythonExpression > parts ,
3710+ TextSpan span ,
3711+ bool isTemplate
3712+ )
3713+ {
3714+ var folded = new List < PythonFormattedStringPart > ( ) ;
3715+ var isRaw = false ;
3716+ foreach ( var part in parts )
3717+ {
3718+ switch ( part )
3719+ {
3720+ case PythonFormattedStringExpression formatted :
3721+ isRaw = formatted . IsRaw ;
3722+ FoldedParts ( folded , formatted . Parts , formatted . IsRaw ) ;
3723+ break ;
3724+ case PythonTemplateStringExpression template :
3725+ isRaw = template . IsRaw ;
3726+ FoldedParts ( folded , template . Parts , template . IsRaw ) ;
3727+ break ;
3728+ case PythonConstantExpression literal :
3729+ folded . Add (
3730+ new PythonFormattedStringLiteralPart (
3731+ literal . TokenText ,
3732+ literal . Span ,
3733+ PythonFormattedStringPartDecoding . EncodedLiteral
3734+ )
3735+ ) ;
3736+ break ;
3737+ }
3738+ }
3739+
3740+ return isTemplate
3741+ ? new PythonTemplateStringExpression ( folded . AsReadOnly ( ) , isRaw , span )
3742+ : new PythonFormattedStringExpression ( folded . AsReadOnly ( ) , isRaw , span ) ;
3743+ }
3744+
3745+ /// <summary>
3746+ /// The parts one folded string contributes, with the rawness of the string they
3747+ /// came from settled on each of them — a raw `rf"a\n"` beside a plain `f"b"` keeps
3748+ /// its backslash whatever the string they end up in says.
3749+ /// </summary>
3750+ private static void FoldedParts (
3751+ List < PythonFormattedStringPart > folded ,
3752+ IReadOnlyList < PythonFormattedStringPart > parts ,
3753+ bool isRaw
3754+ )
3755+ {
3756+ foreach ( var part in parts )
3757+ {
3758+ folded . Add (
3759+ part is PythonFormattedStringLiteralPart literal
3760+ ? literal with
3761+ {
3762+ Decoding = isRaw
3763+ ? PythonFormattedStringPartDecoding . Literal
3764+ : PythonFormattedStringPartDecoding . Escaped ,
3765+ }
3766+ : part
3767+ ) ;
3768+ }
3769+ }
3770+
3771+ private const string BytesMixMessage = "cannot mix bytes and nonbytes literals" ;
3772+
3773+ private const string TemplateMixMessage =
3774+ "cannot mix t-string literals with string or bytes literals" ;
3775+
36293776 private PythonExpression ? ParseParenthesizedOrTuple ( SyntaxToken leftParenthesis )
36303777 {
36313778 if ( Match ( SyntaxTokenKind . RightParenthesis , out var emptyRightParenthesis ) )
0 commit comments