handle_unused.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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/parse/context.h"
  5. #include "toolchain/parse/handle.h"
  6. namespace Carbon::Parse {
  7. auto HandleUnusedPattern(Context& context) -> void {
  8. auto state = context.PopState();
  9. if (state.in_unused_pattern) {
  10. CARBON_DIAGNOSTIC(NestedUnused, Error,
  11. "`unused` nested within another `unused`");
  12. context.emitter().Emit(*context.position(), NestedUnused);
  13. state.has_error = true;
  14. }
  15. context.PushState(StateKind::FinishUnusedPattern);
  16. context.ConsumeChecked(Lex::TokenKind::Unused);
  17. context.PushStateForPattern(StateKind::Pattern, state.in_var_pattern,
  18. /*in_unused_pattern=*/true,
  19. state.ambient_precedence);
  20. }
  21. auto HandleFinishUnusedPattern(Context& context) -> void {
  22. auto state = context.PopState();
  23. context.AddNode(NodeKind::UnusedPattern, state.token, state.has_error);
  24. // Propagate errors to the parent state so that they can take different
  25. // actions on invalid patterns.
  26. if (state.has_error) {
  27. context.ReturnErrorOnState();
  28. }
  29. }
  30. } // namespace Carbon::Parse