Introduce script names and location ids, use them in diagnostics and...

for virtual file names. Also fix compiled script serialization.
This commit is contained in:
Ilya Chernikov
2018-12-19 14:54:07 +01:00
parent 77095cb895
commit 44ea2bf1d4
13 changed files with 133 additions and 33 deletions
@@ -17,6 +17,7 @@ package kotlin.script.experimental.api
data class ScriptDiagnostic(
val message: String,
val severity: Severity = Severity.ERROR,
val sourcePath: String? = null,
val location: SourceCode.Location? = null,
val exception: Throwable? = null
) {
@@ -92,21 +93,25 @@ fun <R> R.asSuccess(reports: List<ScriptDiagnostic> = listOf()): ResultWithDiagn
ResultWithDiagnostics.Success(this, reports)
/**
* Converts the receiver Throwable to the Failure results wrapper with optional [message] and [location]
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [path] and [location]
*/
fun Throwable.asDiagnostics(customMessage: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, location, this)
fun Throwable.asDiagnostics(
customMessage: String? = null,
path: String? = null,
location: SourceCode.Location? = null
): ScriptDiagnostic =
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, path, location, this)
/**
* Converts the receiver String to error diagnostic report with optional [location]
* Converts the receiver String to error diagnostic report with optional [path] and [location]
*/
fun String.asErrorDiagnostics(location: SourceCode.Location? = null): ScriptDiagnostic =
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, location)
fun String.asErrorDiagnostics(path: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, path, location)
/**
* Extracts the result value from the receiver wrapper or null if receiver represents a Failure
*/
fun<R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
fun <R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
is ResultWithDiagnostics.Success<R> -> value
else -> null
}
@@ -222,6 +222,12 @@ interface ScriptCompiler {
*/
interface CompiledScript<out ScriptBase : Any> {
/**
* The location identifier for the script source, taken from SourceCode.locationId
*/
val sourceLocationId: String?
get() = null
/**
* The compilation configuration used for script compilation
*/
@@ -20,6 +20,16 @@ interface SourceCode {
*/
val text: String
/**
* The script file or display name
*/
val name: String?
/**
* The path or other script location identifier
*/
val locationId: String?
/**
* The source code position
* @param line source code position line
@@ -45,6 +45,8 @@ fun getMergedScriptText(script: SourceCode, configuration: ScriptCompilationConf
open class FileScriptSource(val file: File, private val preloadedText: String? = null) : ExternalSourceCode {
override val externalLocation: URL get() = file.toURI().toURL()
override val text: String by lazy { preloadedText ?: file.readText() }
override val name: String? get() = file.name
override val locationId: String? get() = file.path
}
/**
@@ -52,6 +54,8 @@ open class FileScriptSource(val file: File, private val preloadedText: String? =
*/
open class UrlScriptSource(override val externalLocation: URL) : ExternalSourceCode {
override val text: String by lazy { externalLocation.readText() }
override val name: String? get() = externalLocation.file
override val locationId: String? get() = externalLocation.toString()
}
/**
@@ -63,7 +67,11 @@ fun File.toScriptSource(): SourceCode = FileScriptSource(this)
* The implementation of the ScriptSource for a script in a String
*/
open class StringScriptSource(val source: String) : SourceCode {
override val text: String get() = source
override val name: String? = null
override val locationId: String? = null
}
/**