// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Text.RegularExpressions; namespace Progmasoft.Tazel.Label; /// A label that cannot be read, with the reason. public sealed class LabelException : Exception { /// A label that cannot be read. public LabelException(string message) : base(message) { } /// Nothing to say. public LabelException() { } /// A problem and its cause. public LabelException(string message, Exception inner) : base(message, inner) { } } /// /// A package: the directory of one BUILD.csx, relative to the root of its module, in this module /// ( null) or a dependency (its name, alias or local module name). /// /// The dependency, or null for this module. /// The directory, with / between its parts; empty for the root package. public sealed record PackageId(string? Repository, string Path) { /// The root package of this module. public static PackageId Root { get; } = new(null, ""); /// //dir or @name//dir; the root package is //. public override string ToString() => $"{(Repository is null ? "" : "@" + Repository)}//{Path}"; } /// What a command selects: one target, or every target of some packages. public abstract record Selection; /// One target: //dir:name, //:name, @dep//dir:name. /// Its package. /// Its name. public sealed record TargetLabel(PackageId Package, string Name) : Selection { /// public override string ToString() => $"{Package}:{Name}"; } /// //dir/...: the package and every package below it; //... is the whole module. /// The package the pattern starts at. public sealed record PackagePattern(PackageId Package) : Selection { /// Whether is this package or one below it. public bool Covers(PackageId other) { ArgumentNullException.ThrowIfNull(other); return other.Repository == Package.Repository && (Package.Path.Length == 0 || other.Path == Package.Path || other.Path.StartsWith(Package.Path + "/", StringComparison.Ordinal)); } /// public override string ToString() => Package.Path.Length == 0 ? $"{Package}..." : $"{Package}/..."; } /// Reads labels as Bazel spells them; nothing else is a label. public static partial class Labels { /// A target label; :name is a target of . Patterns are refused. public static TargetLabel Target(string text, PackageId current) => Selection(text, current) is TargetLabel label ? label : throw new LabelException($"'{text}' names packages, not one target; write //dir:name"); /// The dependency an @name label names, or null, and the rest of the label from its //. private static (string? Repository, string Remainder) Repository(string text) { if (!text.StartsWith('@')) { return (null, text); } var slashes = text.IndexOf("//", StringComparison.Ordinal); return slashes < 0 ? throw new LabelException($"'{text}' has no //; write @name//:target") : (Checked(text[1..slashes], "dependency name", text), text[slashes..]); } private static LabelException NoTarget(string text, string shown) { var last = shown[(shown.LastIndexOf('/') + 1)..]; return new LabelException($"'{text}' names no target; write //{shown}:{last} or //{shown}/..."); } /// A target label or a package pattern; :name is a target of . public static Selection Selection(string text, PackageId current) { ArgumentNullException.ThrowIfNull(text); ArgumentNullException.ThrowIfNull(current); if (text.StartsWith(':')) { return new TargetLabel(current, Checked(text[1..], "target name", text)); } var (repository, rest) = Repository(text); if (!rest.StartsWith("//", StringComparison.Ordinal)) { throw new LabelException($"'{text}' is not a label; write //dir:name, //:name or :name"); } var body = rest[2..]; if (body == "...") { return new PackagePattern(new PackageId(repository, "")); } if (body.EndsWith("/...", StringComparison.Ordinal)) { return new PackagePattern(new PackageId(repository, PackagePath(body[..^4], text))); } var colon = body.LastIndexOf(':'); if (colon < 0) { throw NoTarget(text, body.TrimEnd('/')); } var directory = body[..colon]; if (directory.EndsWith('/')) { throw new LabelException($"'{text}': no / before the colon; write //{directory.TrimEnd('/')}:..."); } var path = directory.Length == 0 ? "" : PackagePath(directory, text); return new TargetLabel(new PackageId(repository, path), Checked(body[(colon + 1)..], "target name", text)); } /// Checks a target or package name as a project file writes it. public static bool IsName(string name) => NamePattern().IsMatch(name); private static string PackagePath(string path, string text) { if (path.Length == 0) { throw new LabelException($"'{text}' has an empty package"); } foreach (var segment in path.Split('/')) { if (segment is "." or "..") { throw new LabelException($"'{text}': a package path has no . or .. parts"); } Checked(segment, "package directory", text); } return path; } private static string Checked(string name, string what, string text) { if (!IsName(name)) { throw new LabelException( $"'{text}': the {what} '{name}' must be letters, digits and _ + . -"); } return name; } [GeneratedRegex("^[A-Za-z0-9_+.-]+$")] private static partial Regex NamePattern(); }