|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.jetbrains.kotlin.library.abi.parser |
| 18 | + |
| 19 | +internal class Cursor |
| 20 | +private constructor(private val lines: List<String>, rowIndex: Int = 0, columnIndex: Int = 0) { |
| 21 | + constructor(text: String) : this(text.split("\n")) |
| 22 | + |
| 23 | + var rowIndex: Int = rowIndex |
| 24 | + private set |
| 25 | + |
| 26 | + var columnIndex: Int = columnIndex |
| 27 | + private set |
| 28 | + |
| 29 | + val currentLine: String |
| 30 | + get() = lines[rowIndex].slice(columnIndex until lines[rowIndex].length) |
| 31 | + |
| 32 | + val offset = lines.subList(0, rowIndex).sumOf { it.length } + columnIndex |
| 33 | + |
| 34 | + /** Check if we have passed the last line in [lines] and there is nothing left to parse */ |
| 35 | + fun isFinished() = rowIndex >= lines.size |
| 36 | + |
| 37 | + fun nextLine() { |
| 38 | + rowIndex++ |
| 39 | + columnIndex = 0 |
| 40 | + if (!isFinished()) { |
| 41 | + skipInlineWhitespace() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fun parseSymbol( |
| 46 | + pattern: Regex, |
| 47 | + peek: Boolean = false, |
| 48 | + skipInlineWhitespace: Boolean = true, |
| 49 | + ): String? { |
| 50 | + val match = pattern.find(currentLine) |
| 51 | + return match?.value?.also { |
| 52 | + if (!peek) { |
| 53 | + val offset = it.length + currentLine.indexOf(it) |
| 54 | + setColumn(columnIndex + offset) |
| 55 | + if (skipInlineWhitespace) { |
| 56 | + skipInlineWhitespace() |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + fun parseValidIdentifier(peek: Boolean = false): String? = |
| 63 | + parseSymbol(validIdentifierRegex, peek) |
| 64 | + |
| 65 | + fun parseWord(peek: Boolean = false): String? = parseSymbol(wordRegex, peek) |
| 66 | + |
| 67 | + fun copy() = Cursor(lines, rowIndex, columnIndex) |
| 68 | + |
| 69 | + private fun setColumn(index: Int) { |
| 70 | + columnIndex = index |
| 71 | + } |
| 72 | + |
| 73 | + internal fun skipInlineWhitespace() { |
| 74 | + while (currentLine.firstOrNull()?.isWhitespace() == true) { |
| 75 | + setColumn(columnIndex + 1) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Match any '=' not followed by '...' because they're valid characters, but we don't want to |
| 81 | +// parse part of the parameter default symbol (=...) by accident. Otherwise match all non-illegal |
| 82 | +// characters |
| 83 | +private val validIdentifierRegex = |
| 84 | + Regex( |
| 85 | + """ |
| 86 | + ^((=(?!\s?\.\.\.)|[^.;\[\]/<>:\\(){}?=,&])+) |
| 87 | + """ |
| 88 | + .trimIndent() |
| 89 | + ) |
| 90 | +private val wordRegex = Regex("[a-zA-Z]+") |
0 commit comments