diff options
author | Emmanuel Bourg <ebourg@apache.org> | 2016-03-31 09:23:04 +0200 |
---|---|---|
committer | Emmanuel Bourg <ebourg@apache.org> | 2016-03-31 09:23:04 +0200 |
commit | fb9a8bd2676af7f0fdad80774237fdafb2d8d360 (patch) | |
tree | 2840d86293518239513fc5c20ec319f27a36e86e | |
parent | 2998a6427561725f9c07c913cff5e835dbd5446d (diff) | |
parent | a098ea9f9fbd729a7b854028fb07aca8931af702 (diff) |
Merge tag 'upstream/4.5.3'
Upstream version 4.5.3
48 files changed, 237 insertions, 149 deletions
diff --git a/antlr4-maven-plugin/pom.xml b/antlr4-maven-plugin/pom.xml index c525628..b9e144d 100644 --- a/antlr4-maven-plugin/pom.xml +++ b/antlr4-maven-plugin/pom.xml @@ -34,7 +34,7 @@ <parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> </parent> <artifactId>antlr4-maven-plugin</artifactId> <packaging>maven-plugin</packaging> diff --git a/contributors.txt b/contributors.txt index 7d3b44a..687eeaf 100644 --- a/contributors.txt +++ b/contributors.txt @@ -88,3 +88,7 @@ YYYY/MM/DD, github id, Full name, email 2015/12/17, sebadur, Sebastian Badur, sebadur@users.noreply.github.com 2015/12/23, pboyer, Peter Boyer, peter.b.boyer@gmail.com 2015/12/24, dtymon, David Tymon, david.tymon@gmail.com +2016/02/18, reitzig, Raphael Reitzig, reitzig[at]cs.uni-kl.de +2016/03/27, beardlybread, Bradley Steinbacher, bradley.j.steinbacher@gmail.com +2016/03/29, msteiger, Martin Steiger, antlr@martin-steiger.de +2016/03/28, gagern, Martin von Gagern, gagern@ma.tum.de @@ -7,7 +7,7 @@ </parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> <packaging>pom</packaging> <name>ANTLR 4</name> diff --git a/runtime-testsuite/pom.xml b/runtime-testsuite/pom.xml index 08cdde4..e09140d 100644 --- a/runtime-testsuite/pom.xml +++ b/runtime-testsuite/pom.xml @@ -4,7 +4,7 @@ <parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> </parent> <artifactId>antlr4-runtime-testsuite</artifactId> <name>ANTLR 4 Runtime Test Generator</name> diff --git a/runtime/Java/pom.xml b/runtime/Java/pom.xml index 0a76da0..9824ffd 100644 --- a/runtime/Java/pom.xml +++ b/runtime/Java/pom.xml @@ -3,7 +3,7 @@ <parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> <artifactId>antlr4-runtime</artifactId> diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index eb299ec..eda34a1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -34,7 +34,6 @@ import org.antlr.v4.runtime.atn.ATNDeserializationOptions; import org.antlr.v4.runtime.atn.ATNDeserializer; import org.antlr.v4.runtime.atn.ATNSimulator; import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.AmbiguityInfo; import org.antlr.v4.runtime.atn.ParseInfo; import org.antlr.v4.runtime.atn.ParserATNSimulator; import org.antlr.v4.runtime.atn.PredictionMode; @@ -51,7 +50,6 @@ import org.antlr.v4.runtime.tree.pattern.ParseTreePattern; import org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -649,6 +647,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> { } public void enterOuterAlt(ParserRuleContext localctx, int altNum) { + localctx.setAltNumber(altNum); // if we have new localctx, make sure we replace existing ctx // that is previous child of parse tree if ( _buildParseTrees && _ctx != localctx ) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index 6cde47e..0103966 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -29,20 +29,17 @@ */ package org.antlr.v4.runtime; +import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.RuleNode; import org.antlr.v4.runtime.tree.Trees; -import javax.print.PrintException; -import javax.swing.*; -import java.io.IOException; import java.util.Arrays; import java.util.List; -import java.util.concurrent.Future; -/** /** A rule context is a record of a single rule invocation. +/** A rule context is a record of a single rule invocation. * * We form a stack of these context objects using the parent * pointer. A parent pointer of null indicates that the current @@ -169,6 +166,27 @@ public class RuleContext implements RuleNode { public int getRuleIndex() { return -1; } + /** For rule associated with this parse tree internal node, return + * the outer alternative number used to match the input. Default + * implementation does not compute nor store this alt num. Create + * a subclass of ParserRuleContext with backing field and set + * option contextSuperClass. + * to set it. + * + * @since 4.5.3 + */ + public int getAltNumber() { return ATN.INVALID_ALT_NUMBER; } + + /** Set the outer alternative number for this context node. Default + * implementation does nothing to avoid backing field overhead for + * trees that don't need it. Create + * a subclass of ParserRuleContext with backing field and set + * option contextSuperClass. + * + * @since 4.5.3 + */ + public void setAltNumber(int altNumber) { } + @Override public ParseTree getChild(int i) { return null; diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContextWithAltNum.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContextWithAltNum.java new file mode 100644 index 0000000..63d3b22 --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContextWithAltNum.java @@ -0,0 +1,24 @@ +package org.antlr.v4.runtime; + +import org.antlr.v4.runtime.atn.ATN; + +/** A handy class for use with + * + * options {contextSuperClass=org.antlr.v4.runtime.RuleContextWithAltNum;} + * + * that provides a backing field / impl for the outer alternative number + * matched for an internal parse tree node. + * + * I'm only putting into Java runtime as I'm certain I'm the only one that + * will really every use this. + */ +public class RuleContextWithAltNum extends ParserRuleContext { + public int altNum; + public RuleContextWithAltNum() { altNum = ATN.INVALID_ALT_NUMBER; } + + public RuleContextWithAltNum(ParserRuleContext parent, int invokingStateNumber) { + super(parent, invokingStateNumber); + } + @Override public int getAltNumber() { return altNum; } + @Override public void setAltNumber(int altNum) { this.altNum = altNum; } +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuntimeMetaData.java b/runtime/Java/src/org/antlr/v4/runtime/RuntimeMetaData.java index aa1375e..b8bf0d5 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuntimeMetaData.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuntimeMetaData.java @@ -91,7 +91,7 @@ public class RuntimeMetaData { * omitted.</li> * </ul> */ - public static final String VERSION = "4.5.2"; + public static final String VERSION = "4.5.3"; /** * Gets the currently executing version of the ANTLR 4 runtime library. diff --git a/runtime/Java/src/org/antlr/v4/runtime/Vocabulary.java b/runtime/Java/src/org/antlr/v4/runtime/Vocabulary.java index 0b6386c..2df5377 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Vocabulary.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Vocabulary.java @@ -38,6 +38,13 @@ package org.antlr.v4.runtime; */ public interface Vocabulary { /** + * Returns the highest token type value. It can be used to iterate from + * zero to that number, inclusively, thus querying all stored entries. + * @return the highest token type value + */ + int getMaxTokenType(); + + /** * Gets the string literal associated with a token type. The string returned * by this method, when not {@code null}, can be used unaltered in a parser * grammar to represent this token type. @@ -85,7 +92,7 @@ public interface Vocabulary { * * <ul> * <li>Tokens created by lexer rules.</li> - * <li>Tokens defined in a {@code tokens{}} block in a lexer or parser + * <li>Tokens defined in a <code>tokens{}</code> block in a lexer or parser * grammar.</li> * <li>The implicitly defined {@code EOF} token, which has the token type * {@link Token#EOF}.</li> diff --git a/runtime/Java/src/org/antlr/v4/runtime/VocabularyImpl.java b/runtime/Java/src/org/antlr/v4/runtime/VocabularyImpl.java index ff5beef..93566bd 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/VocabularyImpl.java +++ b/runtime/Java/src/org/antlr/v4/runtime/VocabularyImpl.java @@ -57,6 +57,8 @@ public class VocabularyImpl implements Vocabulary { private final String[] displayNames; + private final int maxTokenType; + /** * Constructs a new instance of {@link VocabularyImpl} from the specified * literal and symbolic token names. @@ -94,6 +96,10 @@ public class VocabularyImpl implements Vocabulary { this.literalNames = literalNames != null ? literalNames : EMPTY_NAMES; this.symbolicNames = symbolicNames != null ? symbolicNames : EMPTY_NAMES; this.displayNames = displayNames != null ? displayNames : EMPTY_NAMES; + // See note here on -1 part: https://github.com/antlr/antlr4/pull/1146 + this.maxTokenType = + Math.max(this.displayNames.length, + Math.max(this.literalNames.length, this.symbolicNames.length)) - 1; } /** @@ -144,6 +150,11 @@ public class VocabularyImpl implements Vocabulary { } @Override + public int getMaxTokenType() { + return maxTokenType; + } + + @Override public String getLiteralName(int tokenType) { if (tokenType >= 0 && tokenType < literalNames.length) { return literalNames[tokenType]; diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java index c58ee3d..3cb8668 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -33,7 +33,9 @@ package org.antlr.v4.runtime.tree; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Predicate; import org.antlr.v4.runtime.misc.Utils; @@ -91,9 +93,13 @@ public class Trees { public static String getNodeText(Tree t, List<String> ruleNames) { if ( ruleNames!=null ) { - if ( t instanceof RuleNode ) { - int ruleIndex = ((RuleNode)t).getRuleContext().getRuleIndex(); + if ( t instanceof RuleContext ) { + int ruleIndex = ((RuleContext)t).getRuleContext().getRuleIndex(); String ruleName = ruleNames.get(ruleIndex); + int altNumber = ((RuleContext) t).getAltNumber(); + if ( altNumber!=ATN.INVALID_ALT_NUMBER ) { + return ruleName+":"+altNumber; + } return ruleName; } else if ( t instanceof ErrorNode) { diff --git a/tool-testsuite/pom.xml b/tool-testsuite/pom.xml index 81b4525..c7b1564 100644 --- a/tool-testsuite/pom.xml +++ b/tool-testsuite/pom.xml @@ -4,7 +4,7 @@ <parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> </parent> <artifactId>antlr4-tool-testsuite</artifactId> <name>ANTLR 4 Tool Tests</name> diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/InterpreterTreeTextProvider.java b/tool-testsuite/test/org/antlr/v4/test/tool/InterpreterTreeTextProvider.java index 1a555c4..bff84ae 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/InterpreterTreeTextProvider.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/InterpreterTreeTextProvider.java @@ -1,10 +1,9 @@ package org.antlr.v4.test.tool; +import org.antlr.v4.gui.TreeTextProvider; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.Tree; import org.antlr.v4.runtime.tree.Trees; -import org.antlr.v4.gui.TreeTextProvider; -import org.antlr.v4.tool.GrammarInterpreterRuleContext; import java.util.Arrays; import java.util.List; @@ -17,10 +16,6 @@ public class InterpreterTreeTextProvider implements TreeTextProvider { public String getText(Tree node) { if ( node==null ) return "null"; String nodeText = Trees.getNodeText(node, ruleNames); - if ( node instanceof GrammarInterpreterRuleContext) { - GrammarInterpreterRuleContext ctx = (GrammarInterpreterRuleContext) node; - return nodeText+":"+ctx.getOuterAltNum(); - } if ( node instanceof ErrorNode) { return "<error "+nodeText+">"; } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestActionTranslation.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestActionTranslation.java index e3c44bb..dfe1704 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestActionTranslation.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestActionTranslation.java @@ -31,6 +31,7 @@ package org.antlr.v4.test.tool; import org.antlr.v4.test.runtime.java.BaseTest; +import org.antlr.v4.tool.Grammar; import org.junit.Test; /** */ @@ -180,6 +181,15 @@ public class TestActionTranslation extends BaseTest { testActions(attributeTemplate, "finally", action, expected); } + @Test public void testEmptyActions() throws Exception { + String gS = + "grammar A;\n"+ + "a[] : 'a' ;\n" + + "c : a[] c[] ;\n"; + Grammar g = new Grammar(gS); + } + + @Test public void testDynamicRuleScopeRefInSubrule() throws Exception { String action = "$a::n;"; } diff --git a/tool/pom.xml b/tool/pom.xml index d762947..42e5f82 100644 --- a/tool/pom.xml +++ b/tool/pom.xml @@ -3,7 +3,7 @@ <parent> <groupId>org.antlr</groupId> <artifactId>antlr4-master</artifactId> - <version>4.5.2-1</version> + <version>4.5.4-SNAPSHOT</version> </parent> <artifactId>antlr4</artifactId> <name>ANTLR 4 Tool</name> diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg index 0617385..6d57109 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg @@ -30,7 +30,7 @@ // args must be <object-model-object>, <fields-resulting-in-STs> -ParserFile(file, parser, namedActions) ::= << +ParserFile(file, parser, namedActions, contextSuperClass) ::= << <fileHeader(file.grammarFileName, file.ANTLRVersion)> <if(file.genPackage)> namespace <file.genPackage> { @@ -839,7 +839,7 @@ CaptureNextTokenType(d) ::= "<d.varName> = TokenStream.La(1);" StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers, superClass={ParserRuleContext}) ::= << -public partial class <struct.name> : <superClass><if(interfaces)>, <interfaces; separator=", "><endif> { +public partial class <struct.name> : <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)>, <interfaces; separator=", "><endif> { <attrs:{a | public <a>;}; separator="\n"> <getters:{g | <g>}; separator="\n"> <if(ctorAttrs)>public <struct.name>(ParserRuleContext parent, int invokingState) : base(parent, invokingState) { }<endif> diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 8bd429a..af925be 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -42,7 +42,7 @@ javaTypeInitMap ::= [ // args must be <object-model-object>, <fields-resulting-in-STs> -ParserFile(file, parser, namedActions) ::= << +ParserFile(file, parser, namedActions, contextSuperClass) ::= << <fileHeader(file.grammarFileName, file.ANTLRVersion)> <if(file.genPackage)> package <file.genPackage>; @@ -765,9 +765,9 @@ ListLabelName(label) ::= "<label>" CaptureNextToken(d) ::= "<d.varName> = _input.LT(1);" CaptureNextTokenType(d) ::= "<d.varName> = _input.LA(1);" -StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers, - superClass={ParserRuleContext}) ::= << -public static class <struct.name> extends <superClass><if(interfaces)> implements <interfaces; separator=", "><endif> { +StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) + ::= << +public static class <struct.name> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif> { <attrs:{a | public <a>;}; separator="\n"> <getters:{g | <g>}; separator="\n"> <if(ctorAttrs)>public <struct.name>(ParserRuleContext parent, int invokingState) { super(parent, invokingState); }<endif> diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg index b015c0b..f8101b2 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg @@ -46,7 +46,7 @@ pythonTypeInitMap ::= [ // args must be <object-model-object>, <fields-resulting-in-STs> -ParserFile(file, parser, namedActions) ::= << +ParserFile(file, parser, namedActions, contextSuperClass) ::= << <fileHeader(file.grammarFileName, file.ANTLRVersion)> var antlr4 = require('antlr4/index'); <if(file.genListener)> @@ -671,8 +671,7 @@ ListLabelName(label) ::= "<label>" CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)" CaptureNextTokenType(d) ::= "<d.varName> = this._input.LA(1);" -StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers, - superClass={antlr4.ParserRuleContext}) ::= << +StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= << function <struct.name>(parser, parent, invokingState<struct.ctorAttrs:{a | , <a.name>}>) { if(parent===undefined) { parent = null; @@ -680,7 +679,7 @@ function <struct.name>(parser, parent, invokingState<struct.ctorAttrs:{a | , <a. if(invokingState===undefined || invokingState===null) { invokingState = -1; } - <superClass>.call(this, parent, invokingState); + <if(contextSuperClass)><contextSuperClass><else>antlr4.ParserRuleContext<endif>.call(this, parent, invokingState); this.parser = parser; this.ruleIndex = <parser.name>.RULE_<struct.derivedFromName>; <attrs:{a | <a>}; separator="\n"> @@ -688,14 +687,14 @@ function <struct.name>(parser, parent, invokingState<struct.ctorAttrs:{a | , <a. return this; } -<struct.name>.prototype = Object.create(<superClass>.prototype); +<struct.name>.prototype = Object.create(<if(contextSuperClass)><contextSuperClass><else>antlr4.ParserRuleContext<endif>.prototype); <struct.name>.prototype.constructor = <struct.name>; <getters:{g | <struct.name>.prototype.<g>}; separator="\n\n"> <if(struct.provideCopyFrom)> <! don't need copy unless we have subclasses !> <struct.name>.prototype.copyFrom = function(ctx) { - <superClass>.prototype.copyFrom.call(this, ctx); + <if(contextSuperClass)><contextSuperClass><else>antlr4.ParserRuleContext<endif>.prototype.copyFrom.call(this, ctx); <struct.attrs:{a | this.<a.name> = ctx.<a.name>;}; separator="\n"> }; <endif> diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg index 66d84e6..c4ab7cc 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg @@ -46,7 +46,7 @@ pythonTypeInitMap ::= [ // args must be <object-model-object>, <fields-resulting-in-STs> -ParserFile(file, parser, namedActions) ::= << +ParserFile(file, parser, namedActions, contextSuperClass) ::= << <fileHeader(file.grammarFileName, file.ANTLRVersion)> # encoding: utf-8 from __future__ import print_function @@ -630,9 +630,8 @@ ListLabelName(label) ::= "<label>" CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)" CaptureNextTokenType(d) ::= "<d.varName> = self._input.LA(1)" -StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers, - superClass={ParserRuleContext}) ::= << -class <struct.name>(<superClass>): +StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= << +class <struct.name>(<if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif>): def __init__(self, parser, parent=None, invokingState=-1<struct.ctorAttrs:{a | , <a.name>=None}>): super(<parser.name>.<struct.name>, self).__init__(parent, invokingState) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg index 515cf65..c341079 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg @@ -46,7 +46,7 @@ pythonTypeInitMap ::= [ // args must be <object-model-object>, <fields-resulting-in-STs> -ParserFile(file, parser, namedActions) ::= << +ParserFile(file, parser, namedActions, contextSuperClass) ::= << <fileHeader(file.grammarFileName, file.ANTLRVersion)> # encoding: utf-8 from antlr4 import * @@ -638,9 +638,8 @@ ListLabelName(label) ::= "<label>" CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)" CaptureNextTokenType(d) ::= "<d.varName> = self._input.LA(1)" -StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers, - superClass={ParserRuleContext}) ::= << -class <struct.name>(<superClass>): +StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= << +class <struct.name>(<if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif>): def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1<struct.ctorAttrs:{a | , <a.name><if(a.type)>:<a.type><endif>=None}>): super().__init__(parent, invokingState) diff --git a/tool/src/org/antlr/v4/analysis/AnalysisPipeline.java b/tool/src/org/antlr/v4/analysis/AnalysisPipeline.java index 5bd84be..a04ec31 100644 --- a/tool/src/org/antlr/v4/analysis/AnalysisPipeline.java +++ b/tool/src/org/antlr/v4/analysis/AnalysisPipeline.java @@ -100,7 +100,7 @@ public class AnalysisPipeline { } } - /** Return whether lookahead sets are disjoint; no lookahead => not disjoint */ + /** Return whether lookahead sets are disjoint; no lookahead ⇒ not disjoint */ public static boolean disjoint(IntervalSet[] altLook) { boolean collision = false; IntervalSet combined = new IntervalSet(); diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java index 5b6ba05..7a1d233 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java @@ -68,8 +68,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> binaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>(); public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> ternaryAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>(); public LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> suffixAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>(); - public List<LeftRecursiveRuleAltInfo> prefixAlts = new ArrayList<LeftRecursiveRuleAltInfo>(); - public List<LeftRecursiveRuleAltInfo> otherAlts = new ArrayList<LeftRecursiveRuleAltInfo>(); + public List<LeftRecursiveRuleAltInfo> prefixAndOtherAlts = new ArrayList<LeftRecursiveRuleAltInfo>(); /** Pointer to ID node of ^(= ID element) */ public List<Pair<GrammarAST,String>> leftRecursiveRuleRefLabels = @@ -188,7 +187,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { LeftRecursiveRuleAltInfo a = new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree); a.nextPrec = nextPrec; - prefixAlts.add(a); + prefixAndOtherAlts.add(a); //System.out.println("prefixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText); } @@ -222,7 +221,9 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null; LeftRecursiveRuleAltInfo a = new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree); - otherAlts.add(a); + // We keep other alts with prefix alts since they are all added to the start of the generated rule, and + // we want to retain any prior ordering between them + prefixAndOtherAlts.add(a); // System.out.println("otherAlt " + alt + ": " + altText); } @@ -254,8 +255,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { ruleST.add("opAlts", altST); } - ruleST.add("primaryAlts", prefixAlts); - ruleST.add("primaryAlts", otherAlts); + ruleST.add("primaryAlts", prefixAndOtherAlts); tool.log("left-recursion", ruleST.render()); @@ -330,7 +330,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { return lrlabel; } - /** Strip last 2 tokens if -> label; alter indexes in altAST */ + /** Strip last 2 tokens if → label; alter indexes in altAST */ public void stripAltLabel(GrammarAST altAST) { int start = altAST.getTokenStartIndex(); int stop = altAST.getTokenStopIndex(); @@ -439,8 +439,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { "binaryAlts=" + binaryAlts + ", ternaryAlts=" + ternaryAlts + ", suffixAlts=" + suffixAlts + - ", prefixAlts=" + prefixAlts + - ", otherAlts=" + otherAlts + + ", prefixAndOtherAlts=" +prefixAndOtherAlts+ '}'; } } diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java index ea67493..45cb517 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java @@ -168,8 +168,7 @@ public class LeftRecursiveRuleTransformer { // track recursive alt info for codegen r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>(); - r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts); - r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.otherAlts); + r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts); if (r.recPrimaryAlts.isEmpty()) { tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name); } @@ -244,9 +243,9 @@ public class LeftRecursiveRuleTransformer { * (ALT ID)) * (* (BLOCK * (OPTIONS ...) - * (ALT {7 >= $_p}? '*' (= b e) {$v = $a.v * $b.v;}) - * (ALT {6 >= $_p}? '+' (= b e) {$v = $a.v + $b.v;}) - * (ALT {3 >= $_p}? '++') (ALT {2 >= $_p}? '--')))))) + * (ALT {7 >= $_p}? '*' (= b e) {$v = $a.v * $b.v;}) + * (ALT {6 >= $_p}? '+' (= b e) {$v = $a.v + $b.v;}) + * (ALT {3 >= $_p}? '++') (ALT {2 >= $_p}? '--')))))) * </pre> */ public void setAltASTPointers(LeftRecursiveRule r, RuleAST t) { diff --git a/tool/src/org/antlr/v4/automata/ATNFactory.java b/tool/src/org/antlr/v4/automata/ATNFactory.java index aff2862..b1f61a8 100644 --- a/tool/src/org/antlr/v4/automata/ATNFactory.java +++ b/tool/src/org/antlr/v4/automata/ATNFactory.java @@ -93,7 +93,7 @@ public interface ATNFactory { /** For a non-lexer, just build a simple token reference atom. * For a lexer, a string is a sequence of char to match. That is, * "fog" is treated as 'f' 'o' 'g' not as a single transition in - * the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states + * the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states * for n characters. */ @@ -101,16 +101,16 @@ public interface ATNFactory { /** For reference to rule r, build * - * o-e->(r) o + * o-e->(r) o * * where (r) is the start of rule r and the trailing o is not linked * to from rule ref state directly (it's done thru the transition(0) * RuleClosureTransition. * * If the rule r is just a list of tokens, it's block will be just - * a set on an edge o->o->o-set->o->o->o, could inline it rather than doing + * a set on an edge o->o->o-set->o->o->o, could inline it rather than doing * the rule reference, but i'm not doing this yet as I'm not sure - * it would help much in the ATN->DFA construction. + * it would help much in the ATN->DFA construction. * * TODO add to codegen: collapse alt blks that are sets into single matchSet * @param node @@ -118,7 +118,7 @@ public interface ATNFactory { Handle ruleRef(GrammarAST node); - /** From an empty alternative build Grip o-e->o */ + /** From an empty alternative build Grip o-e->o */ Handle epsilon(GrammarAST node); @@ -143,13 +143,13 @@ public interface ATNFactory { /** From A|B|..|Z alternative block build * - * o->o-A->o->o (last ATNState is blockEndATNState pointed to by all alts) + * o->o-A->o->o (last ATNState is blockEndATNState pointed to by all alts) * | ^ - * o->o-B->o--| + * o->o-B->o--| * | | * ... | * | | - * o->o-Z->o--| + * o->o-Z->o--| * * So every alternative gets begin ATNState connected by epsilon * and every alt right side points at a block end ATNState. There is a @@ -160,7 +160,7 @@ public interface ATNFactory { * begin/end. * * Special case: if just a list of tokens/chars/sets, then collapse - * to a single edge'd o-set->o graph. + * to a single edge'd o-set->o graph. * * Set alt number (1..n) in the left-Transition ATNState. */ @@ -171,9 +171,9 @@ public interface ATNFactory { /** From (A)? build either: * - * o--A->o + * o--A->o * | ^ - * o---->| + * o---->| * * or, if A is a block, just add an empty alt to the end of the block */ @@ -184,7 +184,7 @@ public interface ATNFactory { * * |---| (Transition 2 from A.right points at alt 1) * v | (follow of loop is Transition 1) - * o->o-A-o->o + * o->o-A-o->o * * Meaning that the last ATNState in A points back to A's left Transition ATNState * and we add a new begin/end ATNState. A can be single alternative or @@ -200,7 +200,7 @@ public interface ATNFactory { * * |---| * v | - * o->o-A-o--o (Transition 2 from block end points at alt 1; follow is Transition 1) + * o->o-A-o--o (Transition 2 from block end points at alt 1; follow is Transition 1) * | ^ * o---------| (optional branch is 2nd alt of optional block containing A+) * diff --git a/tool/src/org/antlr/v4/automata/LexerATNFactory.java b/tool/src/org/antlr/v4/automata/LexerATNFactory.java index a5e80e4..8d16bd1 100644 --- a/tool/src/org/antlr/v4/automata/LexerATNFactory.java +++ b/tool/src/org/antlr/v4/automata/LexerATNFactory.java @@ -324,7 +324,7 @@ public class LexerATNFactory extends ParserATNFactory { /** For a lexer, a string is a sequence of char to match. That is, * "fog" is treated as 'f' 'o' 'g' not as a single transition in - * the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states + * the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states * for n characters. */ @Override diff --git a/tool/src/org/antlr/v4/automata/ParserATNFactory.java b/tool/src/org/antlr/v4/automata/ParserATNFactory.java index 3664111..d2114b4 100644 --- a/tool/src/org/antlr/v4/automata/ParserATNFactory.java +++ b/tool/src/org/antlr/v4/automata/ParserATNFactory.java @@ -286,7 +286,7 @@ public class ParserATNFactory implements ATNFactory { * For reference to rule {@code r}, build * * <pre> - * o->(r) o + * o->(r) o * </pre> * * where {@code (r)} is the start of rule {@code r} and the trailing @@ -391,24 +391,24 @@ public class ParserATNFactory implements ATNFactory { * From {@code A|B|..|Z} alternative block build * * <pre> - * o->o-A->o->o (last ATNState is BlockEndState pointed to by all alts) + * o->o-A->o->o (last ATNState is BlockEndState pointed to by all alts) * | ^ - * |->o-B->o--| + * |->o-B->o--| * | | * ... | * | | - * |->o-Z->o--| + * |->o-Z->o--| * </pre> * * So start node points at every alternative with epsilon transition and * every alt right side points at a block end ATNState. - * <p/> + * <p> * Special case: only one alternative: don't make a block with alt * begin/end. - * <p/> + * <p> * Special case: if just a list of tokens/chars/sets, then collapse to a - * single edged o-set->o graph. - * <p/> + * single edged o-set->o graph. + * <p> * TODO: Set alt number (1..n) in the states? */ @@ -506,9 +506,9 @@ public class ParserATNFactory implements ATNFactory { * From {@code (A)?} build either: * * <pre> - * o--A->o + * o--A->o * | ^ - * o---->| + * o---->| * </pre> * * or, if {@code A} is a block, just add an empty alt to the end of the @@ -535,7 +535,7 @@ public class ParserATNFactory implements ATNFactory { * <pre> * |---------| * v | - * [o-blk-o]->o->o + * [o-blk-o]->o->o * </pre> * * We add a decision for loop back node to the existing one at {@code blk} @@ -583,7 +583,7 @@ public class ParserATNFactory implements ATNFactory { * <pre> * |-------------| * v | - * o--[o-blk-o]->o o + * o--[o-blk-o]->o o * | ^ * -----------------| * </pre> diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index f1e5452..53a9a50 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -138,7 +138,7 @@ public class ActionTranslator implements ActionSplitterListener { ActionAST node) { String action = tokenWithinAction.getText(); - if ( action.charAt(0)=='{' ) { + if ( action!=null && action.length()>0 && action.charAt(0)=='{' ) { int firstCurly = action.indexOf('{'); int lastCurly = action.lastIndexOf('}'); if ( firstCurly>=0 && lastCurly>=0 ) { @@ -158,7 +158,7 @@ public class ActionTranslator implements ActionSplitterListener { translator.rf = rf; factory.getGrammar().tool.log("action-translator", "translate " + action); String altLabel = node.getAltLabel(); - if ( rf!=null ) { + if ( rf!=null ) { translator.nodeContext = rf.ruleCtx; if ( altLabel!=null ) translator.nodeContext = rf.altLabelCtxs.get(altLabel); } diff --git a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java index 901e439..c8a1498 100644 --- a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java +++ b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java @@ -30,6 +30,7 @@ package org.antlr.v4.codegen; +import org.antlr.v4.codegen.model.Action; import org.antlr.v4.codegen.model.CodeBlockForOuterMostAlt; import org.antlr.v4.codegen.model.OutputModelObject; import org.antlr.v4.codegen.model.RuleFunction; @@ -38,6 +39,9 @@ import org.antlr.v4.codegen.model.decl.CodeBlock; import org.antlr.v4.codegen.model.decl.Decl; import org.antlr.v4.tool.Alternative; import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.Rule; +import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.STGroup; import java.util.ArrayList; import java.util.Arrays; @@ -73,6 +77,22 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory return controller; } + @Override + public List<SrcOp> rulePostamble(RuleFunction function, Rule r) { + if ( r.namedActions.containsKey("after") || r.namedActions.containsKey("finally") ) { + // See OutputModelController.buildLeftRecursiveRuleFunction + // and Parser.exitRule for other places which set stop. + CodeGenerator gen = getGenerator(); + STGroup codegenTemplates = gen.getTemplates(); + ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken"); + Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST); + List<SrcOp> ops = new ArrayList<SrcOp>(1); + ops.add(setStopTokenAction); + return ops; + } + return super.rulePostamble(function, r); + } + // Convenience methods diff --git a/tool/src/org/antlr/v4/codegen/OutputModelWalker.java b/tool/src/org/antlr/v4/codegen/OutputModelWalker.java index 8a03895..486efd1 100644 --- a/tool/src/org/antlr/v4/codegen/OutputModelWalker.java +++ b/tool/src/org/antlr/v4/codegen/OutputModelWalker.java @@ -53,7 +53,7 @@ import java.util.Set; * We identify those nested objects by the list of arguments in the template * definition. For example, here is the definition of the parser template: * - * Parser(parser, scopes, funcs) ::= <<...>> + * Parser(parser, scopes, funcs) ::= <<...>> * * The first template argument is always the output model object from which * this walker will create the template. Any other arguments identify diff --git a/tool/src/org/antlr/v4/codegen/Target.java b/tool/src/org/antlr/v4/codegen/Target.java index 1f2d748..3d4a93a 100644 --- a/tool/src/org/antlr/v4/codegen/Target.java +++ b/tool/src/org/antlr/v4/codegen/Target.java @@ -56,7 +56,7 @@ public abstract class Target { * predicates and such that may refer to chars that need to be escaped * when represented as strings. Also, templates need to be escaped so * that the target language can hold them as a string. - * <p/> + * <p> * I have defined (via the constructor) the set of typical escapes, * but your {@link Target} subclass is free to alter the translated chars * or add more definitions. This is non-static so each target can have diff --git a/tool/src/org/antlr/v4/codegen/model/ParserFile.java b/tool/src/org/antlr/v4/codegen/model/ParserFile.java index 28ef200..808d4f9 100644 --- a/tool/src/org/antlr/v4/codegen/model/ParserFile.java +++ b/tool/src/org/antlr/v4/codegen/model/ParserFile.java @@ -31,6 +31,8 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; +import org.antlr.v4.codegen.model.chunk.ActionChunk; +import org.antlr.v4.codegen.model.chunk.ActionText; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.ast.ActionAST; @@ -42,10 +44,11 @@ public class ParserFile extends OutputFile { public String genPackage; // from -package cmd-line @ModelElement public Parser parser; @ModelElement public Map<String, Action> namedActions; + @ModelElement public ActionChunk contextSuperClass; public Boolean genListener = false; public Boolean genVisitor = false; public String grammarName; - + public ParserFile(OutputModelFactory factory, String fileName) { super(factory, fileName); Grammar g = factory.getGrammar(); @@ -59,5 +62,9 @@ public class ParserFile extends OutputFile { genListener = g.tool.gen_listener; genVisitor = g.tool.gen_visitor; grammarName = g.name; + + if (g.getOptionString("contextSuperClass") != null) { + contextSuperClass = new ActionText(null, g.getOptionString("contextSuperClass")); + } } } diff --git a/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java index 5d49625..f48f45d 100644 --- a/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java +++ b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java @@ -38,7 +38,7 @@ import org.antlr.v4.tool.Rule; import java.util.ArrayList; -/** A StructDecl to handle a -> label on alt */ +/** A StructDecl to handle a -> label on alt */ public class AltLabelStructDecl extends StructDecl { public int altNum; public AltLabelStructDecl(OutputModelFactory factory, Rule r, diff --git a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java index bfebe4b..b4aa584 100644 --- a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java @@ -51,7 +51,7 @@ public class CSharpTarget extends Target { @Override public String getVersion() { - return "4.5.2"; // crossing fingers that it's close enough. + return "4.5.3"; } @Override diff --git a/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java index 71926ac..9648376 100644 --- a/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java @@ -74,7 +74,7 @@ public class JavaScriptTarget extends Target { @Override public String getVersion() { - return "4.5.2"; + return "4.5.3"; } public Set<String> getBadWords() { @@ -93,11 +93,11 @@ public class JavaScriptTarget extends Target { /** * {@inheritDoc} - * <p/> + * <p> * For Java, this is the translation {@code 'a\n"'} → {@code "a\n\""}. * Expect single quotes around the incoming literal. Just flip the quotes * and replace double quotes with {@code \"}. - * <p/> + * <p> * Note that we have decided to allow people to use '\"' without penalty, so * we must build the target string in a loop as {@link String#replace} * cannot handle both {@code \"} and {@code "} without a lot of messing diff --git a/tool/src/org/antlr/v4/codegen/target/Python2Target.java b/tool/src/org/antlr/v4/codegen/target/Python2Target.java index efa5d2c..c80949a 100644 --- a/tool/src/org/antlr/v4/codegen/target/Python2Target.java +++ b/tool/src/org/antlr/v4/codegen/target/Python2Target.java @@ -50,7 +50,7 @@ public class Python2Target extends Target { "abs", "all", "any", "apply", "as", "bin", "bool", "buffer", "bytearray", "callable", "chr", "classmethod", "coerce", "compile", "complex", - "delattr", "dict", "dir", "divmod", + "del", "delattr", "dict", "dir", "divmod", "enumerate", "eval", "execfile", "file", "filter", "float", "format", "frozenset", "getattr", "globals", @@ -61,7 +61,7 @@ public class Python2Target extends Target { "memoryview", "object", "oct", "open", "ord", "pow", "print", "property", - "range", "raw_input", "reduce", "reload", "repr", "reversed", "round", + "range", "raw_input", "reduce", "reload", "repr", "return", "reversed", "round", "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super", "tuple", "type", "unichr", "unicode", @@ -117,7 +117,7 @@ public class Python2Target extends Target { @Override public String getVersion() { - return "4.5.2.1"; + return "4.5.3"; } public Set<String> getBadWords() { diff --git a/tool/src/org/antlr/v4/codegen/target/Python3Target.java b/tool/src/org/antlr/v4/codegen/target/Python3Target.java index 68d7a16..f70d2a2 100644 --- a/tool/src/org/antlr/v4/codegen/target/Python3Target.java +++ b/tool/src/org/antlr/v4/codegen/target/Python3Target.java @@ -50,7 +50,7 @@ public class Python3Target extends Target { "abs", "all", "any", "apply", "as", "bin", "bool", "buffer", "bytearray", "callable", "chr", "classmethod", "coerce", "compile", "complex", - "delattr", "dict", "dir", "divmod", + "del", "delattr", "dict", "dir", "divmod", "enumerate", "eval", "execfile", "file", "filter", "float", "format", "frozenset", "getattr", "globals", @@ -61,7 +61,7 @@ public class Python3Target extends Target { "memoryview", "object", "oct", "open", "ord", "pow", "print", "property", - "range", "raw_input", "reduce", "reload", "repr", "reversed", "round", + "range", "raw_input", "reduce", "reload", "repr", "return", "reversed", "round", "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super", "tuple", "type", "unichr", "unicode", @@ -119,7 +119,7 @@ public class Python3Target extends Target { @Override public String getVersion() { - return "4.5.2.1"; + return "4.5.3"; } /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ diff --git a/tool/src/org/antlr/v4/misc/Graph.java b/tool/src/org/antlr/v4/misc/Graph.java index 2509c81..a7c63d5 100644 --- a/tool/src/org/antlr/v4/misc/Graph.java +++ b/tool/src/org/antlr/v4/misc/Graph.java @@ -81,7 +81,7 @@ public class Graph<T> { * For sorting, I'm not following convention here since ANTLR * needs the opposite. Here's what I assume for sorting: * - * If there exists an edge u -> v then u depends on v and v + * If there exists an edge u -> v then u depends on v and v * must happen before u. * * So if this gives nonreversed postorder traversal, I get the order @@ -114,4 +114,4 @@ public class Graph<T> { } sorted.add(n.payload); } -}
\ No newline at end of file +} diff --git a/tool/src/org/antlr/v4/misc/Utils.java b/tool/src/org/antlr/v4/misc/Utils.java index c62003c..6d63b7c 100644 --- a/tool/src/org/antlr/v4/misc/Utils.java +++ b/tool/src/org/antlr/v4/misc/Utils.java @@ -54,21 +54,6 @@ public class Utils { static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1]; - /** Integer objects are immutable so share all Integers with the - * same value up to some max size. Use an array as a perfect hash. - * Return shared object for 0..INTEGER_POOL_MAX_VALUE or a new - * Integer object with x in it. Java's autoboxing only caches up to 127. - public static Integer integer(int x) { - if ( x<0 || x>INTEGER_POOL_MAX_VALUE ) { - return new Integer(x); - } - if ( ints[x]==null ) { - ints[x] = new Integer(x); - } - return ints[x]; - } - */ - public static String stripFileExtension(String name) { if ( name==null ) return null; int lastDot = name.lastIndexOf('.'); diff --git a/tool/src/org/antlr/v4/parse/GrammarToken.java b/tool/src/org/antlr/v4/parse/GrammarToken.java index 4d849bf..3e7e190 100644 --- a/tool/src/org/antlr/v4/parse/GrammarToken.java +++ b/tool/src/org/antlr/v4/parse/GrammarToken.java @@ -35,7 +35,7 @@ import org.antlr.runtime.Token; import org.antlr.v4.tool.Grammar; /** A CommonToken that can also track it's original location, - * derived from options on the element ref like BEGIN<line=34,...>. + * derived from options on the element ref like BEGIN<line=34,...>. */ public class GrammarToken extends CommonToken { public Grammar g; diff --git a/tool/src/org/antlr/v4/parse/ScopeParser.java b/tool/src/org/antlr/v4/parse/ScopeParser.java index a0ce182..743fe98 100644 --- a/tool/src/org/antlr/v4/parse/ScopeParser.java +++ b/tool/src/org/antlr/v4/parse/ScopeParser.java @@ -52,11 +52,15 @@ import java.util.List; public class ScopeParser { /** Given an arg or retval scope definition list like * - * Map<String, String>, int[] j3, char *foo32[3] + * <code> + * Map<String, String>, int[] j3, char *foo32[3] + * </code> * * or * + * <code> * int i=3, j=a[34]+20 + * </code> * * convert to an attribute scope. */ diff --git a/tool/src/org/antlr/v4/tool/AttributeDict.java b/tool/src/org/antlr/v4/tool/AttributeDict.java index 911170d..e8d487f 100644 --- a/tool/src/org/antlr/v4/tool/AttributeDict.java +++ b/tool/src/org/antlr/v4/tool/AttributeDict.java @@ -39,7 +39,7 @@ import java.util.LinkedHashMap; import java.util.Set; /** Track the attributes within retval, arg lists etc... - * <p/> + * <p> * Each rule has potentially 3 scopes: return values, * parameters, and an implicitly-named scope (i.e., a scope defined in a rule). * Implicitly-defined scopes are named after the rule; rules and scopes then diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 0fedfc4..32bb574 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -34,12 +34,12 @@ import org.antlr.v4.runtime.Lexer; /** * A complex enumeration of all the error messages that the tool can issue. - * <p/> + * <p> * When adding error messages, also add a description of the message to the * Wiki with a location under the Wiki page * <a href="http://www.antlr.org/wiki/display/ANTLR4/Errors+Reported+by+the+ANTLR+Tool">Errors Reported by the ANTLR Tool</a>. * - * @author Jim Idle <jimi@temporal-wave.com>, Terence Parr + * @author Jim Idle <jimi@temporal-wave.com>, Terence Parr * @since 4.0 */ public enum ErrorType { @@ -702,8 +702,8 @@ public enum ErrorType { * <p>The following rule produces this error.</p> * * <pre> - * X : 'foo' -> type(Foo); // ok - * Y : 'foo' -> token(Foo); // error 149 (token is not a supported lexer command) + * X : 'foo' -> type(Foo); // ok + * Y : 'foo' -> token(Foo); // error 149 (token is not a supported lexer command) * </pre> * * @since 4.1 @@ -719,8 +719,8 @@ public enum ErrorType { * <p>The following rule produces this error.</p> * * <pre> - * X : 'foo' -> type(Foo); // ok - * Y : 'foo' -> type; // error 150 (the type command requires an argument) + * X : 'foo' -> type(Foo); // ok + * Y : 'foo' -> type; // error 150 (the type command requires an argument) * </pre> * * @since 4.1 @@ -737,8 +737,8 @@ public enum ErrorType { * <p>The following rule produces this error.</p> * * <pre> - * X : 'foo' -> popMode; // ok - * Y : 'foo' -> popMode(A); // error 151 (the popMode command does not take an argument) + * X : 'foo' -> popMode; // ok + * Y : 'foo' -> popMode(A); // error 151 (the popMode command does not take an argument) * </pre> * * @since 4.1 @@ -825,8 +825,8 @@ public enum ErrorType { * public static final int CUSTOM = HIDDEN + 1; * } * - * X : 'foo' -> channel(HIDDEN); // ok - * Y : 'bar' -> channel(CUSTOM); // warning 155 + * X : 'foo' -> channel(HIDDEN); // ok + * Y : 'bar' -> channel(CUSTOM); // warning 155 * </pre> * * @since 4.2 @@ -891,12 +891,12 @@ public enum ErrorType { * <p>The following rule produces this warning.</p> * * <pre> - * X1 : 'x' -> more // ok + * X1 : 'x' -> more // ok * ; * Y1 : 'x' {more();} // ok * ; * fragment - * X2 : 'x' -> more // warning 158 + * X2 : 'x' -> more // warning 158 * ; * fragment * Y2 : 'x' {more();} // warning 158 diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index 5632ab4..89e2a26 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -101,6 +101,7 @@ public class Grammar implements AttributeResolver { public static final Set<String> parserOptions = new HashSet<String>(); static { parserOptions.add("superClass"); + parserOptions.add("contextSuperClass"); parserOptions.add("TokenLabelType"); parserOptions.add("tokenVocab"); parserOptions.add("language"); @@ -114,14 +115,14 @@ public class Grammar implements AttributeResolver { public static final Set<String> LexerBlockOptions = new HashSet<String>(); - /** Legal options for rule refs like id<key=value> */ + /** Legal options for rule refs like id<key=value> */ public static final Set<String> ruleRefOptions = new HashSet<String>(); static { ruleRefOptions.add(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME); ruleRefOptions.add(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME); } - /** Legal options for terminal refs like ID<assoc=right> */ + /** Legal options for terminal refs like ID<assoc=right> */ public static final Set<String> tokenOptions = new HashSet<String>(); static { tokenOptions.add("assoc"); @@ -553,15 +554,6 @@ public class Grammar implements AttributeResolver { public List<Grammar> getImportedGrammars() { return importedGrammars; } - /** Get delegates below direct delegates of g - public List<Grammar> getIndirectDelegates(Grammar g) { - List<Grammar> direct = getDirectDelegates(g); - List<Grammar> delegates = getDelegates(g); - delegates.removeAll(direct); - return delegates; - } -*/ - public LexerGrammar getImplicitLexer() { return implicitLexer; } diff --git a/tool/src/org/antlr/v4/tool/GrammarInterpreterRuleContext.java b/tool/src/org/antlr/v4/tool/GrammarInterpreterRuleContext.java index 8457a6a..936b8dc 100644 --- a/tool/src/org/antlr/v4/tool/GrammarInterpreterRuleContext.java +++ b/tool/src/org/antlr/v4/tool/GrammarInterpreterRuleContext.java @@ -54,4 +54,15 @@ public class GrammarInterpreterRuleContext extends InterpreterRuleContext { public void setOuterAltNum(int outerAltNum) { this.outerAltNum = outerAltNum; } + + @Override + public int getAltNumber() { + // override here and called old functionality; makes it backward compatible vs changing names + return getOuterAltNum(); + } + + @Override + public void setAltNumber(int altNumber) { + setOuterAltNum(altNumber); + } } diff --git a/tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java b/tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java index 9361b40..1e77d57 100644 --- a/tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java +++ b/tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java @@ -151,7 +151,7 @@ public class GrammarParserInterpreter extends ParserInterpreter { * it's simple. Set decisionStatesThatSetOuterAltNumInContext * indicates which decision states should set the outer alternative number. * - * Left recursive rules are much more complicated to deal with: + * <p>Left recursive rules are much more complicated to deal with: * there is typically a decision for the primary alternatives and a * decision to choose between the recursive operator alternatives. * For example, the following left recursive rule has two primary and 2 diff --git a/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java b/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java index 6759e20..9668838 100644 --- a/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java +++ b/tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java @@ -166,11 +166,11 @@ public class GrammarTransformPipeline { /** Merge all the rules, token definitions, and named actions from imported grammars into the root grammar tree. Perform: - (tokens { X (= Y 'y')) + (tokens { Z ) -> (tokens { X (= Y 'y') Z) + (tokens { X (= Y 'y')) + (tokens { Z ) -> (tokens { X (= Y 'y') Z) - (@ members {foo}) + (@ members {bar}) -> (@ members {foobar}) + (@ members {foo}) + (@ members {bar}) -> (@ members {foobar}) - (RULES (RULE x y)) + (RULES (RULE z)) -> (RULES (RULE x y z)) + (RULES (RULE x y)) + (RULES (RULE z)) -> (RULES (RULE x y z)) Rules in root prevent same rule from being appended to RULES node. @@ -322,7 +322,7 @@ public class GrammarTransformPipeline { * We'll have this Grammar share token symbols later; don't generate * tokenVocab or tokens{} section. Copy over named actions. * - * Side-effects: it removes children from GRAMMAR & RULES nodes + * Side-effects: it removes children from GRAMMAR & RULES nodes * in combined AST. Anything cut out is dup'd before * adding to lexer to avoid "who's ur daddy" issues */ diff --git a/tool/src/org/antlr/v4/tool/LeftRecursiveRule.java b/tool/src/org/antlr/v4/tool/LeftRecursiveRule.java index 1d3b990..ad06e34 100644 --- a/tool/src/org/antlr/v4/tool/LeftRecursiveRule.java +++ b/tool/src/org/antlr/v4/tool/LeftRecursiveRule.java @@ -136,7 +136,7 @@ public class LeftRecursiveRule extends Rule { return alts; } - /** Get -> labels from those alts we deleted for left-recursive rules. */ + /** Get -> labels from those alts we deleted for left-recursive rules. */ @Override public Map<String, List<Pair<Integer, AltAST>>> getAltLabels() { Map<String, List<Pair<Integer, AltAST>>> labels = new HashMap<String, List<Pair<Integer, AltAST>>>(); |