Question

I am trying to parse a PGN file using ParseKit. I want to recognize the type of the token and take subsequent answer

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]);
}        

I want to add word type into an array only when symbol ( has been received.

Any documentation on using ParseKit will be a great help Thanks

Was it helpful?

Solution

Developer of ParseKit here.

Here's a simple way to capture tokens within parentheses:

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);

Prints:

(
    bar
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top