Parser.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. Vector<String> errors() const { return m_errors; }
  50. const Preprocessor::Definitions& definitions() const { return m_definitions; }
  51. private:
  52. enum class DeclarationType {
  53. Function,
  54. Variable,
  55. Enum,
  56. Struct,
  57. };
  58. bool done();
  59. Optional<DeclarationType> match_declaration();
  60. Optional<DeclarationType> match_declaration_in_translation_unit();
  61. Optional<DeclarationType> match_declaration_in_function_definition();
  62. bool match_function_declaration();
  63. bool match_comment();
  64. bool match_preprocessor();
  65. bool match_whitespace();
  66. bool match_variable_declaration();
  67. bool match_expression();
  68. bool match_function_call();
  69. bool match_secondary_expression();
  70. bool match_enum_declaration();
  71. bool match_struct_declaration();
  72. bool match_literal();
  73. bool match_unary_expression();
  74. bool match_boolean_literal();
  75. bool match_keyword(const String&);
  76. bool match_block_statement();
  77. Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
  78. Optional<Token> consume_whitespace();
  79. void consume_preprocessor();
  80. NonnullRefPtr<Declaration> parse_declaration(ASTNode& parent, DeclarationType);
  81. NonnullRefPtr<FunctionDeclaration> parse_function_declaration(ASTNode& parent);
  82. NonnullRefPtr<FunctionDefinition> parse_function_definition(ASTNode& parent);
  83. NonnullRefPtr<Statement> parse_statement(ASTNode& parent);
  84. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(ASTNode& parent);
  85. NonnullRefPtr<Expression> parse_expression(ASTNode& parent);
  86. NonnullRefPtr<Expression> parse_primary_expression(ASTNode& parent);
  87. NonnullRefPtr<Expression> parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs);
  88. NonnullRefPtr<FunctionCall> parse_function_call(ASTNode& parent);
  89. NonnullRefPtr<StringLiteral> parse_string_literal(ASTNode& parent);
  90. NonnullRefPtr<ReturnStatement> parse_return_statement(ASTNode& parent);
  91. NonnullRefPtr<EnumDeclaration> parse_enum_declaration(ASTNode& parent);
  92. NonnullRefPtr<StructOrClassDeclaration> parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type);
  93. NonnullRefPtr<MemberDeclaration> parse_member_declaration(ASTNode& parent);
  94. NonnullRefPtr<Expression> parse_literal(ASTNode& parent);
  95. NonnullRefPtr<UnaryExpression> parse_unary_expression(ASTNode& parent);
  96. NonnullRefPtr<BooleanLiteral> parse_boolean_literal(ASTNode& parent);
  97. NonnullRefPtr<Type> parse_type(ASTNode& parent);
  98. NonnullRefPtr<BinaryExpression> parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp);
  99. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp);
  100. NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent);
  101. NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent);
  102. NonnullRefPtr<Comment> parse_comment(ASTNode& parent);
  103. NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent);
  104. bool match(Token::Type);
  105. Token consume(Token::Type);
  106. Token consume();
  107. Token consume_keyword(const String&);
  108. Token peek(size_t offset = 0) const;
  109. Optional<Token> peek(Token::Type) const;
  110. Position position() const;
  111. String text_in_range(Position start, Position end) const;
  112. void save_state();
  113. void load_state();
  114. enum class Context {
  115. InTranslationUnit,
  116. InFunctionDefinition,
  117. };
  118. struct State {
  119. Context context { Context::InTranslationUnit };
  120. size_t token_index { 0 };
  121. };
  122. void error(StringView message = {});
  123. template<class T, class... Args>
  124. NonnullRefPtr<T>
  125. create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
  126. {
  127. auto node = adopt(*new T(&parent, start, end, m_filename, forward<Args>(args)...));
  128. m_nodes.append(node);
  129. return node;
  130. }
  131. NonnullRefPtr<TranslationUnit>
  132. create_root_ast_node(const Position& start, Position end)
  133. {
  134. auto node = adopt(*new TranslationUnit(nullptr, start, end, m_filename));
  135. m_nodes.append(node);
  136. m_root_node = node;
  137. return node;
  138. }
  139. bool match_attribute_specification();
  140. void consume_attribute_specification();
  141. bool match_ellipsis();
  142. void initialize_program_tokens(const StringView& program);
  143. void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);
  144. Vector<StringView> parse_type_qualifiers();
  145. Preprocessor::Definitions m_definitions;
  146. String m_filename;
  147. Vector<Token> m_tokens;
  148. State m_state;
  149. Vector<State> m_saved_states;
  150. RefPtr<TranslationUnit> m_root_node;
  151. NonnullRefPtrVector<ASTNode> m_nodes;
  152. Vector<String> m_errors;
  153. };
  154. }