fun parse()

in commons/src/main/kotlin/org/jetbrains/bazel/label/Label.kt [204:246]


    fun parse(value: String): Label {
      if (value.endsWith(SYNTHETIC_TAG)) return synthetic(value)
      val normalized = value.trim().trimStart('@')
      if (normalized.contains(" ")) throw IllegalArgumentException("Label $normalized cannot have whitespaces")
      val repoName = normalized.substringBefore("//", "")
      val pathAndName = normalized.substringAfter("//")
      val packagePath = pathAndName.substringBefore(":")
      val packageSegments = packagePath.split(PATH_SEGMENT_SEPARATOR).filter { it.isNotEmpty() }
      val packageType =
        if (packageSegments.lastOrNull() == ALL_PACKAGES_BENEATH) {
          AllPackagesBeneath(packageSegments.dropLast(1))
        } else {
          Package(packageSegments)
        }
      val targetName = pathAndName.substringAfter(":", packagePath.substringAfterLast(PATH_SEGMENT_SEPARATOR))

      val target =
        when {
          targetName == WILDCARD -> AllRuleTargetsAndFiles
          targetName == ALL_TARGETS -> AllRuleTargetsAndFiles
          targetName == ALL -> AllRuleTargets
          targetName == ALL_PACKAGES_BENEATH -> AllRuleTargets // Special case for //...:...
          pathAndName.contains(":") -> SingleTarget(targetName)
          else -> AmbiguousEmptyTarget
        }

      if (packageType is AllPackagesBeneath && target is SingleTarget) {
        throw IllegalArgumentException("Cannot have a single target in a wildcard package")
      }

      if (!value.contains("//")) {
        return RelativeLabel(packageType, target)
      }

      val repo =
        when {
          repoName.isEmpty() -> Main
          value.startsWith("@@") -> Canonical(repoName)
          else -> Apparent(repoName)
        }

      return ResolvedLabel(repo, packageType, target)
    }