/
GRMustacheCompiler.m
170 lines (141 loc) · 6.31 KB
/
GRMustacheCompiler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// The MIT License
//
// Copyright (c) 2010 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "GRMustacheCompiler_private.h"
#import "GRMustacheTemplateLoader_private.h"
#import "GRMustacheTextElement_private.h"
#import "GRMustacheVariableElement_private.h"
#import "GRMustacheSectionElement_private.h"
#import "GRBoolean.h"
#import "GRMustacheError.h"
@interface GRMustacheCompiler()
@property (nonatomic, retain) NSError *error;
@property (nonatomic, retain) GRMustacheToken *currentSectionOpeningToken;
- (NSError *)parseErrorAtLine:(NSInteger)line description:(NSString *)description;
@end
@implementation GRMustacheCompiler
@synthesize error;
@synthesize currentSectionOpeningToken;
- (NSArray *)parseString:(NSString *)theTemplateString withTokenProducer:(id<GRMustacheTokenProducer>)tokenProducer templateLoader:(GRMustacheTemplateLoader *)theTemplateLoader templateId:(id)theTemplateId error:(NSError **)outError {
templateString = theTemplateString;
templateId = theTemplateId;
templateLoader = theTemplateLoader;
currentElements = [NSMutableArray arrayWithCapacity:20];
elementsStack = [[NSMutableArray alloc] initWithCapacity:20];
[elementsStack addObject:currentElements];
sectionOpeningTokenStack = [[NSMutableArray alloc] initWithCapacity:20];
[tokenProducer parseTemplateString:templateString forTokenConsumer:self];
if (error == nil && currentSectionOpeningToken) {
self.error = [self parseErrorAtLine:currentSectionOpeningToken.line
description:[NSString stringWithFormat:@"Unclosed `%@` section", currentSectionOpeningToken.content]];
}
if (error) {
if (outError != NULL) {
*outError = error;
}
return nil;
}
[currentElements retain];
[elementsStack release];
[sectionOpeningTokenStack release];
return [currentElements autorelease];
}
- (void)dealloc {
[error release];
[currentSectionOpeningToken release];
[super dealloc];
}
#pragma mark GRMustacheTokenConsumer
- (BOOL)tokenProducer:(id<GRMustacheTokenProducer>)tokenProducer shouldContinueParsingAfterReadingToken:(GRMustacheToken *)token {
switch (token.type) {
case GRMustacheTokenTypeText:
[currentElements addObject:[GRMustacheTextElement textElementWithString:token.content]];
break;
case GRMustacheTokenTypeComment:
break;
case GRMustacheTokenTypeEscapedVariable:
[currentElements addObject:[GRMustacheVariableElement variableElementWithName:token.content raw:NO]];
break;
case GRMustacheTokenTypeUnescapedVariable:
[currentElements addObject:[GRMustacheVariableElement variableElementWithName:token.content raw:YES]];
break;
case GRMustacheTokenTypeSectionOpening:
case GRMustacheTokenTypeInvertedSectionOpening:
self.currentSectionOpeningToken = token;
[sectionOpeningTokenStack addObject:token];
currentElements = [NSMutableArray array];
[elementsStack addObject:currentElements];
break;
case GRMustacheTokenTypeSectionClosing:
if ([token.content isEqualToString:currentSectionOpeningToken.content]) {
NSRange currentSectionOpeningTokenRange = currentSectionOpeningToken.range;
GRMustacheSectionElement *section = [GRMustacheSectionElement sectionElementWithName:currentSectionOpeningToken.content
string:[templateString substringWithRange:NSMakeRange(currentSectionOpeningTokenRange.location + currentSectionOpeningTokenRange.length, token.range.location - currentSectionOpeningTokenRange.location - currentSectionOpeningTokenRange.length)]
inverted:currentSectionOpeningToken.type == GRMustacheTokenTypeInvertedSectionOpening
elements:currentElements];
[sectionOpeningTokenStack removeLastObject];
self.currentSectionOpeningToken = [sectionOpeningTokenStack lastObject];
[elementsStack removeLastObject];
currentElements = [elementsStack lastObject];
[currentElements addObject:section];
} else {
self.error = [self parseErrorAtLine:token.line description:[NSString stringWithFormat:@"Unexpected `%@` section closing tag", token.content]];
return NO;
}
break;
case GRMustacheTokenTypePartial: {
NSError *partialError;
GRMustacheTemplate *partialTemplate = [templateLoader parseTemplateNamed:token.content
relativeToTemplateId:templateId
error:&partialError];
if (partialTemplate == nil) {
self.error = partialError;
return NO;
} else {
[currentElements addObject:partialTemplate];
}
} break;
case GRMustacheTokenTypeSetDelimiter:
// ignore
break;
default:
NSAssert(NO, nil);
break;
}
return YES;
}
- (void)tokenProducerDidStart:(id<GRMustacheTokenProducer>)tokenProducer {
}
- (void)tokenProducerDidFinish:(id<GRMustacheTokenProducer>)tokenProducer withError:(NSError *)theError {
self.error = theError;
}
#pragma mark Private
- (NSError *)parseErrorAtLine:(NSInteger)line description:(NSString *)description {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:3];
[userInfo setObject:[NSString stringWithFormat:@"Parse error at line %ld: %@", line, description]
forKey:NSLocalizedDescriptionKey];
[userInfo setObject:[NSNumber numberWithInteger:line]
forKey:GRMustacheErrorLine];
return [NSError errorWithDomain:GRMustacheErrorDomain
code:GRMustacheErrorCodeParseError
userInfo:userInfo];
}
@end