From 30d8135430614573549974e9a6013e324b3d6f8a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 24 Aug 2016 17:23:32 +0200 Subject: Add `Comments` object instead of `Scanners.Comment` case class --- src/dotty/tools/dotc/core/Comments.scala | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/dotty/tools/dotc/core/Comments.scala (limited to 'src/dotty/tools/dotc/core/Comments.scala') 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("", 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) + } + } +} -- cgit v1.2.3