From 4fa768630398f10b0c09549f2bb1ec417b445d4f Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 15 May 2026 17:45:44 +0000 Subject: [PATCH] refactor: extract an OpenSegment helper in SplitIntoSegments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SplitIntoSegments opened a `new Segment { ... }` at five clause-boundary sites with a near-identical four-field initializer. FromSubshell, SubshellDepth, and SubshellStack are uniform across all five — each is a function of the current `depth` — so a one-parameter local OpenSegment helper collapses them; only the preceding operator varies per call site. Behavior-preserving: the `(` branch's former `FromSubshell = true` equals `depth > 0` because it runs after `depth++`, and the initial segment's omitted FromSubshell equals `depth > 0` at depth 0. No public API or parsed-AST change. --- .../Bash/Parsing/BashCommandParser.cs | 49 ++++++------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs b/src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs index 649ffbd..6511414 100644 --- a/src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs +++ b/src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs @@ -631,15 +631,20 @@ private static List SplitIntoSegments( var segments = new List(); var subshellStack = new List { 0 }; // ID 0 is the top-level command. var nextSubshellId = 1; + var depth = 0; - var current = new Segment + // Shared factory for the segment opened at every clause boundary: + // FromSubshell / SubshellDepth / SubshellStack are uniform (driven + // by the current `depth`), so only the preceding operator varies. + Segment OpenSegment(CompoundOperator precedingOperator) => new() { - PrecedingOperator = CompoundOperator.None, - SubshellDepth = 0, + PrecedingOperator = precedingOperator, + FromSubshell = depth > 0, + SubshellDepth = depth, SubshellStack = subshellStack.ToArray(), }; - var depth = 0; + var current = OpenSegment(CompoundOperator.None); for (var i = 0; i < tokens.Count; i++) { @@ -658,13 +663,7 @@ private static List SplitIntoSegments( } segments.Add(current); - current = new Segment - { - PrecedingOperator = CompoundOperator.Sequence, - FromSubshell = depth > 0, - SubshellDepth = depth, - SubshellStack = subshellStack.ToArray(), - }; + current = OpenSegment(CompoundOperator.Sequence); continue; } @@ -681,15 +680,9 @@ private static List SplitIntoSegments( depth++; subshellStack.Add(nextSubshellId++); - current = new Segment - { - PrecedingOperator = current.Tokens.Count > 0 - ? CompoundOperator.Sequence - : current.PrecedingOperator, - FromSubshell = true, - SubshellDepth = depth, - SubshellStack = subshellStack.ToArray(), - }; + current = OpenSegment(current.Tokens.Count > 0 + ? CompoundOperator.Sequence + : current.PrecedingOperator); continue; } @@ -709,13 +702,7 @@ private static List SplitIntoSegments( segments.Add(current); } - current = new Segment - { - PrecedingOperator = CompoundOperator.None, - FromSubshell = depth > 0, - SubshellDepth = depth, - SubshellStack = subshellStack.ToArray(), - }; + current = OpenSegment(CompoundOperator.None); continue; } @@ -732,13 +719,7 @@ private static List SplitIntoSegments( segments.Add(current); } - current = new Segment - { - PrecedingOperator = MapOperator(op), - FromSubshell = depth > 0, - SubshellDepth = depth, - SubshellStack = subshellStack.ToArray(), - }; + current = OpenSegment(MapOperator(op)); continue; }