Skip to content

Scala: Support metavars as elements in interpolated strings#403

Merged
maciejpirog merged 1 commit intomainfrom
mpir/scala-metavars-in-interpolated-strings
Aug 27, 2025
Merged

Scala: Support metavars as elements in interpolated strings#403
maciejpirog merged 1 commit intomainfrom
mpir/scala-metavars-in-interpolated-strings

Conversation

@maciejpirog
Copy link
Copy Markdown
Contributor

@maciejpirog maciejpirog commented Aug 27, 2025

Closes #106

In this PR:

  • The lexer does not ignore $ inside an interpolated string when it is followed by a metavar identifier
  • In the sgrep mode (i.e., when parsing patterns) the parser distinguishes between variables and metavariables by checking if the identifier starts with $. In non-sgrep mode (i.e., when parsing targets), the parser treats all identifiers as variables.

@maciejpirog maciejpirog force-pushed the mpir/scala-metavars-in-interpolated-strings branch 3 times, most recently from 1ffd6f0 to aec7fd2 Compare August 27, 2025 13:09
Comment on lines +2312 to +2315
match s.[0], Domain.DLS.get Flag_parsing.sgrep_mode with
| '$', true -> xs += EncapsExpr (Name (Id x, []))
| '$', false -> xs += EncapsDollarIdent (String.sub s 1 (String.length s - 1), t)
| _ -> xs += EncapsDollarIdent x
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be more efficient to check Char.equal s.[0] '$' and only in that case read from DLS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -0,0 +1,4 @@

//ERROR: match
println(s"this $x value comes $x")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird text, maybe:

println(s"this $x value is $x")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Copy Markdown
Collaborator

@dimitris-m dimitris-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, left some minor comments.

@maciejpirog maciejpirog force-pushed the mpir/scala-metavars-in-interpolated-strings branch from aec7fd2 to 1bad967 Compare August 27, 2025 18:50
@maciejpirog maciejpirog merged commit f52a5c0 into main Aug 27, 2025
6 checks passed
@maciejpirog maciejpirog deleted the mpir/scala-metavars-in-interpolated-strings branch August 27, 2025 19:14
This was referenced Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for metavariables in interpolated strings for Scala

2 participants