我正在尝试使用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的任何文档都将是一个很好的帮助谢谢
答案 0 :(得分:1)
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
)