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

Shell: Add support for regex match patterns

We previously allowed globs as match pattern, but for more complex
matching needs, it's nice to have regular expressions.
And as the existing "name a part of the match" concept maps nicely to
named capture groups, we can simply reuse the same code and make groups
with names available in the match body.
This commit is contained in:
Ali Mohammad Pur 2022-04-15 01:50:36 +04:30 committed by Ali Mohammad Pur
parent 6aceec4535
commit 4ede121d31
6 changed files with 219 additions and 71 deletions

View file

@ -25,7 +25,7 @@ public:
RefPtr<AST::Node> parse();
/// Parse the given string *as* an expression
/// that is to forefully enclose it in double-quotes.
/// that is to forcefully enclose it in double-quotes.
RefPtr<AST::Node> parse_as_single_expression();
NonnullRefPtrVector<AST::Node> parse_as_multiple_expressions();
@ -77,6 +77,7 @@ private:
RefPtr<AST::Node> parse_match_expr();
AST::MatchEntry parse_match_entry();
RefPtr<AST::Node> parse_match_pattern();
Optional<Regex<ECMA262>> parse_regex_pattern();
RefPtr<AST::Node> parse_redirection();
RefPtr<AST::Node> parse_list_expression();
RefPtr<AST::Node> parse_expression();
@ -98,7 +99,7 @@ private:
bool parse_heredoc_entries();
template<typename A, typename... Args>
NonnullRefPtr<A> create(Args... args);
NonnullRefPtr<A> create(Args&&... args);
void set_end_condition(OwnPtr<Function<bool()>> condition) { m_end_condition = move(condition); }
bool at_end() const
@ -228,10 +229,15 @@ subshell :: '{' toplevel '}'
match_expr :: 'match' ws+ expression ws* ('as' ws+ identifier)? '{' match_entry* '}'
match_entry :: match_pattern ws* (as identifier_list)? '{' toplevel '}'
| regex_pattern ws* '{' toplevel '}'
identifier_list :: '(' (identifier ws*)* ')'
match_pattern :: expression (ws* '|' ws* expression)*
regex_pattern :: regex_pattern (ws* '|' ws* regex_pattern)*
match_pattern :: expression (ws* '|' ws* expression)*
regex_pattern :: '(?:' .* ')' { enclosed string must contain balanced parentheses }
command :: redirection command
| list_expression command?