handle_decl_name_and_params.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 HandleDeclNameAndParams(Context& context) -> void {
  8. auto state = context.PopState();
  9. auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
  10. if (!identifier) {
  11. Lex::TokenIndex token = *context.position();
  12. if (context.tokens().GetKind(token) == Lex::TokenKind::FileEnd) {
  13. // The end of file is an unhelpful diagnostic location. Instead, use the
  14. // introducer token.
  15. token = state.token;
  16. }
  17. if (state.token == *context.position()) {
  18. CARBON_DIAGNOSTIC(ExpectedDeclNameAfterPeriod, Error,
  19. "`.` should be followed by a name.");
  20. context.emitter().Emit(token, ExpectedDeclNameAfterPeriod);
  21. } else {
  22. CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
  23. "`{0}` introducer should be followed by a name.",
  24. Lex::TokenKind);
  25. context.emitter().Emit(token, ExpectedDeclName,
  26. context.tokens().GetKind(state.token));
  27. }
  28. context.ReturnErrorOnState();
  29. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  30. /*has_error=*/true);
  31. return;
  32. }
  33. context.AddLeafNode(NodeKind::IdentifierName, *identifier);
  34. switch (context.PositionKind()) {
  35. case Lex::TokenKind::Period:
  36. context.AddNode(NodeKind::NameQualifier,
  37. context.ConsumeChecked(Lex::TokenKind::Period),
  38. state.has_error);
  39. context.PushState(State::DeclNameAndParams);
  40. break;
  41. case Lex::TokenKind::OpenSquareBracket:
  42. state.state = State::DeclNameAndParamsAfterImplicit;
  43. context.PushState(state);
  44. context.PushState(State::PatternListAsImplicit);
  45. break;
  46. case Lex::TokenKind::OpenParen:
  47. state.state = State::DeclNameAndParamsAfterParams;
  48. context.PushState(state);
  49. context.PushState(State::PatternListAsTuple);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
  56. auto state = context.PopState();
  57. if (!context.PositionIs(Lex::TokenKind::OpenParen)) {
  58. CARBON_DIAGNOSTIC(
  59. ParamsRequiredAfterImplicit, Error,
  60. "A `(` for parameters is required after implicit parameters.");
  61. context.emitter().Emit(*context.position(), ParamsRequiredAfterImplicit);
  62. context.ReturnErrorOnState();
  63. return;
  64. }
  65. state.state = State::DeclNameAndParamsAfterParams;
  66. context.PushState(state);
  67. context.PushState(State::PatternListAsTuple);
  68. }
  69. auto HandleDeclNameAndParamsAfterParams(Context& context) -> void {
  70. auto state = context.PopState();
  71. if (auto period = context.ConsumeIf(Lex::TokenKind::Period)) {
  72. context.AddNode(NodeKind::NameQualifier, *period, state.has_error);
  73. context.PushState(State::DeclNameAndParams);
  74. }
  75. }
  76. } // namespace Carbon::Parse