antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Support for Cpp23

Open Gashmob opened this issue 11 months ago • 1 comments

Hi, I use antlr4 cpp target in a project currently with cpp17. There is some features of cpp23 such as https://en.cppreference.com/w/cpp/utility/expected that I want to use which are not available in 17. So I planned to update all my dependencies to meet the requirements and went the turn of antlr. I was already on version 4.13.2 so nothing to except finally upgrading set(CMAKE_CXX_STANDARD 23)

When regenerating then build the parser I got an error:

FilParser.cpp:1064:113: error: non-pointer operand type 'std::string' (aka 'basic_string<char>') incompatible with nullptr
 1064 |               type_name = (antlrcpp::downCast<Variable_declarationContext *>(_localctx)->typeContext != nullptr ? _input->getText(antlrcpp::downCast<Variable_declarationContext *>(_localctx)->typeContext->start, antlrcpp::downCast<Variable_declarationContext *>(_localctx)->typeContext->stop) : nullptr);
      |                                                                                                                 ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This code is in generated parser from this g4 file:

variable_declaration returns[std::shared_ptr<filc::VariableDeclaration> tree]
@init {
    bool is_constant = true;
    std::string type_name;
    std::shared_ptr<filc::Expression> value = nullptr;
}
@after {
    $tree = std::make_shared<filc::VariableDeclaration>(is_constant, $name.text, type_name, value);
}
    : (VAL | VAR {
        is_constant = false;
    }) name=IDENTIFIER (COLON type {
        type_name = $type.text;
    })? (EQ value=expression {
        value = $value.tree;
    })?;

type : IDENTIFIER (STAR | LBRACK INTEGER RBRACK)?;

I don't know if there is a simple way to fix this. If not, is it planned to fully support cpp23 for the generated code?

Gashmob avatar Feb 18 '25 21:02 Gashmob

Hi @Gashmob Yeah there is no easy fix. My plan is to scape the grammar from the spec which I have a copy of. I have a program that scapes the C++ spec pdfs and outputs a grammar. Not sure when I'll get the time to fix it though. Ken

kaby76 avatar Feb 18 '25 23:02 kaby76