Skip to content

Commit

Permalink
Identify illegal $ tokens in string interpolators
Browse files Browse the repository at this point in the history
Fixes #195

Scala 2 stops parsing after one of these failures. It does not matter how we highlight the rest.

Scala 3 continues parsing assuming the string has been closed. Highlighting the following code as if the string was closed helps to understand the following parsing errors.
  • Loading branch information
nicolasstucki committed Jan 29, 2021
1 parent 8dee609 commit 6d3de7b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/typescript/Scala.tmLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export const scalaTmLanguage: TmLanguage = {
},
{
begin: `\\b(raw)(")`,
end: '"',
end: `(")|(\\$(?=[^\\$"_{${letterChars}]))`,
beginCaptures: {
'1': {
name: 'keyword.interpolation.scala'
Expand All @@ -441,14 +441,17 @@ export const scalaTmLanguage: TmLanguage = {
}
],
endCaptures: {
'0': {
'1': {
name: 'string.quoted.double.interpolated.scala punctuation.definition.string.end.scala'
},
'2': {
name: 'invalid.illegal.unrecognized-string-escape.scala'
}
}
},
{
begin: `\\b(${alphaId})(")`,
end: '"',
end: `(")|(\\$(?=[^\\$"_{${letterChars}]))`,
beginCaptures: {
'1': {
name: 'keyword.interpolation.scala'
Expand All @@ -458,6 +461,10 @@ export const scalaTmLanguage: TmLanguage = {
}
},
patterns: [
{
match: "\\$[\\$\"]",
name: 'constant.character.escape.scala'
},
{
include: "#string-interpolation"
},
Expand All @@ -475,8 +482,11 @@ export const scalaTmLanguage: TmLanguage = {
}
],
endCaptures: {
'0': {
'1': {
name: 'string.quoted.double.interpolated.scala punctuation.definition.string.end.scala'
},
'2': {
name: 'invalid.illegal.unrecognized-string-escape.scala'
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/#183.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// ^^ string.quoted.double.interpolated.scala
// ^^ - constant.character.escape.scala
// ^ punctuation.definition.string.end.scala

raw"$$ " // `$$` is an escaped `$` in raw interpolators
// ^^^ source.scala keyword.interpolation.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
Expand Down Expand Up @@ -74,3 +74,8 @@
// ^ meta.template.expression.scala punctuation.definition.template-expression.end.scala
// ^ string.quoted.triple.interpolated.scala
// ^^^ punctuation.definition.string.end.scala

raw"$
// ^^^ source.scala keyword.interpolation.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ invalid.illegal.unrecognized-string-escape.scala
47 changes: 47 additions & 0 deletions tests/unit/#195.test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SYNTAX TEST "source.scala"

s"$a"
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ meta.template.expression.scala punctuation.definition.template-expression.begin.scala
// ^ meta.template.expression.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.end.scala

s"${a}"
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^^ meta.template.expression.scala punctuation.definition.template-expression.begin.scala
// ^ meta.template.expression.scala
// ^ meta.template.expression.scala punctuation.definition.template-expression.end.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.end.scala

s"$_"
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ source.scala meta.template.expression.scala punctuation.definition.template-expression.begin.scala
// ^ source.scala meta.template.expression.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.end.scala

s"$$"
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^^ constant.character.escape.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.end.scala

s"$""
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^^ constant.character.escape.scala
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.end.scala


s"$ //
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ invalid.illegal.unrecognized-string-escape.scala
// ^^ comment.line.double-slash.scala punctuation.definition.comment.scala

s"$+
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ invalid.illegal.unrecognized-string-escape.scala
// ^ keyword.operator.arithmetic.scala

s"$; val a =
// ^ string.quoted.double.interpolated.scala punctuation.definition.string.begin.scala
// ^ invalid.illegal.unrecognized-string-escape.scala
// ^^^^^^^^^^ -string.quoted.double.interpolated.scala
// ^^^ keyword.declaration.stable.scala

0 comments on commit 6d3de7b

Please sign in to comment.