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

commonMain.pro.felixo.protobuf.schemadocument.SchemaDocumentTokenizer.kt Maven / Gradle / Ivy

The newest version!
package pro.felixo.protobuf.schemadocument

/**
 * Converts .proto syntax into sequences of tokens.
 */
class SchemaDocumentTokenizer {
    fun tokenize(input: String): Sequence = sequence {
        var position = 0

        outer@ while (position < input.length) {
            for (type in TokenType.entries) {
                val matchResult = type.regex.find(input, position)

                if (matchResult != null && matchResult.range.first == position) {
                    val tokenText = matchResult.value
                    type.getToken(tokenText)?.let { yield(it) }
                    position += tokenText.length
                    continue@outer
                }
            }

            throw IllegalArgumentException("Unrecognized token at position $position")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy