简体   繁体   English

没有调用ParseKit汇编程序回调:我做错了什么?

[英]ParseKit assembler callbacks not called: What am I doing wrong?

Having got to grips a bit with the ParseKit grammar syntax (playing around in the demo app) I'm now trying to get my own mini demo working, but so far without much success. 我必须掌握ParseKit语法语法(在演示应用程序中玩)我现在正试图让我自己的迷你演示工作,但到目前为止没有太大的成功。 The assembler callbacks are not getting called. 汇编程序回调没有被调用。

Below is a condensed version of the relevant code. 以下是相关代码的精简版。 When testParse runs the parser seems to do it's thing OK and correctly match my string to my anything production (which also works in the demo) but didMatchAnything: is just not getting called. testParse运行时,解析器似乎可以做到这一点正确并正确匹配我的字符串到我的anything生产(这也适用于演示)但是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

Digging around in the ParseKit code I can see that line 129 of PKParser 在ParseKit代码中挖掘我可以看到PKParser的第129行

[assembler performSelector:assemblerSelector withObject:self withObject:a];

Isn't being executed, because assembler is nil. 没有被执行,因为assembler是零。 Which, in turn, leads me to the parser factory; 反过来,这导致我进入解析器工厂; where my understanding of what's going on begins to fail. 我对正在发生的事情的理解开始失败。

Disclaimer; 免责声明; I know, I probably need to read The Book , but one thing at a time. 我知道,我可能需要阅读The Book ,但一次只能做一件事。 I want to get a small proof of concept working, before forking out 30 mice for a book I might never read again if my project is a non-starter :) 我希望得到一个小概念证明工作,然后分配30本老鼠买一本书,如果我的项目是非首发,我可能永远不会再读了:)

Developer of ParseKit here. 这里是ParseKit的开发者。

A while back I changed the signature of the Assembler callbacks to accept two arguments: 前段时间我更改了Assembler回调的签名以接受两个参数:

  1. The Parser which matched the current token. 与当前令牌匹配的Parser
  2. The Assembly containing the current state of the input parsing. Assembly包含输入解析的当前状态。

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: 所以我怀疑如果你只是将Assembler回调方法更改为此方法,它将起作用:

- (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. 对于背景:我做了这个改变,因为我遇到了一个情况,我的Assembler回调真的需要检查刚刚进行当前匹配的Parser。

It also aligned more closely the strong Cocoa convention of Delegate callbacks which always have the delegator object as their first argument. 它还更紧密地对齐了委托回调的强大Cocoa约定,它总是将委托者对象作为它们的第一个参数。 In hindsight I kinda wish I had renamed the whole concept of Assemblers in ParseKit to Delegates . 事后来说,我有点希望我已经将ParseKit中汇编程序的整个概念重命名为代表 Since in Cocoa parlance, that's basically what Assemblers are. 因为在Cocoa的说法中,这基本上就像汇编者一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM