handle_array_expr.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lex/token_kind.h"
  5. #include "toolchain/lex/tokenized_buffer.h"
  6. #include "toolchain/parse/context.h"
  7. #include "toolchain/parse/handle.h"
  8. #include "toolchain/parse/node_kind.h"
  9. #include "toolchain/parse/state.h"
  10. namespace Carbon::Parse {
  11. auto HandleArrayExpr(Context& context) -> void {
  12. auto state = context.PopState();
  13. context.AddLeafNode(NodeKind::ArrayExprStart,
  14. context.ConsumeChecked(Lex::TokenKind::OpenSquareBracket),
  15. state.has_error);
  16. context.PushState(state, State::ArrayExprSemi);
  17. context.PushState(State::Expr);
  18. }
  19. auto HandleArrayExprSemi(Context& context) -> void {
  20. auto state = context.PopState();
  21. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  22. if (!semi) {
  23. context.AddNode(NodeKind::ArrayExprSemi, *context.position(),
  24. state.subtree_start, true);
  25. CARBON_DIAGNOSTIC(ExpectedArraySemi, Error, "Expected `;` in array type.");
  26. context.emitter().Emit(*context.position(), ExpectedArraySemi);
  27. state.has_error = true;
  28. } else {
  29. context.AddNode(NodeKind::ArrayExprSemi, *semi, state.subtree_start,
  30. state.has_error);
  31. }
  32. context.PushState(state, State::ArrayExprFinish);
  33. if (!context.PositionIs(Lex::TokenKind::CloseSquareBracket)) {
  34. context.PushState(State::Expr);
  35. }
  36. }
  37. auto HandleArrayExprFinish(Context& context) -> void {
  38. auto state = context.PopState();
  39. context.ConsumeAndAddCloseSymbol(*(Lex::TokenIterator(state.token)), state,
  40. NodeKind::ArrayExpr);
  41. }
  42. } // namespace Carbon::Parse