Parser.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  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 "AK/NonnullRefPtr.h"
  28. #include "AST.h"
  29. #include "Preprocessor.h"
  30. #include <AK/Noncopyable.h>
  31. #include <LibCpp/Lexer.h>
  32. namespace Cpp {
  33. class Parser final {
  34. AK_MAKE_NONCOPYABLE(Parser);
  35. public:
  36. explicit Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& = {});
  37. ~Parser() = default;
  38. NonnullRefPtr<TranslationUnit> parse();
  39. bool eof() const;
  40. RefPtr<ASTNode> eof_node() const;
  41. RefPtr<ASTNode> node_at(Position) const;
  42. Optional<size_t> index_of_node_at(Position) const;
  43. Optional<Token> token_at(Position) const;
  44. Optional<size_t> index_of_token_at(Position) const;
  45. RefPtr<const TranslationUnit> root_node() const { return m_root_node; }
  46. String text_of_node(const ASTNode&) const;
  47. StringView text_of_token(const Cpp::Token& token) const;
  48. void print_tokens() const;
  49. const Vector<String>& errors() const { return m_state.errors; }
  50. const Preprocessor::Definitions& definitions() const { return m_definitions; }
  51. struct TokenAndPreprocessorDefinition {
  52. Token token;
  53. Preprocessor::DefinedValue preprocessor_value;
  54. };
  55. const Vector<TokenAndPreprocessorDefinition>& replaced_preprocessor_tokens() const { return m_replaced_preprocessor_tokens; }
  56. private:
  57. enum class DeclarationType {
  58. Function,
  59. Variable,
  60. Enum,
  61. Struct,
  62. Namespace,
  63. };
  64. Optional<DeclarationType> match_declaration_in_translation_unit();
  65. bool match_function_declaration();
  66. bool match_comment();
  67. bool match_preprocessor();
  68. bool match_whitespace();
  69. bool match_variable_declaration();
  70. bool match_expression();
  71. bool match_secondary_expression();
  72. bool match_enum_declaration();
  73. bool match_struct_declaration();
  74. bool match_literal();
  75. bool match_unary_expression();
  76. bool match_boolean_literal();
  77. bool match_keyword(const String&);
  78. bool match_block_statement();
  79. bool match_namespace_declaration();
  80. bool match_template_arguments();
  81. bool match_name();
  82. bool match_cpp_cast_expression();
  83. bool match_c_style_cast_expression();
  84. bool match_sizeof_expression();
  85. bool match_braced_init_list();
  86. bool match_type();
  87. Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
  88. Optional<Token> consume_whitespace();
  89. void consume_preprocessor();
  90. NonnullRefPtr<Declaration> parse_declaration(ASTNode& parent, DeclarationType);
  91. NonnullRefPtr<FunctionDeclaration> parse_function_declaration(ASTNode& parent);
  92. NonnullRefPtr<FunctionDefinition> parse_function_definition(ASTNode& parent);
  93. NonnullRefPtr<Statement> parse_statement(ASTNode& parent);
  94. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(ASTNode& parent, bool expect_semicolon = true);
  95. NonnullRefPtr<Expression> parse_expression(ASTNode& parent);
  96. NonnullRefPtr<Expression> parse_primary_expression(ASTNode& parent);
  97. NonnullRefPtr<Expression> parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs);
  98. NonnullRefPtr<FunctionCall> parse_function_call(ASTNode& parent);
  99. NonnullRefPtr<StringLiteral> parse_string_literal(ASTNode& parent);
  100. NonnullRefPtr<ReturnStatement> parse_return_statement(ASTNode& parent);
  101. NonnullRefPtr<EnumDeclaration> parse_enum_declaration(ASTNode& parent);
  102. NonnullRefPtr<StructOrClassDeclaration> parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type);
  103. NonnullRefPtr<MemberDeclaration> parse_member_declaration(ASTNode& parent);
  104. NonnullRefPtr<Expression> parse_literal(ASTNode& parent);
  105. NonnullRefPtr<UnaryExpression> parse_unary_expression(ASTNode& parent);
  106. NonnullRefPtr<BooleanLiteral> parse_boolean_literal(ASTNode& parent);
  107. NonnullRefPtr<Type> parse_type(ASTNode& parent);
  108. NonnullRefPtr<BinaryExpression> parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp);
  109. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp);
  110. NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent);
  111. NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent);
  112. NonnullRefPtr<Comment> parse_comment(ASTNode& parent);
  113. NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent);
  114. NonnullRefPtr<NamespaceDeclaration> parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace = false);
  115. NonnullRefPtrVector<Declaration> parse_declarations_in_translation_unit(ASTNode& parent);
  116. RefPtr<Declaration> parse_single_declaration_in_translation_unit(ASTNode& parent);
  117. NonnullRefPtrVector<Type> parse_template_arguments(ASTNode& parent);
  118. NonnullRefPtr<Name> parse_name(ASTNode& parent);
  119. NonnullRefPtr<CppCastExpression> parse_cpp_cast_expression(ASTNode& parent);
  120. NonnullRefPtr<SizeofExpression> parse_sizeof_expression(ASTNode& parent);
  121. NonnullRefPtr<BracedInitList> parse_braced_init_list(ASTNode& parent);
  122. NonnullRefPtr<CStyleCastExpression> parse_c_style_cast_expression(ASTNode& parent);
  123. bool match(Token::Type);
  124. Token consume(Token::Type);
  125. Token consume();
  126. Token consume_keyword(const String&);
  127. Token peek(size_t offset = 0) const;
  128. Optional<Token> peek(Token::Type) const;
  129. Position position() const;
  130. String text_in_range(Position start, Position end) const;
  131. void save_state();
  132. void load_state();
  133. struct State {
  134. size_t token_index { 0 };
  135. Vector<String> errors;
  136. NonnullRefPtrVector<ASTNode> nodes;
  137. };
  138. void error(StringView message = {});
  139. template<class T, class... Args>
  140. NonnullRefPtr<T>
  141. create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
  142. {
  143. auto node = adopt(*new T(&parent, start, end, m_filename, forward<Args>(args)...));
  144. if (!parent.is_dummy_node()) {
  145. m_state.nodes.append(node);
  146. }
  147. return node;
  148. }
  149. NonnullRefPtr<TranslationUnit>
  150. create_root_ast_node(const Position& start, Position end)
  151. {
  152. auto node = adopt(*new TranslationUnit(nullptr, start, end, m_filename));
  153. m_state.nodes.append(node);
  154. m_root_node = node;
  155. return node;
  156. }
  157. DummyAstNode& get_dummy_node()
  158. {
  159. static NonnullRefPtr<DummyAstNode> dummy = adopt(*new DummyAstNode(nullptr, {}, {}, {}));
  160. return dummy;
  161. }
  162. bool match_attribute_specification();
  163. void consume_attribute_specification();
  164. bool match_ellipsis();
  165. void initialize_program_tokens(const StringView& program);
  166. void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);
  167. Vector<StringView> parse_type_qualifiers();
  168. Vector<StringView> parse_function_qualifiers();
  169. Preprocessor::Definitions m_definitions;
  170. String m_filename;
  171. Vector<Token> m_tokens;
  172. State m_state;
  173. Vector<State> m_saved_states;
  174. RefPtr<TranslationUnit> m_root_node;
  175. Vector<TokenAndPreprocessorDefinition> m_replaced_preprocessor_tokens;
  176. };
  177. }