handle_array_expr.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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(), true);
  24. CARBON_DIAGNOSTIC(ExpectedArraySemi, Error, "Expected `;` in array type.");
  25. context.emitter().Emit(*context.position(), ExpectedArraySemi);
  26. state.has_error = true;
  27. } else {
  28. context.AddNode(NodeKind::ArrayExprSemi, *semi, state.has_error);
  29. }
  30. context.PushState(state, State::ArrayExprFinish);
  31. if (!context.PositionIs(Lex::TokenKind::CloseSquareBracket)) {
  32. context.PushState(State::Expr);
  33. }
  34. }
  35. auto HandleArrayExprFinish(Context& context) -> void {
  36. auto state = context.PopState();
  37. context.ConsumeAndAddCloseSymbol(*(Lex::TokenIterator(state.token)), state,
  38. NodeKind::ArrayExpr);
  39. }
  40. } // namespace Carbon::Parse