handle_array_expr.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 HandleArrayExpr(Context& context) -> void {
  11. auto state = context.PopState();
  12. context.AddLeafNode(NodeKind::ArrayExprStart,
  13. context.ConsumeChecked(Lex::TokenKind::OpenSquareBracket),
  14. state.has_error);
  15. context.PushState(state, State::ArrayExprSemi);
  16. context.PushState(State::Expr);
  17. }
  18. auto HandleArrayExprSemi(Context& context) -> void {
  19. auto state = context.PopState();
  20. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  21. if (!semi) {
  22. context.AddNode(NodeKind::ArrayExprSemi, *context.position(),
  23. state.subtree_start, 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.subtree_start,
  29. state.has_error);
  30. }
  31. context.PushState(state, State::ArrayExprFinish);
  32. if (!context.PositionIs(Lex::TokenKind::CloseSquareBracket)) {
  33. context.PushState(State::Expr);
  34. }
  35. }
  36. auto HandleArrayExprFinish(Context& context) -> void {
  37. auto state = context.PopState();
  38. context.ConsumeAndAddCloseSymbol(*(Lex::TokenIterator(state.token)), state,
  39. NodeKind::ArrayExpr);
  40. }
  41. } // namespace Carbon::Parse