Skip to content
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
4 changes: 2 additions & 2 deletions src/handlebars-objc/ast/HBAstBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
@property (retain, nonatomic) HBAstTag* elseTag;
@property (retain, nonatomic) HBAstTag* closeTag;

@property (retain, nonatomic) NSMutableArray* elseBlocks;

@property (readonly, nonatomic) HBAstExpression* expression; // computed

@property (retain, nonatomic) NSMutableArray* statements;
@property (retain, nonatomic) NSMutableArray* inverseStatements;

@property BOOL invertedBlock;

@end
1 change: 1 addition & 0 deletions src/handlebars-objc/ast/HBAstBlock.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ - (void) dealloc
self.openTag = nil;
self.elseTag = nil;
self.closeTag = nil;
self.elseBlocks = nil;

self.statements = nil;
self.inverseStatements = nil;
Expand Down
94 changes: 67 additions & 27 deletions src/handlebars-objc/astVisitors/HBAstEvaluationVisitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,36 @@ - (void) evaluateContextualParametersInExpression:(HBAstExpression*)expression p
}
}

-(NSString*)evaluateHelperCallExpression:(HBAstBlock*)node forwardStatementsEvaluator:(HBStatementsEvaluator)forwardStatementsEvaluator inverseStatementsEvaluator:(HBStatementsEvaluator)inverseStatementsEvaluator
{
HBHelper* helper = (HBHelper*)[self helperForExpression:node.expression];
if (!helper) {
if (!self.error) // we report only one error for now.
self.error = [HBHelperMissingError HBHelperMissingErrorWithHelperName:[node.expression.mainValue.keyPath[0] key]];
return nil;
}

NSArray* positionalParameters = nil;
NSDictionary* namedParameters = nil;
[self evaluateContextualParametersInExpression:node.expression positionalParameters:&positionalParameters namedParameters:&namedParameters];

HBHelperCallingInfo* callingInfo = [[HBHelperCallingInfo alloc] init];

callingInfo.context = self.contextStack.current.context;
callingInfo.data = self.contextStack.current.dataContext;
callingInfo.positionalParameters = positionalParameters;
callingInfo.namedParameters = namedParameters;
callingInfo.statements = forwardStatementsEvaluator;
callingInfo.inverseStatements = inverseStatementsEvaluator;
callingInfo.template = self.template;
callingInfo.evaluationVisitor = self;
callingInfo.invocationKind = HBHelperInvocationBlock;

NSString* helperResult = helper.block(callingInfo);
[callingInfo release];

return helperResult;
}

#pragma mark -
#pragma mark Visiting High-level nodes
Expand All @@ -237,40 +267,50 @@ - (id) visitBlock:(HBAstBlock*)node
};

HBStatementsEvaluator inverseStatementsEvaluator = ^(id context, HBDataContext* data) {
for( HBAstBlock *elseBlock in node.elseBlocks ) {

if( elseBlock.expression == nil )
{
// else only case
return evaluateStatements(nil, nil, elseBlock.inverseStatements, false);
}
else if( [self expressionIsHelperCall:elseBlock.expression]) {

// This is a block helper. Evaluate expression params and invoke helper
id result = [self evaluateHelperCallExpression:elseBlock forwardStatementsEvaluator:^NSString*(id context, HBDataContext* data){ return @"!"; } inverseStatementsEvaluator:^NSString*(id context, HBDataContext* data){ return @""; }];
if( [HBHelperUtils evaluateObjectAsBool:result] )
return evaluateStatements(nil, nil, elseBlock.statements, false);
}
else
{
id evaluatedExpression = [self visitExpression:elseBlock.expression];

if ([HBHelperUtils isEnumerableByIndex:evaluatedExpression] ) {
// Array-like context
id<NSFastEnumeration> arrayLike = evaluatedExpression;
for( id something in arrayLike )
{
(void)something; // make compiler happy
return evaluateStatements(nil, nil, elseBlock.statements, false);
}
} else {
// String of scalar context
if ([HBHelperUtils evaluateObjectAsBool:evaluatedExpression]) {
return evaluateStatements(nil, nil, elseBlock.statements, false);
}
}
}
// try next else block
}
// no else block, try inverseStatements
return evaluateStatements(nil, nil, node.inverseStatements, false);
};


if ([self expressionIsHelperCall:node.expression]) {
// This is a block helper. Evaluate expression params and invoke helper

HBHelper* helper = (HBHelper*)[self helperForExpression:node.expression];
if (!helper) {
if (!self.error) // we report only one error for now.
self.error = [HBHelperMissingError HBHelperMissingErrorWithHelperName:[node.expression.mainValue.keyPath[0] key]];
return nil;
}

NSArray* positionalParameters = nil;
NSDictionary* namedParameters = nil;
[self evaluateContextualParametersInExpression:node.expression positionalParameters:&positionalParameters namedParameters:&namedParameters];

HBHelperCallingInfo* callingInfo = [[HBHelperCallingInfo alloc] init];

callingInfo.context = self.contextStack.current.context;
callingInfo.data = self.contextStack.current.dataContext;
callingInfo.positionalParameters = positionalParameters;
callingInfo.namedParameters = namedParameters;
callingInfo.statements = forwardStatementsEvaluator;
callingInfo.inverseStatements = inverseStatementsEvaluator;
callingInfo.template = self.template;
callingInfo.evaluationVisitor = self;
callingInfo.invocationKind = HBHelperInvocationBlock;

NSString* helperResult = helper.block(callingInfo);
[callingInfo release];

return helperResult;
return [self evaluateHelperCallExpression:node forwardStatementsEvaluator:forwardStatementsEvaluator inverseStatementsEvaluator:inverseStatementsEvaluator];
} else {
// This is a normal block.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ - (id) visitBlock:(HBAstBlock*)node
[self visitNode:statement];
}
}


for( HBAstBlock *elseBlock in node.elseBlocks )
[self visitBlock:elseBlock];

[self processTag:node.closeTag];

return nil;
}

Expand Down
12 changes: 11 additions & 1 deletion src/handlebars-objc/astVisitors/HBAstParserTestVisitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ - (id) visitBlock:(HBAstBlock*)node
add_cr();
}
}

if (node.inverseStatements) {
[_result appendString:@" {{^}}\n"];
for (HBAstNode* statement in node.inverseStatements) {
Expand All @@ -75,6 +75,16 @@ - (id) visitBlock:(HBAstBlock*)node
add_cr();
}
}

if( node.elseBlocks.count > 0 ) {
[_result appendString:@" {{else}}\n"];
for( HBAstBlock *elseBlock in node.elseBlocks ) {
[_result appendString:@" "];
[self visitNode:elseBlock];
add_cr();
}
}

return nil;
}

Expand Down
4 changes: 3 additions & 1 deletion src/handlebars-objc/helpers/HBBuiltinHelpersRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#import "HBTemplate_Private.h"
#import "HBEscapedString.h"

#import <objc/message.h>

static HBBuiltinHelpersRegistry* _builtinHelpersRegistry = nil;

@interface HBBuiltinHelpersRegistry()
Expand Down Expand Up @@ -165,7 +167,7 @@ + (void) registerEachHelper
NSMutableString* result = [NSMutableString string];
for (id key in dictionaryLike) {
dictionaryData[@"key"] = key;
id statementEvaluation = callingInfo.statements(dictionaryLike[key], dictionaryData);
id statementEvaluation = callingInfo.statements(((id(*)(id, SEL,NSString*))objc_msgSend)(dictionaryLike,@selector(objectForKeyedSubscript:),key), dictionaryData);
if (statementEvaluation) [result appendString:statementEvaluation];
}
[dictionaryData release];
Expand Down
4 changes: 4 additions & 0 deletions src/handlebars-objc/helpers/HBHelperUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ + (BOOL)evaluateObjectAsBool:(id)object
if ([object isKindOfClass:[NSNumber class]]) return [object boolValue];
if ([object isKindOfClass:[NSString class]]) return [object length] > 0;
if ([object isKindOfClass:[NSArray class]]) return [object count] > 0;
if ([object isKindOfClass:[NSDictionary class]]) return [object count] > 0;
if ([object isKindOfClass:[NSSet class]]) return [object count] > 0;
return false;
}

Expand All @@ -110,6 +112,8 @@ + (NSInteger)evaluateObjectAsInteger:(id)object
if ([object isKindOfClass:[NSNumber class]]) return [object integerValue];
if ([object isKindOfClass:[NSString class]]) return [object length];
if ([object isKindOfClass:[NSArray class]]) return [object count];
if ([object isKindOfClass:[NSDictionary class]]) return [object count];
if ([object isKindOfClass:[NSSet class]]) return [object count];
return 0;
}

Expand Down
16 changes: 13 additions & 3 deletions src/handlebars-objc/parser/handlebars-objc.lm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ WS [ \t]*
\{\{!\-\- { yy_push_state(DASHED_COMMENT, yyscanner); found(DASHED_COMMENT_START); }
<DASHED_COMMENT>\-\-\}\} { yy_pop_state(yyscanner); found(DASHED_COMMENT_END); }

/* skip empty inverse sections */
\{\{~?^~?\}\}\{\{\/ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_ENDBLOCK); }
\{\{~?^~?\}\}\{\{~\/ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_ENDBLOCK); }
\{\{~?else~?\}\}\{\{\/ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_ENDBLOCK); }
\{\{~?else~?\}\}\{\{~\/ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_ENDBLOCK); }

\{\{ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN); }
\{\{~ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN); }
Expand All @@ -78,8 +83,13 @@ WS [ \t]*
\{\{~\/ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_ENDBLOCK); }
\{\{^ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_INVERSE); }
\{\{~^ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_INVERSE); }
\{\{[ ]*else { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_INVERSE); }
\{\{~[ ]*else { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_INVERSE); }
\{\{[ ]*else[ ]*\}\} { yylval->ival = 0; found(INVERSE); }
\{\{~[ ]*else[ ]*\}\} { yylval->ival = 1; found(INVERSE); }
\{\{[ ]*else[ ]*~\}\} { yylval->ival = 2; found(INVERSE); }
\{\{~[ ]*else[ ]*~\}\} { yylval->ival = 3; found(INVERSE); }

\{\{[ ]*else { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_INVERSE_CHAIN); }
\{\{~?[ ]*else { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_INVERSE_CHAIN); }
\{\{\{ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_UNESCAPED); }
\{\{~\{ { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 1; found(OPEN_UNESCAPED); }
\{\{& { yy_push_state(EXPRESSION, yyscanner); yylval->ival = 0; found(OPEN_UNESCAPED_AMPERSAND); }
Expand Down Expand Up @@ -112,7 +122,7 @@ WS [ \t]*
"."/[\}\/ \t\)~] { yylval->nsString = nsString(yytext); found(ID); }
".." { yylval->nsString = nsString(yytext); found(ID); }
[\/\.] { yylval->nsString = nsString(yytext); found(PATH_SEPARATOR); }
{ID}+/[=\/\. \t\}\)~] { yylval->nsString = nsString(yytext); found(ID); }
{ID}+/[=\/\. \t\}\)~\(] { yylval->nsString = nsString(yytext); found(ID); }
\[[^\[]*\] { NSString* s = nsString(yytext); yylval->nsString = [s substringWithRange:NSMakeRange(1, s.length -2)]; found(ID); } /* XXX must remove [] */
{WS}* {}
. { found(UNKNOWN); }
Expand Down
Loading