2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-06-17 18:05:06 +04:30
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-05-07 01:12:08 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
#include "AST.h"
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/RefPtr.h>
|
2019-09-06 15:34:26 +02:00
|
|
|
#include <AK/String.h>
|
2020-06-17 18:05:06 +04:30
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-07 01:12:08 +02:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
class Parser {
|
|
|
|
public:
|
|
|
|
Parser(StringView input)
|
|
|
|
: m_input(move(input))
|
|
|
|
{
|
|
|
|
}
|
2020-05-10 10:35:23 +04:30
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
RefPtr<AST::Node> parse();
|
2020-05-24 23:00:46 +04:30
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
private:
|
|
|
|
RefPtr<AST::Node> parse_toplevel();
|
|
|
|
RefPtr<AST::Node> parse_sequence();
|
2020-09-13 15:54:33 +04:30
|
|
|
RefPtr<AST::Node> parse_function_decl();
|
2020-07-12 01:42:46 +04:30
|
|
|
RefPtr<AST::Node> parse_and_logical_sequence();
|
|
|
|
RefPtr<AST::Node> parse_or_logical_sequence();
|
2020-06-17 18:05:06 +04:30
|
|
|
RefPtr<AST::Node> parse_variable_decls();
|
|
|
|
RefPtr<AST::Node> parse_pipe_sequence();
|
|
|
|
RefPtr<AST::Node> parse_command();
|
2020-07-12 01:42:46 +04:30
|
|
|
RefPtr<AST::Node> parse_control_structure();
|
|
|
|
RefPtr<AST::Node> parse_for_loop();
|
2020-08-11 12:05:46 +04:30
|
|
|
RefPtr<AST::Node> parse_if_expr();
|
2020-09-08 15:59:07 +04:30
|
|
|
RefPtr<AST::Node> parse_subshell();
|
2020-06-17 18:05:06 +04:30
|
|
|
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();
|
|
|
|
RefPtr<AST::Node> parse_doublequoted_string_inner();
|
|
|
|
RefPtr<AST::Node> parse_variable();
|
|
|
|
RefPtr<AST::Node> parse_evaluate();
|
|
|
|
RefPtr<AST::Node> parse_comment();
|
|
|
|
RefPtr<AST::Node> parse_bareword();
|
|
|
|
RefPtr<AST::Node> parse_glob();
|
|
|
|
|
|
|
|
template<typename A, typename... Args>
|
2020-08-04 18:16:37 +02:00
|
|
|
NonnullRefPtr<A> create(Args... args);
|
2020-06-17 18:05:06 +04:30
|
|
|
|
|
|
|
bool at_end() const { return m_input.length() <= m_offset; }
|
|
|
|
char peek();
|
|
|
|
char consume();
|
|
|
|
void putback();
|
|
|
|
bool expect(char);
|
|
|
|
bool expect(const StringView&);
|
|
|
|
|
|
|
|
StringView consume_while(Function<bool(char)>);
|
|
|
|
|
|
|
|
struct ScopedOffset {
|
|
|
|
ScopedOffset(Vector<size_t>& offsets, size_t offset)
|
|
|
|
: offsets(offsets)
|
|
|
|
, offset(offset)
|
|
|
|
{
|
|
|
|
offsets.append(offset);
|
|
|
|
}
|
|
|
|
~ScopedOffset()
|
|
|
|
{
|
|
|
|
auto last = offsets.take_last();
|
|
|
|
ASSERT(last == offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<size_t>& offsets;
|
|
|
|
size_t offset;
|
2019-05-28 11:53:16 +02:00
|
|
|
};
|
2019-05-07 01:12:08 +02:00
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
OwnPtr<ScopedOffset> push_start();
|
2019-06-04 20:36:08 +02:00
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
StringView m_input;
|
|
|
|
size_t m_offset { 0 };
|
|
|
|
Vector<size_t> m_rule_start_offsets;
|
2019-05-07 01:12:08 +02:00
|
|
|
};
|
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
#if 0
|
|
|
|
constexpr auto the_grammar = R"(
|
|
|
|
toplevel :: sequence?
|
2019-08-30 14:54:05 +10:00
|
|
|
|
2020-07-12 01:42:46 +04:30
|
|
|
sequence :: variable_decls? or_logical_sequence terminator sequence
|
|
|
|
| variable_decls? or_logical_sequence '&' sequence
|
|
|
|
| variable_decls? or_logical_sequence
|
2020-09-13 15:54:33 +04:30
|
|
|
| variable_decls? function_decl (terminator sequence)?
|
2020-07-12 01:42:46 +04:30
|
|
|
| variable_decls? terminator sequence
|
|
|
|
|
2020-09-13 15:54:33 +04:30
|
|
|
function_decl :: identifier '(' (ws* identifier)* ')' ws* '{' toplevel '}'
|
|
|
|
|
2020-07-12 01:42:46 +04:30
|
|
|
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 18:43:37 +04:30
|
|
|
|
|
|
|
terminator :: ';'
|
|
|
|
| '\n'
|
2019-05-07 01:12:08 +02:00
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
variable_decls :: identifier '=' expression (' '+ variable_decls)? ' '*
|
2020-06-22 15:37:20 +04:30
|
|
|
| identifier '=' '(' pipe_sequence ')' (' '+ variable_decls)? ' '*
|
2019-05-07 01:12:08 +02:00
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
pipe_sequence :: command '|' pipe_sequence
|
|
|
|
| command
|
2020-09-07 20:49:53 +04:30
|
|
|
| control_structure '|' pipe_sequence
|
|
|
|
| control_structure
|
2020-04-30 05:26:16 +04:30
|
|
|
|
2020-08-11 12:05:46 +04:30
|
|
|
control_structure :: for_expr
|
|
|
|
| if_expr
|
2020-09-08 15:59:07 +04:30
|
|
|
| subshell
|
|
|
|
|
2020-08-11 12:05:46 +04:30
|
|
|
for_expr :: 'for' ws+ (identifier ' '+ 'in' ws*)? expression ws+ '{' toplevel '}'
|
2020-07-16 22:34:30 +04:30
|
|
|
|
2020-08-11 12:05:46 +04:30
|
|
|
if_expr :: 'if' ws+ or_logical_sequence ws+ '{' toplevel '}' else_clause?
|
|
|
|
|
|
|
|
else_clause :: else '{' toplevel '}'
|
|
|
|
| else if_expr
|
2020-07-12 01:42:46 +04:30
|
|
|
|
2020-09-08 15:59:07 +04:30
|
|
|
subshell :: '{' toplevel '}'
|
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
command :: redirection command
|
|
|
|
| list_expression command?
|
2020-04-30 05:26:16 +04:30
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
redirection :: number? '>'{1,2} ' '* string_composite
|
|
|
|
| number? '<' ' '* string_composite
|
|
|
|
| number? '>' '&' number
|
2020-06-28 18:43:37 +04:30
|
|
|
| number? '>' '&' '-'
|
2020-04-30 05:26:16 +04:30
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
list_expression :: ' '* expression (' '+ list_expression)?
|
2020-04-30 05:26:16 +04:30
|
|
|
|
2020-06-20 18:00:45 +04:30
|
|
|
expression :: evaluate expression?
|
|
|
|
| string_composite expression?
|
|
|
|
| comment expession?
|
|
|
|
| '(' list_expression ')' expression?
|
2020-04-30 05:26:16 +04:30
|
|
|
|
2020-06-22 00:30:14 +04:30
|
|
|
evaluate :: '$' '(' pipe_sequence ')'
|
|
|
|
| '$' expression {eval / dynamic resolve}
|
2019-05-07 01:12:08 +02:00
|
|
|
|
2020-06-17 18:05:06 +04:30
|
|
|
string_composite :: string string_composite?
|
|
|
|
| variable string_composite?
|
|
|
|
| bareword string_composite?
|
|
|
|
| glob string_composite?
|
|
|
|
|
|
|
|
string :: '"' dquoted_string_inner '"'
|
|
|
|
| "'" [^']* "'"
|
|
|
|
|
|
|
|
dquoted_string_inner :: '\' . dquoted_string_inner? {concat}
|
|
|
|
| variable dquoted_string_inner? {compose}
|
|
|
|
| . dquoted_string_inner?
|
|
|
|
| '\' 'x' digit digit dquoted_string_inner?
|
|
|
|
| '\' [abefrn] dquoted_string_inner?
|
|
|
|
|
|
|
|
variable :: '$' identifier
|
|
|
|
| '$' '$'
|
|
|
|
| '$' '?'
|
2020-08-04 09:27:25 +04:30
|
|
|
| '$' '*'
|
|
|
|
| '$' '#'
|
2020-06-17 18:05:06 +04:30
|
|
|
| ...
|
|
|
|
|
|
|
|
comment :: '#' [^\n]*
|
|
|
|
|
|
|
|
bareword :: [^"'*$&#|()[\]{} ?;<>] bareword?
|
|
|
|
| '\' [^"'*$&#|()[\]{} ?;<>] bareword?
|
|
|
|
|
|
|
|
bareword_with_tilde_expansion :: '~' bareword?
|
|
|
|
|
|
|
|
glob :: [*?] bareword?
|
|
|
|
| bareword [*?]
|
|
|
|
)";
|
|
|
|
#endif
|