Skip to content

Commit

Permalink
Merge pull request #3841 from parrt/fix-2016-round-2
Browse files Browse the repository at this point in the history
Fixes #2016
  • Loading branch information
parrt committed Aug 29, 2022
2 parents e1ff5bd + 6a3b44b commit 51150ab
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 18 deletions.
Expand Up @@ -33,3 +33,5 @@ expression
[input]
a

[skip]
Go
@@ -0,0 +1,34 @@
[notes]
Checks that this compiles; see https://github.com/antlr/antlr4/issues/2016

[type]
Parser

[grammar]
grammar Test;

expression
@after {
<AssertIsList("$args")>
}
: op=NOT args+=expression
| args+=expression (op=AND args+=expression)+
| args+=expression (op=OR args+=expression)+
| IDENTIFIER
;

AND : 'and' ;
OR : 'or' ;
NOT : 'not' ;
IDENTIFIER : [a-zA-Z_][a-zA-Z0-9_]* ;
WS : [ \t\r\n]+ -> skip ;

[start]
expression

[input]
a and b

[skip]
Go
Cpp
Expand Up @@ -18,7 +18,7 @@ AppendStr(a,b) ::= <%<Append(a,b)>%>

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= "System.Collections.IList __ttt__ = <v>;" // just use static type system
AssertIsList(v) ::= "System.Collections.IList __ttt__ = (System.Collections.IList)<v>;" // just use static type system

AssignLocal(s,v) ::= "<s> = <v>;"

Expand Down
Expand Up @@ -18,7 +18,7 @@ AppendStr(a,b) ::= "<a> + <b>"

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= ""
AssertIsList(v) ::= "TODO!!"

AssignLocal(s, v) ::= "<s> = <v>;"

Expand Down
Expand Up @@ -18,7 +18,7 @@ AppendStr(a,b) ::= <%<Append(a,b)>%>

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= <<if ( !(v instanceof Array) ) {throw "value is not an array";}>>
AssertIsList(v) ::= <<if ( !(<v> instanceof Array) ) {throw "value is not an array";}>>

AssignLocal(s,v) ::= "<s> = <v>;"

Expand Down
Expand Up @@ -18,7 +18,7 @@ AppendStr(a,b) ::= "<a> + <b>"

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= "assert isinstance(v, (list, tuple))"
AssertIsList(v) ::= "assert isinstance(<v>, (list, tuple))"

AssignLocal(s,v) ::= "<s> = <v>"

Expand Down
Expand Up @@ -18,7 +18,7 @@ AppendStr(a,b) ::= "<a> + <b>"

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= "assert isinstance(v, (list, tuple))"
AssertIsList(v) ::= "assert isinstance(<v>, (list, tuple))"

AssignLocal(s,v) ::= "<s> = <v>"

Expand Down
Expand Up @@ -19,8 +19,9 @@ public class Processor {
public final Map<String, String> environmentVariables;
public final boolean throwOnNonZeroErrorCode;

public static ProcessorResult run(String[] arguments, String workingDirectory, Map<String, String> environmentVariables
) throws InterruptedException, IOException {
public static ProcessorResult run(String[] arguments, String workingDirectory, Map<String, String> environmentVariables)
throws InterruptedException, IOException
{
return new Processor(arguments, workingDirectory, environmentVariables, true).start();
}

Expand Down
Expand Up @@ -21,6 +21,25 @@
import static org.antlr.v4.test.runtime.RuntimeTestUtils.getOS;
import static org.antlr.v4.test.runtime.RuntimeTestUtils.isWindows;

/**
* For my own information on I'm recording what I needed to do to get a unit test to compile and run in C++ on the Mac.
* I got a segmentation violation and couldn't figure out how to get information about it, so I turned on debugging
* and then figured out lldb enough to create this issue: https://github.com/antlr/antlr4/issues/3845 on a bug.
*
* cd ~/antlr/code/antlr4/runtime/Cpp
* cmake . -D CMAKE_OSX_ARCHITECTURES="arm64; x86_64" -DCMAKE_BUILD_TYPE=Debug
* make -j 8
*
* In test dir with generated test code:
*
* clang++ -g -std=c++17 -I /Users/parrt/antlr/code/antlr4/runtime/Cpp/runtime/src -L. -lantlr4-runtime *.cpp
* ./a.out input
*
* $ lldb ./a.out input
* (lldb) run
* ... crash ...
* (lldb) thread backtrace
*/
public class CppRunner extends RuntimeRunner {
@Override
public String getLanguage() {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Dart/lib/src/prediction_context.dart
Expand Up @@ -427,7 +427,7 @@ abstract class PredictionContext {
return a_;
}

mergedParents = List.generate(k, (n) => mergedParents[n]!);
mergedParents = List.generate(k, (n) => mergedParents[n]);
mergedReturnStates = List.generate(k, (n) => mergedReturnStates[n]);
}

Expand Down
10 changes: 8 additions & 2 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg
Expand Up @@ -1104,9 +1104,15 @@ recRuleAltPredicate(ruleName,opPrec) ::= "precpred(_ctx, <opPrec>)"
recRuleSetReturnAction(src,name) ::= "recRuleSetReturnAction(src,name) $<name>=$<src>.<name>;"
recRuleSetStopToken() ::= "_ctx->stop = _input->LT(-1);"

recRuleAltStartAction(ruleName, ctxName, label) ::= <<
recRuleAltStartAction(ruleName, ctxName, label, isListLabel) ::= <<
_localctx = _tracker.createInstance\<<ctxName>Context>(parentContext, parentState);
<if (label)>_localctx-><label> = previousContext;<endif>
<if(label)>
<if(isListLabel)>
_localctx-><label>.push_back(previousContext);
<else>
_localctx-><label> = previousContext;
<endif>
<endif>
pushNewRecursionContext(_localctx, startState, Rule<ruleName; format = "cap">);
>>

Expand Down
Expand Up @@ -855,7 +855,6 @@ _localctx.<label>.add(_prevctx);
_localctx.<label> = _prevctx;
<endif>
<endif>
<if(label)>_localctx.<label> = _prevctx;<endif>
pushNewRecursionContext(_localctx, _startState, RULE_<ruleName>);
>>

Expand Down
Expand Up @@ -744,9 +744,15 @@ recRuleAltPredicate(ruleName,opPrec) ::= "this.precpred(this._ctx, <opPrec>)"
recRuleSetReturnAction(src,name) ::= "$<name>=$<src>.<name>"
recRuleSetStopToken() ::= "this._ctx.stop = this._input.LT(-1);"

recRuleAltStartAction(ruleName, ctxName, label) ::= <<
recRuleAltStartAction(ruleName, ctxName, label, isListLabel) ::= <<
localctx = new <ctxName>Context(this, _parentctx, _parentState);
<if(label)>localctx.<label> = _prevctx;<endif>
<if(label)>
<if(isListLabel)>
localctx.<label>.push(_prevctx);
<else>
localctx.<label> = _prevctx;
<endif>
<endif>
this.pushNewRecursionContext(localctx, _startState, <parser.name>.RULE_<ruleName>);
>>

Expand Down
Expand Up @@ -707,9 +707,15 @@ recRuleAltPredicate(ruleName,opPrec) ::= "self.precpred(self._ctx, <opPrec>)"
recRuleSetReturnAction(src,name) ::= "$<name>=$<src>.<name>"
recRuleSetStopToken() ::= "self._ctx.stop = self._input.LT(-1)"

recRuleAltStartAction(ruleName, ctxName, label) ::= <<
recRuleAltStartAction(ruleName, ctxName, label, isListLabel) ::= <<
localctx = <parser.name>.<ctxName>Context(self, _parentctx, _parentState)
<if(label)>localctx.<label> = _prevctx<endif>
<if(label)>
<if(isListLabel)>
localctx.<label>.append(_prevctx)
<else>
localctx.<label> = _prevctx
<endif>
<endif>
self.pushNewRecursionContext(localctx, _startState, self.RULE_<ruleName>)
>>

Expand Down
Expand Up @@ -721,9 +721,15 @@ recRuleAltPredicate(ruleName,opPrec) ::= "self.precpred(self._ctx, <opPrec>)"
recRuleSetReturnAction(src,name) ::= "$<name>=$<src>.<name>"
recRuleSetStopToken() ::= "self._ctx.stop = self._input.LT(-1)"

recRuleAltStartAction(ruleName, ctxName, label) ::= <<
recRuleAltStartAction(ruleName, ctxName, label, isListLabel) ::= <<
localctx = <parser.name>.<ctxName>Context(self, _parentctx, _parentState)
<if(label)>localctx.<label> = _prevctx<endif>
<if(label)>
<if(isListLabel)>
localctx.<label>.append(_prevctx)
<else>
localctx.<label> = _prevctx
<endif>
<endif>
self.pushNewRecursionContext(localctx, _startState, self.RULE_<ruleName>)
>>

Expand Down
Expand Up @@ -922,7 +922,6 @@ _localctx.<label>.append(_prevctx)
_localctx.<label> = _prevctx
<endif>
<endif>
<if(label)>_localctx.<label> = _prevctx;<endif>
try pushNewRecursionContext(_localctx, _startState, <parser.name>.RULE_<ruleName>)
>>

Expand Down

0 comments on commit 51150ab

Please sign in to comment.