Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 15 additions & 34 deletions src/ShellSyntaxTree/Internal/Bash/Parsing/BashCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,20 @@ private static List<Segment> SplitIntoSegments(
var segments = new List<Segment>();
var subshellStack = new List<int> { 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++)
{
Expand All @@ -658,13 +663,7 @@ private static List<Segment> SplitIntoSegments(
}

segments.Add(current);
current = new Segment
{
PrecedingOperator = CompoundOperator.Sequence,
FromSubshell = depth > 0,
SubshellDepth = depth,
SubshellStack = subshellStack.ToArray(),
};
current = OpenSegment(CompoundOperator.Sequence);
continue;
}

Expand All @@ -681,15 +680,9 @@ private static List<Segment> 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;
}

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

current = new Segment
{
PrecedingOperator = CompoundOperator.None,
FromSubshell = depth > 0,
SubshellDepth = depth,
SubshellStack = subshellStack.ToArray(),
};
current = OpenSegment(CompoundOperator.None);
continue;
}

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

current = new Segment
{
PrecedingOperator = MapOperator(op),
FromSubshell = depth > 0,
SubshellDepth = depth,
SubshellStack = subshellStack.ToArray(),
};
current = OpenSegment(MapOperator(op));
continue;
}

Expand Down
Loading