handle_declaration_name_and_params.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace Carbon::Parse {
  6. // Handles DeclNameAndParamsAs(Optional|Required).
  7. static auto HandleDeclNameAndParams(Context& context, State after_name)
  8. -> void {
  9. auto state = context.PopState();
  10. // TODO: Should handle designated names.
  11. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  12. state.state = after_name;
  13. context.PushState(state);
  14. if (context.PositionIs(Lex::TokenKind::Period)) {
  15. context.AddLeafNode(NodeKind::Name, *identifier);
  16. state.state = State::PeriodAsDecl;
  17. context.PushState(state);
  18. } else {
  19. context.AddLeafNode(NodeKind::Name, *identifier);
  20. }
  21. } else {
  22. CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
  23. "`{0}` introducer should be followed by a name.",
  24. Lex::TokenKind);
  25. context.emitter().Emit(*context.position(), ExpectedDeclName,
  26. context.tokens().GetKind(state.token));
  27. context.ReturnErrorOnState();
  28. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  29. /*has_error=*/true);
  30. }
  31. }
  32. auto HandleDeclNameAndParamsAsNone(Context& context) -> void {
  33. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsNone);
  34. }
  35. auto HandleDeclNameAndParamsAsOptional(Context& context) -> void {
  36. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsOptional);
  37. }
  38. auto HandleDeclNameAndParamsAsRequired(Context& context) -> void {
  39. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsRequired);
  40. }
  41. enum class Params : int8_t {
  42. None,
  43. Optional,
  44. Required,
  45. };
  46. static auto HandleDeclNameAndParamsAfterName(Context& context, Params params)
  47. -> void {
  48. auto state = context.PopState();
  49. if (context.PositionIs(Lex::TokenKind::Period)) {
  50. // Continue designator processing.
  51. context.PushState(state);
  52. state.state = State::PeriodAsDecl;
  53. context.PushState(state);
  54. return;
  55. }
  56. // TODO: We can have a parameter list after a name qualifier, regardless of
  57. // whether the entity itself permits or requires parameters:
  58. //
  59. // fn Class(T:! type).AnotherClass(U:! type).Function(v: T) {}
  60. //
  61. // We should retain a `DeclNameAndParams...` state on the stack in all
  62. // cases below to check for a period after a parameter list, which indicates
  63. // that we've not finished parsing the declaration name.
  64. if (params == Params::None) {
  65. return;
  66. }
  67. if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
  68. context.PushState(State::DeclNameAndParamsAfterImplicit);
  69. context.PushState(State::ParameterListAsImplicit);
  70. } else if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  71. context.PushState(State::ParameterListAsRegular);
  72. } else if (params == Params::Required) {
  73. CARBON_DIAGNOSTIC(ParametersRequiredByIntroducer, Error,
  74. "`{0}` requires a `(` for parameters.", Lex::TokenKind);
  75. context.emitter().Emit(*context.position(), ParametersRequiredByIntroducer,
  76. context.tokens().GetKind(state.token));
  77. context.ReturnErrorOnState();
  78. }
  79. }
  80. auto HandleDeclNameAndParamsAfterNameAsNone(Context& context) -> void {
  81. HandleDeclNameAndParamsAfterName(context, Params::None);
  82. }
  83. auto HandleDeclNameAndParamsAfterNameAsOptional(Context& context) -> void {
  84. HandleDeclNameAndParamsAfterName(context, Params::Optional);
  85. }
  86. auto HandleDeclNameAndParamsAfterNameAsRequired(Context& context) -> void {
  87. HandleDeclNameAndParamsAfterName(context, Params::Required);
  88. }
  89. auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
  90. context.PopAndDiscardState();
  91. if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  92. context.PushState(State::ParameterListAsRegular);
  93. } else {
  94. CARBON_DIAGNOSTIC(
  95. ParametersRequiredAfterImplicit, Error,
  96. "A `(` for parameters is required after implicit parameters.");
  97. context.emitter().Emit(*context.position(),
  98. ParametersRequiredAfterImplicit);
  99. context.ReturnErrorOnState();
  100. }
  101. }
  102. } // namespace Carbon::Parse