Skip to content

[c] Prefer the longest step keyword #417

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

Merged
merged 4 commits into from
Jun 18, 2025

Conversation

musicinmybrain
Copy link
Contributor

@musicinmybrain musicinmybrain commented Jun 17, 2025

🤔 What's changed?

In C, prefer the longest step keyword by iterating all keywords and continuing to match against keywords that are longer than the longest keyword matched so far. This is equivalent to sorting in descending order by length in bytes before matching as in #416, but does not require creating and sorting a temporary copy of the keywords array.

⚡️ What's your motivation?

Fixes #400 for C.

🏷️ What kind of change is this?

  • 🐛 Bug fix (non-breaking change which fixes a defect)

♻️ Anything particular you want feedback on?

Mostly skim for obvious oversights, particularly those that might be due to my relative unfamiliarity with gherkin.

📋 Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes. N/A, fixes test failures
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

This text was originally generated from a template, then edited by hand. You can modify the template here.


Before this PR:

$ make acceptance
[…]
mkdir -p acceptance/testdata/good
echo bin/gherkin_generate_tokens
bin/gherkin_generate_tokens
bin/gherkin_generate_tokens ../testdata/good/prefixed-keywords.feature > acceptance/testdata/good/prefixed-keywords.feature.tokens
diff --strip-trailing-cr --unified ../testdata/good/prefixed-keywords.feature.tokens acceptance/testdata/good/prefixed-keywords.feature.tokens
--- ../testdata/good/prefixed-keywords.feature.tokens   2025-06-17 07:49:09.772177553 -0400
+++ acceptance/testdata/good/prefixed-keywords.feature.tokens   2025-06-17 13:09:01.627098882 -0400
@@ -4,9 +4,9 @@
 (4:1)Other:/  In this scenario the parser should prefer the longest keyword./
 (5:1)Other://
 (6:3)ScenarioLine:()Senaryo/Erasing agent memory/
-(7:5)StepLine:(Context)Sipoze ke /there is agent J/
+(7:5)StepLine:(Context)Sipoze /ke there is agent J/
 (8:5)StepLine:(Conjunction)Ak /there is agent K/
 (9:5)StepLine:(Action)Le /I erase agent K's memory/
-(10:5)StepLine:(Outcome)Le sa a /there should be agent J/
+(10:5)StepLine:(Unknown)Le sa a /there should be agent J/
 (11:5)StepLine:(Conjunction)Men /there should not be agent K/
 EOF
make: *** [Makefile:100: acceptance/testdata/good/prefixed-keywords.feature.tokens] Error 1
make: *** Deleting file 'acceptance/testdata/good/prefixed-keywords.feature.tokens'

After this PR:


$ make acceptance
[…]
mkdir -p acceptance/testdata/good
echo bin/gherkin_generate_tokens
bin/gherkin_generate_tokens
bin/gherkin_generate_tokens ../testdata/good/prefixed-keywords.feature > acceptance/testdata/good/prefixed-keywords.feature.tokens
diff --strip-trailing-cr --unified ../testdata/good/prefixed-keywords.feature.tokens acceptance/testdata/good/prefixed-keywords.feature.tokens
--- ../testdata/good/prefixed-keywords.feature.tokens   2025-06-17 07:49:09.772177553 -0400
+++ acceptance/testdata/good/prefixed-keywords.feature.tokens   2025-06-17 13:09:37.109611293 -0400
@@ -7,6 +7,6 @@
 (7:5)StepLine:(Context)Sipoze ke /there is agent J/
 (8:5)StepLine:(Conjunction)Ak /there is agent K/
 (9:5)StepLine:(Action)Le /I erase agent K's memory/
-(10:5)StepLine:(Outcome)Le sa a /there should be agent J/
+(10:5)StepLine:(Unknown)Le sa a /there should be agent J/
 (11:5)StepLine:(Conjunction)Men /there should not be agent K/
 EOF
make: *** [Makefile:100: acceptance/testdata/good/prefixed-keywords.feature.tokens] Error 1
make: *** Deleting file 'acceptance/testdata/good/prefixed-keywords.feature.tokens'

I think I can propose a separate fix for the remaining error.

@mpkorstanje mpkorstanje merged commit d74064b into cucumber:main Jun 18, 2025
2 checks passed
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.

Step keywords are not matched properly when some keywords are prefixes of each other
2 participants