Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indirect left recursive rule operator precedence via delegated operator precedence #4480

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[type]
Parser

[grammar]
grammar T;
s @after {<ToStringTree("$ctx"):writeln()>} : e EOF ; // must indicate EOF can follow
e : e '.' ID
| b[0]
| m[0]
| e '*' e
| e ('+'|'-') e
| INT
| ID
;
b [int x]: '(' e ')';
m [int y] : '-' e;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip ;

[start]
s

[input]
-(-a+b)+c

[output]
"""(s (e (e (m - (e (b ( (e (e (m - (e a))) + (e b)) ))))) + (e c)) <EOF>)
"""

[skip]
##Cpp
##CSharp
##Dart
##Go
##Java
##JavaScript
##PHP
##Python3
##Swift
TypeScript
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[type]
Parser

[grammar]
grammar T;
s @after {<ToStringTree("$ctx"):writeln()>} : e EOF ; // must indicate EOF can follow
e : e '.' ID
| b[0]
| m[0]
| e '*' e
| e ('+'|'-') e
| INT
| ID
;
b [number x]: '(' e ')';
m [number y] : '-' e;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip ;

[start]
s

[input]
-(-a+b)+c

[output]
"""(s (e (e (m - (e (b ( (e (e (m - (e a))) + (e b)) ))))) + (e c)) <EOF>)
"""

[skip]
Cpp
CSharp
Dart
Go
Java
JavaScript
PHP
Python3
Swift
##TypeScript
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[type]
Parser

[grammar]
grammar T;
s @after {<ToStringTree("$ctx"):writeln()>} : e EOF ; // must indicate EOF can follow
e : e '.' ID
| b
| m
| e '*' e
| e ('+'|'-') e
| INT
| ID
;
b : '(' e ')';
m : '-' e;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip ;

[start]
s

[input]
-(-a+b)+c

[output]
"""(s (e (e (m - (e (b ( (e (e (m - (e a))) + (e b)) ))))) + (e c)) <EOF>)
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[type]
Parser

[grammar]
grammar T;
s @after {<ToStringTree("$ctx"):writeln()>} : e EOF ; // must indicate EOF can follow
e : e '.' ID
| b1
| m1
| e '*' e
| e ('+'|'-') e
| INT
| ID
;
b1 : b;
b : '(' e ')';
m1 : m;
m : '-' e;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip ;

[start]
s

[input]
-(-a+b)+c

[output]
"""(s (e (e (m1 (m - (e (b1 (b ( (e (e (m1 (m - (e a)))) + (e b)) ))))))) + (e c)) <EOF>)
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[type]
Parser

[grammar]
grammar T;
s @after {<ToStringTree("$ctx"):writeln()>} : e EOF ; // must indicate EOF can follow
e : namee | bracke | minusprefixe | e '+' e ;
namee : ID;
bracke : '(' e ')';
minusprefixe: ('-' tmp0=e) | 'unrelatedThing';
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip ;

[start]
s

[input]
-a+b

[output]
"""(s (e (e (minusprefixe - (e (namee a)))) + (e (namee b))) <EOF>)
"""

Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public void testLabelsForTokensWithMixedTypesLRWithoutLabels() {
"a: 'a';\n" +
"b: 'b';",

"error(" + ErrorType.INTERNAL_ERROR.code + "): Test.g4:2:30: internal error: Rule error undefined \n"
"error(" + ErrorType.INTERNAL_ERROR.code + "): Test.g4:2:35: internal error: Rule error undefined \n"
};

testErrors(test, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">

[RuleVersion(<namedActions.version; null="0">)]
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public <endif><currentRule.ctxType> <currentRule.escapedName>(<args; separator=", ">) {
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public <endif><currentRule.ctxType> <currentRule.escapedName>(<[args, currentRule.hasDelegatedPrecedence:{_|int _dp=0}]; separator=", ">) {
<currentRule.ctxType> _localctx = new <currentRule.ctxType>(Context, State<currentRule.args:{a | , <a.escapedName>}>);
EnterRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>);
<namedActions.init>
Expand Down Expand Up @@ -708,7 +708,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::= <<
State = <r.stateNumber>;
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchToken(m) ::= <<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,14 @@ for (size_t i = 0; i \< count; i++) { <! Rework class ATN to allow standard iter
RuleFunctionHeader(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedActions, finallyAction, postamble, exceptions) ::= <<
<ruleCtx>
<! TODO: untested !><if (altLabelCtxs)><altLabelCtxs: {l | <altLabelCtxs.(l)>}; separator="\n"><endif>
<currentRule.ctxType>* <currentRule.escapedName>(<args; separator=",">);
<currentRule.ctxType>* <currentRule.escapedName>(<[args, currentRule.hasDelegatedPrecedence:{_|int _dp=0}]; separator=",">);

>>

RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedActions, finallyAction, postamble, exceptions) ::= <<
<ruleCtx>
<! TODO: untested !><altLabelCtxs: {l | <altLabelCtxs.(l)>}; separator = "\n">
<parser.name>::<currentRule.ctxType>* <parser.name>::<currentRule.escapedName>(<args; separator=",">) {
<parser.name>::<currentRule.ctxType>* <parser.name>::<currentRule.escapedName>(<[args, currentRule.hasDelegatedPrecedence:{_|int _dp}]; separator=",">) {
<currentRule.ctxType> *_localctx = _tracker.createInstance\<<currentRule.ctxType>\>(_ctx, getState()<currentRule.args:{a | , <a.escapedName>}>);
enterRule(_localctx, <currentRule.startState>, <parser.name>::Rule<currentRule.name; format = "cap">);
<namedActions.init>
Expand Down Expand Up @@ -852,7 +852,7 @@ cases(tokens) ::= <<
InvokeRuleHeader(r, argExprsChunks) ::= "InvokeRuleHeader"
InvokeRule(r, argExprsChunks) ::= <<
setState(<r.stateNumber>);
<if(r.labels)><r.labels: {l | <labelref(l)> = }><endif><r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels: {l | <labelref(l)> = }><endif><r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchTokenHeader(m) ::= "<! Required but unused. !>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ bool _<r.name>_sempred(dynamic _localctx, int predIndex) {

RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<

<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else><endif><currentRule.ctxType> <currentRule.escapedName>(<args; separator=", ">) {
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else><endif><currentRule.ctxType> <currentRule.escapedName>(<[args, currentRule.hasDelegatedPrecedence:{_|[int dp=0]}]; separator=", ">) {
dynamic _localctx = <currentRule.ctxType>(context, state<currentRule.args:{a | , <a.escapedName>}>);
enterRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>);
<namedActions.init>
Expand Down Expand Up @@ -541,7 +541,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::=<<
state = <r.stateNumber>;
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<[{<if(r.ast.options.pv)>dp<else><r.ast.options.p><endif>}, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchToken(m) ::= <<
Expand Down
11 changes: 11 additions & 0 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,14 @@ RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedAction


<endif>
<if(currentRule.hasDelegatedPrecedence)>
func (p *<parser.name>) <currentRule.escapedName; format="cap">(<currentRule.args:{a | <a.escapedName> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
return p.<currentRule.escapedName>(0<currentRule.args:{a | , <a.escapedName>}>)
}
func (p *<parser.name>) <currentRule.escapedName>(_dp int<currentRule.args:{a | , <a.escapedName> <a.type>}>) (localctx I<currentRule.ctxType>) {
<else>
func (p *<parser.name>) <currentRule.escapedName; format="cap">(<currentRule.args:{a | <a.escapedName> <a.type>}; separator=", ">) (localctx I<currentRule.ctxType>) {
<endif>
localctx = New<currentRule.ctxType>(p, p.GetParserRuleContext(), p.GetState()<currentRule.args:{a | , <a.escapedName>}>)
p.EnterRule(localctx, <currentRule.startState>, <parser.name>RULE_<currentRule.name>)
<if(namedActions.init)>
Expand Down Expand Up @@ -796,6 +803,8 @@ InvokeRule(r, argExprsChunks) ::= <<

<if(r.ast.options.p)>
var _x = p.<r.escapedName>(<r.ast.options.p><if(argExprsChunks)>, <endif><argExprsChunks>)
<elseif(r.ast.options.dp)>
var _x = p.<r.escapedName>(<r.ast.options.dp><if(argExprsChunks)>, <endif><argExprsChunks>)
<else>
var _x = p.<r.escapedName; format="cap">(<argExprsChunks>)
<endif>
Expand All @@ -805,6 +814,8 @@ InvokeRule(r, argExprsChunks) ::= <<
<else>
<if(r.ast.options.p)>
p.<r.escapedName>(<r.ast.options.p><if(argExprsChunks)>, <endif><argExprsChunks>)
<elseif(r.ast.options.dp)>
p.<r.escapedName>(<r.ast.options.dp><if(argExprsChunks)>, <endif><argExprsChunks>)
<else>
p.<r.escapedName; format="cap">(<argExprsChunks>)
<endif>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,12 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina

<ruleCtx>
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">

<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.escapedName>(<args; separator=",">) throws RecognitionException {
<if(currentRule.hasDelegatedPrecedence)>
return <currentRule.escapedName>(<currentRule.args:{a | <a.escapedName>, }> 0);
}
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.escapedName>(<args:{a | <a>, }> int _dp ) throws RecognitionException {
<endif>
<currentRule.ctxType> _localctx = new <currentRule.ctxType>(_ctx, getState()<currentRule.args:{a | , <a.escapedName>}>);
enterRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>);
<namedActions.init>
Expand Down Expand Up @@ -636,7 +640,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::= <<
setState(<r.stateNumber>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchToken(m) ::= <<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ RuleSempredFunction(r, actions) ::= <<
RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<


<currentRule.escapedName>(<currentRule.args:{a | <a.escapedName>}; separator=", ">) {
<currentRule.escapedName>(<[currentRule.args:{a | <a.escapedName>}, currentRule.hasDelegatedPrecedence:{_|_dp=0}]; separator=", ">) {
let localctx = new <currentRule.ctxType>(this, this._ctx, this.state<currentRule.args:{a | , <a.escapedName>}>);
this.enterRule(localctx, <currentRule.startState>, <parser.name>.RULE_<currentRule.name>);
<namedActions.init>
Expand Down Expand Up @@ -480,7 +480,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::= <<
this.state = <r.stateNumber>;
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>this.<r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>this.<r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchToken(m) ::= <<
Expand Down
14 changes: 12 additions & 2 deletions tool/resources/org/antlr/v4/tool/templates/codegen/PHP/PHP.stg
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,20 @@ private function sempred<r.name; format="cap">(?Context\\<r.ctxType> $localConte
>>

RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,exceptions,postamble) ::= <<
<if(currentRule.hasDelegatedPrecedence)>
/**
* InvokeRule references the recursive function in case a p option is present - which we re-use for delegated precedence
* @throws RecognitionException
*/
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><endif>public function <currentRule.name>(<args; separator=",">): Context\\<currentRule.ctxType>
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><endif>public function recursive<currentRule.name; format="cap">(<[args, currentRule.hasDelegatedPrecedence:{_|$_dp=0}]; separator=",">): Context\\<currentRule.ctxType>
{
return $this-><currentRule.name>(<[args:{a|<a.name>}, {$_dp}]; separator=",">);
}
<endif>
/**
* @throws RecognitionException
*/
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><endif>public function <currentRule.name>(<[args, currentRule.hasDelegatedPrecedence:{_|$_dp=0}]; separator=",">): Context\\<currentRule.ctxType>
{
$localContext = new Context\\<currentRule.ctxType>($this->ctx, $this->getState()<currentRule.args:{a | , $<a.name>}>);

Expand Down Expand Up @@ -701,7 +711,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::= <<
$this->setState(<r.stateNumber>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>$this-><if(r.ast.options.p)>recursive<r.name; format="cap"><else><r.name><endif>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>$this-><if(r.ast.options.p)>recursive<r.name; format="cap"><else><r.name><endif>(<[{<if(r.ast.options.pv)>$<endif><r.ast.options.p>}, argExprsChunks, r.ast.options.dp]; separator=",">);
>>

MatchToken(m) ::= <<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina

<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">

def <currentRule.escapedName>(self<currentRule.args:{a | , <a.escapedName><if(a.type)>:<a.type><endif>}>):
def <currentRule.escapedName>(self<currentRule.args:{a | , <a.escapedName><if(a.type)>:<a.type><endif>}><if(currentRule.hasDelegatedPrecedence)>, _dp:int=0<endif>):

localctx = <parser.name>.<currentRule.ctxType>(self, self._ctx, self.state<currentRule.args:{a | , <a.escapedName>}>)
self.enterRule(localctx, <currentRule.startState>, self.RULE_<currentRule.name>)
Expand Down Expand Up @@ -491,7 +491,7 @@ if token in [<tokens:{t | <t.type>}; separator=", ">]:

InvokeRule(r, argExprsChunks) ::= <<
self.state = <r.stateNumber>
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>self.<r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>)
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>self.<r.escapedName>( <[r.ast.options.p, argExprsChunks:{a| <a;separator=",">}, r.ast.options.dp:{v | _dp=<v>}]; separator=",">)
>>

MatchToken(m) ::= <<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina
<ruleCtx>
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
@discardableResult
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else> <accessLevelOpenOK(parser)> func <endif><currentRule.escapedName>(<if(first(args))>_ <endif><args; separator=", _">) throws -> <currentRule.ctxType> {
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else> <accessLevelOpenOK(parser)> func <endif><currentRule.escapedName>(<[args:{a | _ <a>}, currentRule.hasDelegatedPrecedence:{_ | _ _dp : Int = 0 }]; separator=", ">) throws -> <currentRule.ctxType> {
var _localctx: <currentRule.ctxType>
_localctx = <currentRule.ctxType>(_ctx, getState()<currentRule.args:{a | , <a.escapedName>}>)
try enterRule(_localctx, <currentRule.startState>, <parser.name>.RULE_<currentRule.name>)
Expand Down Expand Up @@ -645,10 +645,10 @@ InvokeRule(r, argExprsChunks) ::= <<
setState(<r.stateNumber>)
<if(r.labels)>
try {
let assignmentValue = try <r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>)
let assignmentValue = try <r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">)
<r.labels:{l | <labelref(l)> = assignmentValue} ; separator="\n">
}()
<else>try <r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>)<endif>
<else>try <r.escapedName>(<[r.ast.options.p, argExprsChunks, r.ast.options.dp]; separator=",">)<endif>
>>

MatchToken(m) ::= <<
Expand Down
Loading
Loading