diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-08-24 17:23:32 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2016-10-06 17:08:00 +0200 |
commit | 30d8135430614573549974e9a6013e324b3d6f8a (patch) | |
tree | 1cd915ba461a14b817eb4b019e0fd12628dea831 /src/dotty/tools/dotc/core/Comments.scala | |
parent | 77dc769899a37b8f414befa6468d62413b1655fc (diff) | |
download | dotty-30d8135430614573549974e9a6013e324b3d6f8a.tar.gz dotty-30d8135430614573549974e9a6013e324b3d6f8a.tar.bz2 dotty-30d8135430614573549974e9a6013e324b3d6f8a.zip |
Add `Comments` object instead of `Scanners.Comment` case class
Diffstat (limited to 'src/dotty/tools/dotc/core/Comments.scala')
-rw-r--r-- | src/dotty/tools/dotc/core/Comments.scala | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/core/Comments.scala b/src/dotty/tools/dotc/core/Comments.scala new file mode 100644 index 000000000..bf1f25537 --- /dev/null +++ b/src/dotty/tools/dotc/core/Comments.scala @@ -0,0 +1,77 @@ +package dotty.tools +package dotc +package core + +import dotc.ast.{ untpd, tpd } +import Decorators._ +import Symbols._ +import Contexts.Context +import Flags.EmptyFlags +import dotc.util.SourceFile +import dotc.util.Positions.Position +import dotc.parsing.Parsers.Parser +import dotty.tools.dottydoc.model.comment.CommentUtils._ + +object Comments { + + case class Comment(pos: Position, raw: String)(implicit ctx: Context) { + val isDocComment = raw.startsWith("/**") + + private[this] lazy val sections = tagIndex(raw) + + private def fold[A](z: A)(op: => A) = if (!isDocComment) z else op + + lazy val usecases = fold(List.empty[UseCase]) { + sections + .filter { startsWithTag(raw, _, "@usecase") } + .map { case (start, end) => decomposeUseCase(start, end) } + } + + /** Turns a usecase section into a UseCase, with code changed to: + * {{{ + * // From: + * def foo: A + * // To: + * def foo: A = ??? + * }}} + */ + private def decomposeUseCase(start: Int, end: Int): UseCase = { + val codeStart = skipWhitespace(raw, start + "@usecase".length) + val codeEnd = skipToEol(raw, codeStart) + val code = raw.substring(codeStart, codeEnd) + val codePos = Position(codeStart, codeEnd) + val commentStart = skipLineLead(raw, codeEnd + 1) min end + val comment = "/** " + raw.substring(commentStart, end) + "*/" + val commentPos = Position(commentStart, end) + + UseCase(Comment(commentPos, comment), code + " = ???", codePos) + } + } + + case class UseCase(comment: Comment, code: String, codePos: Position)(implicit ctx: Context) { + /** Entered by Namer */ + var symbol: Symbol = _ + + lazy val untpdCode: untpd.Tree = { + val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start, EmptyFlags) + + tree match { + case tree: untpd.DefDef => tree + case _ => + ctx.error("proper def was not found in `@usecase`", codePos) + tree + } + } + + /** Set by typer calling `typeTree` */ + var tpdCode: tpd.DefDef = _ + + def typeTree()(implicit ctx: Context): Unit = untpdCode match { + case df: untpd.DefDef => ctx.typer.typedDefDef(df, symbol) match { + case tree: tpd.DefDef => tpdCode = tree + case _ => ctx.error("proper def was not found in `@usecase`", codePos) + } + case _ => ctx.error("proper def was not found in `@usecase`", codePos) + } + } +} |