tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Split the configuration parser and evaluator into smaller steps
- Commit
cd0f0271c715ea4d3c993c6eef3a760c1c14ddb5- Author
- Leitwolf11 ·
- Parents
33ca70567fbc
Changes
.../Dsl/Configuration/Conditions.cs | 147 +++++++------- .../Dsl/Configuration/ConfigurationParser.cs | 217 +++++++++++++-------- .../Dsl/Configuration/ConfigurationResolver.cs | 63 +++--- .../Dsl/Configuration/TazelHome.cs | 2 +- .../Dsl/Custom/Parser/CustomParser.cs | 12 +- src/Progmasoft.Tazel/Dsl/Envs/TazelEnvs.cs | 22 ++- src/Progmasoft.Tazel/Dsl/Options/TazelOptions.cs | 17 +- 7 files changed, 277 insertions(+), 203 deletions(-) diff --git a/src/Progmasoft.Tazel/Dsl/Configuration/Conditions.cs b/src/Progmasoft.Tazel/Dsl/Configuration/Conditions.cs index b8d9635..224847d 100644 --- a/src/Progmasoft.Tazel/Dsl/Configuration/Conditions.cs +++ b/src/Progmasoft.Tazel/Dsl/Configuration/Conditions.cs @@ -244,12 +244,7 @@ internal sealed class ConditionReader(string text) { _position++; var inner = Or(); - if (!Take(")")) - { - throw new ConditionException("a '(' has no ')'"); - } - - return inner; + return Take(")") ? inner : throw new ConditionException("a '(' has no ')'"); } if (letter is '"' or '\'' or '`') @@ -258,44 +253,47 @@ internal sealed class ConditionReader(string text) } var name = Word(); - if (name.Length == 0) + return name switch + { + "" => throw new ConditionException($"'{letter}' is not an operator of conditions"), + "true" => new LiteralCondition(true), + "false" => new LiteralCondition(false), + "host_system" or "target_system" => Member(name), + "defined" or "env" => Call(name), + _ => throw new ConditionException($"'{name}' is not a name conditions know" + + (Cli.Suggestions.Nearest(name, ["true", "false", "host_system", "target_system", "defined", "env"]) is { } near ? $"; did you mean {near}?" : "")), + }; + } + + // host_system.os and the like, after its name. + private MemberCondition Member(string name) + { + if (!Take(".")) { - throw new ConditionException($"'{letter}' is not an operator of conditions"); + throw new ConditionException($"{name} needs a field: {name}.os, .arch, .abi or .platform"); } - switch (name) + var field = Word(); + return Fields.Contains(field) ? new MemberCondition(name, field) + : throw new ConditionException($"{name}.{field} is not a field; the fields are os, arch, abi and platform"); + } + + // defined("NAME") or env("NAME"), after its name. + private CallCondition Call(string name) + { + if (!Take("(")) { - case "true": - return new LiteralCondition(true); - case "false": - return new LiteralCondition(false); - case "host_system" or "target_system": - if (!Take(".")) - { - throw new ConditionException($"{name} needs a field: {name}.os, .arch, .abi or .platform"); - } - - var field = Word(); - return Fields.Contains(field) ? new MemberCondition(name, field) - : throw new ConditionException($"{name}.{field} is not a field; the fields are os, arch, abi and platform"); - case "defined" or "env": - if (!Take("(")) - { - throw new ConditionException($"{name} is called: {name}(\"NAME\")"); - } - - Space(); - if (_position >= text.Length || text[_position] is not ('"' or '\'' or '`')) - { - throw new ConditionException($"{name} takes the name of a variable in quotes"); - } - - var variable = Quoted(); - return Take(")") ? new CallCondition(name, variable) : throw new ConditionException($"{name}(\"{variable}\" has no ')'"); - default: - var near = Cli.Suggestions.Nearest(name, ["true", "false", "host_system", "target_system", "defined", "env"]); - throw new ConditionException($"'{name}' is not a name conditions know{(near is null ? "" : $"; did you mean {near}?")}"); + throw new ConditionException($"{name} is called: {name}(\"NAME\")"); } + + Space(); + if (_position >= text.Length || text[_position] is not ('"' or '\'' or '`')) + { + throw new ConditionException($"{name} takes the name of a variable in quotes"); + } + + var variable = Quoted(); + return Take(")") ? new CallCondition(name, variable) : throw new ConditionException($"{name}(\"{variable}\" has no ')'"); } private static Condition Booleans(Condition condition, string op) => condition.Type == ConditionType.Boolean @@ -364,50 +362,41 @@ public static class ConditionEvaluator Condition condition, SystemFacts host, SystemFacts? target, IReadOnlyDictionary<string, string> environment) { ArgumentNullException.ThrowIfNull(condition); - return Value(condition, host, target, environment) switch - { - bool truth => truth, - _ => null, - }; + return new Evaluator(host, target, environment).Value(condition) as bool?; } - // A bool, a string, or null for unknown. - private static object? Value(Condition condition, SystemFacts host, SystemFacts? target, IReadOnlyDictionary<string, string> environment) + private sealed class Evaluator(SystemFacts host, SystemFacts? target, IReadOnlyDictionary<string, string> environment) { - switch (condition) + // A bool, a string, or null for unknown. + public object? Value(Condition condition) => condition switch + { + LiteralCondition literal => literal.Value, + MemberCondition member => member.System == "host_system" ? host[member.Field] : target?[member.Field], + CallCondition call => call.Function == "defined" ? environment.ContainsKey(call.Name) : environment.GetValueOrDefault(call.Name, ""), + NotCondition not => Value(not.Operand) is bool truth ? !truth : null, + BinaryCondition { Operator: "&&" or "||" } logical => Logical(logical), + BinaryCondition binary => Compare(binary), + _ => throw new ArgumentException($"no condition {condition.GetType().Name}", nameof(condition)), + }; + + // && and ||: the first side decides when it can; an unknown first side decides only with the second. + private bool? Logical(BinaryCondition logical) + { + var decides = logical.Operator == "||"; + var left = Value(logical.Left) as bool?; + if (left == decides) + { + return decides; + } + + var right = Value(logical.Right) as bool?; + return left is null ? (right == decides ? decides : null) : right; + } + + private bool? Compare(BinaryCondition binary) { - case LiteralCondition literal: - return literal.Value; - case MemberCondition member: - return member.System == "host_system" ? host[member.Field] : target?[member.Field]; - case CallCondition call: - return call.Function == "defined" ? environment.ContainsKey(call.Name) : environment.GetValueOrDefault(call.Name, ""); - case NotCondition not: - return Value(not.Operand, host, target, environment) is bool truth ? !truth : null; - case BinaryCondition { Operator: "&&" } and: - return Value(and.Left, host, target, environment) switch - { - false => false, - true => Value(and.Right, host, target, environment), - _ => Value(and.Right, host, target, environment) is false ? false : null, - }; - case BinaryCondition { Operator: "||" } or: - return Value(or.Left, host, target, environment) switch - { - true => true, - false => Value(or.Right, host, target, environment), - _ => Value(or.Right, host, target, environment) is true ? true : null, - }; - case BinaryCondition binary: - var (left, right) = (Value(binary.Left, host, target, environment), Value(binary.Right, host, target, environment)); - if (left is null || right is null) - { - return null; - } - - return binary.Operator == "==" ? Equals(left, right) : !Equals(left, right); - default: - throw new ArgumentException($"no condition {condition.GetType().Name}", nameof(condition)); + var (left, right) = (Value(binary.Left), Value(binary.Right)); + return left is null || right is null ? null : Equals(left, right) == (binary.Operator == "=="); } } } diff --git a/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationParser.cs b/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationParser.cs index 0cd2849..6c704dc 100644 --- a/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationParser.cs +++ b/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationParser.cs @@ -53,9 +53,19 @@ public sealed record ConfigurationDocument(IReadOnlyList<Node> Nodes, bool Once) public static class ConfigurationParser { /// <summary>The deepest <c>.if</c> may nest, and the deepest includes may go.</summary> - public const int MaxDepth = 64; + public static int MaxDepth { get; } = 64; - private sealed class Frame(Token at) + private static readonly string[] Directives = ["include", "import", "pragma", "if", "elif", "else", "end"]; + + /// <summary>The tree of <paramref name="tokens"/>, read from <paramref name="text"/> of <paramref name="file"/>.</summary> + public static ConfigurationDocument Parse(IReadOnlyList<Token> tokens, string file, string text) + { + ArgumentNullException.ThrowIfNull(tokens); + return new Reader(file, text).Read(tokens); + } + + // An .if being read: its branches so far, and the one being read. + private sealed class Frame(Token at, Condition? test) { public Token At { get; } = at; @@ -63,45 +73,63 @@ public static class ConfigurationParser public Token BranchAt { get; set; } = at; - public Condition? Condition { get; set; } + public Condition? Test { get; set; } = test; public List<Node> Body { get; set; } = []; public bool SawElse { get; set; } + + public void Close() => Branches.Add(new Branch(BranchAt, Test, Body)); } - /// <summary>The tree of <paramref name="tokens"/>, read from <paramref name="text"/> of <paramref name="file"/>.</summary> - public static ConfigurationDocument Parse(IReadOnlyList<Token> tokens, string file, string text) + private sealed class Reader(string file, string text) { - ArgumentNullException.ThrowIfNull(tokens); - var root = new List<Node>(); - var frames = new Stack<Frame>(); - var pending = new List<Token>(); - var once = false; - List<Node> Current() => frames.Count == 0 ? root : frames.Peek().Body; - void Flush() + private readonly List<Node> _root = []; + private readonly Stack<Frame> _frames = new(); + private readonly List<Token> _pending = []; + private bool _once; + + private List<Node> Current => _frames.Count == 0 ? _root : _frames.Peek().Body; + + public ConfigurationDocument Read(IReadOnlyList<Token> tokens) { - if (pending.Count > 0) + for (var i = 0; i < tokens.Count; i++) { - Current().Add(new TokensNode(pending.ToList())); - pending.Clear(); + var token = tokens[i]; + if (token.Kind != TokenKind.Directive) + { + _pending.Add(token); + continue; + } + + Flush(); + Directive(token, token.Argument ?? "", first: i == 0); } - } - CustomCommandsException Fail(string code, Token at, string message, string help, string joke) => - new(Diagnostic.AtToken(code, text, file, at with { Text = "." + at.Text }, message, "this directive", help, joke)); + Flush(); + if (_frames.Count > 0) + { + throw Fail(ErrorCodes.ConditionalUnbalanced, _frames.Peek().At, ".if without its .end", "close the block with .end", + "This .if is still waiting for the end of the story."); + } - for (var i = 0; i < tokens.Count; i++) + return new ConfigurationDocument(_root, _once); + } + + private void Flush() { - var token = tokens[i]; - if (token.Kind != TokenKind.Directive) + if (_pending.Count > 0) { - pending.Add(token); - continue; + Current.Add(new TokensNode(_pending.ToList())); + _pending.Clear(); } + } - Flush(); - var argument = token.Argument ?? ""; + private CustomCommandsException Fail(string code, Token at, string message, string help, string joke) => + new(Diagnostic.AtToken(code, text, file, at with { Text = "." + at.Text }, message, "this directive", help, joke)); + + private void Directive(Token token, string argument, bool first) + { if (argument.EndsWith(';')) { throw Fail(ErrorCodes.DirectiveUnknown, token, $".{token.Text} takes no ';'", "leave the ';' out", @@ -111,71 +139,22 @@ public static class ConfigurationParser switch (token.Text) { case "include" or "import": - var (path, global) = IncludePath(argument) ?? throw Fail(ErrorCodes.IncludePathRefused, token, - $".{token.Text} needs one path, in quotes or in angle brackets, not '{argument}'", - $"write .{token.Text} \"file\" for a file beside this one, or .{token.Text} <file> for one of TAZEL_HOME's Include directory", - "Tazel looked for a path and found an opinion instead."); - Current().Add(new IncludeNode(token, path, global, token.Text == "import")); + Include(token, argument); break; case "pragma": - if (argument != "once") - { - throw Fail(ErrorCodes.DirectiveUnknown, token, $"'.pragma {argument}' is not a pragma Tazel knows", - ".pragma once is the only pragma", "Tazel has exactly one pragma, and it is used once."); - } - - if (i != 0) - { - throw Fail(ErrorCodes.DirectiveUnknown, token, ".pragma once must come first in its file", - "move it to the top, above everything but comments", "Once is a word that only works at the start."); - } - - once = true; + Pragma(token, argument, first); break; case "if": - if (frames.Count >= MaxDepth) - { - throw Fail(ErrorCodes.ConditionalUnbalanced, token, $".if blocks nest more than {MaxDepth} deep", - "fold the conditions together with && and ||", "Somewhere, deep in the ifs, a condition got lost."); - } - - frames.Push(new Frame(token) { Condition = Condition(token, file, text) }); + If(token); break; case "elif" or "else": - var frame = frames.Count > 0 ? frames.Peek() : throw Fail(ErrorCodes.ConditionalUnbalanced, token, - $".{token.Text} without an .if", "open the block with .if", "An answer, and nobody asked."); - if (frame.SawElse) - { - throw Fail(ErrorCodes.ConditionalUnbalanced, token, $".{token.Text} after .else", - ".else is the last branch of its .if", "After else, there is nothing else."); - } - - if (token.Text == "else" && argument.Length > 0) - { - throw Fail(ErrorCodes.ConditionalUnbalanced, token, ".else takes no condition", - "write .elif and the condition, or .else alone", "Else is for everything that is left; it does not choose."); - } - - frame.Branches.Add(new Branch(frame.BranchAt, frame.Condition, frame.Body)); - frame.BranchAt = token; - frame.Condition = token.Text == "elif" ? Condition(token, file, text) : null; - frame.Body = []; - frame.SawElse = token.Text == "else"; + Else(token, argument); break; case "end": - var closing = frames.Count > 0 ? frames.Pop() : throw Fail(ErrorCodes.ConditionalUnbalanced, token, - ".end without an .if", "remove it, or open the block with .if", "Tazel found an ending without a story."); - if (argument.Length > 0) - { - throw Fail(ErrorCodes.ConditionalUnbalanced, token, ".end takes nothing after it", "write .end alone", - "The end is the end. Nothing comes after it on the line."); - } - - closing.Branches.Add(new Branch(closing.BranchAt, closing.Condition, closing.Body)); - Current().Add(new IfNode(closing.At, closing.Branches)); + End(token, argument); break; default: - var near = Cli.Suggestions.Nearest(token.Text, ["include", "import", "pragma", "if", "elif", "else", "end"]); + var near = Cli.Suggestions.Nearest(token.Text, Directives); throw Fail(ErrorCodes.DirectiveUnknown, token, $".{token.Text} is not a directive", near is null ? "the directives are .include, .import, .pragma once, .if, .elif, .else and .end; a name that starts with a dot goes in quotes" : $"did you mean .{near}?", @@ -183,14 +162,80 @@ public static class ConfigurationParser } } - Flush(); - if (frames.Count > 0) + private void Include(Token token, string argument) { - throw Fail(ErrorCodes.ConditionalUnbalanced, frames.Peek().At, ".if without its .end", "close the block with .end", - "This .if is still waiting for the end of the story."); + var (path, global) = IncludePath(argument) ?? throw Fail(ErrorCodes.IncludePathRefused, token, + $".{token.Text} needs one path, in quotes or in angle brackets, not '{argument}'", + $"write .{token.Text} \"file\" for a file beside this one, or .{token.Text} <file> for one of TAZEL_HOME's Include directory", + "Tazel looked for a path and found an opinion instead."); + Current.Add(new IncludeNode(token, path, global, token.Text == "import")); } - return new ConfigurationDocument(root, once); + private void Pragma(Token token, string argument, bool first) + { + if (argument != "once") + { + throw Fail(ErrorCodes.DirectiveUnknown, token, $"'.pragma {argument}' is not a pragma Tazel knows", + ".pragma once is the only pragma", "Tazel has exactly one pragma, and it is used once."); + } + + if (!first) + { + throw Fail(ErrorCodes.DirectiveUnknown, token, ".pragma once must come first in its file", + "move it to the top, above everything but comments", "Once is a word that only works at the start."); + } + + _once = true; + } + + private void If(Token token) + { + if (_frames.Count >= MaxDepth) + { + throw Fail(ErrorCodes.ConditionalUnbalanced, token, $".if blocks nest more than {MaxDepth} deep", + "fold the conditions together with && and ||", "Somewhere, deep in the ifs, a condition got lost."); + } + + _frames.Push(new Frame(token, ConditionOf(token, file, text))); + } + + private void Else(Token token, string argument) + { + var frame = _frames.Count > 0 ? _frames.Peek() : throw Fail(ErrorCodes.ConditionalUnbalanced, token, + $".{token.Text} without an .if", "open the block with .if", "An answer, and nobody asked."); + if (frame.SawElse) + { + throw Fail(ErrorCodes.ConditionalUnbalanced, token, $".{token.Text} after .else", + ".else is the last branch of its .if", "After else, there is nothing else."); + } + + var isElse = token.Text == "else"; + if (isElse && argument.Length > 0) + { + throw Fail(ErrorCodes.ConditionalUnbalanced, token, ".else takes no condition", + "write .elif and the condition, or .else alone", "Else is for everything that is left; it does not choose."); + } + + frame.Close(); + frame.BranchAt = token; + frame.Test = isElse ? null : ConditionOf(token, file, text); + frame.Body = []; + frame.SawElse = isElse; + } + + private void End(Token token, string argument) + { + var closing = _frames.Count > 0 ? _frames.Pop() : throw Fail(ErrorCodes.ConditionalUnbalanced, token, + ".end without an .if", "remove it, or open the block with .if", "Tazel found an ending without a story."); + if (argument.Length > 0) + { + throw Fail(ErrorCodes.ConditionalUnbalanced, token, ".end takes nothing after it", "write .end alone", + "The end is the end. Nothing comes after it on the line."); + } + + closing.Close(); + Current.Add(new IfNode(closing.At, closing.Branches)); + } } // "file", 'file' or `file`, relative to the file that names it; <file>, below the Include directory. @@ -216,7 +261,7 @@ public static class ConfigurationParser }; } - private static Condition Condition(Token token, string file, string text) + private static Condition ConditionOf(Token token, string file, string text) { try { diff --git a/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationResolver.cs b/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationResolver.cs index aa20c1d..7e4776e 100644 --- a/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationResolver.cs +++ b/src/Progmasoft.Tazel/Dsl/Configuration/ConfigurationResolver.cs @@ -57,10 +57,10 @@ public sealed record ConfigurationContext(SystemFacts Host, SystemFacts? Target, public static class ConfigurationResolver { /// <summary>The most files one configuration reads.</summary> - public const int MaxFiles = 4096; + public static int MaxFiles { get; } = 4096; /// <summary>The most tokens one configuration holds.</summary> - public const int MaxTokens = 1_000_000; + public static int MaxTokens { get; } = 1_000_000; /// <summary> /// The tokens of <paramref name="text"/>, the file at <paramref name="path"/> that messages call @@ -110,7 +110,7 @@ public static class ConfigurationResolver _chain.Add((canonical, shown)); var first = _output.Count; - Nodes(document.Nodes, Path.GetDirectoryName(canonical)!, shown, text, origin, onTarget); + Emit(document.Nodes, Path.GetDirectoryName(canonical)!, shown, text, origin, onTarget); _chain.RemoveAt(_chain.Count - 1); // The last entry of a file may leave out its ';': it is put back, so the next file starts afresh. @@ -121,7 +121,7 @@ public static class ConfigurationResolver } } - private void Nodes(IReadOnlyList<Node> nodes, string directory, string shown, string text, Source? origin, bool onTarget) + private void Emit(IReadOnlyList<Node> nodes, string directory, string shown, string text, Source? origin, bool onTarget) { foreach (var node in nodes) { @@ -146,6 +146,8 @@ public static class ConfigurationResolver case IfNode chain: If(chain, directory, shown, text, origin, onTarget); break; + default: + throw new ArgumentException($"no node {node.GetType().Name}", nameof(nodes)); } } } @@ -177,7 +179,7 @@ public static class ConfigurationResolver if (holds == true) { - Nodes(branch.Body, directory, shown, text, origin, dependent); + Emit(branch.Body, directory, shown, text, origin, dependent); return; } } @@ -212,8 +214,9 @@ public static class ConfigurationResolver } } - private void Include(IncludeNode include, string directory, string shown, string text, Source? origin, bool onTarget) + private void Include(IncludeNode node, string directory, string shown, string text, Source? origin, bool onTarget) { + var include = node; var at = origin is null ? include.At : include.At with { Origin = origin }; var target = Target(include, directory, at, shown, text); var canonical = Canonical(target); @@ -256,39 +259,51 @@ public static class ConfigurationResolver private string Target(IncludeNode include, string directory, Token at, string shown, string text) { var path = include.Path; - CustomCommandsException Refused(string why) => Failure(ErrorCodes.IncludePathRefused, (at, shown, text), - $"'{path}' {why}", null); - if (path.IndexOfAny(['*', '?', '[', ']']) >= 0) + var refusal = Refusal(path, include.Global); + if (refusal is not null) { - throw Refused("has a wildcard; name one file"); + throw Failure(ErrorCodes.IncludePathRefused, (at, shown, text), $"'{path}' {refusal}", null); } - if (path.Contains("://", StringComparison.Ordinal)) + if (!include.Global) { - throw Refused("is a URL; Tazel includes files of this machine only"); + var full = Path.GetFullPath(Path.Combine(directory, path)); + return File.Exists(full) ? full + : throw Failure(ErrorCodes.IncludeMissing, (at, shown, text), $"{path} is not a file beside {shown}", null); } - if (Path.IsPathRooted(path) || (path.Length >= 2 && path[1] == ':')) + return Global(path, at, shown, text); + } + + // Why a path is not taken as written, or null when it is. + private static string? Refusal(string path, bool global) + { + if (path.IndexOfAny(['*', '?', '[', ']']) >= 0) { - throw Refused(include.Global ? "is absolute; a path in <> is below the Include directory" : "is absolute; a path in quotes is relative to this file"); + return "has a wildcard; name one file"; } - if (!include.Global) + if (path.Contains("://", StringComparison.Ordinal)) { - var full = Path.GetFullPath(Path.Combine(directory, path)); - if (!File.Exists(full)) - { - throw Failure(ErrorCodes.IncludeMissing, (at, shown, text), $"{path} is not a file beside {shown}", null); - } - - return full; + return "is a URL; Tazel includes files of this machine only"; } - if (path.Replace('\\', '/').Split('/').Any(part => part is ".." or ".")) + if (Path.IsPathRooted(path) || (path.Length >= 2 && path[1] == ':')) { - throw Refused("has . or .. parts; a path in <> stays below the Include directory"); + return global ? "is absolute; a path in <> is below the Include directory" : "is absolute; a path in quotes is relative to this file"; } + return global && path.Replace('\\', '/').Split('/').Any(part => part is ".." or ".") + ? "has . or .. parts; a path in <> stays below the Include directory" + : null; + } + + // A file below the family's Include directory of TAZEL_HOME, which no link may leave. + private string Global(string path, Token at, string shown, string text) + { + CustomCommandsException Refused(string why) => Failure(ErrorCodes.IncludePathRefused, (at, shown, text), + $"'{path}' {why}", null); + string root; try { diff --git a/src/Progmasoft.Tazel/Dsl/Configuration/TazelHome.cs b/src/Progmasoft.Tazel/Dsl/Configuration/TazelHome.cs index 4db91b5..ebccd6f 100644 --- a/src/Progmasoft.Tazel/Dsl/Configuration/TazelHome.cs +++ b/src/Progmasoft.Tazel/Dsl/Configuration/TazelHome.cs @@ -35,7 +35,7 @@ public sealed class ConfigurationHomeException : Exception public static class TazelHome { /// <summary>The name of the variable that sets it.</summary> - public const string Variable = "TAZEL_HOME"; + public static string Variable { get; } = "TAZEL_HOME"; /// <summary>The directory, resolved from the environment as it is now.</summary> public static string Directory() diff --git a/src/Progmasoft.Tazel/Dsl/Custom/Parser/CustomParser.cs b/src/Progmasoft.Tazel/Dsl/Custom/Parser/CustomParser.cs index 1377695..b03af0f 100644 --- a/src/Progmasoft.Tazel/Dsl/Custom/Parser/CustomParser.cs +++ b/src/Progmasoft.Tazel/Dsl/Custom/Parser/CustomParser.cs @@ -17,10 +17,16 @@ public static class CustomParser /// <summary> /// The commands of <paramref name="text"/>; <paramref name="file"/> names it in messages. Its directives are - /// resolved for <paramref name="context"/> (this machine, built for itself, by default), and a quoted include is - /// found beside <paramref name="path"/> (<paramref name="file"/> from the working directory by default). + /// resolved for <c>context</c> (this machine, built for itself, by default), and a quoted include is + /// found beside <c>path</c> (<paramref name="file"/> from the working directory by default). /// </summary> - public static IReadOnlyList<CustomCommand> Parse(string text, string file, string? path = null, Configuration.ConfigurationContext? context = null) => + public static IReadOnlyList<CustomCommand> Parse(string text, string file) => Parse(text, file, null, null); + + /// <summary><see cref="Parse(string, string)"/>, with includes found beside <paramref name="path"/>.</summary> + public static IReadOnlyList<CustomCommand> Parse(string text, string file, string? path) => Parse(text, file, path, null); + + /// <summary><see cref="Parse(string, string, string)"/>, resolved for <paramref name="context"/> when it is not null.</summary> + public static IReadOnlyList<CustomCommand> Parse(string text, string file, string? path, Configuration.ConfigurationContext? context) => new Reader(Configuration.ConfigurationResolver.Resolve(path ?? file, file, text, Configuration.ConfigurationFamily.Custom, context ?? Configuration.ConfigurationContext.ForThisMachine().ForItself()), file, text).Commands(); diff --git a/src/Progmasoft.Tazel/Dsl/Envs/TazelEnvs.cs b/src/Progmasoft.Tazel/Dsl/Envs/TazelEnvs.cs index 7b7fb2e..18d50c0 100644 --- a/src/Progmasoft.Tazel/Dsl/Envs/TazelEnvs.cs +++ b/src/Progmasoft.Tazel/Dsl/Envs/TazelEnvs.cs @@ -50,10 +50,16 @@ public static class TazelEnvsFile /// <summary> /// The entries of <paramref name="text"/>; <paramref name="file"/> names it in messages. Its directives are - /// resolved for <paramref name="context"/> (this machine, built for itself, by default), and a quoted include is - /// found beside <paramref name="path"/>. + /// resolved for <c>context</c> (this machine, built for itself, by default), and a quoted include is + /// found beside <c>path</c>. /// </summary> - public static IReadOnlyList<EnvEntry> Parse(string text, string file, string? path = null, Configuration.ConfigurationContext? context = null) + public static IReadOnlyList<EnvEntry> Parse(string text, string file) => Parse(text, file, null, null); + + /// <summary><see cref="Parse(string, string)"/>, with includes found beside <paramref name="path"/>.</summary> + public static IReadOnlyList<EnvEntry> Parse(string text, string file, string? path) => Parse(text, file, path, null); + + /// <summary><see cref="Parse(string, string, string)"/>, resolved for <paramref name="context"/> when it is not null.</summary> + public static IReadOnlyList<EnvEntry> Parse(string text, string file, string? path, Configuration.ConfigurationContext? context) { ArgumentNullException.ThrowIfNull(text); var tokens = Configuration.ConfigurationResolver.Resolve(path ?? file, file, text, Configuration.ConfigurationFamily.Envs, @@ -221,11 +227,15 @@ internal sealed class ModuleEnvironment : IEnvironmentValues /// <summary> /// The values of the module at <paramref name="root"/>, or null when it has no <c>tazel_envs.txt</c>; its - /// conditions see <paramref name="target"/> as the platform built for (this machine's when null). + /// conditions see <c>target</c> as the platform built for (this machine's when null). /// </summary> - public static ModuleEnvironment? Load(string root, string shown, Common.Platform? target = null) + public static ModuleEnvironment? Load(string root, string shown) => Load(root, shown, null); + + /// <summary><see cref="Load(string, string)"/>, its conditions read for <paramref name="target"/>, else this machine.</summary> + public static ModuleEnvironment? Load(string root, string shown, Common.Platform? target) { - var file = Path.Combine(root, TazelEnvsFile.FileName); + // Join, not Combine: the file stays below the root whatever the name holds. + var file = Path.Join(root, TazelEnvsFile.FileName); if (!File.Exists(file)) { return null; diff --git a/src/Progmasoft.Tazel/Dsl/Options/TazelOptions.cs b/src/Progmasoft.Tazel/Dsl/Options/TazelOptions.cs index 05be515..a20bd08 100644 --- a/src/Progmasoft.Tazel/Dsl/Options/TazelOptions.cs +++ b/src/Progmasoft.Tazel/Dsl/Options/TazelOptions.cs @@ -92,11 +92,17 @@ public static class TazelOptionsFile /// <summary> /// The options of <paramref name="text"/>; <paramref name="file"/> names it in messages and a quoted include is - /// found beside <paramref name="path"/>. It is read twice: first without what depends on <c>target_system</c>, which - /// chooses the platform (<paramref name="requested"/>, else the file's, else this machine's), then for that + /// found beside <c>path</c>. It is read twice: first without what depends on <c>target_system</c>, which + /// chooses the platform (<c>requested</c>, else the file's, else this machine's), then for that /// platform. A platform chosen inside a condition on <c>target_system</c> is an error. /// </summary> - public static TazelOptions Parse(string text, string file, string? path = null, Platform? requested = null) + public static TazelOptions Parse(string text, string file) => Parse(text, file, null, null); + + /// <summary><see cref="Parse(string, string)"/>, with includes found beside <paramref name="path"/>.</summary> + public static TazelOptions Parse(string text, string file, string? path) => Parse(text, file, path, null); + + /// <summary><see cref="Parse(string, string, string)"/>, for the platform <paramref name="requested"/> when it is not null.</summary> + public static TazelOptions Parse(string text, string file, string? path, Platform? requested) { var machine = Configuration.ConfigurationContext.ForThisMachine(); var chosen = Read(text, file, path, machine); @@ -163,7 +169,10 @@ public static class TazelOptionsFile /// for in any file above stays asked for, so a project cannot turn off what a directory around it requires. /// Default options, and no file, without any. /// </summary> - public static (TazelOptions Options, string? SandboxFile) Effective(string directory, Platform? requested = null) + public static (TazelOptions Options, string? SandboxFile) Effective(string directory) => Effective(directory, null); + + /// <summary><see cref="Effective(string)"/>, for the platform <paramref name="requested"/> when it is not null.</summary> + public static (TazelOptions Options, string? SandboxFile) Effective(string directory, Platform? requested) { var files = Find(directory).Select(file => (File: file, Options: Read(file, requested))).ToList(); if (files.Count == 0)