Skip to content
Browse files

* revert porting of delimit state changes from pegkit

  • Loading branch information...
1 parent 84a635b commit 26b48cf5f3192de2ce02a9fb23aa0c190c437e8f @itod committed Apr 17, 2014
View
20 src/PKDelimitDescriptor.h
@@ -1,24 +1,10 @@
-// The MIT License (MIT)
//
-// Copyright (c) 2014 Todd Ditchendorf
+// PKDelimitDescriptor.h
+// ParseKit
//
-// 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:
+// Created by Todd Ditchendorf on 3/20/13.
//
-// 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 <Foundation/Foundation.h>
View
22 src/PKDelimitDescriptor.m
@@ -1,24 +1,10 @@
-// The MIT License (MIT)
//
-// Copyright (c) 2014 Todd Ditchendorf
+// PKDelimitDescriptor.m
+// ParseKit
//
-// 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:
+// Created by Todd Ditchendorf on 3/20/13.
//
-// 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 "PKDelimitDescriptor.h"
@@ -74,7 +60,7 @@ - (BOOL)isEqual:(id)obj {
- (NSString *)description {
- return [NSString stringWithFormat:@"<%@ %p %@ %@ %@>", [self class], self, _startMarker, _endMarker, _characterSet];
+ return [NSString stringWithFormat:@"<%@ %p %@ %@>", [self class], self, _startMarker, _endMarker];
}
@end
View
20 src/PKDelimitDescriptorCollection.h
@@ -1,24 +1,10 @@
-// The MIT License (MIT)
//
-// Copyright (c) 2014 Todd Ditchendorf
+// PKDelimitDescriptorCollection.h
+// ParseKit
//
-// 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:
+// Created by Todd Ditchendorf on 3/20/13.
//
-// 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 <Foundation/Foundation.h>
View
20 src/PKDelimitDescriptorCollection.m
@@ -1,24 +1,10 @@
-// The MIT License (MIT)
//
-// Copyright (c) 2014 Todd Ditchendorf
+// PKDelimitDescriptorCollection.m
+// ParseKit
//
-// 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:
+// Created by Todd Ditchendorf on 3/20/13.
//
-// 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 "PKDelimitDescriptorCollection.h"
#import "PKDelimitDescriptor.h"
View
225 src/PKDelimitState.m
@@ -1,32 +1,24 @@
-// The MIT License (MIT)
-//
-// Copyright (c) 2014 Todd Ditchendorf
-//
-// 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 <PEGKit/PKDelimitState.h>
-#import <PEGKit/PKReader.h>
-#import <PEGKit/PKTokenizer.h>
-#import <PEGKit/PKToken.h>
-#import <PEGKit/PKWhitespaceState.h>
-#import <PEGKit/PKTypes.h>
-#import "PKSymbolRootNode.h"
+// Copyright 2010 Todd Ditchendorf
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#import <ParseKit/PKDelimitState.h>
+#import <ParseKit/PKReader.h>
+#import <ParseKit/PKTokenizer.h>
+#import <ParseKit/PKToken.h>
+#import <ParseKit/PKWhitespaceState.h>
+#import <ParseKit/PKSymbolRootNode.h>
+#import <ParseKit/PKTypes.h>
#import "PKDelimitDescriptorCollection.h"
#import "PKDelimitDescriptor.h"
@@ -46,7 +38,6 @@ - (void)appendString:(NSString *)s;
- (NSString *)bufferedString;
- (PKTokenizerState *)nextTokenizerStateFor:(PKUniChar)c tokenizer:(PKTokenizer *)t;
- (void)addStartMarker:(NSString *)start endMarker:(NSString *)end allowedCharacterSet:(NSCharacterSet *)set tokenKind:(NSInteger)kind;
-@property (nonatomic) NSUInteger offset;
@end
@interface PKDelimitState ()
@@ -60,7 +51,6 @@ - (id)init {
self = [super init];
if (self) {
self.rootNode = [[[PKSymbolRootNode alloc] init] autorelease];
- _rootNode.reportsAddedSymbolsOnly = YES;
self.collection = [[[PKDelimitDescriptorCollection alloc] init] autorelease];
}
return self;
@@ -78,140 +68,147 @@ - (void)addStartMarker:(NSString *)start endMarker:(NSString *)end allowedCharac
NSParameterAssert([start length]);
// add markers to root node
- [_rootNode add:start];
+ [rootNode add:start];
if ([end length]) {
- [_rootNode add:end];
+ [rootNode add:end];
}
// add descriptor to collection
PKDelimitDescriptor *desc = [PKDelimitDescriptor descriptorWithStartMarker:start endMarker:end characterSet:set];
- NSAssert(_collection, @"");
- [_collection add:desc];
+ NSAssert(collection, @"");
+ [collection add:desc];
}
- (PKToken *)nextTokenFromReader:(PKReader *)r startingWith:(PKUniChar)cin tokenizer:(PKTokenizer *)t {
NSParameterAssert(r);
NSParameterAssert(t);
- NSString *startMarker = [_rootNode nextSymbol:r startingWith:cin];
- NSMutableArray *matchingDescs = nil;
+ NSString *startMarker = [rootNode nextSymbol:r startingWith:cin];
+ NSArray *descs = nil;
- // check for false match
if ([startMarker length]) {
- matchingDescs = [[[_collection descriptorsForStartMarker:startMarker] mutableCopy] autorelease];
+ descs = [collection descriptorsForStartMarker:startMarker];
- if (![matchingDescs count]) {
+ if (![descs count]) {
[r unread:[startMarker length] - 1];
return [[self nextTokenizerStateFor:cin tokenizer:t] nextTokenFromReader:r startingWith:cin tokenizer:t];
}
}
- // reset
[self resetWithReader:r];
- self.offset = r.offset - [startMarker length];
[self appendString:startMarker];
- NSUInteger stackCount = 0;
-
- // setup a temp root node with current start and end markers
- PKSymbolRootNode *currRootNode = [[[PKSymbolRootNode alloc] init] autorelease];
- currRootNode.reportsAddedSymbolsOnly = YES;
-
- for (PKDelimitDescriptor *desc in matchingDescs) {
- [currRootNode add:desc.startMarker];
- if (desc.endMarker) {
- [currRootNode add:desc.endMarker];
+ NSUInteger count = [descs count];
+ BOOL hasEndMarkers = NO;
+ PKUniChar endChars[count];
+ PKDelimitDescriptor *selectedDesc = nil;
+
+ NSUInteger i = 0;
+ for (PKDelimitDescriptor *desc in descs) {
+ NSString *endMarker = desc.endMarker;
+ PKUniChar e = PKEOF;
+
+ if ([endMarker length]) {
+ e = [endMarker characterAtIndex:0];
+ hasEndMarkers = YES;
}
+ endChars[i++] = e;
}
-
- PKUniChar c;
- PKDelimitDescriptor *matchedDesc = nil;
+ PKUniChar c;
for (;;) {
c = [r read];
- if ('\\' == c) {
- c = [r read];
- [self append:c];
- continue;
+ //NSLog(@"%C", (UniChar)c);
+ if (PKEOF == c) {
+ if (hasEndMarkers && balancesEOFTerminatedStrings) {
+ [self appendString:[descs[0] endMarker]];
+ } else if (hasEndMarkers) {
+ [r unread:[[self bufferedString] length] - 1];
+ return [[self nextTokenizerStateFor:cin tokenizer:t] nextTokenFromReader:r startingWith:cin tokenizer:t];
+ }
+ break;
}
- if (PKEOF == c) {
- if (!_balancesEOFTerminatedStrings) {
- for (PKDelimitDescriptor *desc in [[matchingDescs copy] autorelease]) {
- if (desc.endMarker) {
- [matchingDescs removeObject:desc];
- }
- }
+ if (!hasEndMarkers && [t.whitespaceState isWhitespaceChar:c]) {
+ // if only the start marker was matched, dont return delimited string token. instead, defer tokenization
+ if ([startMarker isEqualToString:[self bufferedString]]) {
+ [r unread:[startMarker length] - 1];
+ return [[self nextTokenizerStateFor:cin tokenizer:t] nextTokenFromReader:r startingWith:cin tokenizer:t];
}
+ // else, return delimited string tok
break;
}
- NSString *marker = [currRootNode nextSymbol:r startingWith:c];
- if ([marker length]) {
- for (PKDelimitDescriptor *desc in matchingDescs) {
- if (_allowsNestedMarkers && [marker isEqualToString:desc.startMarker] && ![desc.startMarker isEqualToString:desc.endMarker]) {
- ++stackCount;
+ BOOL done = NO;
+ NSString *endMarker = nil;
+ NSCharacterSet *charSet = nil;
+
+ for (NSUInteger i = 0; i < count; ++i) {
+ PKUniChar e = endChars[i];
+
+ if (e == c) {
+ selectedDesc = descs[i];
+ endMarker = [selectedDesc endMarker];
+ charSet = [selectedDesc characterSet];
+
+ NSString *peek = [rootNode nextSymbol:r startingWith:e];
+ if (endMarker && [endMarker isEqualToString:peek]) {
+ [self appendString:endMarker];
+ c = [r read];
+ done = YES;
break;
- } else if ([marker isEqualToString:desc.endMarker]) {
- if (_allowsNestedMarkers && stackCount > 0 && ![desc.startMarker isEqualToString:desc.endMarker]) {
- --stackCount;
- break;
- } else {
- matchedDesc = desc;
- [self appendString:desc.endMarker];
- break;
+ } else {
+ [r unread:[peek length] - 1];
+ if (e != [peek characterAtIndex:0]) {
+ [self append:c];
+ c = [r read];
}
}
}
- if (matchedDesc) {
- break;
- }
}
-
- for (PKDelimitDescriptor *desc in [[matchingDescs copy] autorelease]) {
- if (desc.characterSet && ![desc.characterSet characterIsMember:c]) {
- [matchingDescs removeObject:desc];
+
+ if (done) {
+ if (charSet) {
+ NSString *contents = [self bufferedString];
+ NSUInteger loc = [startMarker length];
+ NSUInteger len = [contents length] - (loc + [endMarker length]);
+ contents = [contents substringWithRange:NSMakeRange(loc, len)];
+
+ for (NSUInteger i = 0; i < len; ++i) {
+ PKUniChar c = [contents characterAtIndex:i];
+
+ // check if char is not in allowed character set (if given)
+ if (![charSet characterIsMember:c]) {
+ // if not, unwind and return a symbol tok for cin
+ [r unread:[[self bufferedString] length] - 1];
+ return [[self nextTokenizerStateFor:cin tokenizer:t] nextTokenFromReader:r startingWith:cin tokenizer:t];
+ }
+ }
}
- }
-
- // no remaining matches. bail
- if (![matchingDescs count]) {
+
break;
}
+
[self append:c];
}
- if (!matchedDesc && [matchingDescs count]) {
- matchedDesc = matchingDescs[0];
-
- if (PKEOF == c && _balancesEOFTerminatedStrings && matchedDesc.endMarker) {
- [self appendString:matchedDesc.endMarker];
- }
+ if (PKEOF != c) {
+ [r unread];
}
- PKToken *tok = nil;
+ PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeDelimitedString stringValue:[self bufferedString] floatValue:0.0];
+ tok.offset = offset;
- if (matchedDesc) {
- tok = [PKToken tokenWithTokenType:PKTokenTypeDelimitedString stringValue:[self bufferedString] doubleValue:0.0];
- tok.offset = self.offset;
-
- NSString *tokenKindKey = [NSString stringWithFormat:@"%@,%@", matchedDesc.startMarker, matchedDesc.endMarker];
- NSInteger tokenKind = [t tokenKindForStringValue:tokenKindKey];
- tok.tokenKind = tokenKind; //selectedDesc.tokenKind;
- } else {
- if (PKEOF != c) {
- [r unread];
- }
-
- NSUInteger buffLen = [[self bufferedString] length];
- [r unread:buffLen - 1];
-
- tok = [[self nextTokenizerStateFor:cin tokenizer:t] nextTokenFromReader:r startingWith:cin tokenizer:t];
- }
+ NSString *tokenKindKey = [NSString stringWithFormat:@"%@,%@", selectedDesc.startMarker, selectedDesc.endMarker];
+ NSInteger tokenKind = [t tokenKindForStringValue:tokenKindKey];
+ tok.tokenKind = tokenKind; //selectedDesc.tokenKind;
return tok;
}
+@synthesize rootNode;
+@synthesize balancesEOFTerminatedStrings;
+@synthesize collection;
@end
View
67 src/PKEmailState.m
@@ -1,30 +1,16 @@
-// The MIT License (MIT)
//
-// Copyright (c) 2014 Todd Ditchendorf
+// PKEmailState.m
+// ParseKit
//
-// 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:
+// Created by Todd Ditchendorf on 3/31/10.
+// Copyright 2010 Todd Ditchendorf. All rights reserved.
//
-// 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 <PEGKit/PKEmailState.h>
-#import <PEGKit/PKReader.h>
-#import <PEGKit/PKTokenizer.h>
-#import <PEGKit/PKToken.h>
-#import <PEGKit/PKTypes.h>
+
+#import <ParseKit/PKEmailState.h>
+#import <ParseKit/PKReader.h>
+#import <ParseKit/PKTokenizer.h>
+#import <ParseKit/PKToken.h>
+#import <ParseKit/PKTypes.h>
@interface PKToken ()
@property (nonatomic, readwrite) NSUInteger offset;
@@ -35,14 +21,11 @@ - (void)resetWithReader:(PKReader *)r;
- (void)append:(PKUniChar)c;
- (NSString *)bufferedString;
- (PKTokenizerState *)nextTokenizerStateFor:(PKUniChar)c tokenizer:(PKTokenizer *)t;
-@property (nonatomic) NSUInteger offset;
@end
@interface PKEmailState ()
- (BOOL)parseNameFromReader:(PKReader *)r;
- (BOOL)parseHostFromReader:(PKReader *)r;
-@property (nonatomic) PKUniChar c;
-@property (nonatomic) PKUniChar lastChar;
@end
@implementation PKEmailState
@@ -53,7 +36,7 @@ - (void)dealloc {
- (void)append:(PKUniChar)ch {
- self.lastChar = ch;
+ lastChar = ch;
[super append:ch];
}
@@ -62,26 +45,26 @@ - (PKToken *)nextTokenFromReader:(PKReader *)r startingWith:(PKUniChar)cin token
NSParameterAssert(r);
[self resetWithReader:r];
- self.lastChar = PKEOF;
- self.c = cin;
+ lastChar = PKEOF;
+ c = cin;
BOOL matched = [self parseNameFromReader:r];
if (matched) {
matched = [self parseHostFromReader:r];
}
- if (PKEOF != _c) {
+ if (PKEOF != c) {
[r unread];
}
NSString *s = [self bufferedString];
if (matched) {
- if ('.' == _lastChar) {
+ if ('.' == lastChar) {
s = [s substringToIndex:[s length] - 1];
[r unread];
}
- PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeEmail stringValue:s doubleValue:0.0];
- tok.offset = self.offset;
+ PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeEmail stringValue:s floatValue:0.0];
+ tok.offset = offset;
return tok;
} else {
[r unread:[s length] - 1];
@@ -95,17 +78,17 @@ - (BOOL)parseNameFromReader:(PKReader *)r {
BOOL hasAtLeastOneChar = NO;
for (;;) {
- if (PKEOF == _c || isspace(_c)) {
+ if (PKEOF == c || isspace(c)) {
result = NO;
break;
- } else if ('@' == _c && hasAtLeastOneChar) {
+ } else if ('@' == c && hasAtLeastOneChar) {
//[self append:c];
result = YES;
break;
} else {
hasAtLeastOneChar = YES;
- [self append:_c];
- self.c = [r read];
+ [self append:c];
+ c = [r read];
}
}
@@ -120,16 +103,16 @@ - (BOOL)parseHostFromReader:(PKReader *)r {
// ^[:space:]()<>/"'
for (;;) {
- if (PKEOF == _c || isspace(_c) || '(' == _c || ')' == _c || '<' == _c || '>' == _c || '/' == _c || '"' == _c || '\'' == _c) {
+ if (PKEOF == c || isspace(c) || '(' == c || ')' == c || '<' == c || '>' == c || '/' == c || '"' == c || '\'' == c) {
result = hasAtLeastOneChar && hasDot;
break;
} else {
- if ('.' == _c) {
+ if ('.' == c) {
hasDot = YES;
}
hasAtLeastOneChar = YES;
- [self append:_c];
- self.c = [r read];
+ [self append:c];
+ c = [r read];
}
}
View
55 src/PKSymbolNode.m
@@ -1,33 +1,25 @@
-// The MIT License (MIT)
-//
-// Copyright (c) 2014 Todd Ditchendorf
-//
-// 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.
+// Copyright 2010 Todd Ditchendorf
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
-#import "PKSymbolNode.h"
-#import "PKSymbolRootNode.h"
+#import <ParseKit/PKSymbolNode.h>
+#import <ParseKit/PKSymbolRootNode.h>
@interface PKSymbolNode ()
-@property (nonatomic, retain, readwrite) NSString *ancestry;
+@property (nonatomic, readwrite, retain) NSString *ancestry;
@property (nonatomic, assign) PKSymbolNode *parent; // this must be 'assign' to avoid retain loop leak
@property (nonatomic, retain) NSMutableDictionary *children;
-@property (nonatomic, assign) PKUniChar character;
+@property (nonatomic) PKUniChar character;
@property (nonatomic, retain) NSString *string;
- (void)determineAncestry;
@@ -44,7 +36,7 @@ - (id)initWithParent:(PKSymbolNode *)p character:(PKUniChar)c {
// this private property is an optimization.
// cache the NSString for the char to prevent it being constantly recreated in -determineAncestry
- self.string = [NSString stringWithFormat:@"%C", (unichar)_character];
+ self.string = [NSString stringWithFormat:@"%C", (unichar)character];
[self determineAncestry];
}
@@ -53,7 +45,7 @@ - (id)initWithParent:(PKSymbolNode *)p character:(PKUniChar)c {
- (void)dealloc {
- self.parent = nil;
+ parent = nil; // makes clang static analyzer happy
self.ancestry = nil;
self.string = nil;
self.children = nil;
@@ -62,8 +54,8 @@ - (void)dealloc {
- (void)determineAncestry {
- if (PKEOF == _parent.character) { // optimization for sinlge-char symbol (parent is symbol root node)
- self.ancestry = _string;
+ if (PKEOF == parent.character) { // optimization for sinlge-char symbol (parent is symbol root node)
+ self.ancestry = string;
} else {
NSMutableString *result = [NSMutableString string];
@@ -83,4 +75,9 @@ - (NSString *)description {
return [NSString stringWithFormat:@"<PKSymbolNode %@>", self.ancestry];
}
+@synthesize ancestry;
+@synthesize parent;
+@synthesize character;
+@synthesize string;
+@synthesize children;
@end
View
96 src/PKSymbolRootNode.m
@@ -1,27 +1,19 @@
-// The MIT License (MIT)
-//
-// Copyright (c) 2014 Todd Ditchendorf
-//
-// 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 <PEGKit/PKReader.h>
-#import "PKSymbolRootNode.h"
+// Copyright 2010 Todd Ditchendorf
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#import <ParseKit/PKSymbolRootNode.h>
+#import <ParseKit/PKReader.h>
@interface PKSymbolNode ()
@property (nonatomic, retain) NSMutableDictionary *children;
@@ -45,28 +37,27 @@ - (id)init {
- (void)add:(NSString *)s {
NSParameterAssert(s);
- if (![s length]) return;
+ if ([s length] < 2) return;
[self addWithFirst:[s characterAtIndex:0] rest:[s substringFromIndex:1] parent:self];
}
- (void)remove:(NSString *)s {
NSParameterAssert(s);
- if (![s length]) return;
+ if ([s length] < 2) return;
[self removeWithFirst:[s characterAtIndex:0] rest:[s substringFromIndex:1] parent:self];
}
- (void)addWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p {
NSParameterAssert(p);
- NSString *key = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
- PKSymbolNode *child = [p.children objectForKey:key];
+ NSNumber *key = [NSNumber numberWithInteger:c];
+ PKSymbolNode *child = [p->children objectForKey:key];
if (!child) {
child = [[[PKSymbolNode alloc] initWithParent:p character:c] autorelease];
- child.reportsAddedSymbolsOnly = self.reportsAddedSymbolsOnly;
- [p.children setObject:child forKey:key];
+ [p->children setObject:child forKey:key];
}
NSUInteger len = [s length];
@@ -85,8 +76,8 @@ - (void)addWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p {
- (void)removeWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p {
NSParameterAssert(p);
- NSString *key = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
- PKSymbolNode *child = [p.children objectForKey:key];
+ NSNumber *key = [NSNumber numberWithInteger:c];
+ PKSymbolNode *child = [p->children objectForKey:key];
if (child) {
NSString *rest = nil;
@@ -98,7 +89,7 @@ - (void)removeWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p
[self removeWithFirst:[s characterAtIndex:0] rest:rest parent:child];
}
- [p.children removeObjectForKey:key];
+ [p->children removeObjectForKey:key];
}
}
@@ -111,19 +102,41 @@ - (NSString *)nextSymbol:(PKReader *)r startingWith:(PKUniChar)cin {
- (NSString *)nextWithFirst:(PKUniChar)c rest:(PKReader *)r parent:(PKSymbolNode *)p {
NSParameterAssert(p);
- NSString *result = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
+ NSString *result = [NSString stringWithFormat:@"%C", (unichar)c];
+
+ // this also works.
+// NSString *result = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
+
+ // none of these work.
+ //NSString *result = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF8StringEncoding] autorelease];
+
+// NSLog(@"c: %d", c);
+// NSLog(@"string for c: %@", result);
+// NSString *chars = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
+// NSString *utfs = [[[NSString alloc] initWithUTF8String:(const char *)&c] autorelease];
+// NSString *utf8 = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF8StringEncoding] autorelease];
+// NSString *utf16 = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF16StringEncoding] autorelease];
+// NSString *ascii = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSASCIIStringEncoding] autorelease];
+// NSString *iso = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSISOLatin1StringEncoding] autorelease];
+//
+// NSLog(@"chars: '%@'", chars);
+// NSLog(@"utfs: '%@'", utfs);
+// NSLog(@"utf8: '%@'", utf8);
+// NSLog(@"utf16: '%@'", utf16);
+// NSLog(@"ascii: '%@'", ascii);
+// NSLog(@"iso: '%@'", iso);
- PKSymbolNode *child = [p.children objectForKey:result];
+ NSNumber *key = [NSNumber numberWithInteger:c];
+ PKSymbolNode *child = [p->children objectForKey:key];
if (!child) {
if (p == self) {
- result = self.reportsAddedSymbolsOnly ? @"" : result;
+ return result;
} else {
[r unread];
- result = @"";
+ return @"";
}
- return result;
- }
+ }
c = [r read];
if (PKEOF == c) {
@@ -133,4 +146,9 @@ - (NSString *)nextWithFirst:(PKUniChar)c rest:(PKReader *)r parent:(PKSymbolNode
return [result stringByAppendingString:[self nextWithFirst:c rest:r parent:child]];
}
+
+- (NSString *)description {
+ return @"<PKSymbolRootNode>";
+}
+
@end

0 comments on commit 26b48cf

Please sign in to comment.
Something went wrong with that request. Please try again.