Skip to content

Commit f680245

Browse files
refactor: extract an OpenSegment helper in SplitIntoSegments (#35)
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.
1 parent 664c72a commit f680245

1 file changed

Lines changed: 15 additions & 34 deletions

File tree

src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -631,15 +631,20 @@ private static List<Segment> SplitIntoSegments(
631631
var segments = new List<Segment>();
632632
var subshellStack = new List<int> { 0 }; // ID 0 is the top-level command.
633633
var nextSubshellId = 1;
634+
var depth = 0;
634635

635-
var current = new Segment
636+
// Shared factory for the segment opened at every clause boundary:
637+
// FromSubshell / SubshellDepth / SubshellStack are uniform (driven
638+
// by the current `depth`), so only the preceding operator varies.
639+
Segment OpenSegment(CompoundOperator precedingOperator) => new()
636640
{
637-
PrecedingOperator = CompoundOperator.None,
638-
SubshellDepth = 0,
641+
PrecedingOperator = precedingOperator,
642+
FromSubshell = depth > 0,
643+
SubshellDepth = depth,
639644
SubshellStack = subshellStack.ToArray(),
640645
};
641646

642-
var depth = 0;
647+
var current = OpenSegment(CompoundOperator.None);
643648

644649
for (var i = 0; i < tokens.Count; i++)
645650
{
@@ -658,13 +663,7 @@ private static List<Segment> SplitIntoSegments(
658663
}
659664

660665
segments.Add(current);
661-
current = new Segment
662-
{
663-
PrecedingOperator = CompoundOperator.Sequence,
664-
FromSubshell = depth > 0,
665-
SubshellDepth = depth,
666-
SubshellStack = subshellStack.ToArray(),
667-
};
666+
current = OpenSegment(CompoundOperator.Sequence);
668667
continue;
669668
}
670669

@@ -681,15 +680,9 @@ private static List<Segment> SplitIntoSegments(
681680

682681
depth++;
683682
subshellStack.Add(nextSubshellId++);
684-
current = new Segment
685-
{
686-
PrecedingOperator = current.Tokens.Count > 0
687-
? CompoundOperator.Sequence
688-
: current.PrecedingOperator,
689-
FromSubshell = true,
690-
SubshellDepth = depth,
691-
SubshellStack = subshellStack.ToArray(),
692-
};
683+
current = OpenSegment(current.Tokens.Count > 0
684+
? CompoundOperator.Sequence
685+
: current.PrecedingOperator);
693686
continue;
694687
}
695688

@@ -709,13 +702,7 @@ private static List<Segment> SplitIntoSegments(
709702
segments.Add(current);
710703
}
711704

712-
current = new Segment
713-
{
714-
PrecedingOperator = CompoundOperator.None,
715-
FromSubshell = depth > 0,
716-
SubshellDepth = depth,
717-
SubshellStack = subshellStack.ToArray(),
718-
};
705+
current = OpenSegment(CompoundOperator.None);
719706
continue;
720707
}
721708

@@ -732,13 +719,7 @@ private static List<Segment> SplitIntoSegments(
732719
segments.Add(current);
733720
}
734721

735-
current = new Segment
736-
{
737-
PrecedingOperator = MapOperator(op),
738-
FromSubshell = depth > 0,
739-
SubshellDepth = depth,
740-
SubshellStack = subshellStack.ToArray(),
741-
};
722+
current = OpenSegment(MapOperator(op));
742723
continue;
743724
}
744725

0 commit comments

Comments
 (0)