Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#132 Port remaining grammar tests to C++ #134

Merged
merged 8 commits into from
Jul 28, 2024

Conversation

vityaman
Copy link
Contributor

@vityaman vityaman commented Jul 28, 2024

  • Ported Whitebox and CPP14 grammar tests.
  • Found serious behavior divergence at CPP14Test.cpp.
  • Edited GitHub Workflows to run only when required.
  • Downgraded C++ from 23 to 20.

Comment on lines 7 to 20
inline auto Candidates(CodeCompletionCore &completion,
std::size_t caretTokenIndex) {
return completion.collectCandidates(caretTokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
}

inline auto Candidates(CodeCompletionCore &completion,
std::size_t caretTokenIndex,
antlr4::ParserRuleContext *context) {
return completion.collectCandidates(caretTokenIndex,
/*context=*/context, /*size_t=*/0,
/*cancel=*/nullptr);
}
Copy link
Contributor Author

@vityaman vityaman Jul 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current collectCandidates is overloaded with parameters. I have thoughts, that it would be better to have something like this (just thoughts):

class CodeCompletionCore final {
  struct Params {
    antlr4::Parser* parser;
    std::chrono::milliseconds timeout = 0ms;
    std::atomic<bool>* is_cancelled = nullptr;
  }

  explicit CodeCompletionCore(Params params);

  CandidatesCollection collectCandidates(std::size_t caretTokenIndex);
  CandidatesCollection collectCandidates(
    std::size_t caretTokenIndex, antlr4::ParserRuleContext* context);
}

auto Usage(antlr4::Parser* parser) {
  CodeCompletionCode completion({
    .parser = parser,
    .timeout = 100ms,
  });
  return completion;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what your point here is. You don't like default parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with current signature

CandidatesCollection collectCandidates(
  size_t caretTokenIndex, 
  antlr4::ParserRuleContext * context = nullptr, 
  size_t timeoutMS = 0, 
  std::atomic<bool> * cancel = nullptr
);

It is that, it seems, that timeoutMS and cancel will not change from call to call, and their presence generates many combinations of arguments sets.

collectCandidates(0);      // OK, readable
collectCandidates(0, ctx); // OK, readable
collectCandidates(0, nullptr, /* timeoutMS = */ 100); // Forces me to put nullptr
collectCandidates(0, nullptr, /* timeoutMS = */ 0, &cancel); // Forces me to add even zero timeout
collectCandidates(0, nullptr, 0, &cancel); // What?

It makes reading caller code harder. Also, it forces putting this comments like /* timeoutMS = */ because there are no named arguments in C++.

In addition, in examples I showed hard-coded values, but in real code there will be variable names, which will make this line of code longer.

But in constructor we can provide user with named arguments with such Params struct.

I think that a default argument is okay if there are no more than one of such argument (talking about languages without named arguments).

Also, I agree that overloading the method is redundant here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added default parameters and removed that functions.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be a simple struct as parameter (object/interface in TypeScript), which allows to specify parameters in any order, have names for each and leave out any that are marked as being optional. I use this often, as it comes as close to named parameters (e.g. like in Swift) as you can get.

Comment on lines +320 to +329
// These are in TypeScript version, but not in C++ port
// CPP14Parser::RuleFunctionbody,
// CPP14Parser::RuleCompoundstatement,
// CPP14Parser::RuleStatementseq,
// CPP14Parser::RuleStatement,
// CPP14Parser::RuleDeclarationstatement,
// CPP14Parser::RuleBlockdeclaration,
// CPP14Parser::RuleSimpledeclaration,
// CPP14Parser::RuleInitdeclaratorlist,
// CPP14Parser::RuleInitdeclarator,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will create an issue to analyze these results.

@vityaman vityaman marked this pull request as ready for review July 28, 2024 08:33
Comment on lines 7 to 20
inline auto Candidates(CodeCompletionCore &completion,
std::size_t caretTokenIndex) {
return completion.collectCandidates(caretTokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
}

inline auto Candidates(CodeCompletionCore &completion,
std::size_t caretTokenIndex,
antlr4::ParserRuleContext *context) {
return completion.collectCandidates(caretTokenIndex,
/*context=*/context, /*size_t=*/0,
/*cancel=*/nullptr);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what your point here is. You don't like default parameters?

@mike-lischke mike-lischke merged commit 71554ed into mike-lischke:main Jul 28, 2024
6 checks passed
@mike-lischke
Copy link
Owner

Thanks!

@vityaman vityaman deleted the 132-port-cpp-grammar-test branch July 31, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants