I have actions with custom objects. I'd like to not have to copy and paste all the #imports each time I generate the parser. Is this possible with some @begin
or some other directive.
for example:
mycustomRule: word {
PUSH([[MyCoolNewObject alloc] initWith:POP_STR()]);
};
It all generates perfectly but then when trying to compile obviously the generated file is missing the #import "MyCoolNewObject.h"
Creator of PEGKit here.
PEGKit (but not ParseKit) has a feature inspired by ANTLR called "Grammar Actions". Grammar Actions are a way to do exactly what you are looking for: inserting arbitrary code in various places in your Parser's .h and .m files. They must be placed at the top of your grammar before any rules are listed.
Use PEGKit tag v0.3.6 or later (or HEAD of master).
Here are all of the Grammar Actions currently available, along with a description of where their bodies are inserted in the source code of your generated parser:
In the .h file:@h
- top of .h file@interface
- inside the @interface
portion of header@m
- top of .m file@extension
- inside a private @interface MyParser ()
class extension in the .m file@ivars
- private ivars inside the @implementation MyParser {}
in the .m file@implementation
- inside your parser's @implementation
. A place for defining methods.@init
- inside your parser's init
method@dealloc
- inside your parser's dealloc
method if ARC is not enabled@before
- setup code goes here. executed before parsing begins.@after
- tear down code goes here. executed after parsing ends.(notice that the @before
and @after
Grammar Actions listed here are distinct from the @before
and @after
which may also be placed in each individual rule.)
For your current needs, the @m
Grammar Action is the correct choice. So add this to the top of your grammar:
@m {
#import "MyCoolNewObject.h"
}