aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-04-07 08:27:43 +0200
committerFelix Mulder <felix.mulder@gmail.com>2016-04-07 08:57:05 +0200
commitf43f520a1a6e60d4a6020af97c52dd6c43ea75cd (patch)
tree2d746b4de308c1fc580e00704254b0b843ede5c4
parent844683edb78f45bf37949bd923f56fc98a68c837 (diff)
downloaddotty-f43f520a1a6e60d4a6020af97c52dd6c43ea75cd.tar.gz
dotty-f43f520a1a6e60d4a6020af97c52dd6c43ea75cd.tar.bz2
dotty-f43f520a1a6e60d4a6020af97c52dd6c43ea75cd.zip
Add binding between Symbol and Untyped tree in base context
This commit also adds a printer for use by dottydoc.
-rw-r--r--src/dotty/tools/dotc/config/Printers.scala1
-rw-r--r--src/dotty/tools/dotc/core/Contexts.scala8
-rw-r--r--src/dotty/tools/dotc/typer/Namer.scala16
-rw-r--r--src/dotty/tools/dotc/typer/Typer.scala2
-rw-r--r--test/test/DottyDocParsingTests.scala79
-rw-r--r--test/test/DottyDocTest.scala30
6 files changed, 95 insertions, 41 deletions
diff --git a/src/dotty/tools/dotc/config/Printers.scala b/src/dotty/tools/dotc/config/Printers.scala
index 21147fe6f..fa36ad12c 100644
--- a/src/dotty/tools/dotc/config/Printers.scala
+++ b/src/dotty/tools/dotc/config/Printers.scala
@@ -13,6 +13,7 @@ object Printers {
}
val default: Printer = new Printer
+ val dottydoc: Printer = noPrinter
val core: Printer = noPrinter
val typr: Printer = noPrinter
val constr: Printer = noPrinter
diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala
index a0bb03e50..ad3a0057d 100644
--- a/src/dotty/tools/dotc/core/Contexts.scala
+++ b/src/dotty/tools/dotc/core/Contexts.scala
@@ -550,6 +550,14 @@ object Contexts {
def squashed(p: Phase): Phase = {
allPhases.find(_.period.containsPhaseId(p.id)).getOrElse(NoPhase)
}
+
+ val _docstrings: mutable.Map[Symbol, String] =
+ mutable.Map.empty
+
+ def docstring(sym: Symbol): Option[String] = _docstrings.get(sym)
+
+ def addDocstring(sym: Symbol, doc: Option[String]): Unit =
+ doc.map(d => _docstrings += (sym -> d))
}
/** The essential mutable state of a context base, collected into a common class */
diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala
index de27333d5..82b3b56e9 100644
--- a/src/dotty/tools/dotc/typer/Namer.scala
+++ b/src/dotty/tools/dotc/typer/Namer.scala
@@ -401,19 +401,29 @@ class Namer { typer: Typer =>
val pkg = createPackageSymbol(pcl.pid)
index(pcl.stats)(ctx.fresh.setOwner(pkg.moduleClass))
invalidateCompanions(pkg, Trees.flatten(pcl.stats map expanded))
+ setDocstring(pkg, stat)
ctx
case imp: Import =>
importContext(createSymbol(imp), imp.selectors)
case mdef: DefTree =>
- enterSymbol(createSymbol(mdef))
+ val sym = enterSymbol(createSymbol(mdef))
+ setDocstring(sym, stat)
ctx
case stats: Thicket =>
- for (tree <- stats.toList) enterSymbol(createSymbol(tree))
+ for (tree <- stats.toList) {
+ val sym = enterSymbol(createSymbol(tree))
+ setDocstring(sym, stat)
+ }
ctx
case _ =>
ctx
}
+ def setDocstring(sym: Symbol, tree: Tree)(implicit ctx: Context) = tree match {
+ case t: MemberDef => ctx.base.addDocstring(sym, t.rawComment)
+ case _ => ()
+ }
+
/** Create top-level symbols for statements and enter them into symbol table */
def index(stats: List[Tree])(implicit ctx: Context): Context = {
@@ -859,7 +869,7 @@ class Namer { typer: Typer =>
WildcardType
}
paramFn(typedAheadType(mdef.tpt, tptProto).tpe)
- }
+ }
/** The type signature of a DefDef with given symbol */
def defDefSig(ddef: DefDef, sym: Symbol)(implicit ctx: Context) = {
diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala
index 3b8ada2a8..84abf85e0 100644
--- a/src/dotty/tools/dotc/typer/Typer.scala
+++ b/src/dotty/tools/dotc/typer/Typer.scala
@@ -586,7 +586,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
case _ => false
}
- /** The funcion body to be returned in the closure. Can become a TypedSplice
+ /** The function body to be returned in the closure. Can become a TypedSplice
* of a typed expression if this is necessary to infer a parameter type.
*/
var fnBody = tree.body
diff --git a/test/test/DottyDocParsingTests.scala b/test/test/DottyDocParsingTests.scala
index 2dd5f41d0..32cfaaddf 100644
--- a/test/test/DottyDocParsingTests.scala
+++ b/test/test/DottyDocParsingTests.scala
@@ -6,26 +6,9 @@ import dotty.tools.dotc.ast.Trees._
import org.junit.Assert._
import org.junit.Test
-class DottyDocParsingTests extends DottyTest {
- def checkDocString(actual: Option[String], expected: String): Unit = actual match {
- case Some(str) =>
- assert(str == expected, s"""Docstring: "$str" didn't match expected "$expected"""")
- case None =>
- assert(false, s"""No docstring found, expected: "$expected"""")
- }
-
- private def defaultAssertion: PartialFunction[Tree[Untyped], Unit] = {
- case x => assert(false, "Couldn't match resulting AST to expected AST in: " + x.show)
- }
+class DottyDocParsingTests extends DottyDocTest {
- private def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]) = {
- checkCompile("frontend", source) { (_, ctx) =>
- implicit val c = ctx
- (docAssert orElse defaultAssertion)(ctx.compilationUnit.untpdTree)
- }
- }
-
- @Test def noComment() = {
+ @Test def noComment = {
import dotty.tools.dotc.ast.untpd._
val source = "class Class"
@@ -35,7 +18,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def singleClassInPackage() = {
+ @Test def singleClassInPackage = {
val source =
"""
|package a
@@ -50,7 +33,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def multipleOpenedOnSingleClassInPackage() = {
+ @Test def multipleOpenedOnSingleClassInPackage = {
val source =
"""
|package a
@@ -64,7 +47,7 @@ class DottyDocParsingTests extends DottyTest {
checkDocString(t.rawComment, "/** Hello /* multiple open */ world! */")
}
}
- @Test def multipleClassesInPackage() = {
+ @Test def multipleClassesInPackage = {
val source =
"""
|package a
@@ -86,7 +69,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def singleCaseClassWithoutPackage() = {
+ @Test def singleCaseClassWithoutPackage = {
val source =
"""
|/** Class without package */
@@ -98,7 +81,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def SingleTraitWihoutPackage() = {
+ @Test def SingleTraitWihoutPackage = {
val source = "/** Trait docstring */\ntrait Trait"
checkFrontend(source) {
@@ -106,7 +89,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def multipleTraitsWithoutPackage() = {
+ @Test def multipleTraitsWithoutPackage = {
val source =
"""
|/** Trait1 docstring */
@@ -124,7 +107,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def multipleMixedEntitiesWithPackage() = {
+ @Test def multipleMixedEntitiesWithPackage = {
val source =
"""
|/** Trait1 docstring */
@@ -152,7 +135,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def nestedClass() = {
+ @Test def nestedClass = {
val source =
"""
|/** Outer docstring */
@@ -173,7 +156,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def nestedClassThenOuter() = {
+ @Test def nestedClassThenOuter = {
val source =
"""
|/** Outer1 docstring */
@@ -198,7 +181,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def objects() = {
+ @Test def objects = {
val source =
"""
|package p
@@ -220,7 +203,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def objectsNestedClass() = {
+ @Test def objectsNestedClass = {
val source =
"""
|package p
@@ -252,7 +235,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def packageObject() = {
+ @Test def packageObject = {
val source =
"""
|/** Package object docstring */
@@ -289,7 +272,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def multipleDocStringsBeforeEntity() = {
+ @Test def multipleDocStringsBeforeEntity = {
val source =
"""
|/** First comment */
@@ -305,7 +288,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def multipleDocStringsBeforeAndAfter() = {
+ @Test def multipleDocStringsBeforeAndAfter = {
val source =
"""
|/** First comment */
@@ -324,7 +307,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def valuesWithDocString() = {
+ @Test def valuesWithDocString = {
val source =
"""
|object Object {
@@ -356,7 +339,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def varsWithDocString() = {
+ @Test def varsWithDocString = {
val source =
"""
|object Object {
@@ -388,7 +371,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def defsWithDocString() = {
+ @Test def defsWithDocString = {
val source =
"""
|object Object {
@@ -420,7 +403,7 @@ class DottyDocParsingTests extends DottyTest {
}
}
- @Test def typesWithDocString() = {
+ @Test def typesWithDocString = {
val source =
"""
|object Object {
@@ -451,4 +434,26 @@ class DottyDocParsingTests extends DottyTest {
}
}
}
+
+ @Test def defInnerClass = {
+ val source =
+ """
+ |object Foo {
+ | def foo() = {
+ | /** Innermost */
+ | class Innermost
+ | }
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) =>
+ o.impl.body match {
+ case (foo: MemberDef) :: Nil =>
+ expectNoDocString(foo.rawComment)
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
} /* End class */
diff --git a/test/test/DottyDocTest.scala b/test/test/DottyDocTest.scala
new file mode 100644
index 000000000..eed5d6cd0
--- /dev/null
+++ b/test/test/DottyDocTest.scala
@@ -0,0 +1,30 @@
+package test
+
+import dotty.tools.dotc.ast.Trees._
+import dotty.tools.dotc.core.Contexts.Context
+
+trait DottyDocTest extends DottyTest {
+ def checkDocString(actual: Option[String], expected: String): Unit = actual match {
+ case Some(str) =>
+ assert(str == expected, s"""Docstring: "$str" didn't match expected "$expected"""")
+ case None =>
+ assert(false, s"""No docstring found, expected: "$expected"""")
+ }
+
+ def expectNoDocString(doc: Option[String]): Unit =
+ doc.fold(()) { d => assert(false, s"""Expected not to find a docstring, but found: "$d"""") }
+
+ def defaultAssertion: PartialFunction[Any, Unit] = {
+ case t: Tree[Untyped] =>
+ assert(false, s"Couldn't match resulting AST to expected AST in: ${t.show}")
+ case x =>
+ assert(false, s"Couldn't match resulting AST to expected AST in: $x")
+ }
+
+ def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]) = {
+ checkCompile("frontend", source) { (_, ctx) =>
+ implicit val c = ctx
+ (docAssert orElse defaultAssertion)(ctx.compilationUnit.untpdTree)
+ }
+ }
+}