Feat: Allow member access on literal expressions such as [1,2,3].length
#36
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.
This pull request enhances the expression parser to support member access directly on literal expressions. This change makes the expression syntax more intuitive and aligns it with common patterns found in other programming languages.
The problem
Previously, the parser grammar could not handle expressions like
'hello'.length
or[1, 2, 3].length
. The parser treated literals and member-accessible expressions (like variables or grouped expressions) as mutually exclusive starting points. This forced the unnatural workaround of wrapping literals in parentheses, such as([1, 2, 3]).length
, just to make the expression parsable.The Solution
The parser grammar in
lib/src/parser.dart
has been refactored to resolve this limitation:These changes make the grammar more flexible while correctly handling the new syntax.
Testing
A new test group, 'literal members', has been added to
test/expressions_test.dart
. It includes tests that specifically verify the new capability by accessing the.length
property on list and string literals, confirming that the changes work as expected. This test would fail without the changes made. All other tests are unaffected and work just fine.Fixes #37.