Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalametaParser: split param clause methods #2983

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,22 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
@inline private def tryAhead[T: TokenClassifier]: Boolean =
tryAhead(token.is[T])

private def tryAhead[T](cond: => Boolean): Boolean = {
private def tryAhead(cond: => Boolean): Boolean = {
val forked = in.fork
next()
val ok = cond
if (!ok) in = forked
ok
}

private def tryAhead[A](bodyFunc: => Option[A]): Option[A] = {
val forked = in.fork
next()
val body = bodyFunc
if (body.isEmpty) in = forked
body
}

/** evaluate block after shifting next */
@inline private def next[T](body: => T): T = {
next()
Expand Down Expand Up @@ -1396,6 +1404,14 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
if (token.is[LF]) tryAhead[T] else token.is[T]
}

def isAfterOptNewLine(cond: => Boolean): Boolean = {
if (token.is[LF]) tryAhead(cond) else cond
}

def getAfterOptNewLine[A](body: => Option[A]): Option[A] = {
if (token.is[LF]) tryAhead(body) else body
}

/* ------------- TYPES ---------------------------------------------------- */

def typedOpt(): Option[Type] =
Expand Down Expand Up @@ -2994,7 +3010,15 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
def termParamClauses(
ownerIsType: Boolean,
ownerIsCase: Boolean = false
): Seq[Term.ParamClause] = {
): List[Term.ParamClause] = {
if (!isAfterOptNewLine[LeftParen]) Nil
else termParamClausesOnParen(ownerIsType, ownerIsCase)
}

private def termParamClausesOnParen(
ownerIsType: Boolean,
ownerIsCase: Boolean = false
): List[Term.ParamClause] = {
var hadModImplicit = false
def paramClause(first: Boolean) = autoPos(inParensOnOpenOr {
def reduceParams(params: List[Term.Param], mod: Option[Mod.ParamsType] = None) =
Expand All @@ -3016,14 +3040,12 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
}
}(Term.ParamClause(Nil)))

if (!isAfterOptNewLine[LeftParen]) Nil
else
listBy[Term.ParamClause] { paramss =>
paramss += paramClause(true)
while (isAfterOptNewLine[LeftParen] && !hadModImplicit) {
paramss += paramClause(false)
}
listBy[Term.ParamClause] { paramss =>
paramss += paramClause(true)
while (isAfterOptNewLine[LeftParen] && !hadModImplicit) {
paramss += paramClause(false)
}
}
}

def paramType(): Type =
Expand Down Expand Up @@ -3143,16 +3165,22 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>

private def emptyTypeParams: Type.ParamClause = autoPos(Type.ParamClause(Nil))

def typeParamClauseOpt(
private def typeParamClauseOpt(
ownerIsType: Boolean,
ctxBoundsAllowed: Boolean,
allowUnderscore: Boolean = true
): Type.ParamClause = {
): Type.ParamClause =
if (!isAfterOptNewLine[LeftBracket]) emptyTypeParams
else
autoPos(inBrackets(commaSeparated {
typeParam(ownerIsType, ctxBoundsAllowed, allowUnderscore)
}.reduceWith(Type.ParamClause.apply)))
else typeParamClauseOnBracket(ownerIsType, ctxBoundsAllowed, allowUnderscore)

private def typeParamClauseOnBracket(
ownerIsType: Boolean,
ctxBoundsAllowed: Boolean,
allowUnderscore: Boolean = true
): Type.ParamClause = {
autoPos(inBrackets(commaSeparated {
typeParam(ownerIsType, ctxBoundsAllowed, allowUnderscore)
}.reduceWith(Type.ParamClause.apply)))
}

def typeParam(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1100,12 +1100,9 @@ object TreeSyntax {
s(beforeBrace, "{ ", f, " }")
case _ => s(args)
}
implicit def syntaxArgss: Syntax[Seq[Term.ArgClause]] = Syntax {
r(_)
}
implicit def syntaxMods: Syntax[Seq[Mod]] = Syntax { mods =>
r(mods, " ")
}

implicit def syntaxArgss: Syntax[Seq[Term.ArgClause]] = Syntax { r(_) }
implicit def syntaxMods: Syntax[Seq[Mod]] = Syntax { r(_, " ") }
private def isUsingOrImplicit(m: Mod): Boolean = m.is[Mod.ParamsType]
private def printParam(t: Term.Param, keepImplicit: Boolean = false): Show.Result = {
val mods = if (keepImplicit) t.mods else t.mods.filterNot(isUsingOrImplicit)
Expand All @@ -1116,9 +1113,7 @@ object TreeSyntax {
}
s(w(mods, " "), nameType, o(" = ", t.default))
}
implicit def syntaxAnnots: Syntax[Seq[Mod.Annot]] = Syntax { annots =>
r(annots, " ")
}
implicit def syntaxAnnots: Syntax[Seq[Mod.Annot]] = Syntax { r(_, " ") }
private def printParams(t: Term.ParamClause, needParens: Boolean = true): Show.Result = {
val v = t.values
val (useParens, mod) = v match {
Expand All @@ -1131,12 +1126,8 @@ object TreeSyntax {
}
w("(", s(mod, r(v.map(printParam(_, mod eq Show.None)), ", ")), ")", useParens)
}
implicit def syntaxParamss: Syntax[Seq[Term.ParamClause]] = Syntax { paramss =>
r(paramss)
}
implicit def syntaxTypeOpt: Syntax[Option[Type]] = Syntax {
o(kw(": "), _)
}
implicit def syntaxMemberParamss: Syntax[Seq[Member.ParamClause]] = Syntax { r(_) }
implicit def syntaxTypeOpt: Syntax[Option[Type]] = Syntax { o(kw(": "), _) }
implicit def syntaxImportee: Syntax[Seq[Importee]] = Syntax {
case Seq(t: Importee.Name) => s(t)
case Seq(t: Importee.Wildcard) => s(t)
Expand Down