Gherkin - Token matching refactor #1917
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This refactors how Tokens are matched to reduce the amount of null checks needed
Details
Currently,
Token
has a bunch ofmatch*
fields that get initialised asnull
and then should be set by theTokenMatcher
. By the time the gherkin document is being built we know they won't be null, but that isn't really enforced in the type system.In the PHP implementation we run static analysis which flags up all these potential nulls, so we had to handle them explicitly. It seems the Java implementation suffers slightly less because of differences in how null is handled.
This change reifies the idea of matching as a new
TokenMatch
object. Rather than each field being individually nullable, a token has amatch
which is eithernull
or aTokenMatch
object. This means downstream during document building, we can deal only withTokenMatch
, which has non-null fields.A better approach may have been to model
UnmatchedToken
vsMatchedToken
, but that would require more extensive changes to theParser
, which currently is mutating a token that is passed by reference. I'd be willing to have a stab at that instead.This is only changed in PHP but I could port to Java (and look at any relevant other implementations)
How Has This Been Tested?
Existing unit tests are modified to reflect the new behaviour
Types of changes
Checklist: