2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-06-17 16:35:06 +03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-05-07 02:12:08 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
#include "AST.h"
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/RefPtr.h>
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2020-06-17 16:35:06 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-07 02:12:08 +03:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2020-10-01 17:43:01 +03:00
|
|
|
namespace Shell {
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
class Parser {
|
|
|
|
public:
|
2021-03-05 15:44:53 +03:00
|
|
|
Parser(StringView input, bool interactive = false)
|
2020-06-17 16:35:06 +03:00
|
|
|
: m_input(move(input))
|
2021-03-05 15:44:53 +03:00
|
|
|
, m_in_interactive_mode(interactive)
|
2020-06-17 16:35:06 +03:00
|
|
|
{
|
|
|
|
}
|
2020-05-10 09:05:23 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse();
|
2021-01-11 12:34:59 +03:00
|
|
|
/// Parse the given string *as* an expression
|
2022-04-15 00:20:36 +03:00
|
|
|
/// that is to forcefully enclose it in double-quotes.
|
2021-01-11 12:34:59 +03:00
|
|
|
RefPtr<AST::Node> parse_as_single_expression();
|
|
|
|
NonnullRefPtrVector<AST::Node> parse_as_multiple_expressions();
|
2020-05-24 21:30:46 +03:00
|
|
|
|
2020-09-28 13:57:20 +03:00
|
|
|
struct SavedOffset {
|
|
|
|
size_t offset;
|
|
|
|
AST::Position::Line line;
|
|
|
|
};
|
|
|
|
SavedOffset save_offset() const;
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
private:
|
2021-01-16 22:50:52 +03:00
|
|
|
enum class ShouldReadMoreSequences {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
2021-08-12 00:40:26 +03:00
|
|
|
|
|
|
|
enum class StringEndCondition {
|
|
|
|
DoubleQuote,
|
|
|
|
Heredoc,
|
|
|
|
};
|
|
|
|
|
2021-01-16 22:50:52 +03:00
|
|
|
struct SequenceParseResult {
|
|
|
|
NonnullRefPtrVector<AST::Node> entries;
|
|
|
|
Vector<AST::Position, 1> separator_positions;
|
|
|
|
ShouldReadMoreSequences decision;
|
|
|
|
};
|
|
|
|
|
2021-04-29 05:34:00 +03:00
|
|
|
struct HeredocInitiationRecord {
|
|
|
|
String end;
|
|
|
|
RefPtr<AST::Heredoc> node;
|
|
|
|
bool interpolate { false };
|
|
|
|
bool deindent { false };
|
|
|
|
};
|
|
|
|
|
2020-11-30 17:02:48 +03:00
|
|
|
constexpr static size_t max_allowed_nested_rule_depth = 2048;
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_toplevel();
|
2021-01-16 22:50:52 +03:00
|
|
|
SequenceParseResult parse_sequence();
|
2020-09-13 14:24:33 +03:00
|
|
|
RefPtr<AST::Node> parse_function_decl();
|
2020-07-12 00:12:46 +03:00
|
|
|
RefPtr<AST::Node> parse_and_logical_sequence();
|
|
|
|
RefPtr<AST::Node> parse_or_logical_sequence();
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_variable_decls();
|
|
|
|
RefPtr<AST::Node> parse_pipe_sequence();
|
|
|
|
RefPtr<AST::Node> parse_command();
|
2020-07-12 00:12:46 +03:00
|
|
|
RefPtr<AST::Node> parse_control_structure();
|
2020-12-10 17:55:13 +03:00
|
|
|
RefPtr<AST::Node> parse_continuation_control();
|
2020-07-12 00:12:46 +03:00
|
|
|
RefPtr<AST::Node> parse_for_loop();
|
2020-12-10 17:55:13 +03:00
|
|
|
RefPtr<AST::Node> parse_loop_loop();
|
2020-08-11 10:35:46 +03:00
|
|
|
RefPtr<AST::Node> parse_if_expr();
|
2020-09-08 14:29:07 +03:00
|
|
|
RefPtr<AST::Node> parse_subshell();
|
2020-09-14 18:02:21 +03:00
|
|
|
RefPtr<AST::Node> parse_match_expr();
|
|
|
|
AST::MatchEntry parse_match_entry();
|
|
|
|
RefPtr<AST::Node> parse_match_pattern();
|
2022-04-15 00:20:36 +03:00
|
|
|
Optional<Regex<ECMA262>> parse_regex_pattern();
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_redirection();
|
|
|
|
RefPtr<AST::Node> parse_list_expression();
|
|
|
|
RefPtr<AST::Node> parse_expression();
|
|
|
|
RefPtr<AST::Node> parse_string_composite();
|
|
|
|
RefPtr<AST::Node> parse_string();
|
2021-08-12 00:40:26 +03:00
|
|
|
RefPtr<AST::Node> parse_string_inner(StringEndCondition);
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_variable();
|
2021-03-13 02:40:18 +03:00
|
|
|
RefPtr<AST::Node> parse_variable_ref();
|
2021-09-03 20:11:51 +03:00
|
|
|
RefPtr<AST::Slice> parse_slice();
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_evaluate();
|
2021-01-11 12:34:59 +03:00
|
|
|
RefPtr<AST::Node> parse_history_designator();
|
2020-06-17 16:35:06 +03:00
|
|
|
RefPtr<AST::Node> parse_comment();
|
|
|
|
RefPtr<AST::Node> parse_bareword();
|
|
|
|
RefPtr<AST::Node> parse_glob();
|
2020-10-24 17:43:02 +03:00
|
|
|
RefPtr<AST::Node> parse_brace_expansion();
|
|
|
|
RefPtr<AST::Node> parse_brace_expansion_spec();
|
2021-03-05 16:03:23 +03:00
|
|
|
RefPtr<AST::Node> parse_immediate_expression();
|
2021-04-29 05:34:00 +03:00
|
|
|
RefPtr<AST::Node> parse_heredoc_initiation_record();
|
|
|
|
bool parse_heredoc_entries();
|
2020-06-17 16:35:06 +03:00
|
|
|
|
|
|
|
template<typename A, typename... Args>
|
2022-04-15 00:20:36 +03:00
|
|
|
NonnullRefPtr<A> create(Args&&... args);
|
2020-06-17 16:35:06 +03:00
|
|
|
|
2021-05-19 19:38:23 +03:00
|
|
|
void set_end_condition(OwnPtr<Function<bool()>> condition) { m_end_condition = move(condition); }
|
2021-04-29 05:34:00 +03:00
|
|
|
bool at_end() const
|
|
|
|
{
|
2021-05-19 19:38:23 +03:00
|
|
|
if (m_end_condition && (*m_end_condition)())
|
2021-04-29 05:34:00 +03:00
|
|
|
return true;
|
|
|
|
return m_input.length() <= m_offset;
|
|
|
|
}
|
2020-06-17 16:35:06 +03:00
|
|
|
char peek();
|
|
|
|
char consume();
|
|
|
|
bool expect(char);
|
2021-11-11 02:55:02 +03:00
|
|
|
bool expect(StringView);
|
|
|
|
bool next_is(StringView);
|
2020-06-17 16:35:06 +03:00
|
|
|
|
2020-09-28 13:57:20 +03:00
|
|
|
void restore_to(size_t offset, AST::Position::Line line)
|
|
|
|
{
|
|
|
|
m_offset = offset;
|
|
|
|
m_line = move(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST::Position::Line line() const { return m_line; }
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
StringView consume_while(Function<bool(char)>);
|
|
|
|
|
2021-01-16 22:50:52 +03:00
|
|
|
struct Offset {
|
|
|
|
size_t offset;
|
|
|
|
AST::Position::Line line;
|
|
|
|
};
|
2020-06-17 16:35:06 +03:00
|
|
|
struct ScopedOffset {
|
2020-09-28 13:57:20 +03:00
|
|
|
ScopedOffset(Vector<size_t>& offsets, Vector<AST::Position::Line>& lines, size_t offset, size_t lineno, size_t linecol)
|
2020-06-17 16:35:06 +03:00
|
|
|
: offsets(offsets)
|
2020-09-28 13:57:20 +03:00
|
|
|
, lines(lines)
|
2020-06-17 16:35:06 +03:00
|
|
|
, offset(offset)
|
2020-09-28 13:57:20 +03:00
|
|
|
, line({ lineno, linecol })
|
2020-06-17 16:35:06 +03:00
|
|
|
{
|
|
|
|
offsets.append(offset);
|
2020-09-28 13:57:20 +03:00
|
|
|
lines.append(line);
|
2020-06-17 16:35:06 +03:00
|
|
|
}
|
|
|
|
~ScopedOffset()
|
|
|
|
{
|
|
|
|
auto last = offsets.take_last();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(last == offset);
|
2020-09-28 13:57:20 +03:00
|
|
|
|
|
|
|
auto last_line = lines.take_last();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(last_line == line);
|
2020-06-17 16:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<size_t>& offsets;
|
2020-09-28 13:57:20 +03:00
|
|
|
Vector<AST::Position::Line>& lines;
|
2020-06-17 16:35:06 +03:00
|
|
|
size_t offset;
|
2020-09-28 13:57:20 +03:00
|
|
|
AST::Position::Line line;
|
2019-05-28 12:53:16 +03:00
|
|
|
};
|
2019-05-07 02:12:08 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void restore_to(ScopedOffset const& offset) { restore_to(offset.offset, offset.line); }
|
2020-09-28 13:57:20 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
OwnPtr<ScopedOffset> push_start();
|
2021-01-16 22:50:52 +03:00
|
|
|
Offset current_position();
|
2019-06-04 21:36:08 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
StringView m_input;
|
|
|
|
size_t m_offset { 0 };
|
2020-09-28 13:57:20 +03:00
|
|
|
|
|
|
|
AST::Position::Line m_line { 0, 0 };
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
Vector<size_t> m_rule_start_offsets;
|
2020-09-28 13:57:20 +03:00
|
|
|
Vector<AST::Position::Line> m_rule_start_lines;
|
2020-10-24 17:43:02 +03:00
|
|
|
|
2021-05-19 19:38:23 +03:00
|
|
|
OwnPtr<Function<bool()>> m_end_condition;
|
2021-04-29 05:34:00 +03:00
|
|
|
Vector<HeredocInitiationRecord> m_heredoc_initiations;
|
2021-01-11 12:34:59 +03:00
|
|
|
Vector<char> m_extra_chars_not_allowed_in_barewords;
|
2020-10-24 17:43:02 +03:00
|
|
|
bool m_is_in_brace_expansion_spec { false };
|
2020-12-10 17:55:13 +03:00
|
|
|
bool m_continuation_controls_allowed { false };
|
2021-03-05 15:44:53 +03:00
|
|
|
bool m_in_interactive_mode { false };
|
2019-05-07 02:12:08 +03:00
|
|
|
};
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
#if 0
|
|
|
|
constexpr auto the_grammar = R"(
|
|
|
|
toplevel :: sequence?
|
2019-08-30 07:54:05 +03:00
|
|
|
|
2020-07-12 00:12:46 +03:00
|
|
|
sequence :: variable_decls? or_logical_sequence terminator sequence
|
|
|
|
| variable_decls? or_logical_sequence '&' sequence
|
|
|
|
| variable_decls? or_logical_sequence
|
2020-09-13 14:24:33 +03:00
|
|
|
| variable_decls? function_decl (terminator sequence)?
|
2020-07-12 00:12:46 +03:00
|
|
|
| variable_decls? terminator sequence
|
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
function_decl :: identifier '(' (ws* identifier)* ')' ws* '{' [!c] toplevel '}'
|
2020-09-13 14:24:33 +03:00
|
|
|
|
2020-07-12 00:12:46 +03:00
|
|
|
or_logical_sequence :: and_logical_sequence '|' '|' and_logical_sequence
|
|
|
|
| and_logical_sequence
|
|
|
|
|
|
|
|
and_logical_sequence :: pipe_sequence '&' '&' and_logical_sequence
|
|
|
|
| pipe_sequence
|
2020-06-28 17:13:37 +03:00
|
|
|
|
|
|
|
terminator :: ';'
|
2021-04-29 05:34:00 +03:00
|
|
|
| '\n' [?!heredoc_stack.is_empty] heredoc_entries
|
|
|
|
|
|
|
|
heredoc_entries :: { .*? (heredoc_entry) '\n' } [each heredoc_entries]
|
2019-05-07 02:12:08 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
variable_decls :: identifier '=' expression (' '+ variable_decls)? ' '*
|
2020-06-22 14:07:20 +03:00
|
|
|
| identifier '=' '(' pipe_sequence ')' (' '+ variable_decls)? ' '*
|
2019-05-07 02:12:08 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
pipe_sequence :: command '|' pipe_sequence
|
|
|
|
| command
|
2020-09-07 19:19:53 +03:00
|
|
|
| control_structure '|' pipe_sequence
|
|
|
|
| control_structure
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
control_structure[c] :: for_expr
|
|
|
|
| loop_expr
|
|
|
|
| if_expr
|
|
|
|
| subshell
|
|
|
|
| match_expr
|
|
|
|
| ?c: continuation_control
|
2020-09-08 14:29:07 +03:00
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
continuation_control :: 'break'
|
|
|
|
| 'continue'
|
|
|
|
|
2021-03-05 17:55:09 +03:00
|
|
|
for_expr :: 'for' ws+ (('index' ' '+ identifier ' '+)? identifier ' '+ 'in' ws*)? expression ws+ '{' [c] toplevel '}'
|
2020-12-10 17:55:13 +03:00
|
|
|
|
|
|
|
loop_expr :: 'loop' ws* '{' [c] toplevel '}'
|
2020-07-16 21:04:30 +03:00
|
|
|
|
2020-08-11 10:35:46 +03:00
|
|
|
if_expr :: 'if' ws+ or_logical_sequence ws+ '{' toplevel '}' else_clause?
|
|
|
|
|
|
|
|
else_clause :: else '{' toplevel '}'
|
|
|
|
| else if_expr
|
2020-07-12 00:12:46 +03:00
|
|
|
|
2020-09-08 14:29:07 +03:00
|
|
|
subshell :: '{' toplevel '}'
|
|
|
|
|
2020-09-14 18:02:21 +03:00
|
|
|
match_expr :: 'match' ws+ expression ws* ('as' ws+ identifier)? '{' match_entry* '}'
|
|
|
|
|
2020-10-25 10:12:03 +03:00
|
|
|
match_entry :: match_pattern ws* (as identifier_list)? '{' toplevel '}'
|
2022-04-15 00:20:36 +03:00
|
|
|
| regex_pattern ws* '{' toplevel '}'
|
2020-10-25 10:12:03 +03:00
|
|
|
|
|
|
|
identifier_list :: '(' (identifier ws*)* ')'
|
2020-09-14 18:02:21 +03:00
|
|
|
|
2022-04-15 00:20:36 +03:00
|
|
|
regex_pattern :: regex_pattern (ws* '|' ws* regex_pattern)*
|
|
|
|
|
|
|
|
match_pattern :: expression (ws* '|' ws* expression)*
|
|
|
|
|
|
|
|
regex_pattern :: '(?:' .* ')' { enclosed string must contain balanced parentheses }
|
2020-09-14 18:02:21 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
command :: redirection command
|
|
|
|
| list_expression command?
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
redirection :: number? '>'{1,2} ' '* string_composite
|
|
|
|
| number? '<' ' '* string_composite
|
|
|
|
| number? '>' '&' number
|
2020-06-28 17:13:37 +03:00
|
|
|
| number? '>' '&' '-'
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
list_expression :: ' '* expression (' '+ list_expression)?
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2020-06-20 16:30:45 +03:00
|
|
|
expression :: evaluate expression?
|
|
|
|
| string_composite expression?
|
2020-10-03 00:14:37 +03:00
|
|
|
| comment expression?
|
2021-03-05 16:03:23 +03:00
|
|
|
| immediate_expression expression?
|
2021-01-11 12:34:59 +03:00
|
|
|
| history_designator expression?
|
2020-06-20 16:30:45 +03:00
|
|
|
| '(' list_expression ')' expression?
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2020-06-21 23:00:14 +03:00
|
|
|
evaluate :: '$' '(' pipe_sequence ')'
|
Shell: Prevent exponential explosion around '$(('
When parse_expression looks at '$((', there are two ways it can end up
in parse_expression again, three consumed characters later. All these
ways fail, so what happened was that the parser tried all possible
combinations, hence taking potentially an exponential amount of time.
1. parse_evaluate swallows the '$(', a new invocation of
parse_expression swallows the other '(', and through
parse_list_expression we're at another parse_expression.
2. parse_evaluate swallows the '$(', but returns a SyntaxError.
parse_expression used to not recognize the error, and treated it as a
regular AST node, calling into read_concat, then a new invocation of
parse_expression swallows the other '(', and through
parse_list_expression we're at another parse_expression.
Fixes #10561.
Found by OSS Fuzz, long-standing issue
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28113
2021-10-21 23:55:52 +03:00
|
|
|
| '$' [lookahead != '('] expression {eval / dynamic resolve}
|
2019-05-07 02:12:08 +03:00
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
string_composite :: string string_composite?
|
|
|
|
| variable string_composite?
|
|
|
|
| bareword string_composite?
|
|
|
|
| glob string_composite?
|
2020-10-24 17:43:02 +03:00
|
|
|
| brace_expansion string_composite?
|
2021-04-29 05:34:00 +03:00
|
|
|
| heredoc_initiator string_composite? {append to heredoc_entries}
|
|
|
|
|
|
|
|
heredoc_initiator :: '<' '<' '-' bareword {*bareword, interpolate, no deindent}
|
|
|
|
| '<' '<' '-' "'" [^']* "'" {*string, no interpolate, no deindent}
|
|
|
|
| '<' '<' '~' bareword {*bareword, interpolate, deindent}
|
|
|
|
| '<' '<' '~' "'" [^']* "'" {*bareword, no interpolate, deindent}
|
2020-06-17 16:35:06 +03:00
|
|
|
|
|
|
|
string :: '"' dquoted_string_inner '"'
|
|
|
|
| "'" [^']* "'"
|
|
|
|
|
|
|
|
dquoted_string_inner :: '\' . dquoted_string_inner? {concat}
|
|
|
|
| variable dquoted_string_inner? {compose}
|
|
|
|
| . dquoted_string_inner?
|
2021-05-10 10:00:42 +03:00
|
|
|
| '\' 'x' xdigit*2 dquoted_string_inner?
|
|
|
|
| '\' 'u' xdigit*8 dquoted_string_inner?
|
2021-05-10 10:16:21 +03:00
|
|
|
| '\' [abefrnt] dquoted_string_inner?
|
2020-06-17 16:35:06 +03:00
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
variable :: variable_ref slice?
|
|
|
|
|
|
|
|
variable_ref :: '$' identifier
|
2020-06-17 16:35:06 +03:00
|
|
|
| '$' '$'
|
|
|
|
| '$' '?'
|
2020-08-04 07:57:25 +03:00
|
|
|
| '$' '*'
|
|
|
|
| '$' '#'
|
2020-06-17 16:35:06 +03:00
|
|
|
| ...
|
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
slice :: '[' brace_expansion_spec ']'
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
comment :: '#' [^\n]*
|
|
|
|
|
2021-03-05 16:03:23 +03:00
|
|
|
immediate_expression :: '$' '{' immediate_function expression* '}'
|
|
|
|
|
|
|
|
immediate_function :: identifier { predetermined list of names, see Shell.h:ENUMERATE_SHELL_IMMEDIATE_FUNCTIONS }
|
|
|
|
|
2021-01-11 12:34:59 +03:00
|
|
|
history_designator :: '!' event_selector (':' word_selector_composite)?
|
|
|
|
|
|
|
|
event_selector :: '!' {== '-0'}
|
|
|
|
| '?' bareword '?'
|
|
|
|
| bareword {number: index, otherwise: lookup}
|
|
|
|
|
|
|
|
word_selector_composite :: word_selector ('-' word_selector)?
|
|
|
|
|
|
|
|
word_selector :: number
|
|
|
|
| '^' {== 0}
|
|
|
|
| '$' {== end}
|
|
|
|
|
2020-06-17 16:35:06 +03:00
|
|
|
bareword :: [^"'*$&#|()[\]{} ?;<>] bareword?
|
|
|
|
| '\' [^"'*$&#|()[\]{} ?;<>] bareword?
|
|
|
|
|
|
|
|
bareword_with_tilde_expansion :: '~' bareword?
|
|
|
|
|
|
|
|
glob :: [*?] bareword?
|
|
|
|
| bareword [*?]
|
2020-10-24 17:43:02 +03:00
|
|
|
|
|
|
|
brace_expansion :: '{' brace_expansion_spec '}'
|
|
|
|
|
|
|
|
brace_expansion_spec :: expression? (',' expression?)*
|
|
|
|
| expression '..' expression
|
2020-06-17 16:35:06 +03:00
|
|
|
)";
|
|
|
|
#endif
|
2020-10-01 17:43:01 +03:00
|
|
|
|
|
|
|
}
|