mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 09:46:04 -05:00
Shell: Document the new 'pattern as (list of names)' match syntax
This commit is contained in:
parent
1a4ac3531f
commit
3fa0b887ed
Notes:
sideshowbarker
2024-07-19 01:39:30 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/3fa0b887ed9 Pull-request: https://github.com/SerenityOS/serenity/pull/3858
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 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.
|
||||
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.
|
||||
|
||||
|
@ -277,10 +278,12 @@ match "$(make_some_value)" as 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)" {
|
||||
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
|
||||
| if_expr
|
||||
| subshell
|
||||
| match_expr
|
||||
|
||||
for_expr :: 'for' ws+ (identifier ' '+ 'in' ws*)? expression ws+ '{' toplevel '}'
|
||||
|
||||
|
@ -328,6 +332,14 @@ else_clause :: else '{' 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
|
||||
| list_expression command?
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue