All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.magics.Parsing.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.magics

/**
 * Split a command argument into a list of library calls
 */
fun splitLibraryCalls(text: String): List {
    return libraryCommaRanges(text)
        .mapNotNull { (from, to) ->
            if (from >= to) {
                null
            } else {
                text.substring(from + 1, to).trim().takeIf { it.isNotEmpty() }
            }
        }
}

fun libraryCommaRanges(text: String): List> {
    return libraryCommaIndices(text, withFirst = true, withLast = true).zipWithNext()
}

/**
 * Need special processing of ',' to skip call argument delimiters in brackets
 * E.g. "use lib1(3), lib2(2, 5)" should split into "lib1(3)" and "lib(2, 5)", not into "lib1(3)", "lib(2", "5)"
 */
fun libraryCommaIndices(
    text: String,
    withFirst: Boolean = false,
    withLast: Boolean = false,
): List {
    return buildList {
        var i = 0
        var commaDepth = 0
        val delimiters = charArrayOf(',', '(', ')')
        if (withFirst) add(-1)

        while (true) {
            i = text.indexOfAny(delimiters, i)
            if (i == -1) {
                if (withLast) add(text.length)
                break
            }
            when (text[i]) {
                ',' ->
                    if (commaDepth == 0) {
                        add(i)
                    }
                '(' -> commaDepth++
                ')' -> commaDepth--
            }
            i++
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy