Skip to content

Commit 7c52ddb

Browse files
iamthiagoThiago Pereira
authored andcommitted
Add error message IdentifierExpected
This commit adds the semantic object fir the ```identifier expected``` error. It is part of the #1589
1 parent 557d448 commit 7c52ddb

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import dotty.tools.dotc.core.Flags
77
import dotty.tools.dotc.core.Flags.FlagSet
88

99
import scala.language.implicitConversions
10-
1110
import JavaTokens._
1211
import JavaScanners._
1312
import Scanners.Offset
@@ -21,8 +20,10 @@ import Symbols._
2120
import ast.Trees._
2221
import Decorators._
2322
import StdNames._
23+
import dotty.tools.dotc.reporting.diagnostic.messages.IdentifierExpected
2424
import dotty.tools.dotc.util.SourceFile
2525
import util.Positions._
26+
2627
import annotation.switch
2728
import scala.collection.mutable.ListBuffer
2829
import scala.reflect.internal.util.Collections._
@@ -230,7 +231,7 @@ object JavaParsers {
230231
case AppliedTypeTree(_, _) | Select(_, _) =>
231232
tree
232233
case _ =>
233-
syntaxError("identifier expected", tree.pos)
234+
syntaxError(IdentifierExpected(), tree.pos)
234235
errorTypeTree
235236
}
236237
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dotty.tools.dotc.parsing
2+
3+
import dotty.tools.dotc.ast.untpd._
4+
import dotty.tools.dotc.core.Contexts.{ContextBase, FreshContext}
5+
import dotty.tools.dotc.parsing.Parsers.Parser
6+
import dotty.tools.dotc.util.SourceFile
7+
8+
import scala.collection.mutable.ListBuffer
9+
import scala.io.Codec
10+
import scala.reflect.io.PlainFile
11+
12+
object MyTest {
13+
14+
def main(args: Array[String]): Unit = {
15+
16+
implicit val ctx: FreshContext = new ContextBase().initialCtx.fresh
17+
18+
var parsed = 0
19+
val parsedTrees = new ListBuffer[Tree]
20+
21+
def parse(name: String): Tree = {
22+
//println("***** parsing " + file)
23+
val file = new PlainFile(name)
24+
val source = new SourceFile(file, Codec.UTF8)
25+
val parser = new Parser(source)
26+
val tree = parser.parse()
27+
parsed += 1
28+
parsedTrees += tree
29+
tree
30+
}
31+
32+
parse("/Users/thiagopereira/Downloads/Test.scala")
33+
}
34+
}

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ object Parsers {
316316
case id @ Select(qual, name) =>
317317
cpy.Select(id)(qual, name.toTypeName)
318318
case _ =>
319-
syntaxError("identifier expected", tree.pos)
319+
syntaxError(IdentifierExpected(), tree.pos)
320320
tree
321321
}
322322

src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ package diagnostic
66
import dotc.core._
77
import Contexts.Context, Decorators._, Symbols._, Names._, NameOps._, Types._
88
import ast.untpd.{Modifiers, ModuleDef}
9-
import util.{SourceFile, NoSource}
10-
import util.{SourcePosition, NoSourcePosition}
9+
import util.SourcePosition
1110
import config.Settings.Setting
1211
import interfaces.Diagnostic.{ERROR, WARNING, INFO}
13-
import dotty.tools.dotc.ast.tpd
1412
import printing.Highlighting._
1513
import printing.Formatting
1614

@@ -678,4 +676,33 @@ object messages {
678676
|$fixedVarInAlternative""".stripMargin
679677
}
680678
}
679+
680+
case class IdentifierExpected()(implicit ctx: Context) extends Message(24) {
681+
val kind = "Syntax"
682+
val msg = "identifier expected"
683+
684+
val typeExplicitly = "def sum(): Int = {...}"
685+
686+
val typeImplicit = "def sum() = {...}"
687+
688+
val explanation = {
689+
hl"""|When using type identifiers, you should pass a valid one.
690+
|Because of Scala's stronger type, you can fix this issue either
691+
|passing a valid type identifier or you can omit it, so the compiler can infer it for you.
692+
|
693+
|Let's write a function called ${"sum"}
694+
|
695+
|Here we explicitly pass a valid type identifier, in this case ${"Int"} for the function:
696+
|
697+
|$typeExplicitly
698+
|
699+
|But you also have the option to omit it completely.
700+
|
701+
|$typeImplicit
702+
|
703+
|This time the compiler will infer this type for you.
704+
|
705+
|""".stripMargin
706+
}
707+
}
681708
}

0 commit comments

Comments
 (0)