Skip to content

Commit

Permalink
Rename MainAnnotation.{CommandInfo/ParamInfo} to Info/Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 6, 2022
1 parent feefe3f commit eb6148f
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 68 deletions.
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/ast/MainProxies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ object MainProxies {
* final class f {
* static def main(args: Array[String]): Unit = {
* val annotation = new myMain(80)
* val info = new CommandInfo(
* val info = new Info(
* name = "f",
* documentation = "Lorem ipsum dolor sit amet consectetur adipiscing elit.",
* parameters = Seq(
* new scala.annotation.MainAnnotation.ParameterInfo("x", "S", false, false, "my param x", Seq(new scala.main.Alias("myX"))),
* new scala.annotation.MainAnnotation.ParameterInfo("y", "S", true, false, "", Seq()),
* new scala.annotation.MainAnnotation.ParameterInfo("ys", "T", false, true, "all my params y", Seq())
* new scala.annotation.MainAnnotation.Parameter("x", "S", false, false, "my param x", Seq(new scala.main.Alias("myX"))),
* new scala.annotation.MainAnnotation.Parameter("y", "S", true, false, "", Seq()),
* new scala.annotation.MainAnnotation.Parameter("ys", "T", false, true, "all my params y", Seq())
* )
* ),
* val command = annot.command(info, args)
Expand Down Expand Up @@ -229,7 +229,7 @@ object MainProxies {
*
* A ParamInfo has the following shape
* ```
* new scala.annotation.MainAnnotation.ParameterInfo("x", "S", false, false, "my param x", Seq(new scala.main.Alias("myX")))
* new scala.annotation.MainAnnotation.Parameter("x", "S", false, false, "my param x", Seq(new scala.main.Alias("myX")))
* ```
*/
def parameterInfos(mt: MethodType): List[Tree] =
Expand All @@ -252,7 +252,7 @@ object MainProxies {
val constructorArgs = List(param, paramTypeStr, hasDefault, isRepeated, paramDoc)
.map(value => Literal(Constant(value)))

New(TypeTree(defn.MainAnnotationParameterInfo.typeRef), List(constructorArgs :+ paramAnnots))
New(TypeTree(defn.MainAnnotationParameter.typeRef), List(constructorArgs :+ paramAnnots))

end parameterInfos

Expand Down Expand Up @@ -319,7 +319,7 @@ object MainProxies {
val nameTree = Literal(Constant(mainFun.showName))
val docTree = Literal(Constant(documentation.mainDoc))
val paramInfos = Apply(ref(defn.SeqModule.termRef), parameterInfos)
New(TypeTree(defn.MainAnnotationCommandInfo.typeRef), List(List(nameTree, docTree, paramInfos)))
New(TypeTree(defn.MainAnnotationInfo.typeRef), List(List(nameTree, docTree, paramInfos)))

val annotVal = ValDef(
nme.annotation,
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ class Definitions {
@tu lazy val XMLTopScopeModule: Symbol = requiredModule("scala.xml.TopScope")

@tu lazy val MainAnnotationClass: ClassSymbol = requiredClass("scala.annotation.MainAnnotation")
@tu lazy val MainAnnotationCommandInfo: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.CommandInfo")
@tu lazy val MainAnnotationParameterInfo: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterInfo")
@tu lazy val MainAnnotationInfo: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.Info")
@tu lazy val MainAnnotationParameter: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.Parameter")
@tu lazy val MainAnnotationParameterAnnotation: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterAnnotation")
@tu lazy val MainAnnotationCommand: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.Command")

Expand Down
16 changes: 8 additions & 8 deletions docs/_docs/reference/experimental/main-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ When a users annotates a method with an annotation that extends `MainAnnotation`
object foo {
def main(args: Array[String]): Unit = {
val mainAnnot = new myMain()
val info = new CommandInfo(
val info = new Info(
name = "foo.main",
documentation = "Sum all the numbers",
parameters = Seq(
new ParameterInfo("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum", Seq()),
new ParameterInfo("second", "scala.Int", hasDefault=true, isVarargs=false, "", Seq()),
new ParameterInfo("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum", Seq())
new Parameter("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum", Seq()),
new Parameter("second", "scala.Int", hasDefault=true, isVarargs=false, "", Seq()),
new Parameter("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum", Seq())
)
)
val mainArgsOpt = mainAnnot.command(info, args)
Expand Down Expand Up @@ -54,9 +54,9 @@ import scala.util.CommandLineParser.FromString[T]

// Result type of the annotated method is Int and arguments are parsed using FromString
@experimental class myMain extends MainAnnotation[FromString, Int]:
import MainAnnotation.{ CommandInfo, ParameterInfo }
import MainAnnotation.{ Info, Parameter }

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] =
def command(info: Info, args: Seq[String]): Option[Seq[String]] =
if args.contains("--help") then
println(info.documentation)
None // do not parse or run the program
Expand All @@ -80,10 +80,10 @@ import scala.util.CommandLineParser.FromString[T]
else
Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
() => parser.fromString(arg)

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
def varargGetter[T](param: Parameter, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
() => args.map(arg => parser.fromString(arg))

def run(program: () => Int): Unit =
Expand Down
26 changes: 13 additions & 13 deletions library/src/scala/annotation/MainAnnotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ package scala.annotation
* object foo {
* def main(args: Array[String]): Unit = {
* val mainAnnot = new myMain()
* val info = new CommandInfo(
* val info = new Info(
* name = "foo.main",
* documentation = "Sum all the numbers",
* parameters = Seq(
* new ParameterInfo("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"),
* new ParameterInfo("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum")
* new Parameter("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"),
* new Parameter("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum")
* )
* )
* val mainArgsOpt = mainAnnot.command(info, args)
Expand All @@ -49,7 +49,7 @@ package scala.annotation
*/
@experimental
trait MainAnnotation[Parser[_], Result] extends StaticAnnotation:
import MainAnnotation.{CommandInfo, ParameterInfo}
import MainAnnotation.{Info, Parameter}

/** Process the command arguments before parsing them.
*
Expand All @@ -62,17 +62,17 @@ trait MainAnnotation[Parser[_], Result] extends StaticAnnotation:
* @param info The information about the command (name, documentation and info about parameters)
* @param args The command line arguments
*/
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]]
def command(info: Info, args: Seq[String]): Option[Seq[String]]

/** The getter for the `idx`th argument of type `T`
*
* @param idx The index of the argument
* @param defaultArgument Optional lambda to instantiate the default argument
*/
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using Parser[T]): () => T
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using Parser[T]): () => T

/** The getter for a final varargs argument of type `T*` */
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using Parser[T]): () => Seq[T]
def varargGetter[T](param: Parameter, args: Seq[String])(using Parser[T]): () => Seq[T]

/** Run `program` if all arguments are valid if all arguments are valid
*
Expand All @@ -88,19 +88,19 @@ object MainAnnotation:
/** Information about the main method
*
* @param name The name of the main method
* @param documentation The documentation of the main method without the `@param` documentation (see ParameterInfo.documentaion)
* @param documentation The documentation of the main method without the `@param` documentation (see Parameter.documentaion)
* @param parameters Information about the parameters of the main method
*/
final class CommandInfo(
final class Info(
val name: String,
val documentation: String,
val parameters: Seq[ParameterInfo],
val parameters: Seq[Parameter],
):

/** If the method ends with a varargs parameter */
def hasVarargs: Boolean = parameters.nonEmpty && parameters.last.isVarargs

end CommandInfo
end Info

/** Information about a parameter of a main method
*
Expand All @@ -111,7 +111,7 @@ object MainAnnotation:
* @param documentation The documentation of the parameter (from `@param` documentation in the main method)
* @param annotations The annotations of the parameter that extend `ParameterAnnotation`
*/
final class ParameterInfo(
final class Parameter(
val name: String,
val typeName: String,
val hasDefault: Boolean,
Expand All @@ -120,7 +120,7 @@ object MainAnnotation:
val annotations: Seq[ParameterAnnotation],
)

/** Marker trait for annotations that will be included in the ParameterInfo annotations. */
/** Marker trait for annotations that will be included in the Parameter annotations. */
trait ParameterAnnotation extends StaticAnnotation

end MainAnnotation
4 changes: 2 additions & 2 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object MiMaFilters {
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$Command"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$CommandInfo"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$ParameterInfo"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$Info"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$Parameter"),
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation$ParameterAnnotation"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
Expand Down
8 changes: 4 additions & 4 deletions tests/run/main-annotation-example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ end Test

@experimental
class myMain extends MainAnnotation[FromString, Int]:
import MainAnnotation.{ CommandInfo, ParameterInfo }
import MainAnnotation.{ Info, Parameter }

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] =
def command(info: Info, args: Seq[String]): Option[Seq[String]] =
if args.contains("--help") then
println(info.documentation)
None // do not parse or run the program
Expand All @@ -48,10 +48,10 @@ class myMain extends MainAnnotation[FromString, Int]:
else
Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
() => parser.fromString(arg)

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
def varargGetter[T](param: Parameter, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
() => args.map(arg => parser.fromString(arg))

def run(program: () => Int): Unit =
Expand Down
6 changes: 3 additions & 3 deletions tests/run/main-annotation-homemade-annot-1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class mainAwait(timeout: Int = 2) extends MainAnnotation[FromString, Future[Any]
import MainAnnotation.*

// This is a toy example, it only works with positional args
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
def command(info: Info, args: Seq[String]): Option[Seq[String]] = Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
() => p.fromString(arg)

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
() => for arg <- args yield p.fromString(arg)

def run(f: () => Future[Any]): Unit = println(Await.result(f(), Duration(timeout, SECONDS)))
Expand Down
6 changes: 3 additions & 3 deletions tests/run/main-annotation-homemade-annot-2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ end Test
class myMain(runs: Int = 3)(after: String*) extends MainAnnotation[FromString, Any]:
import MainAnnotation.*

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
def command(info: Info, args: Seq[String]): Option[Seq[String]] = Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
() => p.fromString(arg)

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
() => for arg <- args yield p.fromString(arg)

def run(f: () => Any): Unit =
Expand Down
6 changes: 3 additions & 3 deletions tests/run/main-annotation-homemade-annot-3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ end Test
class mainNoArgs extends MainAnnotation[FromString, Any]:
import MainAnnotation.*

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
def command(info: Info, args: Seq[String]): Option[Seq[String]] = Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???

def run(program: () => Any): Unit = program()
6 changes: 3 additions & 3 deletions tests/run/main-annotation-homemade-annot-4.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ end Test
class mainManyArgs(i1: Int, s2: String, i3: Int) extends MainAnnotation[FromString, Any]:
import MainAnnotation.*

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
def command(info: Info, args: Seq[String]): Option[Seq[String]] = Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???


def run(program: () => Any): Unit = program()
6 changes: 3 additions & 3 deletions tests/run/main-annotation-homemade-annot-5.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ end Test
class mainManyArgs(o: Option[Int]) extends MainAnnotation[FromString, Any]:
import MainAnnotation.*

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
def command(info: Info, args: Seq[String]): Option[Seq[String]] = Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???

def run(program: () => Any): Unit = program()
8 changes: 4 additions & 4 deletions tests/run/main-annotation-homemade-annot-6.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ command(
foo,
"Foo docs",
Seq(
ParameterInfo(name="i", typeName="scala.Int", hasDefault=false, isVarargs=false, documentation="", annotations=List()),
ParameterInfo(name="j", typeName="java.lang.String", hasDefault=true, isVarargs=false, documentation="", annotations=List())
Parameter(name="i", typeName="scala.Int", hasDefault=false, isVarargs=false, documentation="", annotations=List()),
Parameter(name="j", typeName="java.lang.String", hasDefault=true, isVarargs=false, documentation="", annotations=List())
)*
)
run()
Expand All @@ -15,8 +15,8 @@ command(
bar,
"Bar docs",
Seq(
ParameterInfo(name="i", typeName="scala.collection.immutable.List[Int]", hasDefault=false, isVarargs=false, documentation="the first parameter", annotations=List(MyParamAnnot(3))),
ParameterInfo(name="rest", typeName="scala.Int", hasDefault=false, isVarargs=true, documentation="", annotations=List())
Parameter(name="i", typeName="scala.collection.immutable.List[Int]", hasDefault=false, isVarargs=false, documentation="the first parameter", annotations=List(MyParamAnnot(3))),
Parameter(name="rest", typeName="scala.Int", hasDefault=false, isVarargs=true, documentation="", annotations=List())
)*
)
varargGetter()
Expand Down
10 changes: 5 additions & 5 deletions tests/run/main-annotation-homemade-annot-6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ end Test
class myMain extends MainAnnotation[Make, Any]:
import MainAnnotation.*

def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] =
def paramInfoString(paramInfo: ParameterInfo) =
def command(info: Info, args: Seq[String]): Option[Seq[String]] =
def paramInfoString(paramInfo: Parameter) =
import paramInfo.*
s" ParameterInfo(name=\"$name\", typeName=\"$typeName\", hasDefault=$hasDefault, isVarargs=$isVarargs, documentation=\"$documentation\", annotations=$annotations)"
s" Parameter(name=\"$name\", typeName=\"$typeName\", hasDefault=$hasDefault, isVarargs=$isVarargs, documentation=\"$documentation\", annotations=$annotations)"
println(
s"""command(
| ${args.mkString("Array(", ", ", ")")},
Expand All @@ -33,10 +33,10 @@ class myMain extends MainAnnotation[Make, Any]:
|)""".stripMargin)
Some(args)

def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: Make[T]): () => T =
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: Make[T]): () => T =
() => p.make

def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: Make[T]): () => Seq[T] =
def varargGetter[T](param: Parameter, args: Seq[String])(using p: Make[T]): () => Seq[T] =
println("varargGetter()")
() => Seq(p.make, p.make)

Expand Down
Loading

0 comments on commit eb6148f

Please sign in to comment.