我正在尝试使用ParseKit解析PGN文件。我想识别令牌的类型并采取后续答案
PKTokenizer *t = [PKTokenizer tokenizerWithString:moveString];
[t.symbolState add:@"..."];
[t setTokenizerState:t.commentState from:'{' to:'{'];
[t.commentState addMultiLineStartMarker:@"{" endMarker:@"}"];
[t setTokenizerState:t.wordState from:'$' to:'$'];
t.commentState.reportsCommentTokens = YES;
PKToken *eof = [PKToken EOFToken];
PKToken *tok = nil;
while ((tok = [t nextToken]) != eof) {
NSLog(@" %@", [tok debugDescription]);
}
我只想在收到符号
(
时才将单词类型添加到数组中。有关使用ParseKit的任何文档都将对您有所帮助
最佳答案
ParseKit的开发者在这里。
这是捕获括号内标记的一种简单方法:
BOOL inParens = NO;
PKToken *openParen = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"(" floatValue:0.0];
PKToken *closeParen = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@")" floatValue:0.0];
NSMutableArray *cache = [NSMutableArray array];
PKTokenizer *t = [PKTokenizer tokenizerWithString:@"foo (bar) baz"];
PKToken *eof = [PKToken EOFToken];
PKToken *tok = nil;
while ((tok = [t nextToken]) != eof) {
if ([openParen isEqual:tok]) {
inParens = YES;
} else if (inParens) {
if ([closeParen isEqual:tok]) {
inParens = NO;
} else {
[cache addObject:tok];
}
}
}
NSLog(@"%@", cache);
印刷品:
(
bar
)
关于objective-c - 如何分隔ParseKit的各种类型的符号PKTokenizer类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11225866/