tunnelvisionlabs / antlr4cs

The original, highly-optimized C# Target for ANTLR 4
Other
448 stars 103 forks source link

CancellationToken support #277

Open KvanTTT opened 6 years ago

KvanTTT commented 6 years ago

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.

sharwell commented 6 years ago

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.

Jerome2606 commented 5 years ago

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

asadcr commented 4 years ago

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