Skip to content

Commit 46208b0

Browse files
committed
Snippet compiler PoC
1 parent 55da939 commit 46208b0

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dotty.tools.scaladoc
2+
package snippets
3+
4+
import dotty.tools.scaladoc.DocContext
5+
6+
class SnippetChecker(
7+
private val compiler: SnippetCompiler = SnippetCompiler(),
8+
private val wrapper: SnippetWrapper = SnippetWrapper()
9+
):
10+
def checkSnippet(snippet: String) = {
11+
val wrapped = wrapper.wrap(snippet)
12+
compiler.compile(snippet)
13+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package dotty.tools.scaladoc
2+
package snippets
3+
4+
import dotty.tools.io.{AbstractFile, VirtualDirectory}
5+
import dotty.tools.dotc.interactive.InteractiveDriver
6+
import dotty.tools.dotc.interactive.Interactive
7+
import dotty.tools.dotc.interactive.InteractiveCompiler
8+
import dotty.tools.dotc.core.Contexts.Context
9+
import dotty.tools.dotc.config.Settings.Setting._
10+
import dotty.tools.dotc.interfaces.SourcePosition
11+
import dotty.tools.dotc.ast.Trees.Tree
12+
import dotty.tools.dotc.interfaces.{SourceFile => ISourceFile}
13+
import dotty.tools.dotc.reporting.{ Diagnostic, StoreReporter }
14+
import dotty.tools.dotc.parsing.Parsers.Parser
15+
import dotty.tools.dotc.{ Compiler, Run }
16+
import dotty.tools.io.{AbstractFile, VirtualDirectory}
17+
import dotty.tools.repl.AbstractFileClassLoader
18+
import dotty.tools.dotc.util.SourceFile
19+
20+
class SnippetCompiler(
21+
classpath: String = System.getProperty("java.class.path"), //Probably needs to be done better
22+
val scalacOptions: String = "",
23+
target: AbstractFile = new VirtualDirectory("(memory)")
24+
):
25+
26+
private def newDriver: InteractiveDriver = {
27+
val defaultFlags =
28+
List("-color:never", "-unchecked", "-deprecation", "-Ximport-suggestion-timeout", "0")
29+
val options = scalacOptions.split("\\s+").toList
30+
val settings =
31+
options ::: defaultFlags ::: "-classpath" :: classpath :: Nil
32+
new InteractiveDriver(settings)
33+
}
34+
35+
private val driver = newDriver
36+
37+
private val scala3Compiler = new Compiler
38+
39+
private def newRun(using ctx: Context): Run = scala3Compiler.newRun
40+
41+
private def nullableMessage(msgOrNull: String): String =
42+
if (msgOrNull == null) "" else msgOrNull
43+
44+
private def createReportMessage(diagnostics: Seq[Diagnostic]): Left[SnippetCompilerException, AbstractFile] = {
45+
val infos = diagnostics.toSeq.sortBy(_.pos.source.path)
46+
val errorMessages = infos.map {
47+
case diagnostic if diagnostic.position.isPresent =>
48+
val pos = diagnostic.position.get
49+
val msg = nullableMessage(diagnostic.message)
50+
SnippetCompilerMessage(pos.line, pos.column, pos.lineContent, msg)
51+
case d => SnippetCompilerMessage(-1, -1, "", nullableMessage(d.message))
52+
}
53+
Left(
54+
SnippetCompilerException(errorMessages)
55+
)
56+
}
57+
58+
def compile(
59+
snippets: List[String]
60+
): Either[SnippetCompilerException, AbstractFile] = {
61+
val context = driver.currentCtx.fresh
62+
.setSetting(
63+
driver.currentCtx.settings.outputDir,
64+
target
65+
)
66+
.setReporter(new StoreReporter)
67+
val run = newRun(using context)
68+
run.compileFromStrings(snippets)
69+
if context.reporter.hasErrors then createReportMessage(context.reporter.allErrors)
70+
else Right(target)
71+
}
72+
73+
def compile(
74+
snippet: String
75+
): Either[SnippetCompilerException, AbstractFile] = compile(List(snippet))
76+
77+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dotty.tools.scaladoc
2+
package snippets
3+
4+
class SnippetWrapper:
5+
def wrap(str: String): String = s"""
6+
|package snippets
7+
|object Snippet {
8+
| $str
9+
|}
10+
|""".stripMargin
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dotty.tools.scaladoc
2+
package snippets
3+
4+
case class SnippetCompilerMessage(line: Int, column: Int, sourceLine: String, message: String)
5+
6+
class SnippetCompilerException(messages: Seq[SnippetCompilerMessage]) extends Exception:
7+
override def getMessage: String = "Snippet compiler failed to compile with errors:\n"
8+
+ messages.map(m => s"At ${m.line}:${m.column}:\n${m.sourceLine}Got error: ${m.message}").mkString("\n")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dotty.tools.scaladoc
2+
package snippets
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
import dotty.tools.io.{AbstractFile, VirtualDirectory}
7+
8+
class SnippetCompilerTest {
9+
val compiler = SnippetCompiler()
10+
def runTest(str: String) = compiler.compile(str)
11+
12+
def runTest(str: List[String]) = compiler.compile(str)
13+
14+
def assertSuccessfulCompilation(str: String) = runTest(str) match {
15+
case Right(v) => assert(true)
16+
case Left(ex) => assert(false, ex.getMessage)
17+
}
18+
19+
def assertFailedCompilation(str: String) = runTest(str) match {
20+
case Right(v) => assert(false, "Expected compilation failure")
21+
case Left(ex) => assert(true)
22+
}
23+
24+
def assertSuccessfulCompilation(str: List[String]) = runTest(str) match {
25+
case Right(v) => assert(true)
26+
case Left(ex) => assert(false, ex.getMessage)
27+
}
28+
29+
def assertFailedCompilation(str: List[String]) = runTest(str) match {
30+
case Right(v) => assert(false, "Expected compilation failure")
31+
case Left(ex) => assert(true)
32+
}
33+
34+
@Test
35+
def snippetCompilerTest: Unit = {
36+
val simpleCorrectSnippet = s"""
37+
|package asd
38+
|class A:
39+
| val b: String = "asd"
40+
|""".stripMargin
41+
42+
val simpleIncorrectSnippet = s"""
43+
|package asd
44+
|class A:
45+
| val b: String
46+
|""".stripMargin
47+
assertSuccessfulCompilation(simpleCorrectSnippet)
48+
assertFailedCompilation(simpleIncorrectSnippet)
49+
assertFailedCompilation(List(simpleCorrectSnippet, simpleCorrectSnippet))
50+
}
51+
}

0 commit comments

Comments
 (0)