diff options
Diffstat (limited to 'src/scaladoc')
5 files changed, 143 insertions, 58 deletions
diff --git a/src/scaladoc/scala/tools/nsc/doc/ScaladocAnalyzer.scala b/src/scaladoc/scala/tools/nsc/doc/ScaladocAnalyzer.scala index 8ea8c4deff..2152ce234a 100644 --- a/src/scaladoc/scala/tools/nsc/doc/ScaladocAnalyzer.scala +++ b/src/scaladoc/scala/tools/nsc/doc/ScaladocAnalyzer.scala @@ -101,52 +101,6 @@ trait ScaladocAnalyzer extends Analyzer { abstract class ScaladocSyntaxAnalyzer[G <: Global](val global: G) extends SyntaxAnalyzer { import global._ - class ScaladocJavaUnitParser(unit: CompilationUnit) extends { - override val in = new ScaladocJavaUnitScanner(unit) - } with JavaUnitParser(unit) { } - - class ScaladocJavaUnitScanner(unit: CompilationUnit) extends JavaUnitScanner(unit) { - /** buffer for the documentation comment - */ - var docBuffer: StringBuilder = null - - /** add the given character to the documentation buffer - */ - protected def putDocChar(c: Char) { - if (docBuffer ne null) docBuffer.append(c) - } - - override protected def skipComment(): Boolean = { - if (in.ch == '/') { - do { - in.next - } while ((in.ch != CR) && (in.ch != LF) && (in.ch != SU)) - true - } else if (in.ch == '*') { - docBuffer = null - in.next - val scaladoc = ("/**", "*/") - if (in.ch == '*') - docBuffer = new StringBuilder(scaladoc._1) - do { - do { - if (in.ch != '*' && in.ch != SU) { - in.next; putDocChar(in.ch) - } - } while (in.ch != '*' && in.ch != SU) - while (in.ch == '*') { - in.next; putDocChar(in.ch) - } - } while (in.ch != '/' && in.ch != SU) - if (in.ch == '/') in.next - else incompleteInputError("unclosed comment") - true - } else { - false - } - } - } - class ScaladocUnitScanner(unit0: CompilationUnit, patches0: List[BracePatch]) extends UnitScanner(unit0, patches0) { private var docBuffer: StringBuilder = null // buffer for comments (non-null while scanning) @@ -259,4 +213,77 @@ abstract class ScaladocSyntaxAnalyzer[G <: Global](val global: G) extends Syntax else trees } } + + class ScaladocJavaUnitScanner(unit: CompilationUnit) extends JavaUnitScanner(unit) { + + private val docBuffer: StringBuilder = new StringBuilder + private var inDocComment = false + private var docStart: Int = 0 + private var lastDoc: DocComment = null + + // get last doc comment + def flushDoc(): DocComment = try lastDoc finally lastDoc = null + + override protected def putCommentChar(): Unit = { + if (inDocComment) docBuffer append in.ch + in.next + } + + override protected def skipBlockComment(isDoc: Boolean): Unit = { + // condition is true when comment is entered the first time, + // i.e. immediately after "/*" and when current character is "*" + if (!inDocComment && isDoc) { + docBuffer append "/*" + docStart = currentPos.start + inDocComment = true + } + super.skipBlockComment(isDoc) + } + + override protected def skipComment(): Boolean = { + val skipped = super.skipComment() + if (skipped && inDocComment) { + val raw = docBuffer.toString + val position = Position.range(unit.source, docStart, docStart, in.cpos) + lastDoc = DocComment(raw, position) + signalParsedDocComment(raw, position) + docBuffer.setLength(0) // clear buffer + inDocComment = false + true + } else { + skipped + } + } + + } + + class ScaladocJavaUnitParser(unit: CompilationUnit) extends { + override val in = new ScaladocJavaUnitScanner(unit) + } with JavaUnitParser(unit) { + + override def joinComment(trees: => List[Tree]): List[Tree] = { + val doc = in.flushDoc() + + if ((doc ne null) && doc.raw.length > 0) { + log(s"joinComment(doc=$doc)") + val joined = trees map { t => + DocDef(doc, t) setPos { + if (t.pos.isDefined) { + val pos = doc.pos.withEnd(t.pos.end) + pos.makeTransparent + } else { + t.pos + } + } + } + joined.find(_.pos.isOpaqueRange) foreach { main => + val mains = List(main) + joined foreach { t => if (t ne main) ensureNonOverlapping(t, mains) } + } + joined + } else { + trees + } + } + } } diff --git a/src/scaladoc/scala/tools/nsc/doc/ScaladocGlobal.scala b/src/scaladoc/scala/tools/nsc/doc/ScaladocGlobal.scala index 10bfe6f94b..625d074df5 100644 --- a/src/scaladoc/scala/tools/nsc/doc/ScaladocGlobal.scala +++ b/src/scaladoc/scala/tools/nsc/doc/ScaladocGlobal.scala @@ -13,6 +13,7 @@ trait ScaladocGlobalTrait extends Global { override val useOffsetPositions = false override def newUnitParser(unit: CompilationUnit) = new syntaxAnalyzer.ScaladocUnitParser(unit, Nil) + override def newJavaUnitParser(unit: CompilationUnit) = new syntaxAnalyzer.ScaladocJavaUnitParser(unit) override lazy val syntaxAnalyzer = new ScaladocSyntaxAnalyzer[outer.type](outer) { val runsAfter = List[String]() @@ -40,6 +41,8 @@ class ScaladocGlobal(settings: doc.Settings, reporter: Reporter) extends Global( phasesSet += analyzer.typerFactory } override def forScaladoc = true + override def createJavadoc = true + override lazy val analyzer = new { val global: ScaladocGlobal.this.type = ScaladocGlobal.this } with ScaladocAnalyzer diff --git a/src/scaladoc/scala/tools/nsc/doc/html/page/IndexScript.scala b/src/scaladoc/scala/tools/nsc/doc/html/page/IndexScript.scala index 6b24c0f568..8f58a7b845 100644 --- a/src/scaladoc/scala/tools/nsc/doc/html/page/IndexScript.scala +++ b/src/scaladoc/scala/tools/nsc/doc/html/page/IndexScript.scala @@ -10,7 +10,6 @@ package page import scala.tools.nsc.doc import scala.tools.nsc.doc.model.{Package, DocTemplateEntity} import scala.tools.nsc.doc.html.{Page, HtmlFactory} -import scala.util.parsing.json.{JSONObject, JSONArray, JSONType} class IndexScript(universe: doc.Universe) extends Page { import model._ @@ -21,7 +20,7 @@ class IndexScript(universe: doc.Universe) extends Page { override def writeFor(site: HtmlFactory) { writeFile(site) { - _.write("Index.PACKAGES = " + packages.toString() + ";") + _.write(s"Index.PACKAGES = $packages;") } } @@ -30,7 +29,7 @@ class IndexScript(universe: doc.Universe) extends Page { case (pack, templates) => { val merged = mergeByQualifiedName(templates) - val ary = merged.keys.toList.sortBy(_.toLowerCase).map { key => + val ary = merged.keys.toVector.sortBy(_.toLowerCase).map { key => /** One pair is generated for the class/trait and one for the * companion object, both will have the same {"name": key} * @@ -44,7 +43,7 @@ class IndexScript(universe: doc.Universe) extends Page { Seq( kind -> relativeLinkTo(t), "kind" -> kind, - s"members_$kind" -> membersToJSON(t.members.filter(!_.isShadowedOrAmbiguousImplicit), t), + s"members_$kind" -> membersToJSON(t.members.toVector.filter(!_.isShadowedOrAmbiguousImplicit), t), "shortDescription" -> shortDesc(t)) } @@ -58,18 +57,18 @@ class IndexScript(universe: doc.Universe) extends Page { JSONObject(Map(pairs : _*)) } - def mergeByQualifiedName(source: List[DocTemplateEntity]) = { - var result = Map[String, List[DocTemplateEntity]]() + private def mergeByQualifiedName(source: List[DocTemplateEntity]): collection.mutable.Map[String, List[DocTemplateEntity]] = { + val result = collection.mutable.Map[String, List[DocTemplateEntity]]() for (t <- source) { val k = t.qualifiedName - result += k -> (result.getOrElse(k, List()) :+ t) + result += k -> (result.getOrElse(k, Nil) :+ t) } result } - def allPackages = { + def allPackages: List[Package] = { def f(parent: Package): List[Package] = { parent.packages.flatMap( p => f(p) :+ p @@ -78,7 +77,7 @@ class IndexScript(universe: doc.Universe) extends Page { f(universe.rootPackage).sortBy(_.toString) } - def allPackagesWithTemplates = { + def allPackagesWithTemplates: Map[Package, List[DocTemplateEntity]] = { Map(allPackages.map((key) => { key -> key.templates.collect { case t: DocTemplateEntity if !t.isPackage && !universe.settings.hardcoded.isExcluded(t.qualifiedName) => t @@ -92,8 +91,8 @@ class IndexScript(universe: doc.Universe) extends Page { } /** Returns the json representation of the supplied members */ - def membersToJSON(entities: List[MemberEntity], parent: DocTemplateEntity): JSONType = - JSONArray(entities map (memberToJSON(_, parent))) + def membersToJSON(entities: Vector[MemberEntity], parent: DocTemplateEntity): JSONArray = + JSONArray(entities.map(memberToJSON(_, parent))) private def memberToJSON(mbr: MemberEntity, parent: DocTemplateEntity): JSONObject = { /** This function takes a member and gets eventual parameters and the diff --git a/src/scaladoc/scala/tools/nsc/doc/html/page/JSON.scala b/src/scaladoc/scala/tools/nsc/doc/html/page/JSON.scala new file mode 100644 index 0000000000..5f6cb7e799 --- /dev/null +++ b/src/scaladoc/scala/tools/nsc/doc/html/page/JSON.scala @@ -0,0 +1,56 @@ +package scala.tools.nsc.doc.html.page + +import JSONFormat.format + +private[page] object JSONFormat { + + def format(x: Any): String = x match { + case s: String => s"""\"${quoteString(s)}\"""" + case jo: JSONObject => jo.toString + case ja: JSONArray => ja.toString + case other => throw new UnsupportedOperationException(s"Value $other of class ${other.getClass} cannot be formatted.") + } + + /** This function can be used to properly quote Strings for JSON output. */ + def quoteString(s: String): String = { + val len: Int = s.length + val buf = new StringBuilder(len + len/4) + var i: Int = 0 + while (i < len) { + s.apply(i) match { + case '"' => buf ++= "\\\"" + case '\\' => buf ++= "\\\\" + case '/' => buf ++= "\\/" + case '\b' => buf ++= "\\b" + case '\f' => buf ++= "\\f" + case '\n' => buf ++= "\\n" + case '\r' => buf ++= "\\r" + case '\t' => buf ++= "\\t" + /* We'll unicode escape any control characters. These include: + * 0x00 -> 0x1f : ASCII Control (C0 Control Codes) + * 0x7f : ASCII DELETE + * 0x80 -> 0x9f : C1 Control Codes + * + * Per RFC4627, section 2.5, we're not technically required to + * encode the C1 codes, but we do to be safe. + */ + case c if ((c >= '\u0000' && c <= '\u001f') || (c >= '\u007f' && c <= '\u009f')) => + val cint = c.toInt + buf ++= f"\\u$cint%04x" + case c => buf += c + } + i += 1 + } + buf.toString() + } +} + +/** Represents a JSON Object (map). */ +private[page] case class JSONObject(obj: Map[String,Any]) { + override def toString = obj.map({ case (k,v) => format(k) + " : " + format(v) }).mkString("{", ", ", "}") +} + +/** Represents a JSON Array (vector). */ +private[page] case class JSONArray(vector: Vector[Any]) { + override def toString = vector.map(format).mkString("[", ", ", "]") +} diff --git a/src/scaladoc/scala/tools/partest/ScaladocModelTest.scala b/src/scaladoc/scala/tools/partest/ScaladocModelTest.scala index 1ad3b3ff2b..44c1146a14 100644 --- a/src/scaladoc/scala/tools/partest/ScaladocModelTest.scala +++ b/src/scaladoc/scala/tools/partest/ScaladocModelTest.scala @@ -81,7 +81,7 @@ abstract class ScaladocModelTest extends DirectTest { private[this] var settings: doc.Settings = null // create a new scaladoc compiler - private[this] def newDocFactory: DocFactory = { + def newDocFactory: DocFactory = { settings = new doc.Settings(_ => ()) settings.scaladocQuietRun = true // yaay, no more "model contains X documentable templates"! val args = extraSettings + " " + scaladocSettings |