mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
Shell: Document the new 'pattern as (list of names)' match syntax
This commit is contained in:
parent
1a4ac3531f
commit
3fa0b887ed
1 changed files with 14 additions and 2 deletions
|
@ -266,6 +266,7 @@ $ fn 1 2 3 4
|
||||||
The pattern matching construct `match` shall choose from a sequence of patterns, and execute the corresponding action in a new frame.
|
The pattern matching construct `match` shall choose from a sequence of patterns, and execute the corresponding action in a new frame.
|
||||||
The choice is done by matching the result of the _matched expression_ (after expansion) against the _patterns_ (expanded down to either globs or literals).
|
The choice is done by matching the result of the _matched expression_ (after expansion) against the _patterns_ (expanded down to either globs or literals).
|
||||||
Multiple _patterns_ can be attributed to a single given action by delimiting them with a pipe ('|') symbol.
|
Multiple _patterns_ can be attributed to a single given action by delimiting them with a pipe ('|') symbol.
|
||||||
|
A _pattern_ (or the series of) may be annotated with an extra `as (...)` clause, which allows globbed parts of the matching pattern to be named and used in the matching block.
|
||||||
|
|
||||||
The expanded _matched expression_ can optionally be given a name using the `as name` clause after the _matched expression_, with which it may be accessible in the action clauses.
|
The expanded _matched expression_ can optionally be given a name using the `as name` clause after the _matched expression_, with which it may be accessible in the action clauses.
|
||||||
|
|
||||||
|
@ -277,10 +278,12 @@ match "$(make_some_value)" as value {
|
||||||
(say\ *) { echo "No, I will not $value" }
|
(say\ *) { echo "No, I will not $value" }
|
||||||
}
|
}
|
||||||
|
|
||||||
# Match the result of running 'make_some_value', cast to a string.
|
# Match the result of running 'make_some_value', cast to a string
|
||||||
|
# Note the `as (expr)` in the second pattern, which assigns whatever the `*` matches
|
||||||
|
# to the name `expr` inside the block.
|
||||||
match "$(make_some_value)" {
|
match "$(make_some_value)" {
|
||||||
hello* { echo "Hi!" }
|
hello* { echo "Hi!" }
|
||||||
say\ * { echo "No, I will not!" }
|
say\ * as (expr) { echo "No, I will not say $expr!" }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -318,6 +321,7 @@ pipe_sequence :: command '|' pipe_sequence
|
||||||
control_structure :: for_expr
|
control_structure :: for_expr
|
||||||
| if_expr
|
| if_expr
|
||||||
| subshell
|
| subshell
|
||||||
|
| match_expr
|
||||||
|
|
||||||
for_expr :: 'for' ws+ (identifier ' '+ 'in' ws*)? expression ws+ '{' toplevel '}'
|
for_expr :: 'for' ws+ (identifier ' '+ 'in' ws*)? expression ws+ '{' toplevel '}'
|
||||||
|
|
||||||
|
@ -328,6 +332,14 @@ else_clause :: else '{' toplevel '}'
|
||||||
|
|
||||||
subshell :: '{' toplevel '}'
|
subshell :: '{' toplevel '}'
|
||||||
|
|
||||||
|
match_expr :: 'match' ws+ expression ws* ('as' ws+ identifier)? '{' match_entry* '}'
|
||||||
|
|
||||||
|
match_entry :: match_pattern ws* (as identifier_list)? '{' toplevel '}'
|
||||||
|
|
||||||
|
identifier_list :: '(' (identifier ws*)* ')'
|
||||||
|
|
||||||
|
match_pattern :: expression (ws* '|' ws* expression)*
|
||||||
|
|
||||||
command :: redirection command
|
command :: redirection command
|
||||||
| list_expression command?
|
| list_expression command?
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue