// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading; using Progmasoft.Tazel.Dsl.Common; namespace Progmasoft.Tazel.Dsl; /// What a project file wrote wrong, with the line it wrote it on. public sealed class DslException : Exception { /// A problem without a place. public DslException(string message) : base(message) { } /// A problem and its cause. public DslException(string message, Exception inner) : base(message, inner) { } /// Nothing to say. public DslException() { } } /// The languages a target is written in. internal enum Family { CC, ObjectiveC, Zig, /// A build step a plugin adds. Plugin, } /// What a target makes. internal enum TargetKind { StaticLibrary, SharedLibrary, Executable, Test, Module, /// A build step of a plugin. Step, } /// A target as a project file declared it, before anything is checked against other files. internal sealed record TargetDeclaration( Family Family, TargetKind Kind, string Name, string Label, object Settings, string File, int Line); /// A toolchain as a project file declared it. internal sealed record ToolchainDeclaration( Family Family, string Name, string Label, object Settings, string File, int Line); /// What one project file declares while it runs. internal sealed class DeclarationScope(string packagePath, string file) { private readonly HashSet _names = new(StringComparer.Ordinal); public string PackagePath { get; } = packagePath; public string File { get; } = file; public List Targets { get; } = []; public List Toolchains { get; } = []; public string LabelOf(string name) => PackagePath.Length == 0 ? $"//:{name}" : $"//{PackagePath}:{name}"; public string Claim(string name, int line) { Names.Check(name, "target name", Place(line)); if (!_names.Add(name)) { throw new DslException($"{Place(line)}: '{name}' is declared twice in this file"); } return LabelOf(name); } public string Place(int line) => $"{File}:{line}"; } /// Checks the names a project file gives. internal static partial class Names { public static void Check(string name, string what, string place) { ArgumentNullException.ThrowIfNull(name); if (!Pattern().IsMatch(name)) { throw new DslException( $"{place}: the {what} '{name}' must be letters, digits and _ + . -"); } } [GeneratedRegex("^[A-Za-z0-9_+.-]+$")] private static partial Regex Pattern(); } /// The project file that is running, for the declarations it makes. internal static class Declarations { private static readonly AsyncLocal CurrentScope = new(); public static DeclarationScope Current => CurrentScope.Value ?? throw new DslException("targets and toolchains are declared in a BUILD.csx"); /// Makes the file that is running until the result is disposed. public static IDisposable Enter(DeclarationScope scope) { var previous = CurrentScope.Value; CurrentScope.Value = scope; return new Restore(() => CurrentScope.Value = previous); } public static Target Declare(Family family, TargetKind kind, string name, object settings, string file, int line) { var scope = Current; if (settings is null) { throw new DslException($"{scope.Place(line)}: '{name}' needs its settings: new() {{ ... }}"); } var label = scope.Claim(name, line); scope.Targets.Add(new TargetDeclaration(family, kind, name, label, settings, scope.File, line)); return new Target(label); } public static void DeclareToolchain(Family family, string name, object settings, string file, int line) { var scope = Current; if (settings is null) { throw new DslException($"{scope.Place(line)}: the toolchain '{name}' needs its settings"); } var label = scope.Claim(name, line); scope.Toolchains.Add(new ToolchainDeclaration(family, name, label, settings, scope.File, line)); } private sealed class Restore(Action undo) : IDisposable { public void Dispose() => undo(); } }