Skip to content

Commit f0ed454

Browse files
committed
fix: Cpp $rule.text fail if $rule is nullptr
With a grammar like this: ```antlr rule: (VAL | VAR { is_constant = false; }) name=IDENTIFIER (COLON type { type_name = $type.text; <-- we look this line })? (EQ value=expression { value = $value.tree; })? ``` When generating for Cpp target we get this specific result: ```cpp 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); ``` If input doesn't match the `type` branch then we fallback on nullptr. As of cpp23 standard this line doesn't compile as it waits for `std::string`. AS `Token::getText` returns `std::string`, then fallback value should be empty string Signed-off-by: Kevin Traini <kevin@ktraini.com>
1 parent cb85012 commit f0ed454

File tree

1 file changed

+1
-1
lines changed
  • tool/resources/org/antlr/v4/tool/templates/codegen/Cpp

1 file changed

+1
-1
lines changed

tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ RulePropertyRef_stopHeader(r) ::= "<! Required but unused. !>"
979979
RulePropertyRef_stop(r) ::= "(<ctx(r)>-><r.label> != nullptr ? (<ctx(r)>-><r.label>->stop) : nullptr)"
980980

981981
RulePropertyRef_textHeader(r) ::= "<! Required but unused. !>"
982-
RulePropertyRef_text(r) ::= "(<ctx(r)>-><r.label> != nullptr ? _input->getText(<ctx(r)>-><r.label>->start, <ctx(r)>-><r.label>->stop) : nullptr)"
982+
RulePropertyRef_text(r) ::= "(<ctx(r)>-><r.label> != nullptr ? _input->getText(<ctx(r)>-><r.label>->start, <ctx(r)>-><r.label>->stop) : \"\")"
983983

984984
RulePropertyRef_ctxHeader(r) ::= "<! Required but unused. !>"
985985
RulePropertyRef_ctx(r) ::= "<ctx(r)>-><r.label>"

0 commit comments

Comments
 (0)