Skip to content

Commit

Permalink
#19 support for .foo keys
Browse files Browse the repository at this point in the history
A `.foo` key will look for `foo` at the top of the context stack only.
  • Loading branch information
groue committed May 26, 2012
1 parent 74aca2c commit 3642305
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
23 changes: 11 additions & 12 deletions src/classes/GRMustacheTemplateParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,24 @@ - (GRMustacheInvocation *)invocationWithToken:(GRMustacheToken *)token error:(NS
{
NSString *content = token.content;
NSUInteger length = content.length;
BOOL acceptDotIdentifier = YES;
BOOL acceptIdentifier = YES;
BOOL acceptSeparator = NO;
BOOL acceptSeparator = YES;
NSMutableArray *keys = [NSMutableArray array];
unichar c;
NSUInteger identifierStart = 0;
for (NSUInteger i = 0; i < length; ++i) {
c = [content characterAtIndex:i];
switch (c) {
case '.':
if (acceptDotIdentifier) {
[keys addObject:[content substringWithRange:NSMakeRange(identifierStart, i+1-identifierStart)]];
acceptDotIdentifier = NO;
acceptIdentifier = NO;
acceptSeparator = NO;
} else if (acceptSeparator) {
[keys addObject:[content substringWithRange:NSMakeRange(identifierStart, i-identifierStart)]];
if (acceptSeparator) {
if (i==0) {
// leading dot: "." or ".foo…"
[keys addObject:@"."];
} else {
// dot in the middle: "foo.bar…"
[keys addObject:[content substringWithRange:NSMakeRange(identifierStart, i-identifierStart)]];
}
identifierStart = i + 1;
acceptDotIdentifier = NO;
acceptIdentifier = YES;
acceptSeparator = NO;
} else {
Expand All @@ -273,7 +272,6 @@ - (GRMustacheInvocation *)invocationWithToken:(GRMustacheToken *)token error:(NS

default:
if (acceptIdentifier) {
acceptDotIdentifier = NO;
acceptIdentifier = YES;
acceptSeparator = YES;
} else {
Expand All @@ -287,7 +285,8 @@ - (GRMustacheInvocation *)invocationWithToken:(GRMustacheToken *)token error:(NS
}
if (acceptSeparator) {
[keys addObject:[content substringWithRange:NSMakeRange(identifierStart, length - identifierStart)]];
} else if (acceptIdentifier) {
} else if (acceptIdentifier && length > 1) {
// dot at the end: "…foo."
if (outError != NULL) {
*outError = [self parseErrorAtToken:token description:[NSString stringWithFormat:@"Invalid identifier: %@", content]];
}
Expand Down
6 changes: 0 additions & 6 deletions src/tests/Public/v3.0/GRMustacheParsingErrorsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ - (void)testParsingReportsEmptyVariableTag
- (void)testParsingReportsBadlyFormattedVariableTag
{
NSError *error;

// ".a" could mean anchored access to "self.a".
// But it doesn't mean anything, actually.
STAssertNil([GRMustacheTemplate templateFromString:@"{{.a}}" error:&error], nil);
STAssertEquals(error.code, (NSInteger)GRMustacheErrorCodeParseError, nil);

STAssertNil([GRMustacheTemplate templateFromString:@"{{a.}}" error:&error], nil);
STAssertEquals(error.code, (NSInteger)GRMustacheErrorCodeParseError, nil);
STAssertNil([GRMustacheTemplate templateFromString:@"{{..}}" error:&error], nil);
Expand Down
42 changes: 42 additions & 0 deletions src/tests/Public/v3.0/GRMustacheSuites/compound_keys.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@
"data": { "person": { } },
"template": "{{person.name}}",
"expected": ""
},
{
"name": "key lookup is anchored",
"data": { "b": "b", "a": { } },
"template": "-{{#a}}{{b}}{{/a}}-{{a.b}}-",
"expected": "-b--"
},
{
"name": "key lookup is anchored",
"data": { "c": "c", "b": { "c" : "cb" } },
"template": "-{{a.b.c}}-",
"expected": "--"
},
{
"name": "key lookup is anchored",
"data": { "c": "c", "b": { "c" : "cb" }, "a": { } },
"template": "-{{a.b.c}}-",
"expected": "--"
},
{
"name": "key lookup is anchored",
"data": { "c": "c", "b": { "c" : "cb" }, "a": { "b": { } } },
"template": "-{{a.b.c}}-",
"expected": "--"
},
{
"name": "key lookup is anchored",
"data": { "c": "c", "b": { "c" : "cb" }, "a": { "b": { "c": "ca" } } },
"template": "-{{a.b.c}}-",
"expected": "-ca-"
},
{
"name": "key lookup is anchored",
"data": { "b": "b", "a": { } },
"template": "-{{#a}}{{b}}{{/a}}-{{#a}}{{.b}}{{/a}}-{{a.b}}-",
"expected": "-b---"
},
{
"name": "key lookup is anchored",
"data": { "b": "b", "a": { "b": "ba"} },
"template": "-{{#a}}{{b}}{{/a}}-{{#a}}{{.b}}{{/a}}-{{a.b}}-",
"expected": "-ba-ba-ba-"
}
]
}
Expand Down

0 comments on commit 3642305

Please sign in to comment.