Parsekit Assemblerコールバックが呼び出されません:私は何が悪いのですか?

StackOverflow https://stackoverflow.com//questions/9646054

質問

Parsekit文法の構文(デモAppで演奏する)で少しグリップしたくなっています。アセンブラコールバックは呼び出されていません。 下記

関連コードの縮合版です。 testParseが実行されると、パーサーが実行されているようです。

#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