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