Skip to content

Commit

Permalink
#45 #46 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Feb 27, 2013
1 parent 4a86cf2 commit b624e4d
Show file tree
Hide file tree
Showing 8 changed files with 1,521 additions and 1,297 deletions.
178 changes: 127 additions & 51 deletions src/classes/GRMustacheCompiler.m

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/classes/GRMustacheCompiler_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@
@interface GRMustacheCompiler : NSObject<GRMustacheParserDelegate> {
@private
NSError *_fatalError;
NSMutableArray *_componentsStack;
NSMutableArray *_openingTokenStack;

NSMutableArray *_currentComponents;
NSMutableArray *_componentsStack;

GRMustacheToken *_currentOpeningToken;
NSMutableArray *_openingTokenStack;

NSObject *_currentTagValue;
NSMutableArray *_tagValueStack;

GRMustacheTemplateRepository *_templateRepository;
GRMustacheContentType _contentType;
BOOL _contentTypeLocked;
Expand Down
784 changes: 480 additions & 304 deletions src/classes/GRMustacheParser.m

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/classes/GRMustacheParser_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,22 @@
*/
+ (GRMustacheExpression *)parseExpression:(NSString *)string invalid:(BOOL *)outInvalid GRMUSTACHE_API_INTERNAL;

/**
* Returns a template name from a string.
*
* @param string A string.
*
* @return a template name, or nil if the string is not a partial name.
*/
+ (NSString *)parseTemplateName:(NSString *)string GRMUSTACHE_API_INTERNAL;

/**
* Returns a pragma from a string
*
* @param string A string
*
* @return a pragma, or nil if the string is not a pragma.
*/
+ (NSString *)parsePragma:(NSString *)string GRMUSTACHE_API_INTERNAL;

@end
53 changes: 13 additions & 40 deletions src/classes/GRMustacheToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,13 @@
#import "GRMustacheToken_private.h"


@interface GRMustacheToken()

/**
* @see +[GRMustacheToken tokenWithType:value:templateString:templateID:line:range:]
*/
- (id)initWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range text:(NSString *)text expression:(GRMustacheExpression *)expression invalidExpression:(BOOL)invalidExpression partialName:(NSString *)partialName pragma:(NSString *)pragma;

@end

@implementation GRMustacheToken
@synthesize type=_type;
@synthesize templateString=_templateString;
@synthesize templateID=_templateID;
@synthesize line=_line;
@synthesize range=_range;
@synthesize text=_text;
@synthesize expression=_expression;
@synthesize invalidExpression=_invalidExpression;
@synthesize partialName=_partialName;
@synthesize pragma=_pragma;
@synthesize tagInnerRange=_tagInnerRange;

- (void)dealloc
{
Expand All @@ -51,39 +38,25 @@ - (void)dealloc
[super dealloc];
}

+ (instancetype)tokenWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range text:(NSString *)text expression:(GRMustacheExpression *)expression invalidExpression:(BOOL)invalidExpression partialName:(NSString *)partialName pragma:(NSString *)pragma
{
return [[[self alloc] initWithType:type templateString:templateString templateID:templateID line:line range:range text:text expression:expression invalidExpression:invalidExpression partialName:partialName pragma:pragma] autorelease];
}

- (id)tokenWithExpression:(GRMustacheExpression *)expression
+ (instancetype)tokenWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range
{
return [[[GRMustacheToken alloc] initWithType:_type templateString:_templateString templateID:_templateID line:_line range:_range text:_text expression:expression invalidExpression:_invalidExpression partialName:_partialName pragma:_pragma] autorelease];
GRMustacheToken *token = [[[self alloc] init] autorelease];
token.type = type;
token.templateString = templateString;
token.templateID = templateID;
token.line = line;
token.range = range;
return token;
}

#pragma mark Private

- (id)initWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range text:(NSString *)text expression:(GRMustacheExpression *)expression invalidExpression:(BOOL)invalidExpression partialName:(NSString *)partialName pragma:(NSString *)pragma;
- (NSString *)templateSubstring
{
self = [self init];
if (self) {
_type = type;
_templateString = [templateString retain];
_templateID = [templateID retain];
_line = line;
_range = range;
_text = text;
_expression = expression;
_invalidExpression = invalidExpression;
_partialName = partialName;
_pragma = pragma;
}
return self;
return [_templateString substringWithRange:_range];
}

- (NSString *)templateSubstring
- (NSString *)tagInnerContent
{
return [_templateString substringWithRange:_range];
return [_templateString substringWithRange:_tagInnerRange];
}

@end
Expand Down
108 changes: 38 additions & 70 deletions src/classes/GRMustacheToken_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,35 @@
*/
typedef NS_ENUM(NSInteger, GRMustacheTokenType) {
/**
* The kind of tokens representing escaped variable tags such as `{{name}}`.
*
* The implementation of GRMustacheParser depends on the fact that
* GRMustacheTokenTypeEscapedVariable is 0.
* TODO
*/
GRMustacheTokenTypeEscapedVariable = 0,
GRMustacheTokenTypeBlankLine,

/**
* The kind of tokens representing raw text.
* TODO
*/
GRMustacheTokenTypeText,
GRMustacheTokenTypeBlankEndOfLine,

/**
* TODO
*/
GRMustacheTokenTypeBlankPrefix,

/**
* TODO
*/
GRMustacheTokenTypeBlankSuffix,

/**
* TODO
*/
GRMustacheTokenTypeContent,

/**
* The kind of tokens representing escaped variable tags such as `{{name}}`.
*/
GRMustacheTokenTypeEscapedVariable,

/**
* The kind of tokens representing a comment tag such as `{{! comment }}`.
*/
Expand Down Expand Up @@ -115,68 +132,30 @@ typedef NS_ENUM(NSInteger, GRMustacheTokenType) {
id _templateID;
NSUInteger _line;
NSRange _range;
NSString *_text;
GRMustacheExpression *_expression;
BOOL _invalidExpression;
NSString *_partialName;
NSString *_pragma;
NSRange _tagInnerRange;
}

/**
* The type of the token.
*/
@property (nonatomic, readonly) GRMustacheTokenType type GRMUSTACHE_API_INTERNAL;

/**
* Returns the text of tokens GRMustacheTokenTypeText and
* GRMustacheTokenTypeComment.
*/
@property (nonatomic, assign, readonly) NSString *text GRMUSTACHE_API_INTERNAL;

/**
* Returns the expression of tokens GRMustacheTokenTypeEscapedVariable,
* GRMustacheTokenTypeUnescapedVariable, GRMustacheTokenTypeSectionOpening,
* GRMustacheTokenTypeInvertedSectionOpening,
* GRMustacheTokenTypeOverridableSectionOpening and GRMustacheTokenTypeClosing.
*/
@property (nonatomic, assign, readonly) GRMustacheExpression *expression GRMUSTACHE_API_INTERNAL;

/**
* Returns whether the expresion of tokens GRMustacheTokenTypeEscapedVariable,
* GRMustacheTokenTypeUnescapedVariable, GRMustacheTokenTypeSectionOpening,
* GRMustacheTokenTypeInvertedSectionOpening,
* GRMustacheTokenTypeOverridableSectionOpening and GRMustacheTokenTypeClosing
* is invald.
*/
@property (nonatomic, readonly) BOOL invalidExpression GRMUSTACHE_API_INTERNAL;

/**
* Returns the partial name of tokens GRMustacheTokenTypePartial,
* GRMustacheTokenTypeOverridablePartial and GRMustacheTokenTypeClosing.
*/
@property (nonatomic, assign, readonly) NSString *partialName GRMUSTACHE_API_INTERNAL;

/**
* Returns the pragma name of tokens GRMustacheTokenTypePragma.
*/
@property (nonatomic, assign, readonly) NSString *pragma GRMUSTACHE_API_INTERNAL;
@property (nonatomic) GRMustacheTokenType type GRMUSTACHE_API_INTERNAL;

/**
* The Mustache template string this token comes from.
*/
@property (nonatomic, readonly, retain) NSString *templateString GRMUSTACHE_API_INTERNAL;
@property (nonatomic, retain) NSString *templateString GRMUSTACHE_API_INTERNAL;

/**
* The template ID of the template this token comes from.
*
* @see GRMustacheTemplateRepository
*/
@property (nonatomic, readonly, retain) id templateID GRMUSTACHE_API_INTERNAL;
@property (nonatomic, retain) id templateID GRMUSTACHE_API_INTERNAL;

/**
* The line in templateString where this token lies.
*/
@property (nonatomic, readonly) NSUInteger line GRMUSTACHE_API_INTERNAL;
@property (nonatomic) NSUInteger line GRMUSTACHE_API_INTERNAL;

/**
* The range in templateString where this token lies.
Expand All @@ -187,36 +166,25 @@ typedef NS_ENUM(NSInteger, GRMustacheTokenType) {
* For tokens of a tag type, the range is the full range of the tag, from
* `{{` to `}}` included.
*/
@property (nonatomic, readonly) NSRange range GRMUSTACHE_API_INTERNAL;
@property (nonatomic) NSRange range GRMUSTACHE_API_INTERNAL;

/**
* TODO
*/
@property (nonatomic) NSRange tagInnerRange GRMUSTACHE_API_INTERNAL;

/**
* The substring of the template represented by this token.
*/
@property (nonatomic, readonly) NSString *templateSubstring GRMUSTACHE_API_INTERNAL;

/**
* Return a new token that is a copy of the receiving token, but for the
* expression.
* TODO
*/
- (id)tokenWithExpression:(GRMustacheExpression *)expression GRMUSTACHE_API_INTERNAL;
@property (nonatomic, readonly) NSString *tagInnerContent GRMUSTACHE_API_INTERNAL;

/**
* Builds and return a token.
*
* The caller is responsible for honoring the template properties semantics and
* relationships, especially providing for the last parameters a value
* suitable for its type.
*
* @see type
* @see templateString
* @see templateID
* @see line
* @see range
* @see text
* @see expression
* @see invalidExpression
* @see partialName
* @see pragma
*/
+ (instancetype)tokenWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range text:(NSString *)text expression:(GRMustacheExpression *)expression invalidExpression:(BOOL)invalidExpression partialName:(NSString *)partialName pragma:(NSString *)pragma GRMUSTACHE_API_INTERNAL;
+ (instancetype)tokenWithType:(GRMustacheTokenType)type templateString:(NSString *)templateString templateID:(id)templateID line:(NSUInteger)line range:(NSRange)range GRMUSTACHE_API_INTERNAL;
@end
Loading

0 comments on commit b624e4d

Please sign in to comment.