문제

Parsekit Grammar 구문 (데모 응용 프로그램에서 재생)으로 조금만 그립해야합니다. 이제는 내 자신의 미니 데모가 일하고 있기 위해 노력하고 있습니다. 어셈블러 콜백이 호출되지 않습니다.

아래의 관련 코드의 응축 된 버전입니다. testParse가 실행되면 Parser가 작업을 수행하고 내 문자열을 올바르게 일치하는 것처럼 보입니다 (데모에서 작동하지만 DidmatchanyThing).

#import <Foundation/Foundation.h>

@class PKParser;

@interface FileParserThing : NSObject {
    PKParser* _parser;
}
- (void)testParse;
@end


#import <ParseKit/ParseKit.h>
#import "FileParserThing.h"

@interface FileParserThing ()
@property (nonatomic, retain)PKParser* parser;
- (void)didMatchAnything:(PKAssembly *)a;
@end

@implementation FileParserThing

@synthesize parser = _parser;

-(id)init
{
    if (!(self = [super init])) return nil;

    NSString *g = @"@start = anything; anything = Any+;";
    self.parser = [[PKParserFactory factory] parserFromGrammar:g assembler:self];

    return self;
}

- (void)testParse
{
    NSString *s = @"Foo Bar";
    NSLog(@"test parse with: %@", s);
    [self.parser parse:s];
}

- (void)didMatchAnything:(PKAssembly *)a
{
    NSLog(@"Hooray!");
}

@end
.

Parsekit 코드에서 파고 나는 pkparser의 129 행을 볼 수 있습니다

[assembler performSelector:assemblerSelector withObject:self withObject:a];
. anything가 nil이기 때문에

가 실행되지 않습니다. 어느 것, 차례로 파서 공장으로 이어집니다. 무슨 일이 일어나는지에 대한 나의 이해가 실패하기 시작합니다.

면책 조항; 알아, 나는 아마도 을 읽을 필요가있을 것입니다. 그러나 한 번에 한 가지. 나는 책을 위해 30 마리의 마우스를 포기하기 전에 작은 개념을 얻고 싶다.

도움이 되었습니까?

해결책

Developer of ParseKit here.

A while back I changed the signature of the Assembler callbacks to accept two arguments:

  1. The Parser which matched the current token.
  2. The Assembly containing the current state of the input parsing.

Previously, there had only been one argument: The Assembly.

I'm not sure the docs are fully updated to reflect this.

So I suspect that if you simply change your Assembler callback method to this, it will work:

- (void)parser:(PKParser *)p didMatchAnything:(PKAssembly *)a {
    NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
}

If not, let me know, and I'll help to further debug.


For background: I made this change because I ran into a situation where my Assembler callback really needed to inspect the Parser which had just made the current match.

It also aligned more closely the strong Cocoa convention of Delegate callbacks which always have the delegator object as their first argument. In hindsight I kinda wish I had renamed the whole concept of Assemblers in ParseKit to Delegates. Since in Cocoa parlance, that's basically what Assemblers are.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top