Skip to content

Commit 212c756

Browse files
committed
Add initial support for raw docstrings in ASTs
1 parent a509267 commit 212c756

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ object Trees {
317317
private[ast] def rawMods: Modifiers[T] =
318318
if (myMods == null) genericEmptyModifiers else myMods
319319

320+
private[this] var myComment: Option[String] = None
321+
322+
def rawComment: Option[String] = myComment
323+
320324
def withMods(mods: Modifiers[Untyped]): ThisTree[Untyped] = {
321325
val tree = if (myMods == null || (myMods == mods)) this else clone.asInstanceOf[MemberDef[Untyped]]
322326
tree.setMods(mods)
@@ -325,6 +329,11 @@ object Trees {
325329

326330
def withFlags(flags: FlagSet): ThisTree[Untyped] = withMods(Modifiers(flags))
327331

332+
def withComment(cmt: Option[String]): ThisTree[Untyped] = {
333+
myComment = cmt
334+
asInstanceOf[ThisTree[Untyped]]
335+
}
336+
328337
protected def setMods(mods: Modifiers[T @uncheckedVariance]) = myMods = mods
329338

330339
override def envelope: Position = rawMods.pos.union(pos).union(initialPos)

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,10 +1874,14 @@ object Parsers {
18741874
val tparams = typeParamClauseOpt(ParamOwner.Class)
18751875
val cmods = constrModsOpt()
18761876
val vparamss = paramClauses(name, mods is Case)
1877+
18771878
makeConstructor(tparams, vparamss).withMods(cmods)
18781879
}
18791880
val templ = templateOpt(constr)
1880-
TypeDef(name, templ).withMods(mods)
1881+
1882+
TypeDef(name, templ)
1883+
.withMods(mods)
1884+
.withComment(in.getDocString())
18811885
}
18821886

18831887
/** ConstrMods ::= AccessModifier
@@ -1896,7 +1900,10 @@ object Parsers {
18961900
def objectDef(mods: Modifiers): ModuleDef = {
18971901
val name = ident()
18981902
val template = templateOpt(emptyConstructor())
1899-
ModuleDef(name, template).withMods(mods)
1903+
1904+
ModuleDef(name, template)
1905+
.withMods(mods)
1906+
.withComment(in.getDocString())
19001907
}
19011908

19021909
/* -------- TEMPLATES ------------------------------------------- */

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ object Scanners {
182182
*/
183183
var revComments: List[Comment] = Nil
184184

185+
/** The currently closest docstring, replaced every time a new docstring is
186+
* encountered
187+
*/
188+
var closestDocString: Option[Comment] = None
189+
190+
/** Returns `closestDocString`'s raw string and sets it to `None` */
191+
def getDocString(): Option[String] = {
192+
val current = closestDocString.map(_.chrs)
193+
closestDocString = None
194+
current
195+
}
196+
185197
/** A buffer for comments */
186198
val commentBuf = new StringBuilder
187199

@@ -528,8 +540,7 @@ object Scanners {
528540
}
529541

530542
private def skipComment(): Boolean = {
531-
def appendToComment(ch: Char) =
532-
if (keepComments) commentBuf.append(ch)
543+
def appendToComment(ch: Char) = commentBuf.append(ch)
533544
def nextChar() = {
534545
appendToComment(ch)
535546
Scanner.this.nextChar()
@@ -556,11 +567,17 @@ object Scanners {
556567
}
557568
val start = lastCharOffset
558569
def finishComment(): Boolean = {
559-
if (keepComments) {
560-
val pos = Position(start, charOffset, start)
561-
nextChar()
562-
revComments = Comment(pos, flushBuf(commentBuf)) :: revComments
563-
}
570+
val pos = Position(start, charOffset, start)
571+
nextChar()
572+
val comment = Comment(pos, flushBuf(commentBuf))
573+
574+
closestDocString =
575+
if (comment.isDocComment) Option(comment)
576+
else closestDocString
577+
578+
if (keepComments)
579+
revComments = comment :: revComments
580+
564581
true
565582
}
566583
nextChar()

0 commit comments

Comments
 (0)