Function: bazel--parse-label

bazel--parse-label is a natively compiled function defined in bazel.el.

Signature

(bazel--parse-label LABEL)

Documentation

Parse Bazel label LABEL.

If LABEL isn’t syntactically valid, return nil. Otherwise, return a triple (REPOSITORY PACKAGE TARGET). REPOSITORY is nil (for the current repository) or a string referring to some external repository. PACKAGE is nil (for the current package) or a package name string. TARGET is a string referring to the local name of LABEL. See URL
‘https://bazel.build/concepts/labels#labels-lexical-specification’
for the lexical syntax of labels.

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel--parse-label (label)
  "Parse Bazel label LABEL.
If LABEL isn’t syntactically valid, return nil.  Otherwise,
return a triple (REPOSITORY PACKAGE TARGET).  REPOSITORY is
nil (for the current repository) or a string referring to some
external repository.  PACKAGE is nil (for the current package) or
a package name string.  TARGET is a string referring to the local
name of LABEL.  See URL
‘https://bazel.build/concepts/labels#labels-lexical-specification’
for the lexical syntax of labels."
  (declare (ftype (function (string) list)))
  (cl-check-type label string)
  (let ((case-fold-search nil)
        (search-spaces-regexp nil))
    (pcase (substring-no-properties label)
      ((rx bos
           (or
            ;; @repository//package:target
            (seq "@" (let repository (+ (not (any ?: ?/))))
                 "//" (let package (* (not (any ?:))))
                 ?: (let target (+ (not (any ?:)))))
            ;; @repository//package
            (seq "@" (let repository (+ (not (any ?: ?/))))
                 "//" (let package (+ (not (any ?:)))))
            ;; @repository
            ;; This syntax isn’t documented in
            ;; https://bazel.build/concepts/labels, but follows from
            ;; https://github.com/bazelbuild/buildtools/blob/v6.3.3/build/rewrite.go#L260-L261.
            (seq ?@ (let repository (+ (not (any ?: ?/)))))
            ;; //package:target
            (seq "//" (let package (* (not (any ?:))))
                 ?: (let target (+ (not (any ?:)))))
            ;; //package
            (seq "//" (let package (+ (not (any ?:)))))
            ;; :target
            (seq ?: (let target (+ (not (any ?:)))))
            ;; target
            (seq (let target (not (any ?: ?/ ?@)) (* (not (any ?:))))))
           eos)
       (unless target (setq target (bazel--default-target repository package)))
       (and (or (null repository)
                ;; https://bazel.build/rules/lib/globals/workspace#parameters_3
                (string-match-p
                 (rx bos (any "A-Z" "a-z") (* (any ?- ?. ?_ "A-Z" "a-z")) eos)
                 repository))
            (or (null package) (string-empty-p package)
                ;; https://bazel.build/concepts/labels#package-names
                (string-match-p (rx bos
                                    (any "A-Z" "a-z" "0-9" ?- ?. ?_)
                                    (* (any "A-Z" "a-z" "0-9" ?/ ?- ?. ?_))
                                    eos)
                                package))
            ;; https://bazel.build/concepts/labels#target-names
            (string-match-p (rx bos
                                (+ (any "a-z" "A-Z" "0-9" ?-
                                        "!%@^_` \"#$&'()*+,;<=>?[]{|}~/."))
                                eos)
                            target)
            (list repository package target))))))