Developer of ParseKit here. I'll answer both of your questions:
1) You are taking the correct approach, but this is a tricky case. There are several small gotchas, and your Grammar needs to be changed a bit.
I've developed a grammar which is working for me:
// Tokenizer Directives
@symbolState = '"' "'"; // effectively tells the tokenizer to turn off QuoteState.
// Otherwise, variables enclosed in quotes would not be found (they'd be embedded in quoted strings).
// now single- & double-quotes will be recognized as individual symbols, not start- & end-markers for quoted strings
@symbols = '${'; // declare '${' as a multi-char symbol
@reportsWhitespaceTokens = YES; // tell the tokenizer to preserve/report whitespace
// Grammar
@start = content*;
content = passthru | variable;
passthru = /[^$].*/;
variable = start name end;
start = '${';
end = '}';
name = Word;
Then implement these two callbacks in your Assembler:
- (void)parser:(PKParser *)p didMatchName:(PKAssembly *)a {
NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
PKToken *tok = [a pop];
NSString *name = tok.stringValue;
// do something with name
}
- (void)parser:(PKParser *)p didMatchPassthru:(PKAssembly *)a {
NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
PKToken *tok = [a pop];
NSMutableString *s = a.target;
if (!s) {
s = [NSMutableString string];
}
[s appendString:tok.stringValue];
a.target = s;
}
And then your client/driver code will look something like this:
NSString *g = // fetch grammar
PKParser *p = [[PKParserFactory factory] parserFromGrammar:g assembler:self];
NSString *s = @"<img src=\"${image}\" />";
[p parse:s];
NSString *result = [p parse:s];
NSLog(@"result %@", result);
This will be printed:
result: <img src="" />
2) Yes, I think it would definitely be much better to use the Tokenizer directly for this relatively simple case. Performance will be massively better. Here's how you might approach the task with the Tokenizer:
PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
[t setTokenizerState:t.symbolState from:'"' to:'"'];
[t setTokenizerState:t.symbolState from:'\'' to:'\''];
[t.symbolState add:@"${"];
t.whitespaceState.reportsWhitespaceTokens = YES;
NSMutableString *result = [NSMutableString string];
PKToken *eof = [PKToken EOFToken];
PKToken *tok = nil;
while (eof != (tok = [t nextToken])) {
if ([@"${" isEqualToString:tok.stringValue]) {
tok = [t nextToken];
NSString *varName = tok.stringValue;
// do something with variable
} else if ([@"}" isEqualToString:tok.stringValue]) {
// do nothing
} else {
[result appendString:tok.stringValue];
}
}