Domanda

Avendo dovuto afferrare un po 'con la sintassi della grammatica di Parsekit (giocando in giro nell'app Demo) Ora sto cercando di far funzionare la mia mini demo, ma finora senza molto successo. Le callback degli assemblatore non vengono chiamate.

Di seguito è riportato una versione condensata del codice pertinente . Quando testParse gestisce il parser sembra fare è una cosa ok e abbina correttamente la mia stringa alla mia produzione anything (che funziona anche nella demo) ma è stata chiamata.

#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
.

Scavatura nel codice di Parsekit Posso vedere quella riga 129 di PKParser

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

non viene eseguito, perché assembler è NIL. Che, a sua volta, mi conduce alla fabbrica di parser; dove la mia comprensione di ciò che sta succedendo inizia a fallire.

Disclaimer; Lo so, probabilmente ho bisogno di leggere il libro , ma una cosa alla volta. Voglio ottenere una piccola prova del concetto di lavoro, prima di biforcare 30 topi per un libro che potrei mai leggere di nuovo se il mio progetto è un non antipasto:)

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top