I have a parser created from the PEGKit (example project here).
I want to pause the parsing, without halting the main thread. Since PEGKit
has infinite backtracking and a knows where the cursor/head is at the input string, it should be possible to resume the parsing.
It would be very helpful, so that I can create a step by step parser. So the parser must wait for a ui action, like a press of a UIButton
.
How to I implement or pause and then resume the parsing?
As an example I would want to pause the parsing when a certain symbol is reached. Here is would be after a ;
(semicolon or EXPRESSIONPARSER_TOKEN_KIND_SEMI_COLON
).
So after the token ;
is should save the state, so I can return and parse from this position.
- (void)start {
[self main_];
[self matchEOF:YES];
}
- (void)__main {
while ([self speculate:^{ [self expression_]; }]) {
[self expression_];
}
[self fireDelegateSelector:@selector(parser:didMatchMain:)];
}
- (void)__expression {
if ([self speculate:...) {
if ([self predicts:...) {
[self _subExpression];
} else {
[self raise:@"No viable alternative found in rule 'expression'."];
}
}
[self match:EXPRESSIONPARSER_TOKEN_KIND_SEMI_COLON discard:NO];
[self fireDelegateSelector:@selector(parser:didMatchExpression:)];
}
Developer of PEGKit here.
I think it will be obvious when you think on it, that PEGKit + threads is what you are looking for. And that no extra features need be added to PEGKit for this.
You're looking for the ability to pause and resume execution of parsing via PEGKit. You already have access to this functionality with threads.
Simply perform parsing on a background thread, and pause that thread by calling a method which blocks until the user has indicated she would like to continue. This is essentially the same type of environment/experience that a terminal provides: execute and pause while awaiting user input. (I have implemented this very thing myself with some thread utils I posted here.)
So I think adding all the features that threads provide directly to PEGKit would be the wrong approach.
If you want to track the state of a PEGKit parse, simply manage a stack of method names in your parser delegate callbacks.