Parser.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include "AST.h"
  28. #include <AK/Function.h>
  29. #include <AK/RefPtr.h>
  30. #include <AK/String.h>
  31. #include <AK/StringBuilder.h>
  32. #include <AK/Vector.h>
  33. class Parser {
  34. public:
  35. Parser(StringView input)
  36. : m_input(move(input))
  37. {
  38. }
  39. RefPtr<AST::Node> parse();
  40. private:
  41. RefPtr<AST::Node> parse_toplevel();
  42. RefPtr<AST::Node> parse_sequence();
  43. RefPtr<AST::Node> parse_and_logical_sequence();
  44. RefPtr<AST::Node> parse_or_logical_sequence();
  45. RefPtr<AST::Node> parse_variable_decls();
  46. RefPtr<AST::Node> parse_pipe_sequence();
  47. RefPtr<AST::Node> parse_command();
  48. RefPtr<AST::Node> parse_control_structure();
  49. RefPtr<AST::Node> parse_for_loop();
  50. RefPtr<AST::Node> parse_if_expr();
  51. RefPtr<AST::Node> parse_redirection();
  52. RefPtr<AST::Node> parse_list_expression();
  53. RefPtr<AST::Node> parse_expression();
  54. RefPtr<AST::Node> parse_string_composite();
  55. RefPtr<AST::Node> parse_string();
  56. RefPtr<AST::Node> parse_doublequoted_string_inner();
  57. RefPtr<AST::Node> parse_variable();
  58. RefPtr<AST::Node> parse_evaluate();
  59. RefPtr<AST::Node> parse_comment();
  60. RefPtr<AST::Node> parse_bareword();
  61. RefPtr<AST::Node> parse_glob();
  62. template<typename A, typename... Args>
  63. NonnullRefPtr<A> create(Args... args);
  64. bool at_end() const { return m_input.length() <= m_offset; }
  65. char peek();
  66. char consume();
  67. void putback();
  68. bool expect(char);
  69. bool expect(const StringView&);
  70. StringView consume_while(Function<bool(char)>);
  71. struct ScopedOffset {
  72. ScopedOffset(Vector<size_t>& offsets, size_t offset)
  73. : offsets(offsets)
  74. , offset(offset)
  75. {
  76. offsets.append(offset);
  77. }
  78. ~ScopedOffset()
  79. {
  80. auto last = offsets.take_last();
  81. ASSERT(last == offset);
  82. }
  83. Vector<size_t>& offsets;
  84. size_t offset;
  85. };
  86. OwnPtr<ScopedOffset> push_start();
  87. StringView m_input;
  88. size_t m_offset { 0 };
  89. Vector<size_t> m_rule_start_offsets;
  90. };
  91. #if 0
  92. constexpr auto the_grammar = R"(
  93. toplevel :: sequence?
  94. sequence :: variable_decls? or_logical_sequence terminator sequence
  95. | variable_decls? or_logical_sequence '&' sequence
  96. | variable_decls? or_logical_sequence
  97. | variable_decls? terminator sequence
  98. or_logical_sequence :: and_logical_sequence '|' '|' and_logical_sequence
  99. | and_logical_sequence
  100. and_logical_sequence :: pipe_sequence '&' '&' and_logical_sequence
  101. | pipe_sequence
  102. terminator :: ';'
  103. | '\n'
  104. variable_decls :: identifier '=' expression (' '+ variable_decls)? ' '*
  105. | identifier '=' '(' pipe_sequence ')' (' '+ variable_decls)? ' '*
  106. pipe_sequence :: command '|' pipe_sequence
  107. | command
  108. | control_structure '|' pipe_sequence
  109. | control_structure
  110. control_structure :: for_expr
  111. | if_expr
  112. for_expr :: 'for' ws+ (identifier ' '+ 'in' ws*)? expression ws+ '{' toplevel '}'
  113. if_expr :: 'if' ws+ or_logical_sequence ws+ '{' toplevel '}' else_clause?
  114. else_clause :: else '{' toplevel '}'
  115. | else if_expr
  116. command :: redirection command
  117. | list_expression command?
  118. redirection :: number? '>'{1,2} ' '* string_composite
  119. | number? '<' ' '* string_composite
  120. | number? '>' '&' number
  121. | number? '>' '&' '-'
  122. list_expression :: ' '* expression (' '+ list_expression)?
  123. expression :: evaluate expression?
  124. | string_composite expression?
  125. | comment expession?
  126. | '(' list_expression ')' expression?
  127. evaluate :: '$' '(' pipe_sequence ')'
  128. | '$' expression {eval / dynamic resolve}
  129. string_composite :: string string_composite?
  130. | variable string_composite?
  131. | bareword string_composite?
  132. | glob string_composite?
  133. string :: '"' dquoted_string_inner '"'
  134. | "'" [^']* "'"
  135. dquoted_string_inner :: '\' . dquoted_string_inner? {concat}
  136. | variable dquoted_string_inner? {compose}
  137. | . dquoted_string_inner?
  138. | '\' 'x' digit digit dquoted_string_inner?
  139. | '\' [abefrn] dquoted_string_inner?
  140. variable :: '$' identifier
  141. | '$' '$'
  142. | '$' '?'
  143. | '$' '*'
  144. | '$' '#'
  145. | ...
  146. comment :: '#' [^\n]*
  147. bareword :: [^"'*$&#|()[\]{} ?;<>] bareword?
  148. | '\' [^"'*$&#|()[\]{} ?;<>] bareword?
  149. bareword_with_tilde_expansion :: '~' bareword?
  150. glob :: [*?] bareword?
  151. | bareword [*?]
  152. )";
  153. #endif