Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (88 sloc) 2.459 kB
ee99867 @ccgus oh geeze
authored
1 //
2 // TDParser.m
3 // TDParseKit
4 //
5 // Created by Todd Ditchendorf on 1/20/06.
6 // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 //
8
9 #import "TDParser.h"
10 #import "TDAssembly.h"
11
12 @interface TDParser ()
13 - (NSSet *)matchAndAssemble:(NSSet *)inAssemblies;
14 - (TDAssembly *)best:(NSSet *)inAssemblies;
15 @end
16
17 @implementation TDParser
18
19 + (id)parser {
20 return [[[self alloc] init] autorelease];
21 }
22
23
24 - (id)init {
25 self = [super init];
26 if (self) {
27 }
28 return self;
29 }
30
31
32 - (void)dealloc {
33 assembler = nil;
34 self.selector = nil;
35 self.name = nil;
36 [super dealloc];
37 }
38
39
40 - (void)setAssembler:(id)a selector:(SEL)sel {
41 self.assembler = a;
42 self.selector = sel;
43 }
44
45
46 - (NSSet *)allMatchesFor:(NSSet *)inAssemblies {
4916b19 @ccgus Fixed a bunch of string formatting errors (thanks to clang 3.1 pointi…
authored
47 NSAssert1(0, @"-[TDParser %@] must be overriden", NSStringFromSelector(_cmd));
ee99867 @ccgus oh geeze
authored
48 return nil;
49 }
50
51
52 - (TDAssembly *)bestMatchFor:(TDAssembly *)a {
53 NSParameterAssert(a);
54 NSSet *initialState = [NSSet setWithObject:a];
55 NSSet *finalState = [self matchAndAssemble:initialState];
56 return [self best:finalState];
57 }
58
59
60 - (TDAssembly *)completeMatchFor:(TDAssembly *)a {
61 NSParameterAssert(a);
62 TDAssembly *best = [self bestMatchFor:a];
63 if (best && ![best hasMore]) {
64 return best;
65 }
66 return nil;
67 }
68
69
70 - (NSSet *)matchAndAssemble:(NSSet *)inAssemblies {
71 NSParameterAssert(inAssemblies);
72 NSSet *outAssemblies = [self allMatchesFor:inAssemblies];
73 if (assembler) {
4916b19 @ccgus Fixed a bunch of string formatting errors (thanks to clang 3.1 pointi…
authored
74 NSAssert2([assembler respondsToSelector:selector], @"provided assembler %@ should respond to %@", assembler, NSStringFromSelector(selector));
ee99867 @ccgus oh geeze
authored
75 for (TDAssembly *a in outAssemblies) {
76 [assembler performSelector:selector withObject:a];
77 }
78 }
79 return outAssemblies;
80 }
81
82
83 - (TDAssembly *)best:(NSSet *)inAssemblies {
84 NSParameterAssert(inAssemblies);
85 TDAssembly *best = nil;
86
87 for (TDAssembly *a in inAssemblies) {
88 if (![a hasMore]) {
89 best = a;
90 break;
91 }
92 if (!best || a.objectsConsumed > best.objectsConsumed) {
93 best = a;
94 }
95 }
96
97 return best;
98 }
99
100
101 - (NSString *)description {
102 NSString *className = [[self className] substringFromIndex:2];
103 if (name.length) {
104 return [NSString stringWithFormat:@"%@ (%@)", className, name];
105 } else {
106 return [NSString stringWithFormat:@"%@", className];
107 }
108 }
109
110 @synthesize assembler;
111 @synthesize selector;
112 @synthesize name;
113 @end
Something went wrong with that request. Please try again.