Skip to content
This repository
Newer
Older
100644 114 lines (89 sloc) 3.08 kb
05eedb5e »
2010-01-25 initial add
1 // Copyright 2010 Todd Ditchendorf
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #import <ParseKit/PKSymbolState.h>
16 #import <ParseKit/PKToken.h>
17 #import <ParseKit/PKSymbolRootNode.h>
18 #import <ParseKit/PKReader.h>
19 #import <ParseKit/PKTokenizer.h>
20
21 @interface PKToken ()
22 @property (nonatomic, readwrite) NSUInteger offset;
23 @end
24
25 @interface PKTokenizerState ()
26 - (void)resetWithReader:(PKReader *)r;
27 - (PKTokenizerState *)nextTokenizerStateFor:(PKUniChar)c tokenizer:(PKTokenizer *)t;
28 @end
29
30 @interface PKSymbolState ()
31 - (PKToken *)symbolTokenWith:(PKUniChar)cin;
32 - (PKToken *)symbolTokenWithSymbol:(NSString *)s;
33
34 @property (nonatomic, retain) PKSymbolRootNode *rootNode;
35 @property (nonatomic, retain) NSMutableArray *addedSymbols;
36 @end
37
38 @implementation PKSymbolState
39
40 - (id)init {
41 if (self = [super init]) {
42 self.rootNode = [[[PKSymbolRootNode alloc] init] autorelease];
43 self.addedSymbols = [NSMutableArray array];
44 }
45 return self;
46 }
47
48
49 - (void)dealloc {
50 self.rootNode = nil;
51 self.addedSymbols = nil;
52 [super dealloc];
53 }
54
55
56 - (PKToken *)nextTokenFromReader:(PKReader *)r startingWith:(PKUniChar)cin tokenizer:(PKTokenizer *)t {
57 NSParameterAssert(r);
58 [self resetWithReader:r];
59
60 NSString *symbol = [rootNode nextSymbol:r startingWith:cin];
61 NSUInteger len = [symbol length];
62
63 while (len > 1) {
64 if ([addedSymbols containsObject:symbol]) {
65 return [self symbolTokenWithSymbol:symbol];
66 }
67
68 symbol = [symbol substringToIndex:[symbol length] - 1];
69 len = [symbol length];
70 [r unread:1];
71 }
72
73 if (1 == len) {
74 return [self symbolTokenWith:cin];
75 } else {
76 PKTokenizerState *state = [self nextTokenizerStateFor:cin tokenizer:t];
77 if (!state || state == self) {
78 return [self symbolTokenWith:cin];
79 } else {
80 return [state nextTokenFromReader:r startingWith:cin tokenizer:t];
81 }
82 }
83 }
84
85
86 - (void)add:(NSString *)s {
87 NSParameterAssert(s);
88 [rootNode add:s];
89 [addedSymbols addObject:s];
90 }
91
92
93 - (void)remove:(NSString *)s {
94 NSParameterAssert(s);
95 [rootNode remove:s];
96 [addedSymbols removeObject:s];
97 }
98
99
100 - (PKToken *)symbolTokenWith:(PKUniChar)cin {
101 return [self symbolTokenWithSymbol:[NSString stringWithFormat:@"%C", cin]];
102 }
103
104
105 - (PKToken *)symbolTokenWithSymbol:(NSString *)s {
106 PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:s floatValue:0.0];
107 tok.offset = offset;
108 return tok;
109 }
110
111 @synthesize rootNode;
112 @synthesize addedSymbols;
113 @end
Something went wrong with that request. Please try again.