Skip to content

Commit

Permalink
edit: genCompl
Browse files Browse the repository at this point in the history
  • Loading branch information
stonechoe committed Oct 31, 2024
1 parent 0908421 commit ff5f313
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/esmeta/phase/Test262Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ case object Test262Test extends Phase[CFG, Summary] {
),
(
"peval",
KnownStrOption(
StrOption(
(c, p) => {
c.peval = Some(
Test262PEvalPolicy
.parseOpt(p)
.getOrElse(throw OptInvalidError(p, "test262-test")),
)
},
strs = Test262PEvalPolicy.values.map(_.toParsedString).toSet,
prefer = Test262PEvalPolicy.values.map(_.toParsedString),
),
"turn on partial evaluation. (not possible with `coverage` option)",
),
Expand Down
12 changes: 4 additions & 8 deletions src/main/scala/esmeta/util/GenCompl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,10 @@ object GenCompl {
for {
phase <- phases
(optName, opt, _) <- phase.options
optCompls <- (opt match
case BoolOption(_) =>
Some(phase.name, optName, List("true", "false"))
case NumOption(_) => None
case StrOption(_) => None
case KnownStrOption(_, strs) =>
Some(phase.name, optName, strs.toList)
case StrListOption(_) => None
optCompls <- (
opt.completions.isEmpty match
case true => None
case false => Some(phase.name, optName, opt.completions)
)
} do {
(app :> "").wrap(s"-${optCompls._1}:${optCompls._2}=*)", "") {
Expand Down
32 changes: 14 additions & 18 deletions src/main/scala/esmeta/util/OptionKind.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ sealed abstract class OptionKind[T] {

/** a list of argument regular expressions */
def argRegexList(name: String): List[ArgRegex[T]]

/** completions for value T */
def completions: Iterable[String]
}

/** boolean options */
Expand All @@ -25,6 +28,7 @@ case class BoolOption[T](assign: (T, Boolean) => Unit) extends OptionKind[T] {
(("-" + name).r, "".r, (c, _) => assign(c, true)),
(("-" + name + "=").r, ".*".r, (c, _) => throw ExtraArgError(name)),
)
def completions = List("true", "false")
}

/** number options */
Expand All @@ -35,34 +39,24 @@ case class NumOption[T](assign: (T, Int) => Unit) extends OptionKind[T] {
(("-" + name + "=").r, ".*".r, (_, _) => throw NoNumArgError(name)),
(("-" + name).r, "".r, (_, _) => throw NoNumArgError(name)),
)

def completions = Nil
}

/** string options */
case class StrOption[T](assign: (T, String) => Unit) extends OptionKind[T] {
case class StrOption[T](
assign: (T, String) => Unit,
prefer: Iterable[String] = Iterable.empty,
) extends OptionKind[T] {
def postfix: String = "={string}"
def argRegexList(name: String): List[ArgRegex[T]] = List(
(("-" + name + "=").r, ".+".r, (c, s) => assign(c, s)),
(("-" + name + "=").r, ".*".r, (_, _) => throw NoStrArgError(name)),
(("-" + name).r, "".r, (_, _) => throw NoStrArgError(name)),
)
}

/** known string options */
case class KnownStrOption[T](
assign: (T, String) => Unit,
strs: Set[String],
) extends OptionKind[T] {
def postfix: String = s"={${strs.mkString(", ")}}"
def argRegexList(name: String): List[ArgRegex[T]] = List(
(
("-" + name + "=").r,
".+".r,
(c, s) =>
if strs.contains(s) then assign(c, s) else throw NoStrArgError(name),
),
(("-" + name + "=").r, ".*".r, (_, _) => throw NoStrArgError(name)),
(("-" + name).r, "".r, (_, _) => throw NoStrArgError(name)),
)
def completions = prefer

}

/** string list options */
Expand All @@ -74,4 +68,6 @@ case class StrListOption[T](assign: (T, List[String]) => Unit)
(("-" + name + "=").r, ".*".r, (_, _) => throw NoStrArgError(name)),
(("-" + name).r, "".r, (_, _) => throw NoStrArgError(name)),
)

def completions = Nil
}

0 comments on commit ff5f313

Please sign in to comment.