handle_array_expr.cpp 1.8 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. auto array_token = context.ConsumeChecked(Lex::TokenKind::Array);
  14. context.AddLeafNode(NodeKind::ArrayExprKeyword, array_token, state.has_error);
  15. if (auto open_paren = context.ConsumeAndAddOpenParen(
  16. array_token, NodeKind::ArrayExprOpenParen)) {
  17. state.token = *open_paren;
  18. } else {
  19. state.has_error = true;
  20. }
  21. context.PushState(state, StateKind::ArrayExprComma);
  22. context.PushState(StateKind::Expr);
  23. }
  24. auto HandleArrayExprComma(Context& context) -> void {
  25. auto state = context.PopState();
  26. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Comma,
  27. NodeKind::ArrayExprComma)) {
  28. context.AddLeafNode(NodeKind::ArrayExprComma, *context.position(), true);
  29. CARBON_DIAGNOSTIC(ExpectedArrayComma, Error,
  30. "expected `,` in `array(Type, Count)`");
  31. context.emitter().Emit(*context.position(), ExpectedArrayComma);
  32. state.has_error = true;
  33. }
  34. context.PushState(state, StateKind::ArrayExprFinish);
  35. context.PushState(StateKind::Expr);
  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