Skip to content

sourcepath: Handle end markers in outline parsing #7717

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

Merged
merged 2 commits into from
Dec 11, 2019
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
17 changes: 11 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,17 @@ object Parsers {
def skipBraces(): Unit = {
accept(if (in.token == INDENT) INDENT else LBRACE)
var openBraces = 1
while (in.token != EOF && openBraces > 0)
skipBracesHook() getOrElse {
if (in.token == LBRACE || in.token == INDENT) openBraces += 1
else if (in.token == RBRACE || in.token == OUTDENT) openBraces -= 1
in.nextToken()
}
val savedCheckEndMarker = in.checkEndMarker
try
in.checkEndMarker = false
while (in.token != EOF && openBraces > 0)
skipBracesHook() getOrElse {
if (in.token == LBRACE || in.token == INDENT) openBraces += 1
else if (in.token == RBRACE || in.token == OUTDENT) openBraces -= 1
in.nextToken()
}
finally
in.checkEndMarker = savedCheckEndMarker
}
}

Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ object Scanners {
/** the base of a number */
var base: Int = 0

/** Set to false to disable end marker alignment checks, used for outline parsing. */
var checkEndMarker: Boolean = true

def copyFrom(td: TokenData): Unit = {
this.token = td.token
this.offset = td.offset
Expand Down Expand Up @@ -331,10 +334,12 @@ object Scanners {
def endMarkerScope[T](tag: EndMarkerTag)(op: => T): T =
val saved = openEndMarkers
openEndMarkers = (tag, currentRegion.indentWidth) :: openEndMarkers
try op finally openEndMarkers = saved
try op
finally openEndMarkers = saved

/** If this token and the next constitute an end marker, skip them and check they
* align with an opening construct with the same end marker tag.
* align with an opening construct with the same end marker tag,
* unless `checkEndMarker` is false.
*/
protected def skipEndMarker(width: IndentWidth): Unit =
if next.token == IDENTIFIER && next.name == nme.end then
Expand All @@ -348,8 +353,9 @@ object Scanners {
openEndMarkers = rest
checkAligned()
case _ =>
lexical.println(i"misaligned end marker $tag, $width, $openEndMarkers")
errorButContinue("misaligned end marker", start)
if checkEndMarker
lexical.println(i"misaligned end marker $tag, $width, $openEndMarkers")
errorButContinue("misaligned end marker", start)

val skipTo = lookahead.charOffset
lookahead.nextToken()
Expand Down
4 changes: 1 addition & 3 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class CompilationTests extends ParallelTesting {
compileFile("tests/pos-scala2/rewrites.scala", scala2CompatMode.and("-rewrite")).copyToTarget(),
compileFile("tests/pos-special/utf8encoded.scala", explicitUTF8),
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
compileFile("tests/pos-special/sourcepath/outer/Test.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFile("tests/pos-special/sourcepath/outer/Test2.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFile("tests/pos-special/sourcepath/outer/Test3.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFile("tests/pos-special/sourcepath/outer/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-feature")),
compileFilesInDir("tests/pos-special/spec-t5545", defaultOptions),
Expand Down
7 changes: 7 additions & 0 deletions tests/pos-special/sourcepath/outer/indent2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package outer
import nested._

object indent2 {
val x2: Int = indent1.inner.x
val y2: Int = indent1.y
}
2 changes: 1 addition & 1 deletion tests/pos-special/sourcepath/outer/nested/BC.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package outer.nested


case class B(x: Int) extends A(x)
case class B(override val x: Int) extends A(x)

case class C(s: String) extends A(s.length)
9 changes: 9 additions & 0 deletions tests/pos-special/sourcepath/outer/nested/indent1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package outer
package nested

object indent1
object inner with
def x: Int = 1
end inner
val y: Int = 2
end indent1