Skip to content

Fix 'when' keyword parsing to respect 'use feature switch'#114

Merged
fglock merged 1 commit intomasterfrom
fix-when-keyword-parsing
Dec 31, 2025
Merged

Fix 'when' keyword parsing to respect 'use feature switch'#114
fglock merged 1 commit intomasterfrom
fix-when-keyword-parsing

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented Dec 31, 2025

Problem

The when keyword was being treated as a reserved keyword unconditionally, causing parser errors when used as a bareword hash key. This broke ExifTool's XMP.pm which uses when as a hash key:

when => { %dateTimeInfo, Groups => { 2 => 'Time' } }

The when keyword should only be reserved when use feature 'switch' is active, similar to how try and class are handled.

Changes

StatementResolver.java

  • Added feature flag checks for given, when, and default keywords
  • These keywords now only act as statement keywords when use feature 'switch' is enabled
  • Without the feature, they fall through to be treated as regular identifiers

ListParser.java (2 locations)

  • Added when to the list of keywords that should be treated as barewords when followed by => (fat comma)
  • This matches the existing behavior for and, or, and xor keywords

Testing

Tested with ExifTool 13.44:

Before fix:

Expected token OPERATOR with text ) but got LexerToken{type=IDENTIFIER, text='when'}

After fix:

  • XMP.pm parses successfully
  • Commands like -h, -json, -X, -listx now proceed past the parser stage
  • The when => { ... } hash key is correctly recognized as a bareword

Impact

This fix allows code that uses when as a bareword (hash keys, subroutine names, etc.) to work correctly when the switch feature is not enabled, matching standard Perl behavior.

The 'when' keyword should only be treated as a reserved keyword when
'use feature "switch"' is active. Without this feature enabled, 'when'
should be allowed as a bareword, particularly as a hash key.

Changes:
- StatementResolver.java: Add feature flag checks for 'given', 'when',
  and 'default' keywords (similar to 'try' and 'class')
- ListParser.java: Add 'when' to the list of keywords that should be
  treated as barewords when followed by '=>' (fat comma)

This fixes parsing errors in ExifTool's XMP.pm where 'when' is used
as a hash key: when => { %dateTimeInfo, Groups => { 2 => 'Time' } }

Tested with ExifTool 13.44 - XMP-related commands now parse correctly.
@fglock fglock merged commit 31355fd into master Dec 31, 2025
2 checks passed
@fglock fglock deleted the fix-when-keyword-parsing branch December 31, 2025 14:44
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.

1 participant