antlr4cs
antlr4cs copied to clipboard
CancellationToken support
Sometimes parsing works too much time especially for very big files with ambiguous grammars.
But we can stop parsing in .NET by several ways:
- Use threads with timeouts and stop it by
Thread.About
. - Run a process with parsing task and kill the process if required.
- Use
CancellationToken
withThrowIfCancellationRequested
.
The first way is not safe because of state corruption, see also What's wrong with using Thread.Abort() . Moreover, it's not supported in .NET Core: Can't find Abort method in Thread .
The second way is too slow if there are many files. Moreover, it's harder to implement.
The third way is the best but requires ThrowIfCancellationRequested
calls from parsing code.
By the way, Roslyn supports cancellation tokens and async methods.
This could probably be done efficiently by adding a check to the code that calculates the closure from an ATNConfigSet. This code is typically called frequently in long running operations but avoided on performance critical paths.
@sharwell Sorry but I don't see where to add the check.
Btw is it in plans to support visitAsync ? How can I in my .g4 customize the Accept to change it in Async method ?
@KvanTTT @sharwell Any chances this can be implemented and merged ?