1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:27:44 +00:00

Shell: Add support for brace expansions

This adds support for (basic) brace expansions with the following
syntaxes:
- `{expr?,expr?,expr?,...}` which is directly equivalent to `(expr expr
  expr ...)`, with the missing expressions replaced with an empty string
  literal.
- `{expr..expr}` which is a new range expansion, with two modes:
    - if both expressions are one unicode code point long, the range is
      equivalent to the two code points and all code points between the
      two (numerically).
    - if both expressions are numeric, the range is equivalent to both
      numbers, and all numbers between the two.
    - otherwise, it is equivalent to `(expr expr)`.

Closes #3832.
This commit is contained in:
AnotherTest 2020-10-24 18:13:02 +03:30 committed by Andreas Kling
parent 567f2f3548
commit 5640e1bc3a
9 changed files with 374 additions and 3 deletions

View file

@ -190,6 +190,25 @@ void Formatter::visit(const AST::BarewordLiteral* node)
visited(node);
}
void Formatter::visit(const AST::BraceExpansion* node)
{
will_visit(node);
test_and_update_output_cursor(node);
current_builder().append('{');
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
bool first = true;
for (auto& entry : node->entries()) {
if (!first)
current_builder().append(',');
first = false;
entry.visit(*this);
}
current_builder().append('}');
visited(node);
}
void Formatter::visit(const AST::CastToCommand* node)
{
will_visit(node);
@ -480,6 +499,21 @@ void Formatter::visit(const AST::Pipe* node)
visited(node);
}
void Formatter::visit(const AST::Range* node)
{
will_visit(node);
test_and_update_output_cursor(node);
current_builder().append('{');
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
node->start()->visit(*this);
current_builder().append("..");
node->end()->visit(*this);
current_builder().append('}');
visited(node);
}
void Formatter::visit(const AST::ReadRedirection* node)
{
will_visit(node);