antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
9.94k stars 3.68k forks source link

CPP14 constructor and destructor declarations #1072

Open anasuti opened 6 years ago

anasuti commented 6 years ago

seems like the following c++ codeit is difficult to understand ctor and dtor declarations. This is the grammar I used: CPP14.g4 This is the constructor of the class DataComponent:

DataComponent::DataComponent(constchar*instanceId,PortableServer::POA_ptrpoa):poa(PortableServer::POA::_duplicate(poa)),initialized(0),started(0),ipDataInPort(NULL),ipCtrlOutPort(NULL),fsOutPort(NULL),notifOutPort(NULL) { this->resourceID=newchar[strlen(instanceId)+1]; strcpy(this->resourceID,instanceId); }

in the Listener enterFunctiondefinition we have the following:

attributespecifierseq= null declspecifierseq= DataComponent declarator= ::SDRLabWFDataComponent(constchar*instanceId,PortableServer::POA_ptrpoa) virtspecifierseq= null

how can the grammar understand if this is a ctor? same for destructor

Marti2203 commented 3 years ago

Hi, I think this should be solved in the listener as it is not always easy to differentiate constructors from other functions. Destructors have their special prefix ~ so they are easy to catch, while for constructors I suggest making your own checks.