Parser.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. enum class TemplatizedMatchResult {
  87. NoMatch,
  88. Regular,
  89. Templatized,
  90. };
  91. TemplatizedMatchResult match_type();
  92. TemplatizedMatchResult match_function_call();
  93. Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
  94. Optional<Token> consume_whitespace();
  95. void consume_preprocessor();
  96. NonnullRefPtr<Declaration> parse_declaration(ASTNode& parent, DeclarationType);
  97. NonnullRefPtr<FunctionDeclaration> parse_function_declaration(ASTNode& parent);
  98. NonnullRefPtr<FunctionDefinition> parse_function_definition(ASTNode& parent);
  99. NonnullRefPtr<Statement> parse_statement(ASTNode& parent);
  100. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(ASTNode& parent, bool expect_semicolon = true);
  101. NonnullRefPtr<Expression> parse_expression(ASTNode& parent);
  102. NonnullRefPtr<Expression> parse_primary_expression(ASTNode& parent);
  103. NonnullRefPtr<Expression> parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs);
  104. NonnullRefPtr<FunctionCall> parse_function_call(ASTNode& parent);
  105. NonnullRefPtr<StringLiteral> parse_string_literal(ASTNode& parent);
  106. NonnullRefPtr<ReturnStatement> parse_return_statement(ASTNode& parent);
  107. NonnullRefPtr<EnumDeclaration> parse_enum_declaration(ASTNode& parent);
  108. NonnullRefPtr<StructOrClassDeclaration> parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type);
  109. NonnullRefPtr<MemberDeclaration> parse_member_declaration(ASTNode& parent);
  110. NonnullRefPtr<Expression> parse_literal(ASTNode& parent);
  111. NonnullRefPtr<UnaryExpression> parse_unary_expression(ASTNode& parent);
  112. NonnullRefPtr<BooleanLiteral> parse_boolean_literal(ASTNode& parent);
  113. NonnullRefPtr<Type> parse_type(ASTNode& parent);
  114. NonnullRefPtr<BinaryExpression> parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp);
  115. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp);
  116. NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent);
  117. NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent);
  118. NonnullRefPtr<Comment> parse_comment(ASTNode& parent);
  119. NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent);
  120. NonnullRefPtr<NamespaceDeclaration> parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace = false);
  121. NonnullRefPtrVector<Declaration> parse_declarations_in_translation_unit(ASTNode& parent);
  122. RefPtr<Declaration> parse_single_declaration_in_translation_unit(ASTNode& parent);
  123. NonnullRefPtrVector<Type> parse_template_arguments(ASTNode& parent);
  124. NonnullRefPtr<Name> parse_name(ASTNode& parent);
  125. NonnullRefPtr<CppCastExpression> parse_cpp_cast_expression(ASTNode& parent);
  126. NonnullRefPtr<SizeofExpression> parse_sizeof_expression(ASTNode& parent);
  127. NonnullRefPtr<BracedInitList> parse_braced_init_list(ASTNode& parent);
  128. NonnullRefPtr<CStyleCastExpression> parse_c_style_cast_expression(ASTNode& parent);
  129. bool match(Token::Type);
  130. Token consume(Token::Type);
  131. Token consume();
  132. Token consume_keyword(const String&);
  133. Token peek(size_t offset = 0) const;
  134. Optional<Token> peek(Token::Type) const;
  135. Position position() const;
  136. String text_in_range(Position start, Position end) const;
  137. void save_state();
  138. void load_state();
  139. struct State {
  140. size_t token_index { 0 };
  141. Vector<String> errors;
  142. };
  143. void error(StringView message = {});
  144. template<class T, class... Args>
  145. NonnullRefPtr<T>
  146. create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
  147. {
  148. auto node = adopt(*new T(&parent, start, end, m_filename, forward<Args>(args)...));
  149. m_nodes.append(node);
  150. return node;
  151. }
  152. NonnullRefPtr<TranslationUnit>
  153. create_root_ast_node(const Position& start, Position end)
  154. {
  155. auto node = adopt(*new TranslationUnit(nullptr, start, end, m_filename));
  156. m_nodes.append(node);
  157. m_root_node = node;
  158. return node;
  159. }
  160. bool match_attribute_specification();
  161. void consume_attribute_specification();
  162. bool match_ellipsis();
  163. void initialize_program_tokens(const StringView& program);
  164. void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);
  165. Vector<StringView> parse_type_qualifiers();
  166. Vector<StringView> parse_function_qualifiers();
  167. Preprocessor::Definitions m_definitions;
  168. String m_filename;
  169. Vector<Token> m_tokens;
  170. State m_state;
  171. Vector<State> m_saved_states;
  172. RefPtr<TranslationUnit> m_root_node;
  173. NonnullRefPtrVector<ASTNode> m_nodes;
  174. Vector<TokenAndPreprocessorDefinition> m_replaced_preprocessor_tokens;
  175. };
  176. }