forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKTrack.m
91 lines (71 loc) · 2.42 KB
/
PKTrack.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
//
// PKTrack.m
// ParseKit
//
// Created by Todd Ditchendorf on 8/13/08.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//
#import <ParseKit/PKTrack.h>
#import <ParseKit/PKAssembly.h>
#import <ParseKit/PKTrackException.h>
@interface PKAssembly ()
- (id)peek;
- (NSString *)consumedObjectsJoinedByString:(NSString *)delimiter;
@end
@interface PKParser ()
- (NSSet *)matchAndAssemble:(NSSet *)inAssemblies;
- (PKAssembly *)best:(NSSet *)inAssemblies;
@end
@interface PKTrack ()
- (void)throwTrackExceptionWithPreviousState:(NSSet *)inAssemblies parser:(PKParser *)p;
@end
@interface PKCollectionParser ()
+ (id)collectionParserWithFirst:(PKParser *)p1 rest:(va_list)rest;
@end
@implementation PKTrack
+ (id)track {
return [self trackWithSubparsers:nil];
}
+ (id)trackWithSubparsers:(PKParser *)p1, ... {
va_list vargs;
va_start(vargs, p1);
PKTrack *tr = [self collectionParserWithFirst:p1 rest:vargs];
va_end(vargs);
return tr;
}
- (NSSet *)allMatchesFor:(NSSet *)inAssemblies {
NSParameterAssert(inAssemblies);
BOOL inTrack = NO;
NSSet *lastAssemblies = inAssemblies;
NSSet *outAssemblies = inAssemblies;
for (PKParser *p in subparsers) {
outAssemblies = [p matchAndAssemble:outAssemblies];
if (![outAssemblies count]) {
if (inTrack) {
[self throwTrackExceptionWithPreviousState:lastAssemblies parser:p];
}
break;
}
inTrack = YES;
lastAssemblies = outAssemblies;
}
return outAssemblies;
}
- (void)throwTrackExceptionWithPreviousState:(NSSet *)inAssemblies parser:(PKParser *)p {
PKAssembly *best = [self best:inAssemblies];
NSString *after = [best consumedObjectsJoinedByString:@" "];
if (![after length]) {
after = @"-nothing-";
}
NSString *expected = [p description];
id next = [best peek];
NSString *found = next ? [next description] : @"-nothing-";
NSString *reason = [NSString stringWithFormat:@"\n\nAfter : %@\nExpected : %@\nFound : %@\n\n", after, expected, found];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
after, @"after",
expected, @"expected",
found, @"found",
nil];
[[PKTrackException exceptionWithName:PKTrackExceptionName reason:reason userInfo:userInfo] raise];
}
@end