antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.17k stars 3.28k forks source link

Async/await in Antlr C# #2442

Open Jerome2606 opened 5 years ago

Jerome2606 commented 5 years ago

Hello,

I was looking how I can use my existing grammar to support async/await.

I was reading this post: https://stackoverflow.com/questions/51640627/how-an-antlr-visitor-or-listener-can-be-written-with-async-await-on-its-methods?rq=1

But actually the way, I'm using Antlr is by calling this method:

public virtual Result Visit(IParseTree tree)
    {
      return tree.Accept<Result>((IParseTreeVisitor<Result>) this);
    }

I don't understand how I can override this method to make my code fully async/await.

Source of my question is issue with long process in visitor and api unstability.

Thanks

ericvergnaud commented 5 years ago

Hi Support is on the google discussion group

Envoyé de mon iPhone

Le 14 déc. 2018 à 19:06, Jérôme Lambert notifications@github.com a écrit :

Hello,

I was looking how I can use my existing grammar to support async/await.

I was reading this post: https://stackoverflow.com/questions/51640627/how-an-antlr-visitor-or-listener-can-be-written-with-async-await-on-its-methods?rq=1

But actually the way, I'm using Antlr is by calling this method:

public virtual Result Visit(IParseTree tree) { return tree.Accept((IParseTreeVisitor) this); } I don't understand how I can override this method to make my code fully async/await.

Source of my question is issue with long process in visitor and api unstability.

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Jerome2606 commented 5 years ago

Oh ok I will switch to the discussion group.

Jerome2606 commented 5 years ago

Seems there is no support from google groups discussion.

Any idea from here ? Async support ?

ericvergnaud commented 5 years ago

The so article you are referring to seems pretty straightforward. Not sure what you expect from the runtime that can't be achieved in your own code.

Jerome2606 commented 5 years ago

For C# there is not article from my search. I'm looking for a way to generate the C# class code from .g4 with async/await ready.

Someking of "now supporting async/await in C# generated code"

In my own code, I'm using the Visit / Parse and VisitChildren but nothing is async/await.

ericvergnaud commented 5 years ago

I don’t support the idea of generating this, because it would convert each node visit into an asynchronous call... It seems trivial to override the generated visitor and implement Async await where you need it.

Envoyé de mon iPhone

Le 28 janv. 2019 à 18:53, Jérôme Lambert notifications@github.com a écrit :

For C# there is not article from my search. I'm looking for a way to generate the C# class code from .g4 with async/await ready.

Someking of "now supporting async/await in C# generated code"

In my own code, I'm using the Visit / Parse and VisitChildren but nothing is async/await.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

sharwell commented 5 years ago

@Jerome2606 The listeners do not support async/await because the methods return void. However, the visitors do support async/await:

  1. Use Task<TResult> or ValueTask<TResult> as the return value
  2. Override DefaultResult to return Task.FromResult(default(TResult)) (preferably cached in a static readonly field)
Jerome2606 commented 5 years ago

@sharwell mmh seems very interesting, I will give a try (lot of code to changes but I will do it).

Thanks