Parser.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <LibCpp/Lexer.h>
  30. namespace Cpp {
  31. class Parser final {
  32. public:
  33. explicit Parser(const StringView& program, const String& filename);
  34. ~Parser() = default;
  35. NonnullRefPtr<TranslationUnit> parse();
  36. bool eof() const;
  37. RefPtr<ASTNode> eof_node() const;
  38. RefPtr<ASTNode> node_at(Position) const;
  39. Optional<Token> token_at(Position) const;
  40. RefPtr<const TranslationUnit> root_node() const { return m_root_node; }
  41. StringView text_of_node(const ASTNode&) const;
  42. StringView text_of_token(const Cpp::Token& token) const;
  43. void print_tokens() const;
  44. Vector<String> errors() const { return m_errors; }
  45. private:
  46. enum class DeclarationType {
  47. Function,
  48. Variable,
  49. Enum,
  50. Struct,
  51. };
  52. bool done();
  53. Optional<DeclarationType> match_declaration();
  54. Optional<DeclarationType> match_declaration_in_translation_unit();
  55. Optional<DeclarationType> match_declaration_in_function_definition();
  56. bool match_function_declaration();
  57. bool match_comment();
  58. bool match_preprocessor();
  59. bool match_whitespace();
  60. bool match_variable_declaration();
  61. bool match_expression();
  62. bool match_function_call();
  63. bool match_secondary_expression();
  64. bool match_enum_declaration();
  65. bool match_struct_declaration();
  66. bool match_literal();
  67. bool match_unary_expression();
  68. bool match_boolean_literal();
  69. bool match_keyword(const String&);
  70. bool match_block_statement();
  71. Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
  72. Optional<Token> consume_whitespace();
  73. void consume_preprocessor();
  74. NonnullRefPtr<Declaration> parse_declaration(ASTNode& parent, DeclarationType);
  75. NonnullRefPtr<FunctionDeclaration> parse_function_declaration(ASTNode& parent);
  76. NonnullRefPtr<FunctionDefinition> parse_function_definition(ASTNode& parent);
  77. NonnullRefPtr<Statement> parse_statement(ASTNode& parent);
  78. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(ASTNode& parent);
  79. NonnullRefPtr<Expression> parse_expression(ASTNode& parent);
  80. NonnullRefPtr<Expression> parse_primary_expression(ASTNode& parent);
  81. NonnullRefPtr<Expression> parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs);
  82. NonnullRefPtr<FunctionCall> parse_function_call(ASTNode& parent);
  83. NonnullRefPtr<StringLiteral> parse_string_literal(ASTNode& parent);
  84. NonnullRefPtr<ReturnStatement> parse_return_statement(ASTNode& parent);
  85. NonnullRefPtr<EnumDeclaration> parse_enum_declaration(ASTNode& parent);
  86. NonnullRefPtr<StructOrClassDeclaration> parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type);
  87. NonnullRefPtr<MemberDeclaration> parse_member_declaration(ASTNode& parent);
  88. NonnullRefPtr<Expression> parse_literal(ASTNode& parent);
  89. NonnullRefPtr<UnaryExpression> parse_unary_expression(ASTNode& parent);
  90. NonnullRefPtr<BooleanLiteral> parse_boolean_literal(ASTNode& parent);
  91. NonnullRefPtr<Type> parse_type(ASTNode& parent);
  92. NonnullRefPtr<BinaryExpression> parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp);
  93. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp);
  94. NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent);
  95. NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent);
  96. NonnullRefPtr<Comment> parse_comment(ASTNode& parent);
  97. NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent);
  98. bool match(Token::Type);
  99. Token consume(Token::Type);
  100. Token consume();
  101. Token consume_keyword(const String&);
  102. Token peek() const;
  103. Optional<Token> peek(Token::Type) const;
  104. Position position() const;
  105. StringView text_of_range(Position start, Position end) const;
  106. void save_state();
  107. void load_state();
  108. enum class Context {
  109. InTranslationUnit,
  110. InFunctionDefinition,
  111. };
  112. struct State {
  113. Context context { Context::InTranslationUnit };
  114. size_t token_index { 0 };
  115. };
  116. void error(StringView message = {});
  117. size_t node_span_size(const ASTNode& node) const;
  118. template<class T, class... Args>
  119. NonnullRefPtr<T>
  120. create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
  121. {
  122. auto node = adopt(*new T(&parent, start, end, m_filename, forward<Args>(args)...));
  123. m_nodes.append(node);
  124. return node;
  125. }
  126. NonnullRefPtr<TranslationUnit>
  127. create_root_ast_node(const Position& start, Position end)
  128. {
  129. auto node = adopt(*new TranslationUnit(nullptr, start, end, m_filename));
  130. m_nodes.append(node);
  131. m_root_node = node;
  132. return node;
  133. }
  134. StringView m_program;
  135. Vector<StringView> m_lines;
  136. String m_filename;
  137. Vector<Token> m_tokens;
  138. State m_state;
  139. Vector<State> m_saved_states;
  140. RefPtr<TranslationUnit> m_root_node;
  141. NonnullRefPtrVector<ASTNode> m_nodes;
  142. Vector<String> m_errors;
  143. Vector<StringView> parse_type_qualifiers();
  144. };
  145. }