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: terminate generator on outdent #3891

Merged
merged 2 commits into from
Aug 9, 2024
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 @@ -456,9 +456,11 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) {
@inline
def acceptIfStatSep(): Boolean = acceptIf(StatSep)

@inline
def isImplicitStatSep(): Boolean = prev[Indentation.Outdent]

def acceptStatSep(): Boolean = {
val ok = acceptIfStatSep() || isAtEndMarker() ||
prev[Indentation.Outdent] && !at[Indentation.Outdent]
val ok = acceptIfStatSep() || isAtEndMarker() || isImplicitStatSep() && !at[Indentation.Outdent]
if (!ok) syntaxErrorExpected[Semicolon]
ok
}
Expand Down Expand Up @@ -2463,10 +2465,14 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) {
private def enumeratorGuardOnIf() = autoPos(Enumerator.Guard(guardOnIf()))

def enumerators(): List[Enumerator] = listBy[Enumerator] { enums =>
def notEnumsEnd(token: Token): Boolean = !token.isAny[Indentation.Outdent, KwDo, CloseDelim]
doWhile {
enums += enumerator(isFirst = enums.isEmpty)
while (at[Token.KwIf]) enums += enumeratorGuardOnIf()
}(StatSep(currToken) && nextIf(!peekToken.isAny[Indentation.Outdent, KwDo, CloseDelim]))
} {
if (StatSep(currToken)) nextIf(notEnumsEnd(peekToken))
else isImplicitStatSep() && notEnumsEnd(currToken)
}
}

private def enumerator(isFirst: Boolean = false): Enumerator = currToken match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4108,4 +4108,39 @@ class ControlSyntaxSuite extends BaseDottySuite {
runTestAssert[Stat](code, layout)(tree)
}

test("indent in enumerator") {
val code = """|object a:
| val abstractTypeNames =
| for (
| parent <-
| parents;
| mbr <- parent.abstractTypeMembers if qualifies(mbr.symbol))
| yield mbr.name.asTypeName
|""".stripMargin
val layout =
"object a { val abstractTypeNames = for (parent <- parents; mbr <- parent.abstractTypeMembers; if qualifies(mbr.symbol)) yield mbr.name.asTypeName }"
val tree = Defn.Object(
Nil,
tname("a"),
tpl(Defn.Val(
Nil,
List(Pat.Var(tname("abstractTypeNames"))),
None,
Term.ForYield(
List(
Enumerator.Generator(Pat.Var(tname("parent")), tname("parents")),
Enumerator.Generator(
Pat.Var(tname("mbr")),
Term.Select(tname("parent"), tname("abstractTypeMembers"))
),
Enumerator
.Guard(Term.Apply(tname("qualifies"), List(Term.Select(tname("mbr"), tname("symbol")))))
),
Term.Select(Term.Select(tname("mbr"), tname("name")), tname("asTypeName"))
)
))
)
runTestAssert[Stat](code, layout)(tree)
}

}
Loading