Rename ScriptSource to SourceCode, extract location to another i-face...

other related refactorings. The goal - make interface terminologically
more suitable for usage in REPL.
This commit is contained in:
Ilya Chernikov
2018-09-03 13:37:27 +02:00
parent 1e85a26b62
commit 67ba8b5361
10 changed files with 37 additions and 42 deletions
@@ -10,7 +10,7 @@ package kotlin.script.experimental.api
data class ScriptDiagnostic(
val message: String,
val severity: Severity = Severity.ERROR,
val location: ScriptSource.Location? = null,
val location: SourceCode.Location? = null,
val exception: Throwable? = null
) {
enum class Severity { FATAL, ERROR, WARNING, INFO, DEBUG }
@@ -56,10 +56,10 @@ operator fun <R> List<ScriptDiagnostic>.plus(res: ResultWithDiagnostics<R>): Res
fun <R> R.asSuccess(reports: List<ScriptDiagnostic> = listOf()): ResultWithDiagnostics.Success<R> =
ResultWithDiagnostics.Success(this, reports)
fun Throwable.asDiagnostics(customMessage: String? = null, location: ScriptSource.Location? = null): ScriptDiagnostic =
fun Throwable.asDiagnostics(customMessage: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, location, this)
fun String.asErrorDiagnostics(location: ScriptSource.Location? = null): ScriptDiagnostic =
fun String.asErrorDiagnostics(location: SourceCode.Location? = null): ScriptDiagnostic =
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, location)
fun<R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
@@ -120,7 +120,7 @@ class RefineConfigurationOnSectionsData(
interface ScriptCompiler {
suspend operator fun invoke(
script: ScriptSource,
script: SourceCode,
scriptCompilationConfiguration: ScriptCompilationConfiguration
): ResultWithDiagnostics<CompiledScript<*>>
}
@@ -10,16 +10,19 @@ package kotlin.script.experimental.api
import java.net.URL
import kotlin.script.experimental.util.PropertiesCollection
interface ScriptSource {
val location: URL?
val text: String?
interface SourceCode {
val text: String
data class Position(val line: Int, val col: Int, val absolutePos: Int? = null)
data class Range(val start: Position, val end: Position)
data class Location(val start: Position, val end: Position? = null)
}
data class ScriptSourceNamedFragment(val name: String?, val range: ScriptSource.Range)
interface ExternalSourceCode : SourceCode {
val externalLocation: URL
}
data class ScriptSourceNamedFragment(val name: String?, val range: SourceCode.Range)
enum class ScriptBodyTarget {
Constructor,
@@ -53,7 +56,7 @@ val ScriptCollectedDataKeys.foundAnnotations by PropertiesCollection.key<List<An
val ScriptCollectedDataKeys.foundFragments by PropertiesCollection.key<List<ScriptSourceNamedFragment>>()
class ScriptConfigurationRefinementContext(
val source: ScriptSource,
val script: SourceCode,
val compilationConfiguration: ScriptCompilationConfiguration,
val collectedData: ScriptCollectedData? = null
)
@@ -29,7 +29,7 @@ abstract class BasicScriptingHost(
open fun <T> runInCoroutineContext(block: suspend CoroutineScope.() -> T): T = runBlocking { block() }
open fun eval(
script: ScriptSource,
script: SourceCode,
scriptCompilationConfiguration: ScriptCompilationConfiguration,
configuration: ScriptEvaluationConfiguration?
): ResultWithDiagnostics<EvaluationResult> =
@@ -9,15 +9,8 @@ import java.io.File
import java.net.URL
import kotlin.script.experimental.api.*
fun ScriptSource.getScriptText(): String = when {
text != null -> text!!
location != null ->
location!!.openStream().bufferedReader().readText()
else -> throw RuntimeException("unable to get text from null script")
}
fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompilationConfiguration?): String {
val originalScriptText = script.getScriptText()
fun getMergedScriptText(script: SourceCode, configuration: ScriptCompilationConfiguration?): String {
val originalScriptText = script.text
val sourceFragments = configuration?.get(ScriptCompilationConfiguration.sourceFragments)
return if (sourceFragments == null || sourceFragments.isEmpty()) {
originalScriptText
@@ -45,16 +38,15 @@ fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompilationCo
}
}
open class FileScriptSource(val file: File) : ScriptSource {
override val location: URL? get() = file.toURI().toURL()
override val text: String? get() = null
open class FileScriptSource(val file: File) : ExternalSourceCode {
override val externalLocation: URL get() = file.toURI().toURL()
override val text: String by lazy { file.readText() }
}
fun File.toScriptSource(): ScriptSource = FileScriptSource(this)
fun File.toScriptSource(): SourceCode = FileScriptSource(this)
open class StringScriptSource(val source: String) : ScriptSource {
override val location: URL? get() = null
override val text: String? get() = source
open class StringScriptSource(val source: String) : SourceCode {
override val text: String get() = source
}
fun String.toScriptSource(): ScriptSource = StringScriptSource(this)
fun String.toScriptSource(): SourceCode = StringScriptSource(this)
@@ -5,7 +5,7 @@
package kotlin.script.experimental.repl
import kotlin.script.experimental.api.ScriptSource
import kotlin.script.experimental.api.SourceCode
interface ReplSnippetSource : ScriptSource, ReplSnippetId
interface ReplSnippetSource : SourceCode, ReplSnippetId