Skip to content

Commit 39c2da6

Browse files
committed
Add Comments object instead of Scanners.Comment case class
1 parent 073d045 commit 39c2da6

File tree

7 files changed

+128
-56
lines changed

7 files changed

+128
-56
lines changed

dottydoc/src/dotty/tools/dottydoc/model/comment/CommentExpander.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ trait CommentExpander {
7777
*/
7878
def cookedDocComment(sym: Symbol, docStr: String = "")(implicit ctx: Context): String = cookedDocComments.getOrElseUpdate(sym, {
7979
var ownComment =
80-
if (docStr.length == 0) ctx.docbase.docstring(sym).map(c => template(c.chrs)).getOrElse("")
80+
if (docStr.length == 0) ctx.docbase.docstring(sym).map(c => template(c.raw)).getOrElse("")
8181
else template(docStr)
8282
ownComment = replaceInheritDocToInheritdoc(ownComment)
8383

@@ -287,7 +287,7 @@ trait CommentExpander {
287287
def defineVariables(sym: Symbol)(implicit ctx: Context) = {
288288
val Trim = "(?s)^[\\s&&[^\n\r]]*(.*?)\\s*$".r
289289

290-
val raw = ctx.docbase.docstring(sym).map(_.chrs).getOrElse("")
290+
val raw = ctx.docbase.docstring(sym).map(_.raw).getOrElse("")
291291
defs(sym) ++= defines(raw).map {
292292
str => {
293293
val start = skipWhitespace(str, "@define".length)

src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package ast
44

55
import core._
66
import Types._, Names._, Flags._, util.Positions._, Contexts._, Constants._, SymDenotations._, Symbols._
7-
import Denotations._, StdNames._
7+
import Denotations._, StdNames._, Comments._
88
import annotation.tailrec
99
import language.higherKinds
1010
import collection.IndexedSeqOptimized
@@ -15,7 +15,6 @@ import printing.Printer
1515
import util.{Stats, Attachment, DotClass}
1616
import annotation.unchecked.uncheckedVariance
1717
import language.implicitConversions
18-
import parsing.Scanners.Comment
1918

2019
object Trees {
2120

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package dotty.tools
2+
package dotc
3+
package core
4+
5+
import dotc.ast.{ untpd, tpd }
6+
import Decorators._
7+
import Symbols._
8+
import Contexts.Context
9+
import Flags.EmptyFlags
10+
import dotc.util.SourceFile
11+
import dotc.util.Positions.Position
12+
import dotc.parsing.Parsers.Parser
13+
import dotty.tools.dottydoc.model.comment.CommentUtils._
14+
15+
object Comments {
16+
17+
case class Comment(pos: Position, raw: String)(implicit ctx: Context) {
18+
val isDocComment = raw.startsWith("/**")
19+
20+
private[this] lazy val sections = tagIndex(raw)
21+
22+
private def fold[A](z: A)(op: => A) = if (!isDocComment) z else op
23+
24+
lazy val usecases = fold(List.empty[UseCase]) {
25+
sections
26+
.filter { startsWithTag(raw, _, "@usecase") }
27+
.map { case (start, end) => decomposeUseCase(start, end) }
28+
}
29+
30+
/** Turns a usecase section into a UseCase, with code changed to:
31+
* {{{
32+
* // From:
33+
* def foo: A
34+
* // To:
35+
* def foo: A = ???
36+
* }}}
37+
*/
38+
private def decomposeUseCase(start: Int, end: Int): UseCase = {
39+
val codeStart = skipWhitespace(raw, start + "@usecase".length)
40+
val codeEnd = skipToEol(raw, codeStart)
41+
val code = raw.substring(codeStart, codeEnd)
42+
val codePos = Position(codeStart, codeEnd)
43+
val commentStart = skipLineLead(raw, codeEnd + 1) min end
44+
val comment = "/** " + raw.substring(commentStart, end) + "*/"
45+
val commentPos = Position(commentStart, end)
46+
47+
UseCase(Comment(commentPos, comment), code + " = ???", codePos)
48+
}
49+
}
50+
51+
case class UseCase(comment: Comment, code: String, codePos: Position)(implicit ctx: Context) {
52+
/** Entered by Namer */
53+
var symbol: Symbol = _
54+
55+
lazy val untpdCode: untpd.Tree = {
56+
val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start, EmptyFlags)
57+
58+
tree match {
59+
case tree: untpd.DefDef => tree
60+
case _ =>
61+
ctx.error("proper def was not found in `@usecase`", codePos)
62+
tree
63+
}
64+
}
65+
66+
/** Set by typer calling `typeTree` */
67+
var tpdCode: tpd.DefDef = _
68+
69+
def typeTree()(implicit ctx: Context): Unit = untpdCode match {
70+
case df: untpd.DefDef => ctx.typer.typedDefDef(df, symbol) match {
71+
case tree: tpd.DefDef => tpdCode = tree
72+
case _ => ctx.error("proper def was not found in `@usecase`", codePos)
73+
}
74+
case _ => ctx.error("proper def was not found in `@usecase`", codePos)
75+
}
76+
}
77+
}

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Scopes._
1313
import NameOps._
1414
import Uniques._
1515
import SymDenotations._
16+
import Comments._
1617
import Flags.ParamAccessor
1718
import util.Positions._
1819
import ast.Trees._
@@ -29,7 +30,6 @@ import printing._
2930
import config.{Settings, ScalaSettings, Platform, JavaPlatform, SJSPlatform}
3031
import language.implicitConversions
3132
import DenotTransformers.DenotTransformer
32-
import parsing.Scanners.Comment
3333
import xsbti.AnalysisCallback
3434

3535
object Contexts {

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import StdNames._
1919
import util.Positions._
2020
import Constants._
2121
import ScriptParsers._
22+
import Comments._
2223
import scala.annotation.{tailrec, switch}
2324
import util.DotClass
2425
import rewrite.Rewrites.patch
25-
import Scanners.Comment
2626

2727
object Parsers {
2828

src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package parsing
44

55
import core.Names._, core.Contexts._, core.Decorators._, util.Positions._
6-
import core.StdNames._
6+
import core.StdNames._, core.Comments._
77
import util.SourceFile
88
import java.lang.Character.isDigit
99
import scala.reflect.internal.Chars._
@@ -22,10 +22,6 @@ object Scanners {
2222
/** An undefined offset */
2323
val NoOffset: Offset = -1
2424

25-
case class Comment(pos: Position, chrs: String) {
26-
def isDocComment = chrs.startsWith("/**")
27-
}
28-
2925
type Token = Int
3026

3127
trait TokenData {

0 commit comments

Comments
 (0)