Skip to content

Commit fe38615

Browse files
authored
Merge pull request #3957 from parrt/compare-ATN-sim-output
Generate identical atn simulation output across targets to compare parsing functionality
2 parents 2ff60cc + 1cfb962 commit fe38615

File tree

61 files changed

+829
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+829
-292
lines changed

doc/creating-a-language-target.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,26 @@ $ mvn compile
4545
```
4646

4747
That should proceed with success. See [Building ANTLR](building-antlr.md) for more details.
48+
49+
## Comparing your target's parsing decisionmaking with Java's
50+
51+
ANTLR's power comes from it's dynamic parsing strategy, but that means each target
52+
must implement that complicated algorithm. You should compare your target's debug
53+
output for ParserATNSimulator with Java's.
54+
55+
Run this so we get right jars before trying this script:
56+
57+
```
58+
cd ANTLR-ROOT-DIR
59+
mvn install -DskipTests=true
60+
cd runtime-tests
61+
mvn install -DskipTests=true # yes do it again
62+
```
63+
64+
Run the script from `runtime-tests` dir with
65+
66+
```
67+
../scripts/traceatn.sh /tmp/JSON.g4 json -target Go /tmp/foo.json
68+
```
69+
70+
or whatever your test grammar, start rule, target, test input are.

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.cpp.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main(int argc, const char* argv[]) {
3333
CommonTokenStream tokens(&lexer);
3434
<if(parserName)>
3535
<parserName> parser(&tokens);
36-
<if(debug)>
36+
<if(showDiagnosticErrors)>
3737
DiagnosticErrorListener errorListener;
3838
parser.addErrorListener(&errorListener);
3939
<endif>

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.cs.stg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Antlr4.Runtime;
3+
using Antlr4.Runtime.Atn;
34
using Antlr4.Runtime.Tree;
45
using System.Text;
56

@@ -12,8 +13,11 @@ public class Test {
1213
var tokens = new CommonTokenStream(lex);
1314
<if(parserName)>
1415
var parser = new <parserName>(tokens);
15-
<if(debug)>
16+
<if(showDiagnosticErrors)>
1617
parser.AddErrorListener(new DiagnosticErrorListener());
18+
<endif>
19+
<if(traceATN)>
20+
ParserATNSimulator.trace_atn_sim = true;
1721
<endif>
1822
parser.BuildParseTree = true;
1923
var tree = parser.<parserStartRuleName>();

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.dart.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void main(List\<String> args) async {
1212
final tokens = CommonTokenStream(lex);
1313
<if(parserName)>
1414
final parser = <parserName>(tokens);
15-
<if(debug)>
15+
<if(showDiagnosticErrors)>
1616
parser.addErrorListener(new DiagnosticErrorListener());
1717
<endif>
1818
<if(profile)>

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.go.stg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func (this *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) {
2727
<endif>
2828

2929
func main() {
30+
<if(traceATN)>
31+
antlr.ParserATNSimulatorTraceATNSim = true
32+
<endif>
3033
input, err := antlr.NewFileStream(os.Args[1])
3134
if err != nil {
3235
fmt.Printf("Failed to find file: %v", err)
@@ -36,7 +39,7 @@ func main() {
3639
stream := antlr.NewCommonTokenStream(lexer,0)
3740
<if(parserName)>
3841
p := parser.New<parserName>(stream)
39-
<if(debug)>
42+
<if(showDiagnosticErrors)>
4043
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
4144
<endif>
4245
p.BuildParseTrees = true

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.java.stg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ public class Test {
2929
parser.setOutStream(outStream);
3030
parser.removeErrorListeners();
3131
parser.addErrorListener(errorListener);
32-
<if(debug)>
32+
<if(showDiagnosticErrors)>
3333
parser.addErrorListener(new DiagnosticErrorListener());
34+
<endif>
35+
<if(traceATN)>
36+
ParserATNSimulator.trace_atn_sim = true;
3437
<endif>
3538
parser.setBuildParseTree(true);
3639
<if(profile)>

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.js.stg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function main(argv) {
2828
var stream = new antlr4.CommonTokenStream(lexer);
2929
<if(parserName)>
3030
var parser = new <parserName>(stream);
31-
<if(debug)>
31+
<if(showDiagnosticErrors)>
3232
parser.addErrorListener(new antlr4.error.DiagnosticErrorListener());
3333
<endif>
3434
parser.buildParseTrees = true;
@@ -38,6 +38,10 @@ function main(argv) {
3838
return this;
3939
};
4040
parser.printer = new printer();
41+
<if(traceATN)>
42+
parser._interp.trace_atn_sim = true;
43+
antlr4.context.PredictionContext.trace_atn_sim = true;
44+
<endif>
4145
var tree = parser.<parserStartRuleName>();
4246
antlr4.tree.ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);
4347
<else>

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.php.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $lexer->addErrorListener(new ConsoleErrorListener());
4848
$tokens = new CommonTokenStream($lexer);
4949
<if(parserName)>
5050
$parser = new <parserName>($tokens);
51-
<if(debug)>
51+
<if(showDiagnosticErrors)>
5252
$parser->addErrorListener(new DiagnosticErrorListener());
5353
<endif>
5454
$parser->addErrorListener(new ConsoleErrorListener());

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.py.stg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ class TreeShapeListener(ParseTreeListener):
2727
<endif>
2828

2929
def main(argv):
30+
<if(traceATN)>
31+
ParserATNSimulator.trace_atn_sim = True
32+
PredictionContext._trace_atn_sim = True
33+
<endif>
3034
input = FileStream(argv[1], encoding='utf-8', errors='replace')
3135
lexer = <lexerName>(input)
3236
stream = CommonTokenStream(lexer)
3337
<if(parserName)>
3438
parser = <parserName>(stream)
35-
<if(debug)>
39+
<if(showDiagnosticErrors)>
3640
parser.addErrorListener(DiagnosticErrorListener())
3741
<endif>
3842
parser.buildParseTrees = True

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/main.swift.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let lex = <lexerName>(input)
2323
let tokens = CommonTokenStream(lex)
2424
<if(parserName)>
2525
let parser = try <parserName>(tokens)
26-
<if(debug)>
26+
<if(showDiagnosticErrors)>
2727
parser.addErrorListener(DiagnosticErrorListener())
2828
<endif>
2929
parser.setBuildParseTree(true)

0 commit comments

Comments
 (0)