antlr4cs icon indicating copy to clipboard operation
antlr4cs copied to clipboard

CancellationToken support

Open KvanTTT opened this issue 6 years ago • 3 comments

Sometimes parsing works too much time especially for very big files with ambiguous grammars.

But we can stop parsing in .NET by several ways:

  1. Use threads with timeouts and stop it by Thread.About.
  2. Run a process with parsing task and kill the process if required.
  3. Use CancellationToken with ThrowIfCancellationRequested.

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.

KvanTTT avatar Apr 11 '18 12:04 KvanTTT

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 avatar Apr 12 '18 02:04 sharwell

@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 ?

Jerome2606 avatar Dec 17 '18 13:12 Jerome2606

@KvanTTT @sharwell Any chances this can be implemented and merged ?

asadcr avatar Apr 07 '20 08:04 asadcr