handle_pattern.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. static auto IsBindingPatternOperator(Lex::TokenKind kind) -> bool {
  8. return kind == Lex::TokenKind::Colon ||
  9. kind == Lex::TokenKind::ColonExclaim ||
  10. kind == Lex::TokenKind::ColonQuestion;
  11. }
  12. auto HandlePattern(Context& context) -> void {
  13. auto state = context.PopState();
  14. switch (context.PositionKind()) {
  15. case Lex::TokenKind::OpenParen:
  16. context.PushStateForPattern(StateKind::PatternListAsTuple,
  17. state.in_var_pattern, state.in_unused_pattern,
  18. state.ambient_precedence);
  19. break;
  20. case Lex::TokenKind::Var:
  21. context.PushStateForPattern(StateKind::VariablePattern,
  22. state.in_var_pattern, state.in_unused_pattern,
  23. state.ambient_precedence);
  24. break;
  25. case Lex::TokenKind::Unused:
  26. context.PushStateForPattern(StateKind::UnusedPattern,
  27. state.in_var_pattern, state.in_unused_pattern,
  28. state.ambient_precedence);
  29. break;
  30. case Lex::TokenKind::Template:
  31. case Lex::TokenKind::Ref:
  32. context.PushStateForPattern(StateKind::BindingPattern,
  33. state.in_var_pattern, state.in_unused_pattern,
  34. state.ambient_precedence);
  35. break;
  36. case Lex::TokenKind::Identifier:
  37. case Lex::TokenKind::SelfValueIdentifier:
  38. case Lex::TokenKind::Underscore: {
  39. if (IsBindingPatternOperator(
  40. context.PositionKind(Lookahead::NextToken))) {
  41. context.PushStateForPattern(
  42. StateKind::BindingPattern, state.in_var_pattern,
  43. state.in_unused_pattern, state.ambient_precedence);
  44. break;
  45. }
  46. [[fallthrough]];
  47. }
  48. default:
  49. context.PushState(StateKind::ExprPattern);
  50. context.PushStateForExpr(state.ambient_precedence);
  51. break;
  52. }
  53. }
  54. auto HandleExprPattern(Context& context) -> void {
  55. auto state = context.PopState();
  56. // If we parsed an expression followed by a binding operator, we most likely
  57. // have a malformed attempt to introduce a binding pattern that we interpreted
  58. // as an expression pattern, so diagnose that here rather than diagnosing a
  59. // missing `;` at an outer level.
  60. if (IsBindingPatternOperator(context.PositionKind())) {
  61. if (!state.has_error) {
  62. CARBON_DIAGNOSTIC(ExpectedBindingName, Error,
  63. "unexpected expression before {0} in binding pattern",
  64. Lex::TokenKind);
  65. // TODO: Underline the parsed expression.
  66. context.emitter().Emit(*context.position(), ExpectedBindingName,
  67. context.PositionKind());
  68. state.has_error = true;
  69. }
  70. context.Consume();
  71. // It'd be nice to skip the type expression here too, but we can't determine
  72. // the end of it.
  73. }
  74. if (state.has_error) {
  75. context.ReturnErrorOnState();
  76. }
  77. }
  78. } // namespace Carbon::Parse