parser_handle_array_expression.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/lexer/token_kind.h"
  5. #include "toolchain/lexer/tokenized_buffer.h"
  6. #include "toolchain/parser/parse_node_kind.h"
  7. #include "toolchain/parser/parser_context.h"
  8. #include "toolchain/parser/parser_state.h"
  9. namespace Carbon {
  10. auto ParserHandleArrayExpression(ParserContext& context) -> void {
  11. auto state = context.PopState();
  12. state.state = ParserState::ArrayExpressionSemi;
  13. context.AddLeafNode(ParseNodeKind::ArrayExpressionStart,
  14. context.ConsumeChecked(TokenKind::OpenSquareBracket),
  15. state.has_error);
  16. context.PushState(state);
  17. context.PushState(ParserState::Expression);
  18. }
  19. auto ParserHandleArrayExpressionSemi(ParserContext& context) -> void {
  20. auto state = context.PopState();
  21. auto semi = context.ConsumeIf(TokenKind::Semi);
  22. if (!semi) {
  23. context.AddNode(ParseNodeKind::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(ParseNodeKind::ArrayExpressionSemi, *semi,
  30. state.subtree_start, state.has_error);
  31. }
  32. state.state = ParserState::ArrayExpressionFinish;
  33. context.PushState(state);
  34. if (!context.PositionIs(TokenKind::CloseSquareBracket)) {
  35. context.PushState(ParserState::Expression);
  36. }
  37. }
  38. auto ParserHandleArrayExpressionFinish(ParserContext& context) -> void {
  39. auto state = context.PopState();
  40. context.ConsumeAndAddCloseSymbol(
  41. *(TokenizedBuffer::TokenIterator(state.token)), state,
  42. ParseNodeKind::ArrayExpression);
  43. }
  44. } // namespace Carbon