Skip to content

Feat: Allow member access on literal expressions such as [1,2,3].length #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ExpressionParser {
(l) => l[1] == null
? l[0]
: ConditionalExpression(l[0], l[1][0], l[1][1])));
token.set((literal | unaryExpression | variable).cast<Expression>());
token.set((unaryExpression | variable).cast<Expression>());
}

// Gobbles only identifiers
Expand Down Expand Up @@ -99,6 +99,10 @@ class ExpressionParser {
mapLiteral)
.cast();

Parser<Expression> get _primary =>
(literal | group | thisExpression | identifier.map((v) => Variable(v)))
.cast();

// An individual part of a binary expression:
// e.g. `foo.bar(baz)`, `1`, `'abc'`, `(a % 2)` (because it's in parenthesis)
final SettableParser<Expression> token = undefined<Expression>();
Expand Down Expand Up @@ -207,9 +211,8 @@ class ExpressionParser {
// e.g. `foo`, `bar.baz`, `foo['bar'].baz`
// It also gobbles function calls:
// e.g. `Math.acos(obj.angle)`
Parser<Expression> get variable => groupOrIdentifier
.seq((memberArgument.cast() | indexArgument | callArgument).star())
.map((l) {
Parser<Expression> get variable =>
_primary.seq((memberArgument.cast() | indexArgument | callArgument).star()).map((l) {
var a = l[0] as Expression;
var b = l[1] as List;
return b.fold(a, (Expression object, argument) {
Expand All @@ -234,9 +237,6 @@ class ExpressionParser {
Parser<Expression> get group =>
(char('(') & expression.trim() & char(')')).pick(1).cast();

Parser<Expression> get groupOrIdentifier =>
(group | thisExpression | identifier.map((v) => Variable(v))).cast();

Parser<Identifier> get memberArgument =>
(char('.') & identifier).pick(1).cast();

Expand Down
9 changes: 9 additions & 0 deletions test/expressions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ void main() {
});

group('member expressions', () {
test('literal members', () {
var evaluator = ExpressionEvaluator(memberAccessors: [
MemberAccessor<List>({'length': (v) => v.length}),
MemberAccessor<String>({'length': (v) => v.length})
]);
expect(evaluator.eval(Expression.parse('[1,2,3].length'), {}), 3);
expect(evaluator.eval(Expression.parse("'hello'.length"), {}), 5);
});

test('toString member', () {
var evaluator = ExpressionEvaluator(memberAccessors: [
MemberAccessor<Object?>({'toString': (v) => v.toString})
Expand Down