diff options
Diffstat (limited to 'compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala')
-rw-r--r-- | compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 4d61f21cf..94611e10d 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -4,10 +4,17 @@ package reporting package diagnostic import dotc.core._ -import Contexts.Context, Decorators._, Symbols._, Names._, NameOps._, Types._ +import Contexts.Context +import Decorators._ +import Symbols._ +import Names._ +import NameOps._ +import Types._ import util.SourcePosition import config.Settings.Setting -import interfaces.Diagnostic.{ERROR, WARNING, INFO} +import interfaces.Diagnostic.{ERROR, INFO, WARNING} +import dotc.parsing.Scanners.Token +import dotc.parsing.Tokens import printing.Highlighting._ import printing.Formatting @@ -1053,4 +1060,30 @@ object messages { |""".stripMargin } + case class ExpectedTokenButFound(expected: Token, found: Token, foundName: TermName)(implicit ctx: Context) + extends Message(40) { + val kind = "Syntax" + + private val expectedText = + if (Tokens.isIdentifier(expected)) "an identifier" + else Tokens.showToken(expected) + + private val foundText = + if (foundName != null) hl"`${foundName.show}`" + else Tokens.showToken(found) + + val msg = hl"""${expectedText} expected, but ${foundText} found""" + + private val ifKeyword = + if (Tokens.isIdentifier(expected) && Tokens.isKeyword(found)) + s""" + |If you necessarily want to use $foundText as identifier, you may put it in backticks.""".stripMargin + else + "" + + val explanation = + s"""|The text ${foundText} may not occur at that position, the compiler expected ${expectedText}.$ifKeyword + |""".stripMargin + } + } |