Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(parser): Allow operator access to non-map, non-list objects
Browse files Browse the repository at this point in the history
Closes #416
  • Loading branch information
jbdeboer committed Jan 27, 2014
1 parent 4ce32ad commit 51e167b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions bin/parser_generator_for_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ main(arguments) {
// node node_modules/karma/bin/karma run | grep -Eo ":XNAY:.*:XNAY:" | sed -e 's/:XNAY://g' | sed -e "s/^/'/" | sed -e "s/$/',/" | sort | uniq > missing_expressions
injector.get(isGetter ? ParserGetterSetter : ParserGenerator).generateParser([
"foo == 'bar' ||\nbaz",
"nonmap['hello']",
"nonmap['hello']=3",
"this['a'].b",
"const",
"null",
Expand Down
5 changes: 3 additions & 2 deletions lib/core/parser/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ getKeyed(object, key) {
return object["$key"]; // toString dangerous?
} else if (object == null) {
throw new EvalError('Accessing null object');
} else {
return object[key];
}
throw new EvalError("Attempted field access on a non-list, non-map");
}

/// Set a keyed element in the given [object].
Expand All @@ -90,7 +91,7 @@ setKeyed(object, key, value) {
} else if (object is Map) {
object["$key"] = value; // toString dangerous?
} else {
throw new EvalError("Attempting to set a field on a non-list, non-map");
object[key] = value;
}
return value;
}
17 changes: 11 additions & 6 deletions test/core/parser/parser_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ main() {
expect(eval("4 + 4 + ' str'")).toEqual("8 str");
expect(eval("'str ' + 4 + 4")).toEqual("str 44");
});

it('should allow keyed access on non-maps', () {
scope['nonmap'] = new BracketButNotMap();
expect(eval("nonmap['hello']")).toEqual('hello');
expect(eval("nonmap['hello']=3")).toEqual(3);
});
});

describe('error handling', () {
Expand Down Expand Up @@ -171,12 +177,6 @@ main() {
});


it('should throw on non-list, non-map field access', () {
expectEval("6[3]").toThrow(errStr('Eval Error: Attempted field access on a non-list, non-map while evaling [6[3]]'));
expectEval("6[3]=2").toThrow(errStr('Eval Error: Attempting to set a field on a non-list, non-map while evaling [6[3]=2'));
});


it('should throw on incorrect ternary operator syntax', () {
expectEval("true?1").toThrow('Parser Error: Conditional expression true?1 requires all 3 expressions');
});
Expand Down Expand Up @@ -928,6 +928,11 @@ class OverloadObject implements Map {
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class BracketButNotMap {
operator[](String name) => name;
operator[]=(String name, value) {}
}

class ScopeWithErrors {
String get boo { throw "boo to you"; }
String foo() { throw "foo to you"; }
Expand Down

0 comments on commit 51e167b

Please sign in to comment.