handle_array_expression.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/node_kind.h"
  8. #include "toolchain/parse/state.h"
  9. namespace Carbon::Parse {
  10. auto HandleArrayExpression(Context& context) -> void {
  11. auto state = context.PopState();
  12. state.state = State::ArrayExpressionSemi;
  13. context.AddLeafNode(NodeKind::ArrayExpressionStart,
  14. context.ConsumeChecked(Lex::TokenKind::OpenSquareBracket),
  15. state.has_error);
  16. context.PushState(state);
  17. context.PushState(State::Expression);
  18. }
  19. auto HandleArrayExpressionSemi(Context& context) -> void {
  20. auto state = context.PopState();
  21. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  22. if (!semi) {
  23. context.AddNode(NodeKind::ArrayExpressionSemi, *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::ArrayExpressionSemi, *semi, state.subtree_start,
  30. state.has_error);
  31. }
  32. state.state = State::ArrayExpressionFinish;
  33. context.PushState(state);
  34. if (!context.PositionIs(Lex::TokenKind::CloseSquareBracket)) {
  35. context.PushState(State::Expression);
  36. }
  37. }
  38. auto HandleArrayExpressionFinish(Context& context) -> void {
  39. auto state = context.PopState();
  40. context.ConsumeAndAddCloseSymbol(*(Lex::TokenIterator(state.token)), state,
  41. NodeKind::ArrayExpression);
  42. }
  43. } // namespace Carbon::Parse