|
| 1 | +package org.jetbrains.kotlinx.jupyter.libraries |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.net.URL |
| 5 | + |
| 6 | +fun parseReferenceWithArgs(str: String): Pair<LibraryReference, List<Variable>> { |
| 7 | + val (fullName, vars) = parseLibraryName(str) |
| 8 | + val reference = parseReference(fullName) |
| 9 | + return reference to vars |
| 10 | +} |
| 11 | + |
| 12 | +private fun parseResolutionInfo(string: String): LibraryResolutionInfo { |
| 13 | + // In case of empty string after `@`: %use lib@ |
| 14 | + if (string.isBlank()) return LibraryResolutionInfo.Default() |
| 15 | + |
| 16 | + val (type, vars) = parseCall(string, Brackets.SQUARE) |
| 17 | + return defaultParsers[type]?.getInfo(vars) ?: LibraryResolutionInfo.Default(type) |
| 18 | +} |
| 19 | + |
| 20 | +private fun parseReference(string: String): LibraryReference { |
| 21 | + val sepIndex = string.indexOf('@') |
| 22 | + if (sepIndex == -1) return LibraryReference(LibraryResolutionInfo.Default(), string) |
| 23 | + |
| 24 | + val nameString = string.substring(0, sepIndex) |
| 25 | + val infoString = string.substring(sepIndex + 1) |
| 26 | + val info = parseResolutionInfo(infoString) |
| 27 | + return LibraryReference(info, nameString) |
| 28 | +} |
| 29 | + |
| 30 | +private val defaultParsers = listOf( |
| 31 | + LibraryResolutionInfoParser.make("ref", listOf(Parameter.Required("ref"))) { args -> |
| 32 | + LibraryResolutionInfo.getInfoByRef(args["ref"] ?: error("Argument 'ref' should be specified")) |
| 33 | + }, |
| 34 | + LibraryResolutionInfoParser.make("file", listOf(Parameter.Required("path"))) { args -> |
| 35 | + LibraryResolutionInfo.ByFile(File(args["path"] ?: error("Argument 'path' should be specified"))) |
| 36 | + }, |
| 37 | + LibraryResolutionInfoParser.make("dir", listOf(Parameter.Required("dir"))) { args -> |
| 38 | + LibraryResolutionInfo.ByDir(File(args["dir"] ?: error("Argument 'dir' should be specified"))) |
| 39 | + }, |
| 40 | + LibraryResolutionInfoParser.make("url", listOf(Parameter.Required("url"))) { args -> |
| 41 | + LibraryResolutionInfo.ByURL(URL(args["url"] ?: error("Argument 'url' should be specified"))) |
| 42 | + }, |
| 43 | +).map { it.name to it }.toMap() |
0 commit comments