+ * 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 \"}.
+ *
+ *
+ * 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
+ * around.
+ *
+ */
+ public String getTargetStringLiteralFromANTLRStringLiteral(
+ CodeGenerator generator,
+ String literal,
+ boolean addQuotes)
+ {
+ StringBuilder sb = new StringBuilder();
+ String is = literal;
+
+ if ( addQuotes ) sb.append('"');
+
+ for (int i = 1; i < is.length() -1; i++) {
+ if (is.charAt(i) == '\\') {
+ // Anything escaped is what it is! We assume that
+ // people know how to escape characters correctly. However
+ // we catch anything that does not need an escape in Java (which
+ // is what the default implementation is dealing with and remove
+ // the escape. The C target does this for instance.
+ //
+ switch (is.charAt(i+1)) {
+ // Pass through any escapes that Java also needs
+ //
+ case '"':
+ case 'n':
+ case 'r':
+ case 't':
+ case 'b':
+ case 'f':
+ case '\\':
+ // Pass the escape through
+ sb.append('\\');
+ break;
+
+ case 'u': // Assume unnnn
+ // Pass the escape through as double \\
+ // so that Java leaves as \u0000 string not char
+ sb.append('\\');
+ sb.append('\\');
+ break;
+
+ default:
+ // Remove the escape by virtue of not adding it here
+ // Thus \' becomes ' and so on
+ break;
+ }
+
+ // Go past the \ character
+ i++;
+ } else {
+ // Characters that don't need \ in ANTLR 'strings' but do in Java
+ if (is.charAt(i) == '"') {
+ // We need to escape " in Java
+ sb.append('\\');
+ }
+ }
+ // Add in the next character, which may have been escaped
+ sb.append(is.charAt(i));
+ }
+
+ if ( addQuotes ) sb.append('"');
+
+ return sb.toString();
+ }
+
+ /** Assume 16-bit char */
+ public String encodeIntAsCharEscape(int v) {
+ if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) {
+ throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v));
+ }
+
+ if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) {
+ return targetCharValueEscape[v];
+ }
+
+ if (v >= 0x20 && v < 127 && (!Character.isDigit(v) || v == '8' || v == '9')) {
+ return String.valueOf((char)v);
+ }
+
+ if ( v>=0 && v<=127 ) {
+ String oct = Integer.toOctalString(v);
+ return "\\"+ oct;
+ }
+
+ String hex = Integer.toHexString(v|0x10000).substring(1,5);
+ return "\\u"+hex;
+ }
+
+ public String getLoopLabel(GrammarAST ast) {
+ return "loop"+ ast.token.getTokenIndex();
+ }
+
+ public String getLoopCounter(GrammarAST ast) {
+ return "cnt"+ ast.token.getTokenIndex();
+ }
+
+ public String getListLabel(String label) {
+ ST st = getTemplates().getInstanceOf("ListLabelName");
+ st.add("label", label);
+ return st.render();
+ }
+
+ public String getRuleFunctionContextStructName(Rule r) {
+ if ( r.g.isLexer() ) {
+ return getTemplates().getInstanceOf("LexerRuleContext").render();
+ }
+ return Utils.capitalize(r.name)+getTemplates().getInstanceOf("RuleContextNameSuffix").render();
+ }
+
+ public String getAltLabelContextStructName(String label) {
+ return Utils.capitalize(label)+getTemplates().getInstanceOf("RuleContextNameSuffix").render();
+ }
+
+ /** If we know which actual function, we can provide the actual ctx type.
+ * This will contain implicit labels etc... From outside, though, we
+ * see only ParserRuleContext unless there are externally visible stuff
+ * like args, locals, explicit labels, etc...
+ */
+ public String getRuleFunctionContextStructName(RuleFunction function) {
+ Rule r = function.rule;
+ if ( r.g.isLexer() ) {
+ return getTemplates().getInstanceOf("LexerRuleContext").render();
+ }
+ return Utils.capitalize(r.name)+getTemplates().getInstanceOf("RuleContextNameSuffix").render();
+ }
+
+ // should be same for all refs to same token like ctx.ID within single rule function
+ // for literals like 'while', we gen _s
+ public String getImplicitTokenLabel(String tokenName) {
+ ST st = getTemplates().getInstanceOf("ImplicitTokenLabel");
+ int ttype = getCodeGenerator().g.getTokenType(tokenName);
+ if ( tokenName.startsWith("'") ) {
+ return "s"+ttype;
+ }
+ String text = getTokenTypeAsTargetLabel(getCodeGenerator().g, ttype);
+ st.add("tokenName", text);
+ return st.render();
+ }
+
+ // x=(A|B)
+ public String getImplicitSetLabel(String id) {
+ ST st = getTemplates().getInstanceOf("ImplicitSetLabel");
+ st.add("id", id);
+ return st.render();
+ }
+
+ public String getImplicitRuleLabel(String ruleName) {
+ ST st = getTemplates().getInstanceOf("ImplicitRuleLabel");
+ st.add("ruleName", ruleName);
+ return st.render();
+ }
+
+ public String getElementListName(String name) {
+ ST st = getTemplates().getInstanceOf("ElementListName");
+ st.add("elemName", getElementName(name));
+ return st.render();
+ }
+
+ public String getElementName(String name) {
+ if (".".equals(name)) {
+ return "_wild";
+ }
+
+ if ( getCodeGenerator().g.getRule(name)!=null ) return name;
+ int ttype = getCodeGenerator().g.getTokenType(name);
+ if ( ttype==Token.INVALID_TYPE ) return name;
+ return getTokenTypeAsTargetLabel(getCodeGenerator().g, ttype);
+ }
+
+ /**
+ * Gets the maximum number of 16-bit unsigned integers that can be encoded
+ * in a single segment of the serialized ATN.
+ *
+ * @see SerializedATN#getSegments
+ *
+ * @return the serialized ATN segment limit
+ */
+ public int getSerializedATNSegmentLimit() {
+ return Integer.MAX_VALUE;
+ }
+
+ /** How many bits should be used to do inline token type tests? Java assumes
+ * a 64-bit word for bitsets. Must be a valid wordsize for your target like
+ * 8, 16, 32, 64, etc...
+ *
+ * @since 4.5
+ */
+ public int getInlineTestSetWordSize() { return 64; }
+
+ public boolean grammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ switch (idNode.getParent().getType()) {
+ case ANTLRParser.ASSIGN:
+ switch (idNode.getParent().getParent().getType()) {
+ case ANTLRParser.ELEMENT_OPTIONS:
+ case ANTLRParser.OPTIONS:
+ return false;
+
+ default:
+ break;
+ }
+
+ break;
+
+ case ANTLRParser.AT:
+ case ANTLRParser.ELEMENT_OPTIONS:
+ return false;
+
+ case ANTLRParser.LEXER_ACTION_CALL:
+ if (idNode.getChildIndex() == 0) {
+ // first child is the command name which is part of the ANTLR language
+ return false;
+ }
+
+ // arguments to the command should be checked
+ break;
+
+ default:
+ break;
+ }
+
+ return visibleGrammarSymbolCausesIssueInGeneratedCode(idNode);
+ }
+
+ protected abstract boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode);
+
+ public boolean templatesExist() {
+ String groupFileName = CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION;
+ STGroup result = null;
+ try {
+ result = new STGroupFile(groupFileName);
+ }
+ catch (IllegalArgumentException iae) {
+ result = null;
+ }
+ return result!=null;
+ }
+
+
+ protected STGroup loadTemplates() {
+ String groupFileName = CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION;
+ STGroup result = null;
+ try {
+ result = new STGroupFile(groupFileName);
+ }
+ catch (IllegalArgumentException iae) {
+ gen.tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES,
+ iae,
+ language);
+ }
+ if ( result==null ) return null;
+ result.registerRenderer(Integer.class, new NumberRenderer());
+ result.registerRenderer(String.class, new StringRenderer());
+ result.setListener(new STErrorListener() {
+ @Override
+ public void compileTimeError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void runTimeError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void IOError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void internalError(STMessage msg) {
+ reportError(msg);
+ }
+
+ private void reportError(STMessage msg) {
+ getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
+ }
+ });
+
+ return result;
+ }
+
+ /**
+ * @since 4.3
+ */
+ public boolean wantsBaseListener() {
+ return true;
+ }
+
+ /**
+ * @since 4.3
+ */
+ public boolean wantsBaseVisitor() {
+ return true;
+ }
+
+ /**
+ * @since 4.3
+ */
+ public boolean supportsOverloadedMethods() {
+ return true;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/Wildcard.java b/tool/src/org/antlr/v4/codegen/Wildcard.java
new file mode 100644
index 0000000..5cff64e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/Wildcard.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen;
+
+import org.antlr.v4.codegen.model.MatchToken;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+public class Wildcard extends MatchToken {
+ public Wildcard(OutputModelFactory factory, GrammarAST ast) {
+ super(factory, ast);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Action.java b/tool/src/org/antlr/v4/codegen/model/Action.java
new file mode 100644
index 0000000..0b9c692
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Action.java
@@ -0,0 +1,83 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.runtime.CommonToken;
+import org.antlr.v4.codegen.ActionTranslator;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.chunk.ActionChunk;
+import org.antlr.v4.codegen.model.chunk.ActionTemplate;
+import org.antlr.v4.codegen.model.chunk.ActionText;
+import org.antlr.v4.codegen.model.decl.StructDecl;
+import org.antlr.v4.parse.ANTLRParser;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.stringtemplate.v4.ST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** */
+public class Action extends RuleElement {
+ @ModelElement public List chunks;
+
+ public Action(OutputModelFactory factory, ActionAST ast) {
+ super(factory,ast);
+ RuleFunction rf = factory.getCurrentRuleFunction();
+ if (ast != null) {
+ chunks = ActionTranslator.translateAction(factory, rf, ast.token, ast);
+ }
+ else {
+ chunks = new ArrayList();
+ }
+ //System.out.println("actions="+chunks);
+ }
+
+ public Action(OutputModelFactory factory, StructDecl ctx, String action) {
+ super(factory,null);
+ ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
+ RuleFunction rf = factory.getCurrentRuleFunction();
+ if ( rf!=null ) { // we can translate
+ ast.resolver = rf.rule;
+ chunks = ActionTranslator.translateActionChunk(factory, rf, action, ast);
+ }
+ else {
+ chunks = new ArrayList();
+ chunks.add(new ActionText(ctx, action));
+ }
+ }
+
+ public Action(OutputModelFactory factory, StructDecl ctx, ST actionST) {
+ super(factory, null);
+ chunks = new ArrayList();
+ chunks.add(new ActionTemplate(ctx, actionST));
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java
new file mode 100644
index 0000000..066b858
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java
@@ -0,0 +1,46 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.Decl;
+
+/** */
+public class AddToLabelList extends SrcOp {
+ public Decl label;
+ public String listName;
+
+ public AddToLabelList(OutputModelFactory factory, String listName, Decl label) {
+ super(factory);
+ this.label = label;
+ this.listName = listName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/AltBlock.java b/tool/src/org/antlr/v4/codegen/model/AltBlock.java
new file mode 100644
index 0000000..b3459e9
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/AltBlock.java
@@ -0,0 +1,51 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.BlockStartState;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+public class AltBlock extends Choice {
+// @ModelElement public ThrowNoViableAlt error;
+
+ public AltBlock(OutputModelFactory factory,
+ GrammarAST blkOrEbnfRootAST,
+ List alts)
+ {
+ super(factory, blkOrEbnfRootAST, alts);
+ decision = ((BlockStartState)blkOrEbnfRootAST.atnState).decision;
+ // interp.predict() throws exception
+// this.error = new ThrowNoViableAlt(factory, blkOrEbnfRootAST, null);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ArgAction.java b/tool/src/org/antlr/v4/codegen/model/ArgAction.java
new file mode 100644
index 0000000..38ef4d9
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ArgAction.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.ActionAST;
+
+public class ArgAction extends Action {
+ /** Context type of invoked rule */
+ public String ctxType;
+ public ArgAction(OutputModelFactory factory, ActionAST ast, String ctxType) {
+ super(factory, ast);
+ this.ctxType = ctxType;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/BaseListenerFile.java b/tool/src/org/antlr/v4/codegen/model/BaseListenerFile.java
new file mode 100644
index 0000000..505d505
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/BaseListenerFile.java
@@ -0,0 +1,38 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class BaseListenerFile extends ListenerFile {
+ public BaseListenerFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/BaseVisitorFile.java b/tool/src/org/antlr/v4/codegen/model/BaseVisitorFile.java
new file mode 100644
index 0000000..62e669b
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/BaseVisitorFile.java
@@ -0,0 +1,38 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class BaseVisitorFile extends VisitorFile {
+ public BaseVisitorFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/CaptureNextToken.java b/tool/src/org/antlr/v4/codegen/model/CaptureNextToken.java
new file mode 100644
index 0000000..63dd3db
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/CaptureNextToken.java
@@ -0,0 +1,41 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class CaptureNextToken extends SrcOp {
+ public String varName;
+ public CaptureNextToken(OutputModelFactory factory, String varName) {
+ super(factory);
+ this.varName = varName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/CaptureNextTokenType.java b/tool/src/org/antlr/v4/codegen/model/CaptureNextTokenType.java
new file mode 100644
index 0000000..ffb4cfc
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/CaptureNextTokenType.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** */
+public class CaptureNextTokenType extends SrcOp {
+ public String varName;
+ public CaptureNextTokenType(OutputModelFactory factory, String varName) {
+ super(factory);
+ this.varName = varName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Choice.java b/tool/src/org/antlr/v4/codegen/model/Choice.java
new file mode 100644
index 0000000..bef2c45
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Choice.java
@@ -0,0 +1,97 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.Decl;
+import org.antlr.v4.codegen.model.decl.TokenTypeDecl;
+import org.antlr.v4.misc.Utils;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** The class hierarchy underneath SrcOp is pretty deep but makes sense that,
+ * for example LL1StarBlock is a kind of LL1Loop which is a kind of Choice.
+ * The problem is it's impossible to figure
+ * out how to construct one of these deeply nested objects because of the
+ * long super constructor call chain. Instead, I decided to in-line all of
+ * this and then look for opportunities to re-factor code into functions.
+ * It makes sense to use a class hierarchy to share data fields, but I don't
+ * think it makes sense to factor code using super constructors because
+ * it has too much work to do.
+ */
+public abstract class Choice extends RuleElement {
+ public int decision = -1;
+ public Decl label;
+
+ @ModelElement public List alts;
+ @ModelElement public List preamble = new ArrayList();
+
+ public Choice(OutputModelFactory factory,
+ GrammarAST blkOrEbnfRootAST,
+ List alts)
+ {
+ super(factory, blkOrEbnfRootAST);
+ this.alts = alts;
+ }
+
+ public void addPreambleOp(SrcOp op) {
+ preamble.add(op);
+ }
+
+ public List getAltLookaheadAsStringLists(IntervalSet[] altLookSets) {
+ List altLook = new ArrayList();
+ for (IntervalSet s : altLookSets) {
+ altLook.add(factory.getGenerator().getTarget().getTokenTypesAsTargetLabels(factory.getGrammar(), s.toArray()));
+ }
+ return altLook;
+ }
+
+ public TestSetInline addCodeForLookaheadTempVar(IntervalSet look) {
+ List testOps = factory.getLL1Test(look, ast);
+ TestSetInline expr = Utils.find(testOps, TestSetInline.class);
+ if (expr != null) {
+ Decl d = new TokenTypeDecl(factory, expr.varName);
+ factory.getCurrentRuleFunction().addLocalDecl(d);
+ CaptureNextTokenType nextType = new CaptureNextTokenType(factory,expr.varName);
+ addPreambleOp(nextType);
+ }
+ return expr;
+ }
+
+ public ThrowNoViableAlt getThrowNoViableAlt(OutputModelFactory factory,
+ GrammarAST blkAST,
+ IntervalSet expecting) {
+ return new ThrowNoViableAlt(factory, blkAST, expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/CodeBlockForAlt.java b/tool/src/org/antlr/v4/codegen/model/CodeBlockForAlt.java
new file mode 100644
index 0000000..7c2c8b7
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/CodeBlockForAlt.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.CodeBlock;
+
+/** Contains Rewrite block (usually as last op) */
+public class CodeBlockForAlt extends CodeBlock {
+
+ public CodeBlockForAlt(OutputModelFactory factory) {
+ super(factory);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/CodeBlockForOuterMostAlt.java b/tool/src/org/antlr/v4/codegen/model/CodeBlockForOuterMostAlt.java
new file mode 100644
index 0000000..856d15a
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/CodeBlockForOuterMostAlt.java
@@ -0,0 +1,54 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Alternative;
+
+/** The code associated with the outermost alternative of a rule.
+ * Sometimes we might want to treat them differently in the
+ * code generation.
+ */
+public class CodeBlockForOuterMostAlt extends CodeBlockForAlt {
+ /**
+ * The label for the alternative; or null if the alternative is not labeled.
+ */
+ public String altLabel;
+ /**
+ * The alternative.
+ */
+ public Alternative alt;
+
+ public CodeBlockForOuterMostAlt(OutputModelFactory factory, Alternative alt) {
+ super(factory);
+ this.alt = alt;
+ altLabel = alt.ast.altLabel!=null ? alt.ast.altLabel.getText() : null;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/DispatchMethod.java b/tool/src/org/antlr/v4/codegen/model/DispatchMethod.java
new file mode 100644
index 0000000..ab119f6
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/DispatchMethod.java
@@ -0,0 +1,38 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class DispatchMethod extends OutputModelObject {
+ public DispatchMethod(OutputModelFactory factory) {
+ super(factory);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ElementFrequenciesVisitor.java b/tool/src/org/antlr/v4/codegen/model/ElementFrequenciesVisitor.java
new file mode 100644
index 0000000..2226d37
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ElementFrequenciesVisitor.java
@@ -0,0 +1,166 @@
+package org.antlr.v4.codegen.model;
+
+import org.antlr.runtime.tree.TreeNodeStream;
+import org.antlr.v4.misc.FrequencySet;
+import org.antlr.v4.misc.MutableInt;
+import org.antlr.v4.parse.GrammarTreeVisitor;
+import org.antlr.v4.tool.ErrorManager;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.AltAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.antlr.v4.tool.ast.TerminalAST;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Map;
+
+public class ElementFrequenciesVisitor extends GrammarTreeVisitor {
+ final Deque> frequencies;
+
+ public ElementFrequenciesVisitor(TreeNodeStream input) {
+ super(input);
+ frequencies = new ArrayDeque>();
+ frequencies.push(new FrequencySet());
+ }
+
+ /** During code gen, we can assume tree is in good shape */
+ @Override
+ public ErrorManager getErrorManager() { return super.getErrorManager(); }
+
+ /*
+ * Common
+ */
+
+ /**
+ * Generate a frequency set as the union of two input sets. If an
+ * element is contained in both sets, the value for the output will be
+ * the maximum of the two input values.
+ *
+ * @param a The first set.
+ * @param b The second set.
+ * @return The union of the two sets, with the maximum value chosen
+ * whenever both sets contain the same key.
+ */
+ protected static FrequencySet combineMax(FrequencySet a, FrequencySet b) {
+ FrequencySet result = combineAndClip(a, b, 1);
+ for (Map.Entry entry : a.entrySet()) {
+ result.get(entry.getKey()).v = entry.getValue().v;
+ }
+
+ for (Map.Entry entry : b.entrySet()) {
+ MutableInt slot = result.get(entry.getKey());
+ slot.v = Math.max(slot.v, entry.getValue().v);
+ }
+
+ return result;
+ }
+
+ /**
+ * Generate a frequency set as the union of two input sets, with the
+ * values clipped to a specified maximum value. If an element is
+ * contained in both sets, the value for the output, prior to clipping,
+ * will be the sum of the two input values.
+ *
+ * @param a The first set.
+ * @param b The second set.
+ * @param clip The maximum value to allow for any output.
+ * @return The sum of the two sets, with the individual elements clipped
+ * to the maximum value gived by {@code clip}.
+ */
+ protected static FrequencySet combineAndClip(FrequencySet a, FrequencySet b, int clip) {
+ FrequencySet result = new FrequencySet();
+ for (Map.Entry entry : a.entrySet()) {
+ for (int i = 0; i < entry.getValue().v; i++) {
+ result.add(entry.getKey());
+ }
+ }
+
+ for (Map.Entry entry : b.entrySet()) {
+ for (int i = 0; i < entry.getValue().v; i++) {
+ result.add(entry.getKey());
+ }
+ }
+
+ for (Map.Entry entry : result.entrySet()) {
+ entry.getValue().v = Math.min(entry.getValue().v, clip);
+ }
+
+ return result;
+ }
+
+ @Override
+ public void tokenRef(TerminalAST ref) {
+ frequencies.peek().add(ref.getText());
+ }
+
+ @Override
+ public void ruleRef(GrammarAST ref, ActionAST arg) {
+ frequencies.peek().add(ref.getText());
+ }
+
+ /*
+ * Parser rules
+ */
+
+ @Override
+ protected void enterAlternative(AltAST tree) {
+ frequencies.push(new FrequencySet());
+ }
+
+ @Override
+ protected void exitAlternative(AltAST tree) {
+ frequencies.push(combineMax(frequencies.pop(), frequencies.pop()));
+ }
+
+ @Override
+ protected void enterElement(GrammarAST tree) {
+ frequencies.push(new FrequencySet());
+ }
+
+ @Override
+ protected void exitElement(GrammarAST tree) {
+ frequencies.push(combineAndClip(frequencies.pop(), frequencies.pop(), 2));
+ }
+
+ @Override
+ protected void exitSubrule(GrammarAST tree) {
+ if (tree.getType() == CLOSURE || tree.getType() == POSITIVE_CLOSURE) {
+ for (Map.Entry entry : frequencies.peek().entrySet()) {
+ entry.getValue().v = 2;
+ }
+ }
+ }
+
+ /*
+ * Lexer rules
+ */
+
+ @Override
+ protected void enterLexerAlternative(GrammarAST tree) {
+ frequencies.push(new FrequencySet());
+ }
+
+ @Override
+ protected void exitLexerAlternative(GrammarAST tree) {
+ frequencies.push(combineMax(frequencies.pop(), frequencies.pop()));
+ }
+
+ @Override
+ protected void enterLexerElement(GrammarAST tree) {
+ frequencies.push(new FrequencySet());
+ }
+
+ @Override
+ protected void exitLexerElement(GrammarAST tree) {
+ frequencies.push(combineAndClip(frequencies.pop(), frequencies.pop(), 2));
+ }
+
+ @Override
+ protected void exitLexerSubrule(GrammarAST tree) {
+ if (tree.getType() == CLOSURE || tree.getType() == POSITIVE_CLOSURE) {
+ for (Map.Entry entry : frequencies.peek().entrySet()) {
+ entry.getValue().v = 2;
+ }
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ExceptionClause.java b/tool/src/org/antlr/v4/codegen/model/ExceptionClause.java
new file mode 100644
index 0000000..3009a7d
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ExceptionClause.java
@@ -0,0 +1,48 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.ActionAST;
+
+public class ExceptionClause extends SrcOp {
+ @ModelElement public Action catchArg;
+ @ModelElement public Action catchAction;
+
+ public ExceptionClause(OutputModelFactory factory,
+ ActionAST catchArg,
+ ActionAST catchAction)
+ {
+ super(factory, catchArg);
+ this.catchArg = new Action(factory, catchArg);
+ this.catchAction = new Action(factory, catchAction);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/InvokeRule.java b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java
new file mode 100644
index 0000000..70c9f29
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java
@@ -0,0 +1,105 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.ActionTranslator;
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.ParserFactory;
+import org.antlr.v4.codegen.model.chunk.ActionChunk;
+import org.antlr.v4.codegen.model.decl.Decl;
+import org.antlr.v4.codegen.model.decl.RuleContextDecl;
+import org.antlr.v4.codegen.model.decl.RuleContextListDecl;
+import org.antlr.v4.parse.ANTLRParser;
+import org.antlr.v4.runtime.atn.RuleTransition;
+import org.antlr.v4.runtime.misc.OrderedHashSet;
+import org.antlr.v4.tool.Rule;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** */
+public class InvokeRule extends RuleElement implements LabeledOp {
+ public String name;
+ public OrderedHashSet labels = new OrderedHashSet(); // TODO: should need just 1
+ public String ctxName;
+
+ @ModelElement public List argExprsChunks;
+
+ public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST) {
+ super(factory, ast);
+ if ( ast.atnState!=null ) {
+ RuleTransition ruleTrans = (RuleTransition)ast.atnState.transition(0);
+ stateNumber = ast.atnState.stateNumber;
+ }
+
+ this.name = ast.getText();
+ CodeGenerator gen = factory.getGenerator();
+ Rule r = factory.getGrammar().getRule(name);
+ ctxName = gen.getTarget().getRuleFunctionContextStructName(r);
+
+ // TODO: move to factory
+ RuleFunction rf = factory.getCurrentRuleFunction();
+ if ( labelAST!=null ) {
+ // for x=r, define x and list_x
+ String label = labelAST.getText();
+ if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
+ factory.defineImplicitLabel(ast, this);
+ String listLabel = gen.getTarget().getListLabel(label);
+ RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName);
+ rf.addContextDecl(ast.getAltLabel(), l);
+ }
+ else {
+ RuleContextDecl d = new RuleContextDecl(factory,label,ctxName);
+ labels.add(d);
+ rf.addContextDecl(ast.getAltLabel(), d);
+ }
+ }
+
+ ActionAST arg = (ActionAST)ast.getFirstChildWithType(ANTLRParser.ARG_ACTION);
+ if ( arg != null ) {
+ argExprsChunks = ActionTranslator.translateAction(factory, rf, arg.token, arg);
+ }
+
+ // If action refs rule as rulename not label, we need to define implicit label
+ if ( factory.getCurrentOuterMostAlt().ruleRefsInActions.containsKey(ast.getText()) ) {
+ String label = gen.getTarget().getImplicitRuleLabel(ast.getText());
+ RuleContextDecl d = new RuleContextDecl(factory,label,ctxName);
+ labels.add(d);
+ rf.addContextDecl(ast.getAltLabel(), d);
+ }
+ }
+
+ @Override
+ public List getLabels() {
+ return labels.elements();
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1AltBlock.java b/tool/src/org/antlr/v4/codegen/model/LL1AltBlock.java
new file mode 100644
index 0000000..1bac9a4
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1AltBlock.java
@@ -0,0 +1,53 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.DecisionState;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** (A | B | C) */
+public class LL1AltBlock extends LL1Choice {
+ public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, List alts) {
+ super(factory, blkAST, alts);
+ this.decision = ((DecisionState)blkAST.atnState).decision;
+
+ /** Lookahead for each alt 1..n */
+ IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
+ altLook = getAltLookaheadAsStringLists(altLookSets);
+
+ IntervalSet expecting = IntervalSet.or(altLookSets); // combine alt sets
+ this.error = getThrowNoViableAlt(factory, blkAST, expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1Choice.java b/tool/src/org/antlr/v4/codegen/model/LL1Choice.java
new file mode 100644
index 0000000..3153ad9
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1Choice.java
@@ -0,0 +1,48 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+public abstract class LL1Choice extends Choice {
+ /** Token names for each alt 0..n-1 */
+ public List altLook;
+ @ModelElement public ThrowNoViableAlt error;
+
+ public LL1Choice(OutputModelFactory factory, GrammarAST blkAST,
+ List alts)
+ {
+ super(factory, blkAST, alts);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1Loop.java b/tool/src/org/antlr/v4/codegen/model/LL1Loop.java
new file mode 100644
index 0000000..6cd0779
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1Loop.java
@@ -0,0 +1,71 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** */
+public abstract class LL1Loop extends Choice {
+ /** The state associated wih the (A|B|...) block not loopback, which
+ * is super.stateNumber
+ */
+ public int blockStartStateNumber;
+ public int loopBackStateNumber;
+
+ @ModelElement public OutputModelObject loopExpr;
+ @ModelElement public List iteration;
+
+ public LL1Loop(OutputModelFactory factory,
+ GrammarAST blkAST,
+ List alts)
+ {
+ super(factory, blkAST, alts);
+ }
+
+ public void addIterationOp(SrcOp op) {
+ if ( iteration==null ) iteration = new ArrayList();
+ iteration.add(op);
+ }
+
+ public SrcOp addCodeForLoopLookaheadTempVar(IntervalSet look) {
+ TestSetInline expr = addCodeForLookaheadTempVar(look);
+ if (expr != null) {
+ CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
+ addIterationOp(nextType);
+ }
+ return expr;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlock.java b/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlock.java
new file mode 100644
index 0000000..74ca312
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlock.java
@@ -0,0 +1,47 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** An optional block is just an alternative block where the last alternative
+ * is epsilon. The analysis takes care of adding to the empty alternative.
+ *
+ * (A | B | C)?
+ */
+public class LL1OptionalBlock extends LL1AltBlock {
+ public LL1OptionalBlock(OutputModelFactory factory, GrammarAST blkAST, List alts) {
+ super(factory, blkAST, alts);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlockSingleAlt.java b/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlockSingleAlt.java
new file mode 100644
index 0000000..eacfd5a
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1OptionalBlockSingleAlt.java
@@ -0,0 +1,65 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.DecisionState;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** (A B C)? */
+public class LL1OptionalBlockSingleAlt extends LL1Choice {
+ @ModelElement public SrcOp expr;
+ @ModelElement public List followExpr; // might not work in template if size>1
+
+ public LL1OptionalBlockSingleAlt(OutputModelFactory factory,
+ GrammarAST blkAST,
+ List alts)
+ {
+ super(factory, blkAST, alts);
+ this.decision = ((DecisionState)blkAST.atnState).decision;
+
+ /** Lookahead for each alt 1..n */
+// IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
+ IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
+ altLook = getAltLookaheadAsStringLists(altLookSets);
+ IntervalSet look = altLookSets[0];
+ IntervalSet followLook = altLookSets[1];
+
+ IntervalSet expecting = look.or(followLook);
+ this.error = getThrowNoViableAlt(factory, blkAST, expecting);
+
+ expr = addCodeForLookaheadTempVar(look);
+ followExpr = factory.getLL1Test(followLook, blkAST);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1PlusBlockSingleAlt.java b/tool/src/org/antlr/v4/codegen/model/LL1PlusBlockSingleAlt.java
new file mode 100644
index 0000000..a7076ce
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1PlusBlockSingleAlt.java
@@ -0,0 +1,58 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.PlusBlockStartState;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.BlockAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** */
+public class LL1PlusBlockSingleAlt extends LL1Loop {
+ public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, List alts) {
+ super(factory, plusRoot, alts);
+
+ BlockAST blkAST = (BlockAST)plusRoot.getChild(0);
+ PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;
+
+ stateNumber = blkStart.loopBackState.stateNumber;
+ blockStartStateNumber = blkStart.stateNumber;
+ PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState;
+ this.decision = plus.loopBackState.decision;
+ IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
+
+ IntervalSet loopBackLook = altLookSets[0];
+ loopExpr = addCodeForLoopLookaheadTempVar(loopBackLook);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java b/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java
new file mode 100644
index 0000000..4c1bf94
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java
@@ -0,0 +1,54 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.StarLoopEntryState;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** */
+public class LL1StarBlockSingleAlt extends LL1Loop {
+ public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, List alts) {
+ super(factory, starRoot, alts);
+
+ StarLoopEntryState star = (StarLoopEntryState)starRoot.atnState;
+ loopBackStateNumber = star.loopBackState.stateNumber;
+ this.decision = star.decision;
+ IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
+ assert altLookSets.length == 2;
+ IntervalSet enterLook = altLookSets[0];
+ IntervalSet exitLook = altLookSets[1];
+ loopExpr = addCodeForLoopLookaheadTempVar(enterLook);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LabeledOp.java b/tool/src/org/antlr/v4/codegen/model/LabeledOp.java
new file mode 100644
index 0000000..26ad2d3
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LabeledOp.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.model.decl.Decl;
+
+import java.util.List;
+
+/** All the rule elements we can label like tokens, rules, sets, wildcard. */
+public interface LabeledOp {
+ public List getLabels();
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LeftRecursiveRuleFunction.java b/tool/src/org/antlr/v4/codegen/model/LeftRecursiveRuleFunction.java
new file mode 100644
index 0000000..adbc3a6
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LeftRecursiveRuleFunction.java
@@ -0,0 +1,76 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.RuleContextDecl;
+import org.antlr.v4.codegen.model.decl.RuleContextListDecl;
+import org.antlr.v4.codegen.model.decl.StructDecl;
+import org.antlr.v4.parse.ANTLRParser;
+import org.antlr.v4.runtime.misc.Pair;
+import org.antlr.v4.tool.LeftRecursiveRule;
+import org.antlr.v4.tool.Rule;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+public class LeftRecursiveRuleFunction extends RuleFunction {
+ public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r) {
+ super(factory, r);
+
+ CodeGenerator gen = factory.getGenerator();
+ // Since we delete x=lr, we have to manually add decls for all labels
+ // on left-recur refs to proper structs
+ for (Pair pair : r.leftRecursiveRuleRefLabels) {
+ GrammarAST idAST = pair.a;
+ String altLabel = pair.b;
+ String label = idAST.getText();
+ GrammarAST rrefAST = (GrammarAST)idAST.getParent().getChild(1);
+ if ( rrefAST.getType() == ANTLRParser.RULE_REF ) {
+ Rule targetRule = factory.getGrammar().getRule(rrefAST.getText());
+ String ctxName = gen.getTarget().getRuleFunctionContextStructName(targetRule);
+ RuleContextDecl d;
+ if (idAST.getParent().getType() == ANTLRParser.ASSIGN) {
+ d = new RuleContextDecl(factory, label, ctxName);
+ }
+ else {
+ d = new RuleContextListDecl(factory, label, ctxName);
+ }
+
+ StructDecl struct = ruleCtx;
+ if ( altLabelCtxs!=null ) {
+ StructDecl s = altLabelCtxs.get(altLabel);
+ if ( s!=null ) struct = s; // if alt label, use subctx
+ }
+ struct.addDecl(d); // stick in overall rule's ctx
+ }
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Lexer.java b/tool/src/org/antlr/v4/codegen/model/Lexer.java
new file mode 100644
index 0000000..ef44e4c
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Lexer.java
@@ -0,0 +1,58 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.LexerGrammar;
+import org.antlr.v4.tool.Rule;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class Lexer extends Recognizer {
+ public Map channels;
+ public LexerFile file;
+ public Collection modes;
+
+ @ModelElement public LinkedHashMap actionFuncs =
+ new LinkedHashMap();
+
+ public Lexer(OutputModelFactory factory, LexerFile file) {
+ super(factory);
+ this.file = file; // who contains us?
+
+ Grammar g = factory.getGrammar();
+ channels = new LinkedHashMap(g.channelNameToValueMap);
+ modes = ((LexerGrammar)g).modes.keySet();
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/LexerFile.java b/tool/src/org/antlr/v4/codegen/model/LexerFile.java
new file mode 100644
index 0000000..8bec073
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/LexerFile.java
@@ -0,0 +1,55 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.ast.ActionAST;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class LexerFile extends OutputFile {
+ public String genPackage; // from -package cmd-line
+ @ModelElement public Lexer lexer;
+ @ModelElement public Map namedActions;
+
+ public LexerFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ namedActions = new HashMap();
+ Grammar g = factory.getGrammar();
+ for (String name : g.namedActions.keySet()) {
+ ActionAST ast = g.namedActions.get(name);
+ namedActions.put(name, new Action(factory, ast));
+ }
+ genPackage = factory.getGrammar().tool.genPackage;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ListenerDispatchMethod.java b/tool/src/org/antlr/v4/codegen/model/ListenerDispatchMethod.java
new file mode 100644
index 0000000..9332d13
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ListenerDispatchMethod.java
@@ -0,0 +1,41 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class ListenerDispatchMethod extends DispatchMethod {
+ public boolean isEnter;
+
+ public ListenerDispatchMethod(OutputModelFactory factory, boolean isEnter) {
+ super(factory);
+ this.isEnter = isEnter;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ListenerFile.java b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java
new file mode 100644
index 0000000..99f6ebf
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java
@@ -0,0 +1,87 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.Pair;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.Rule;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.AltAST;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** A model object representing a parse tree listener file.
+ * These are the rules specific events triggered by a parse tree visitor.
+ */
+public class ListenerFile extends OutputFile {
+ public String genPackage; // from -package cmd-line
+ public String grammarName;
+ public String parserName;
+ /**
+ * The names of all listener contexts.
+ */
+ public Set listenerNames = new LinkedHashSet();
+ /**
+ * For listener contexts created for a labeled outer alternative, maps from
+ * a listener context name to the name of the rule which defines the
+ * context.
+ */
+ public Map listenerLabelRuleNames = new LinkedHashMap();
+
+ @ModelElement public Action header;
+
+ public ListenerFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ Grammar g = factory.getGrammar();
+ parserName = g.getRecognizerName();
+ grammarName = g.name;
+ for (Rule r : g.rules.values()) {
+ Map>> labels = r.getAltLabels();
+ if ( labels!=null ) {
+ for (Map.Entry>> pair : labels.entrySet()) {
+ listenerNames.add(pair.getKey());
+ listenerLabelRuleNames.put(pair.getKey(), r.name);
+ }
+ }
+ else {
+ // only add rule context if no labels
+ listenerNames.add(r.name);
+ }
+ }
+ ActionAST ast = g.namedActions.get("header");
+ if ( ast!=null ) header = new Action(factory, ast);
+ genPackage = factory.getGrammar().tool.genPackage;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Loop.java b/tool/src/org/antlr/v4/codegen/model/Loop.java
new file mode 100644
index 0000000..50fbd6f
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Loop.java
@@ -0,0 +1,60 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.antlr.v4.tool.ast.QuantifierAST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Loop extends Choice {
+ public int blockStartStateNumber;
+ public int loopBackStateNumber;
+ public final int exitAlt;
+
+ @ModelElement public List iteration;
+
+ public Loop(OutputModelFactory factory,
+ GrammarAST blkOrEbnfRootAST,
+ List alts)
+ {
+ super(factory, blkOrEbnfRootAST, alts);
+ boolean nongreedy = (blkOrEbnfRootAST instanceof QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).isGreedy();
+ exitAlt = nongreedy ? 1 : alts.size() + 1;
+ }
+
+ public void addIterationOp(SrcOp op) {
+ if ( iteration==null ) iteration = new ArrayList();
+ iteration.add(op);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/MatchNotSet.java b/tool/src/org/antlr/v4/codegen/model/MatchNotSet.java
new file mode 100644
index 0000000..8df741c
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/MatchNotSet.java
@@ -0,0 +1,41 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+public class MatchNotSet extends MatchSet {
+ public String varName = "_la";
+ public MatchNotSet(OutputModelFactory factory, GrammarAST ast) {
+ super(factory, ast);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/MatchSet.java b/tool/src/org/antlr/v4/codegen/model/MatchSet.java
new file mode 100644
index 0000000..e6d02a1
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/MatchSet.java
@@ -0,0 +1,52 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.Decl;
+import org.antlr.v4.codegen.model.decl.TokenTypeDecl;
+import org.antlr.v4.runtime.atn.SetTransition;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+public class MatchSet extends MatchToken {
+ @ModelElement public TestSetInline expr;
+ @ModelElement public CaptureNextTokenType capture;
+
+ public MatchSet(OutputModelFactory factory, GrammarAST ast) {
+ super(factory, ast);
+ SetTransition st = (SetTransition)ast.atnState.transition(0);
+ int wordSize = factory.getGenerator().getTarget().getInlineTestSetWordSize();
+ expr = new TestSetInline(factory, null, st.set, wordSize);
+ Decl d = new TokenTypeDecl(factory, expr.varName);
+ factory.getCurrentRuleFunction().addLocalDecl(d);
+ capture = new CaptureNextTokenType(factory,expr.varName);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/MatchToken.java b/tool/src/org/antlr/v4/codegen/model/MatchToken.java
new file mode 100644
index 0000000..8405e78
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/MatchToken.java
@@ -0,0 +1,63 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.Decl;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.antlr.v4.tool.ast.TerminalAST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** */
+public class MatchToken extends RuleElement implements LabeledOp {
+ public String name;
+ public int ttype;
+ public List labels = new ArrayList();
+
+ public MatchToken(OutputModelFactory factory, TerminalAST ast) {
+ super(factory, ast);
+ Grammar g = factory.getGrammar();
+ CodeGenerator gen = factory.getGenerator();
+ ttype = g.getTokenType(ast.getText());
+ name = gen.getTarget().getTokenTypeAsTargetLabel(g, ttype);
+ }
+
+ public MatchToken(OutputModelFactory factory, GrammarAST ast) {
+ super(factory, ast);
+ }
+
+ @Override
+ public List getLabels() { return labels; }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ModelElement.java b/tool/src/org/antlr/v4/codegen/model/ModelElement.java
new file mode 100644
index 0000000..0dc38c2
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ModelElement.java
@@ -0,0 +1,41 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/** Indicate field of OutputModelObject is an element to be walked by
+ * OutputModelWalker.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ModelElement {
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/OptionalBlock.java b/tool/src/org/antlr/v4/codegen/model/OptionalBlock.java
new file mode 100644
index 0000000..c1f2cb3
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/OptionalBlock.java
@@ -0,0 +1,46 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** */
+public class OptionalBlock extends AltBlock {
+ public OptionalBlock(OutputModelFactory factory,
+ GrammarAST questionAST,
+ List alts)
+ {
+ super(factory, questionAST, alts);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/OutputFile.java b/tool/src/org/antlr/v4/codegen/model/OutputFile.java
new file mode 100644
index 0000000..16b2384
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/OutputFile.java
@@ -0,0 +1,53 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.Tool;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Grammar;
+
+public abstract class OutputFile extends OutputModelObject {
+ public final String fileName;
+ public final String grammarFileName;
+ public final String ANTLRVersion;
+ public final String TokenLabelType;
+ public final String InputSymbolType;
+
+ public OutputFile(OutputModelFactory factory, String fileName) {
+ super(factory);
+ this.fileName = fileName;
+ Grammar g = factory.getGrammar();
+ grammarFileName = g.fileName;
+ ANTLRVersion = Tool.VERSION;
+ TokenLabelType = g.getOptionString("TokenLabelType");
+ InputSymbolType = TokenLabelType;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/OutputModelObject.java b/tool/src/org/antlr/v4/codegen/model/OutputModelObject.java
new file mode 100644
index 0000000..6f793be
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/OutputModelObject.java
@@ -0,0 +1,49 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public abstract class OutputModelObject {
+ public OutputModelFactory factory;
+ public GrammarAST ast;
+
+ public OutputModelObject() {}
+
+ public OutputModelObject(OutputModelFactory factory) { this(factory, null); }
+
+ public OutputModelObject(OutputModelFactory factory, GrammarAST ast) {
+ this.factory = factory;
+ this.ast = ast;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Parser.java b/tool/src/org/antlr/v4/codegen/model/Parser.java
new file mode 100644
index 0000000..770d904
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Parser.java
@@ -0,0 +1,47 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Parser extends Recognizer {
+ public ParserFile file;
+
+ @ModelElement public List funcs = new ArrayList();
+
+ public Parser(OutputModelFactory factory, ParserFile file) {
+ super(factory);
+ this.file = file; // who contains us?
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ParserFile.java b/tool/src/org/antlr/v4/codegen/model/ParserFile.java
new file mode 100644
index 0000000..28ef200
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ParserFile.java
@@ -0,0 +1,63 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.ast.ActionAST;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/** */
+public class ParserFile extends OutputFile {
+ public String genPackage; // from -package cmd-line
+ @ModelElement public Parser parser;
+ @ModelElement public Map namedActions;
+ public Boolean genListener = false;
+ public Boolean genVisitor = false;
+ public String grammarName;
+
+ public ParserFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ Grammar g = factory.getGrammar();
+ namedActions = new HashMap();
+ for (String name : g.namedActions.keySet()) {
+ ActionAST ast = g.namedActions.get(name);
+ namedActions.put(name, new Action(factory, ast));
+ }
+ genPackage = g.tool.genPackage;
+ // need the below members in the ST for Python
+ genListener = g.tool.gen_listener;
+ genVisitor = g.tool.gen_visitor;
+ grammarName = g.name;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/PlusBlock.java b/tool/src/org/antlr/v4/codegen/model/PlusBlock.java
new file mode 100644
index 0000000..a200d30
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/PlusBlock.java
@@ -0,0 +1,58 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.PlusBlockStartState;
+import org.antlr.v4.runtime.atn.PlusLoopbackState;
+import org.antlr.v4.tool.ast.BlockAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+public class PlusBlock extends Loop {
+ @ModelElement public ThrowNoViableAlt error;
+
+ public PlusBlock(OutputModelFactory factory,
+ GrammarAST plusRoot,
+ List alts)
+ {
+ super(factory, plusRoot, alts);
+ BlockAST blkAST = (BlockAST)plusRoot.getChild(0);
+ PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;
+ PlusLoopbackState loop = blkStart.loopBackState;
+ stateNumber = blkStart.loopBackState.stateNumber;
+ blockStartStateNumber = blkStart.stateNumber;
+ loopBackStateNumber = loop.stateNumber;
+ this.error = getThrowNoViableAlt(factory, plusRoot, null);
+ decision = loop.decision;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Recognizer.java b/tool/src/org/antlr/v4/codegen/model/Recognizer.java
new file mode 100644
index 0000000..7e3f95b
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Recognizer.java
@@ -0,0 +1,135 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2014 Terence Parr
+ * Copyright (c) 2014 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.CodeGenerator;
+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.Rule;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+public abstract class Recognizer extends OutputModelObject {
+ public String name;
+ public String grammarName;
+ public String grammarFileName;
+ public Map tokens;
+
+ /**
+ * @deprecated This field is provided only for compatibility with code
+ * generation targets which have not yet been updated to use
+ * {@link #literalNames} and {@link #symbolicNames}.
+ */
+ @Deprecated
+ public String[] tokenNames;
+
+ public String[] literalNames;
+ public String[] symbolicNames;
+ public Set ruleNames;
+ public Collection rules;
+ @ModelElement public ActionChunk superClass;
+
+ @ModelElement public SerializedATN atn;
+ @ModelElement public LinkedHashMap sempredFuncs =
+ new LinkedHashMap();
+
+ public Recognizer(OutputModelFactory factory) {
+ super(factory);
+
+ Grammar g = factory.getGrammar();
+ grammarFileName = new File(g.fileName).getName();
+ grammarName = g.name;
+ name = g.getRecognizerName();
+ tokens = new LinkedHashMap();
+ for (Map.Entry entry : g.tokenNameToTypeMap.entrySet()) {
+ Integer ttype = entry.getValue();
+ if ( ttype>0 ) {
+ tokens.put(entry.getKey(), ttype);
+ }
+ }
+
+ ruleNames = g.rules.keySet();
+ rules = g.rules.values();
+ atn = new SerializedATN(factory, g.atn);
+ if (g.getOptionString("superClass") != null) {
+ superClass = new ActionText(null, g.getOptionString("superClass"));
+ }
+ else {
+ superClass = null;
+ }
+
+ CodeGenerator gen = factory.getGenerator();
+ tokenNames = translateTokenStringsToTarget(g.getTokenDisplayNames(), gen);
+ literalNames = translateTokenStringsToTarget(g.getTokenLiteralNames(), gen);
+ symbolicNames = translateTokenStringsToTarget(g.getTokenSymbolicNames(), gen);
+ }
+
+ protected static String[] translateTokenStringsToTarget(String[] tokenStrings, CodeGenerator gen) {
+ String[] result = tokenStrings.clone();
+ for (int i = 0; i < tokenStrings.length; i++) {
+ result[i] = translateTokenStringToTarget(tokenStrings[i], gen);
+ }
+
+ int lastTrueEntry = result.length - 1;
+ while (lastTrueEntry >= 0 && result[lastTrueEntry] == null) {
+ lastTrueEntry --;
+ }
+
+ if (lastTrueEntry < result.length - 1) {
+ result = Arrays.copyOf(result, lastTrueEntry + 1);
+ }
+
+ return result;
+ }
+
+ protected static String translateTokenStringToTarget(String tokenName, CodeGenerator gen) {
+ if (tokenName == null) {
+ return null;
+ }
+
+ if (tokenName.charAt(0) == '\'') {
+ boolean addQuotes = false;
+ String targetString =
+ gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen, tokenName, addQuotes);
+ return "\"'" + targetString + "'\"";
+ }
+ else {
+ return gen.getTarget().getTargetStringLiteralFromString(tokenName, true);
+ }
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java
new file mode 100644
index 0000000..8e65462
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java
@@ -0,0 +1,53 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Rule;
+
+import java.util.LinkedHashMap;
+
+public class RuleActionFunction extends OutputModelObject {
+ public String name;
+ public String ctxType;
+ public int ruleIndex;
+
+ /** Map actionIndex to Action */
+ @ModelElement public LinkedHashMap actions =
+ new LinkedHashMap();
+
+ public RuleActionFunction(OutputModelFactory factory, Rule r, String ctxType) {
+ super(factory);
+ name = r.name;
+ ruleIndex = r.index;
+ this.ctxType = ctxType;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/RuleElement.java b/tool/src/org/antlr/v4/codegen/model/RuleElement.java
new file mode 100644
index 0000000..c2b2f52
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/RuleElement.java
@@ -0,0 +1,45 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+public class RuleElement extends SrcOp {
+ /** Associated ATN state for this rule elements (action, token, ruleref, ...) */
+ public int stateNumber;
+
+ public RuleElement(OutputModelFactory factory, GrammarAST ast) {
+ super(factory, ast);
+ if ( ast != null && ast.atnState!=null ) stateNumber = ast.atnState.stateNumber;
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java
new file mode 100644
index 0000000..25d6d0f
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java
@@ -0,0 +1,290 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.tree.CommonTreeNodeStream;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.AltLabelStructDecl;
+import org.antlr.v4.codegen.model.decl.AttributeDecl;
+import org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl;
+import org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl;
+import org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl;
+import org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl;
+import org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl;
+import org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl;
+import org.antlr.v4.codegen.model.decl.Decl;
+import org.antlr.v4.codegen.model.decl.StructDecl;
+import org.antlr.v4.misc.FrequencySet;
+import org.antlr.v4.misc.Utils;
+import org.antlr.v4.parse.GrammarASTAdaptor;
+import org.antlr.v4.runtime.atn.ATNState;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.runtime.misc.OrderedHashSet;
+import org.antlr.v4.runtime.misc.Pair;
+import org.antlr.v4.tool.Attribute;
+import org.antlr.v4.tool.ErrorType;
+import org.antlr.v4.tool.Rule;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.AltAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.antlr.v4.parse.ANTLRParser.RULE_REF;
+import static org.antlr.v4.parse.ANTLRParser.TOKEN_REF;
+
+/** */
+public class RuleFunction extends OutputModelObject {
+ public String name;
+ public List modifiers;
+ public String ctxType;
+ public Collection ruleLabels;
+ public Collection tokenLabels;
+ public ATNState startState;
+ public int index;
+ public Rule rule;
+ public AltLabelStructDecl[] altToContext;
+ public boolean hasLookaheadBlock;
+
+ @ModelElement public List code;
+ @ModelElement public OrderedHashSet locals; // TODO: move into ctx?
+ @ModelElement public Collection args = null;
+ @ModelElement public StructDecl ruleCtx;
+ @ModelElement public Map altLabelCtxs;
+ @ModelElement public Map namedActions;
+ @ModelElement public Action finallyAction;
+ @ModelElement public List exceptions;
+ @ModelElement public List postamble;
+
+ public RuleFunction(OutputModelFactory factory, Rule r) {
+ super(factory);
+ this.name = r.name;
+ this.rule = r;
+ if ( r.modifiers!=null && !r.modifiers.isEmpty() ) {
+ this.modifiers = new ArrayList();
+ for (GrammarAST t : r.modifiers) modifiers.add(t.getText());
+ }
+ modifiers = Utils.nodesToStrings(r.modifiers);
+
+ index = r.index;
+
+ ruleCtx = new StructDecl(factory, r);
+ altToContext = new AltLabelStructDecl[r.getOriginalNumberOfAlts()+1];
+ addContextGetters(factory, r);
+
+ if ( r.args!=null ) {
+ Collection decls = r.args.attributes.values();
+ if ( decls.size()>0 ) {
+ args = new ArrayList();
+ ruleCtx.addDecls(decls);
+ for (Attribute a : decls) {
+ args.add(new AttributeDecl(factory, a));
+ }
+ ruleCtx.ctorAttrs = args;
+ }
+ }
+ if ( r.retvals!=null ) {
+ ruleCtx.addDecls(r.retvals.attributes.values());
+ }
+ if ( r.locals!=null ) {
+ ruleCtx.addDecls(r.locals.attributes.values());
+ }
+
+ ruleLabels = r.getElementLabelNames();
+ tokenLabels = r.getTokenRefs();
+ if ( r.exceptions!=null ) {
+ exceptions = new ArrayList();
+ for (GrammarAST e : r.exceptions) {
+ ActionAST catchArg = (ActionAST)e.getChild(0);
+ ActionAST catchAction = (ActionAST)e.getChild(1);
+ exceptions.add(new ExceptionClause(factory, catchArg, catchAction));
+ }
+ }
+
+ startState = factory.getGrammar().atn.ruleToStartState[r.index];
+ }
+
+ public void addContextGetters(OutputModelFactory factory, Rule r) {
+ // Add ctx labels for elements in alts with no -> label
+ List altsNoLabels = r.getUnlabeledAltASTs();
+ if ( altsNoLabels!=null ) {
+ Set decls = getDeclsForAllElements(altsNoLabels);
+ // we know to put in rule ctx, so do it directly
+ for (Decl d : decls) ruleCtx.addDecl(d);
+ }
+
+ // make structs for -> labeled alts, define ctx labels for elements
+ altLabelCtxs = new HashMap();
+ Map>> labels = r.getAltLabels();
+ if ( labels!=null ) {
+ for (Map.Entry>> entry : labels.entrySet()) {
+ String label = entry.getKey();
+ List alts = new ArrayList();
+ for (Pair pair : entry.getValue()) {
+ alts.add(pair.b);
+ }
+
+ Set decls = getDeclsForAllElements(alts);
+ for (Pair pair : entry.getValue()) {
+ Integer altNum = pair.a;
+ altToContext[altNum] = new AltLabelStructDecl(factory, r, altNum, label);
+ if (!altLabelCtxs.containsKey(label)) {
+ altLabelCtxs.put(label, altToContext[altNum]);
+ }
+
+ // we know which ctx to put in, so do it directly
+ for (Decl d : decls) {
+ altToContext[altNum].addDecl(d);
+ }
+ }
+ }
+ }
+ }
+
+ public void fillNamedActions(OutputModelFactory factory, Rule r) {
+ if ( r.finallyAction!=null ) {
+ finallyAction = new Action(factory, r.finallyAction);
+ }
+
+ namedActions = new HashMap();
+ for (String name : r.namedActions.keySet()) {
+ ActionAST ast = r.namedActions.get(name);
+ namedActions.put(name, new Action(factory, ast));
+ }
+ }
+
+ /** for all alts, find which ref X or r needs List
+ Must see across alts. If any alt needs X or r as list, then
+ define as list.
+ */
+ public Set getDeclsForAllElements(List altASTs) {
+ Set needsList = new HashSet();
+ List allRefs = new ArrayList();
+ for (AltAST ast : altASTs) {
+ IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF);
+ List refs = ast.getNodesWithType(reftypes);
+ allRefs.addAll(refs);
+ FrequencySet altFreq = getElementFrequenciesForAlt(ast);
+ for (GrammarAST t : refs) {
+ String refLabelName = t.getText();
+ if ( altFreq.count(refLabelName)>1 ) {
+ needsList.add(refLabelName);
+ }
+ }
+ }
+ Set decls = new LinkedHashSet();
+ for (GrammarAST t : allRefs) {
+ String refLabelName = t.getText();
+ List d = getDeclForAltElement(t,
+ refLabelName,
+ needsList.contains(refLabelName));
+ decls.addAll(d);
+ }
+ return decls;
+ }
+
+ /** Given list of X and r refs in alt, compute how many of each there are */
+ protected FrequencySet getElementFrequenciesForAlt(AltAST ast) {
+ try {
+ ElementFrequenciesVisitor visitor = new ElementFrequenciesVisitor(new CommonTreeNodeStream(new GrammarASTAdaptor(), ast));
+ visitor.outerAlternative();
+ if (visitor.frequencies.size() != 1) {
+ factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR);
+ return new FrequencySet();
+ }
+
+ return visitor.frequencies.peek();
+ }
+ catch (RecognitionException ex) {
+ factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, ex);
+ return new FrequencySet();
+ }
+ }
+
+ public List getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList) {
+ List decls = new ArrayList();
+ if ( t.getType()==RULE_REF ) {
+ Rule rref = factory.getGrammar().getRule(t.getText());
+ String ctxName = factory.getGenerator().getTarget()
+ .getRuleFunctionContextStructName(rref);
+ if ( needList) {
+ if(factory.getGenerator().getTarget().supportsOverloadedMethods())
+ decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );
+ decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) );
+ }
+ else {
+ decls.add( new ContextRuleGetterDecl(factory, refLabelName, ctxName) );
+ }
+ }
+ else {
+ if ( needList ) {
+ if(factory.getGenerator().getTarget().supportsOverloadedMethods())
+ decls.add( new ContextTokenListGetterDecl(factory, refLabelName) );
+ decls.add( new ContextTokenListIndexedGetterDecl(factory, refLabelName) );
+ }
+ else {
+ decls.add( new ContextTokenGetterDecl(factory, refLabelName) );
+ }
+ }
+ return decls;
+ }
+
+ /** Add local var decl */
+ public void addLocalDecl(Decl d) {
+ if ( locals ==null ) locals = new OrderedHashSet();
+ locals.add(d);
+ d.isLocal = true;
+ }
+
+ /** Add decl to struct ctx for rule or alt if labeled */
+ public void addContextDecl(String altLabel, Decl d) {
+ CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock();
+ // if we found code blk and might be alt label, try to add to that label ctx
+ if ( alt!=null && altLabelCtxs!=null ) {
+// System.out.println(d.name+" lives in alt "+alt.alt.altNum);
+ AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel);
+ if ( altCtx!=null ) { // we have an alt ctx
+// System.out.println("ctx is "+ altCtx.name);
+ altCtx.addDecl(d);
+ return;
+ }
+ }
+ ruleCtx.addDecl(d); // stick in overall rule's ctx
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/RuleSempredFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleSempredFunction.java
new file mode 100644
index 0000000..2b1f6a1
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/RuleSempredFunction.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Rule;
+
+public class RuleSempredFunction extends RuleActionFunction {
+ public RuleSempredFunction(OutputModelFactory factory, Rule r, String ctxType) {
+ super(factory, r, ctxType);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/SemPred.java b/tool/src/org/antlr/v4/codegen/model/SemPred.java
new file mode 100644
index 0000000..605091f
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/SemPred.java
@@ -0,0 +1,97 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.ActionTranslator;
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.chunk.ActionChunk;
+import org.antlr.v4.runtime.atn.AbstractPredicateTransition;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+/** */
+public class SemPred extends Action {
+ /**
+ * The user-specified terminal option {@code fail}, if it was used and the
+ * value is a string literal. For example:
+ *
+ *
+ * {@code {pred}?}
+ */
+ public String msg;
+ /**
+ * The predicate string with { and }? stripped from the ends.
+ */
+ public String predicate;
+
+ /**
+ * The translated chunks of the user-specified terminal option {@code fail},
+ * if it was used and the value is an action. For example:
+ *
+ *
+ * {@code {pred}?}
+ */
+ @ModelElement public List failChunks;
+
+ public SemPred(OutputModelFactory factory, ActionAST ast) {
+ super(factory,ast);
+
+ assert ast.atnState != null
+ && ast.atnState.getNumberOfTransitions() == 1
+ && ast.atnState.transition(0) instanceof AbstractPredicateTransition;
+
+ GrammarAST failNode = ast.getOptionAST("fail");
+ CodeGenerator gen = factory.getGenerator();
+ predicate = ast.getText();
+ if (predicate.startsWith("{") && predicate.endsWith("}?")) {
+ predicate = predicate.substring(1, predicate.length() - 2);
+ }
+ predicate = gen.getTarget().getTargetStringLiteralFromString(predicate);
+
+ if ( failNode==null ) return;
+
+ if ( failNode instanceof ActionAST ) {
+ ActionAST failActionNode = (ActionAST)failNode;
+ RuleFunction rf = factory.getCurrentRuleFunction();
+ failChunks = ActionTranslator.translateAction(factory, rf,
+ failActionNode.token,
+ failActionNode);
+ }
+ else {
+ msg = gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen,
+ failNode.getText(),
+ true);
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/SerializedATN.java b/tool/src/org/antlr/v4/codegen/model/SerializedATN.java
new file mode 100644
index 0000000..ae64c43
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/SerializedATN.java
@@ -0,0 +1,65 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.ATN;
+import org.antlr.v4.runtime.atn.ATNSerializer;
+import org.antlr.v4.runtime.misc.IntegerList;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class SerializedATN extends OutputModelObject {
+ // TODO: make this into a kind of decl or multiple?
+ public List serialized;
+ public SerializedATN(OutputModelFactory factory, ATN atn) {
+ super(factory);
+ IntegerList data = ATNSerializer.getSerialized(atn);
+ serialized = new ArrayList(data.size());
+ for (int c : data.toArray()) {
+ String encoded = factory.getGenerator().getTarget().encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c);
+ serialized.add(encoded);
+ }
+// System.out.println(ATNSerializer.getDecoded(factory.getGrammar(), atn));
+ }
+
+ public String[][] getSegments() {
+ List segments = new ArrayList();
+ int segmentLimit = factory.getGenerator().getTarget().getSerializedATNSegmentLimit();
+ for (int i = 0; i < serialized.size(); i += segmentLimit) {
+ List currentSegment = serialized.subList(i, Math.min(i + segmentLimit, serialized.size()));
+ segments.add(currentSegment.toArray(new String[currentSegment.size()]));
+ }
+
+ return segments.toArray(new String[segments.size()][]);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/SrcOp.java b/tool/src/org/antlr/v4/codegen/model/SrcOp.java
new file mode 100644
index 0000000..d4a9834
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/SrcOp.java
@@ -0,0 +1,82 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.decl.CodeBlock;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public abstract class SrcOp extends OutputModelObject {
+ /** Used to create unique var names etc... */
+ public int uniqueID; // TODO: do we need?
+
+ /** All operations know in which block they live:
+ *
+ * CodeBlock, CodeBlockForAlt
+ *
+ * Templates might need to know block nesting level or find
+ * a specific declaration, etc...
+ */
+ public CodeBlock enclosingBlock;
+
+ public RuleFunction enclosingRuleRunction;
+
+ public SrcOp(OutputModelFactory factory) { this(factory,null); }
+ public SrcOp(OutputModelFactory factory, GrammarAST ast) {
+ super(factory,ast);
+ if ( ast!=null ) uniqueID = ast.token.getTokenIndex();
+ enclosingBlock = factory.getCurrentBlock();
+ enclosingRuleRunction = factory.getCurrentRuleFunction();
+ }
+
+ /** Walk upwards in model tree, looking for outer alt's code block */
+ public CodeBlockForOuterMostAlt getOuterMostAltCodeBlock() {
+ if ( this instanceof CodeBlockForOuterMostAlt ) {
+ return (CodeBlockForOuterMostAlt)this;
+ }
+ CodeBlock p = enclosingBlock;
+ while ( p!=null ) {
+ if ( p instanceof CodeBlockForOuterMostAlt ) {
+ return (CodeBlockForOuterMostAlt)p;
+ }
+ p = p.enclosingBlock;
+ }
+ return null;
+ }
+
+ /** Return label alt or return name of rule */
+ public String getContextName() {
+ CodeBlockForOuterMostAlt alt = getOuterMostAltCodeBlock();
+ if ( alt!=null && alt.altLabel!=null ) return alt.altLabel;
+ return enclosingRuleRunction.name;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/StarBlock.java b/tool/src/org/antlr/v4/codegen/model/StarBlock.java
new file mode 100644
index 0000000..e61ef72
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/StarBlock.java
@@ -0,0 +1,52 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.atn.StarLoopEntryState;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.List;
+
+public class StarBlock extends Loop {
+ public String loopLabel;
+
+ public StarBlock(OutputModelFactory factory,
+ GrammarAST blkOrEbnfRootAST,
+ List alts)
+ {
+ super(factory, blkOrEbnfRootAST, alts);
+ loopLabel = factory.getGenerator().getTarget().getLoopLabel(blkOrEbnfRootAST);
+ StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState;
+ loopBackStateNumber = star.loopBackState.stateNumber;
+ decision = star.decision;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/Sync.java b/tool/src/org/antlr/v4/codegen/model/Sync.java
new file mode 100644
index 0000000..b9c20fc
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/Sync.java
@@ -0,0 +1,52 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public class Sync extends SrcOp {
+ public int decision;
+// public BitSetDecl expecting;
+ public Sync(OutputModelFactory factory,
+ GrammarAST blkOrEbnfRootAST,
+ IntervalSet expecting,
+ int decision,
+ String position)
+ {
+ super(factory, blkOrEbnfRootAST);
+ this.decision = decision;
+// this.expecting = factory.createExpectingBitSet(ast, decision, expecting, position);
+// factory.defineBitSet(this.expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/TestSetInline.java b/tool/src/org/antlr/v4/codegen/model/TestSetInline.java
new file mode 100644
index 0000000..0b3956c
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/TestSetInline.java
@@ -0,0 +1,84 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** */
+public class TestSetInline extends SrcOp {
+ public int bitsetWordSize;
+ public String varName;
+ public Bitset[] bitsets;
+
+ public TestSetInline(OutputModelFactory factory, GrammarAST ast, IntervalSet set, int wordSize) {
+ super(factory, ast);
+ bitsetWordSize = wordSize;
+ Bitset[] withZeroOffset = createBitsets(factory, set, wordSize, true);
+ Bitset[] withoutZeroOffset = createBitsets(factory, set, wordSize, false);
+ this.bitsets = withZeroOffset.length <= withoutZeroOffset.length ? withZeroOffset : withoutZeroOffset;
+ this.varName = "_la";
+ }
+
+ private static Bitset[] createBitsets(OutputModelFactory factory,
+ IntervalSet set,
+ int wordSize,
+ boolean useZeroOffset) {
+ List bitsetList = new ArrayList();
+ for (int ttype : set.toArray()) {
+ Bitset current = !bitsetList.isEmpty() ? bitsetList.get(bitsetList.size() - 1) : null;
+ if (current == null || ttype > (current.shift + wordSize-1)) {
+ current = new Bitset();
+ if (useZeroOffset && ttype >= 0 && ttype < wordSize-1) {
+ current.shift = 0;
+ }
+ else {
+ current.shift = ttype;
+ }
+
+ bitsetList.add(current);
+ }
+
+ current.ttypes.add(factory.getGenerator().getTarget().getTokenTypeAsTargetLabel(factory.getGrammar(), ttype));
+ }
+
+ return bitsetList.toArray(new Bitset[bitsetList.size()]);
+ }
+
+ public static final class Bitset {
+ public int shift;
+ public final List ttypes = new ArrayList();
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ThrowEarlyExitException.java b/tool/src/org/antlr/v4/codegen/model/ThrowEarlyExitException.java
new file mode 100644
index 0000000..17f8574
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ThrowEarlyExitException.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public class ThrowEarlyExitException extends ThrowRecognitionException {
+ public ThrowEarlyExitException(OutputModelFactory factory, GrammarAST ast, IntervalSet expecting) {
+ super(factory, ast, expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ThrowNoViableAlt.java b/tool/src/org/antlr/v4/codegen/model/ThrowNoViableAlt.java
new file mode 100644
index 0000000..eb07be7
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ThrowNoViableAlt.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public class ThrowNoViableAlt extends ThrowRecognitionException {
+ public ThrowNoViableAlt(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST,
+ IntervalSet expecting)
+ {
+ super(factory, blkOrEbnfRootAST, expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/ThrowRecognitionException.java b/tool/src/org/antlr/v4/codegen/model/ThrowRecognitionException.java
new file mode 100644
index 0000000..8815764
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/ThrowRecognitionException.java
@@ -0,0 +1,53 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.IntervalSet;
+import org.antlr.v4.tool.ast.GrammarAST;
+
+/** */
+public class ThrowRecognitionException extends SrcOp {
+ public int decision;
+ public String grammarFile;
+ public int grammarLine;
+ public int grammarCharPosInLine;
+
+ public ThrowRecognitionException(OutputModelFactory factory, GrammarAST ast, IntervalSet expecting) {
+ super(factory, ast);
+ //this.decision = ((BlockStartState)ast.ATNState).decision;
+ grammarLine = ast.getLine();
+ grammarLine = ast.getCharPositionInLine();
+ grammarFile = factory.getGrammar().fileName;
+ //this.expecting = factory.createExpectingBitSet(ast, decision, expecting, "error");
+// factory.defineBitSet(this.expecting);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/VisitorDispatchMethod.java b/tool/src/org/antlr/v4/codegen/model/VisitorDispatchMethod.java
new file mode 100644
index 0000000..a56dc6e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/VisitorDispatchMethod.java
@@ -0,0 +1,38 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class VisitorDispatchMethod extends DispatchMethod {
+ public VisitorDispatchMethod(OutputModelFactory factory) {
+ super(factory);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/VisitorFile.java b/tool/src/org/antlr/v4/codegen/model/VisitorFile.java
new file mode 100644
index 0000000..e1370a9
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/VisitorFile.java
@@ -0,0 +1,84 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.model;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.Pair;
+import org.antlr.v4.tool.Grammar;
+import org.antlr.v4.tool.Rule;
+import org.antlr.v4.tool.ast.ActionAST;
+import org.antlr.v4.tool.ast.AltAST;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class VisitorFile extends OutputFile {
+ public String genPackage; // from -package cmd-line
+ public String grammarName;
+ public String parserName;
+ /**
+ * The names of all rule contexts which may need to be visited.
+ */
+ public Set visitorNames = new LinkedHashSet();
+ /**
+ * For rule contexts created for a labeled outer alternative, maps from
+ * a listener context name to the name of the rule which defines the
+ * context.
+ */
+ public Map visitorLabelRuleNames = new LinkedHashMap();
+
+ @ModelElement public Action header;
+
+ public VisitorFile(OutputModelFactory factory, String fileName) {
+ super(factory, fileName);
+ Grammar g = factory.getGrammar();
+ parserName = g.getRecognizerName();
+ grammarName = g.name;
+ for (Rule r : g.rules.values()) {
+ Map>> labels = r.getAltLabels();
+ if ( labels!=null ) {
+ for (Map.Entry>> pair : labels.entrySet()) {
+ visitorNames.add(pair.getKey());
+ visitorLabelRuleNames.put(pair.getKey(), r.name);
+ }
+ }
+ else {
+ // if labels, must label all. no need for generic rule visitor then
+ visitorNames.add(r.name);
+ }
+ }
+ ActionAST ast = g.namedActions.get("header");
+ if ( ast!=null ) header = new Action(factory, ast);
+ genPackage = factory.getGrammar().tool.genPackage;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ActionChunk.java b/tool/src/org/antlr/v4/codegen/model/chunk/ActionChunk.java
new file mode 100644
index 0000000..3c7e364
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ActionChunk.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.OutputModelObject;
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ActionChunk extends OutputModelObject {
+ /** Where is the ctx that defines attrs,labels etc... for this action? */
+ public StructDecl ctx;
+
+ public ActionChunk(StructDecl ctx) {
+ this.ctx = ctx;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ActionTemplate.java b/tool/src/org/antlr/v4/codegen/model/chunk/ActionTemplate.java
new file mode 100644
index 0000000..2c2edc1
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ActionTemplate.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+import org.stringtemplate.v4.ST;
+
+public class ActionTemplate extends ActionChunk {
+ public ST st;
+
+ public ActionTemplate(StructDecl ctx, ST st) {
+ super(ctx);
+ this.st = st;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ActionText.java b/tool/src/org/antlr/v4/codegen/model/chunk/ActionText.java
new file mode 100644
index 0000000..de3acff
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ActionText.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ActionText extends ActionChunk {
+ public String text;
+
+ public ActionText(StructDecl ctx, String text) {
+ super(ctx);
+ this.text = text;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ArgRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/ArgRef.java
new file mode 100644
index 0000000..2d39271
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ArgRef.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ArgRef extends LocalRef {
+ public ArgRef(StructDecl ctx, String name) {
+ super(ctx, name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/LabelRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/LabelRef.java
new file mode 100644
index 0000000..eae4a65
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/LabelRef.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class LabelRef extends ActionChunk {
+ public String name;
+
+ public LabelRef(StructDecl ctx, String name) {
+ super(ctx);
+ this.name = name;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ListLabelRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/ListLabelRef.java
new file mode 100644
index 0000000..c7af76a
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ListLabelRef.java
@@ -0,0 +1,37 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class ListLabelRef extends LabelRef {
+ public ListLabelRef(StructDecl ctx, String name) { super(ctx, name); }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/LocalRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/LocalRef.java
new file mode 100644
index 0000000..3463dc8
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/LocalRef.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class LocalRef extends ActionChunk {
+ public String name;
+
+ public LocalRef(StructDecl ctx, String name) {
+ super(ctx);
+ this.name = name;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/NonLocalAttrRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/NonLocalAttrRef.java
new file mode 100644
index 0000000..d9a28f7
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/NonLocalAttrRef.java
@@ -0,0 +1,47 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class NonLocalAttrRef extends ActionChunk {
+ public String ruleName;
+ public String name;
+ public int ruleIndex;
+
+ public NonLocalAttrRef(StructDecl ctx, String ruleName, String name, int ruleIndex) {
+ super(ctx);
+ this.name = name;
+ this.ruleName = ruleName;
+ this.ruleIndex = ruleIndex;
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/QRetValueRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/QRetValueRef.java
new file mode 100644
index 0000000..d799e78
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/QRetValueRef.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class QRetValueRef extends RetValueRef {
+ public String dict;
+ public QRetValueRef(StructDecl ctx, String dict, String name) {
+ super(ctx,name);
+ this.dict = dict;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RetValueRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/RetValueRef.java
new file mode 100644
index 0000000..59e45fe
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RetValueRef.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RetValueRef extends ActionChunk {
+ public String name;
+
+ public RetValueRef(StructDecl ctx, String name) {
+ super(ctx);
+ this.name = name;
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef.java
new file mode 100644
index 0000000..ae14863
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RulePropertyRef extends ActionChunk {
+ public String label;
+
+ public RulePropertyRef(StructDecl ctx, String label) {
+ super(ctx);
+ this.label = label;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_ctx.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_ctx.java
new file mode 100644
index 0000000..aebded7
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_ctx.java
@@ -0,0 +1,39 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class RulePropertyRef_ctx extends RulePropertyRef {
+ public RulePropertyRef_ctx(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_parser.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_parser.java
new file mode 100644
index 0000000..a427810
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_parser.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RulePropertyRef_parser extends RulePropertyRef {
+ public RulePropertyRef_parser(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_start.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_start.java
new file mode 100644
index 0000000..826c5d0
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_start.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RulePropertyRef_start extends RulePropertyRef {
+ public RulePropertyRef_start(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_stop.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_stop.java
new file mode 100644
index 0000000..7b9e3f2
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_stop.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RulePropertyRef_stop extends RulePropertyRef {
+ public RulePropertyRef_stop(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_text.java b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_text.java
new file mode 100644
index 0000000..1230105
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/RulePropertyRef_text.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class RulePropertyRef_text extends RulePropertyRef {
+ public RulePropertyRef_text(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/SetAttr.java b/tool/src/org/antlr/v4/codegen/model/chunk/SetAttr.java
new file mode 100644
index 0000000..1b6e15b
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/SetAttr.java
@@ -0,0 +1,48 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.ModelElement;
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+import java.util.List;
+
+/** */
+public class SetAttr extends ActionChunk {
+ public String name;
+ @ModelElement public List rhsChunks;
+
+ public SetAttr(StructDecl ctx, String name, List rhsChunks) {
+ super(ctx);
+ this.name = name;
+ this.rhsChunks = rhsChunks;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/SetNonLocalAttr.java b/tool/src/org/antlr/v4/codegen/model/chunk/SetNonLocalAttr.java
new file mode 100644
index 0000000..9872b6e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/SetNonLocalAttr.java
@@ -0,0 +1,49 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+import java.util.List;
+
+public class SetNonLocalAttr extends SetAttr {
+ public String ruleName;
+ public int ruleIndex;
+
+ public SetNonLocalAttr(StructDecl ctx,
+ String ruleName, String name, int ruleIndex,
+ List rhsChunks)
+ {
+ super(ctx, name, rhsChunks);
+ this.ruleName = ruleName;
+ this.ruleIndex = ruleIndex;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_ctx.java b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_ctx.java
new file mode 100644
index 0000000..e8dc913
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_ctx.java
@@ -0,0 +1,39 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+public class ThisRulePropertyRef_ctx extends RulePropertyRef {
+ public ThisRulePropertyRef_ctx(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_parser.java b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_parser.java
new file mode 100644
index 0000000..7cbf5d6
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_parser.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ThisRulePropertyRef_parser extends RulePropertyRef {
+ public ThisRulePropertyRef_parser(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_start.java b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_start.java
new file mode 100644
index 0000000..614c973
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_start.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ThisRulePropertyRef_start extends RulePropertyRef {
+ public ThisRulePropertyRef_start(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_stop.java b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_stop.java
new file mode 100644
index 0000000..65b5322
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_stop.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ThisRulePropertyRef_stop extends RulePropertyRef {
+ public ThisRulePropertyRef_stop(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_text.java b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_text.java
new file mode 100644
index 0000000..7ee4d8e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/ThisRulePropertyRef_text.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class ThisRulePropertyRef_text extends RulePropertyRef {
+ public ThisRulePropertyRef_text(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef.java
new file mode 100644
index 0000000..8a3f7d5
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef extends ActionChunk {
+ public String label;
+
+ public TokenPropertyRef(StructDecl ctx, String label) {
+ super(ctx);
+ this.label = label;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_channel.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_channel.java
new file mode 100644
index 0000000..9c93690
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_channel.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_channel extends TokenPropertyRef {
+ public TokenPropertyRef_channel(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_index.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_index.java
new file mode 100644
index 0000000..a456876
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_index.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_index extends TokenPropertyRef {
+ public TokenPropertyRef_index(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_int.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_int.java
new file mode 100644
index 0000000..91c13bd
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_int.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_int extends TokenPropertyRef {
+ public TokenPropertyRef_int(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_line.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_line.java
new file mode 100644
index 0000000..f9c08f4
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_line.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_line extends TokenPropertyRef {
+ public TokenPropertyRef_line(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_pos.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_pos.java
new file mode 100644
index 0000000..469b228
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_pos.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_pos extends TokenPropertyRef {
+ public TokenPropertyRef_pos(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_text.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_text.java
new file mode 100644
index 0000000..3da4f53
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_text.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_text extends TokenPropertyRef {
+ public TokenPropertyRef_text(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_type.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_type.java
new file mode 100644
index 0000000..d495e19
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenPropertyRef_type.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenPropertyRef_type extends TokenPropertyRef {
+ public TokenPropertyRef_type(StructDecl ctx, String label) {
+ super(ctx, label);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/chunk/TokenRef.java b/tool/src/org/antlr/v4/codegen/model/chunk/TokenRef.java
new file mode 100644
index 0000000..ac2569b
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/chunk/TokenRef.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.chunk;
+
+import org.antlr.v4.codegen.model.decl.StructDecl;
+
+/** */
+public class TokenRef extends ActionChunk {
+ public String name;
+
+ public TokenRef(StructDecl ctx, String name) {
+ super(ctx);
+ this.name = name;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/dbg.java b/tool/src/org/antlr/v4/codegen/model/dbg.java
new file mode 100644
index 0000000..4eb8386
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/dbg.java
@@ -0,0 +1,35 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model;
+
+/** */
+public class dbg extends OutputModelObject {
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java
new file mode 100644
index 0000000..5d49625
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java
@@ -0,0 +1,78 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.DispatchMethod;
+import org.antlr.v4.codegen.model.ListenerDispatchMethod;
+import org.antlr.v4.codegen.model.VisitorDispatchMethod;
+import org.antlr.v4.tool.Rule;
+
+import java.util.ArrayList;
+
+/** A StructDecl to handle a -> label on alt */
+public class AltLabelStructDecl extends StructDecl {
+ public int altNum;
+ public AltLabelStructDecl(OutputModelFactory factory, Rule r,
+ int altNum, String label)
+ {
+ super(factory, r);
+ this.altNum = altNum;
+ this.name = // override name set in super to the label ctx
+ factory.getGenerator().getTarget().getAltLabelContextStructName(label);
+ derivedFromName = label;
+ }
+
+ @Override
+ public void addDispatchMethods(Rule r) {
+ dispatchMethods = new ArrayList();
+ if ( factory.getGrammar().tool.gen_listener ) {
+ dispatchMethods.add(new ListenerDispatchMethod(factory, true));
+ dispatchMethods.add(new ListenerDispatchMethod(factory, false));
+ }
+ if ( factory.getGrammar().tool.gen_visitor ) {
+ dispatchMethods.add(new VisitorDispatchMethod(factory));
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return name.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( obj == this ) return true;
+ if (!(obj instanceof AltLabelStructDecl)) return false;
+
+ return name.equals(((AltLabelStructDecl)obj).name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/AttributeDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/AttributeDecl.java
new file mode 100644
index 0000000..7b77ecd
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/AttributeDecl.java
@@ -0,0 +1,45 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.tool.Attribute;
+
+/** */
+public class AttributeDecl extends Decl {
+ public String type;
+ public String initValue;
+ public AttributeDecl(OutputModelFactory factory, Attribute a) {
+ super(factory, a.name, a.decl);
+ this.type = a.type;
+ this.initValue = a.initValue;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/CodeBlock.java b/tool/src/org/antlr/v4/codegen/model/decl/CodeBlock.java
new file mode 100644
index 0000000..8a0890e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/CodeBlock.java
@@ -0,0 +1,85 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.ModelElement;
+import org.antlr.v4.codegen.model.SrcOp;
+import org.antlr.v4.runtime.misc.OrderedHashSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CodeBlock extends SrcOp {
+ public int codeBlockLevel;
+ public int treeLevel;
+
+ @ModelElement public OrderedHashSet locals;
+ @ModelElement public List preamble;
+ @ModelElement public List ops;
+
+ public CodeBlock(OutputModelFactory factory) {
+ super(factory);
+ }
+
+ public CodeBlock(OutputModelFactory factory, int treeLevel, int codeBlockLevel) {
+ super(factory);
+ this.treeLevel = treeLevel;
+ this.codeBlockLevel = codeBlockLevel;
+ }
+
+ /** Add local var decl */
+ public void addLocalDecl(Decl d) {
+ if ( locals==null ) locals = new OrderedHashSet();
+ locals.add(d);
+ d.isLocal = true;
+ }
+
+ public void addPreambleOp(SrcOp op) {
+ if ( preamble==null ) preamble = new ArrayList();
+ preamble.add(op);
+ }
+
+ public void addOp(SrcOp op) {
+ if ( ops==null ) ops = new ArrayList();
+ ops.add(op);
+ }
+
+ public void insertOp(int i, SrcOp op) {
+ if ( ops==null ) ops = new ArrayList();
+ ops.add(i, op);
+ }
+
+ public void addOps(List ops) {
+ if ( this.ops==null ) this.ops = new ArrayList();
+ this.ops.addAll(ops);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextGetterDecl.java
new file mode 100644
index 0000000..99ceef2
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextGetterDecl.java
@@ -0,0 +1,68 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.runtime.misc.MurmurHash;
+
+public abstract class ContextGetterDecl extends Decl {
+ public ContextGetterDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+
+ /** Not used for output; just used to distinguish between decl types
+ * to avoid dups.
+ */
+ public String getArgType() { return ""; }; // assume no args
+
+ @Override
+ public int hashCode() {
+ int hash = MurmurHash.initialize();
+ hash = MurmurHash.update(hash, name);
+ hash = MurmurHash.update(hash, getArgType());
+ hash = MurmurHash.finish(hash, 2);
+ return hash;
+ }
+
+ /** Make sure that a getter does not equal a label. X() and X are ok.
+ * OTOH, treat X() with two diff return values as the same. Treat
+ * two X() with diff args as different.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if ( this==obj ) return true;
+ // A() and label A are different
+ if ( !(obj instanceof ContextGetterDecl) ) return false;
+ return
+ name.equals(((Decl) obj).name) &&
+ getArgType().equals(((ContextGetterDecl) obj).getArgType());
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleGetterDecl.java
new file mode 100644
index 0000000..2bb4f9b
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleGetterDecl.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** {@code public XContext X() { }} */
+public class ContextRuleGetterDecl extends ContextGetterDecl {
+ public String ctxName;
+ public ContextRuleGetterDecl(OutputModelFactory factory, String name, String ctxName) {
+ super(factory, name);
+ this.ctxName = ctxName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListGetterDecl.java
new file mode 100644
index 0000000..c3bfae9
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListGetterDecl.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** {@code public List X() { }
+ * public XContext X(int i) { }}
+ */
+public class ContextRuleListGetterDecl extends ContextGetterDecl {
+ public String ctxName;
+ public ContextRuleListGetterDecl(OutputModelFactory factory, String name, String ctxName) {
+ super(factory, name);
+ this.ctxName = ctxName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListIndexedGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListIndexedGetterDecl.java
new file mode 100644
index 0000000..a2c3948
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextRuleListIndexedGetterDecl.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class ContextRuleListIndexedGetterDecl extends ContextRuleListGetterDecl {
+ public ContextRuleListIndexedGetterDecl(OutputModelFactory factory, String name, String ctxName) {
+ super(factory, name, ctxName);
+ }
+
+ @Override
+ public String getArgType() {
+ return "int";
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenGetterDecl.java
new file mode 100644
index 0000000..431e817
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenGetterDecl.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** {@code public Token X() { }} */
+public class ContextTokenGetterDecl extends ContextGetterDecl {
+ public ContextTokenGetterDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListGetterDecl.java
new file mode 100644
index 0000000..d293720
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListGetterDecl.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** {@code public List X() { }
+ * public Token X(int i) { }}
+ */
+public class ContextTokenListGetterDecl extends ContextGetterDecl {
+ public ContextTokenListGetterDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListIndexedGetterDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListIndexedGetterDecl.java
new file mode 100644
index 0000000..ff7e48e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ContextTokenListIndexedGetterDecl.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class ContextTokenListIndexedGetterDecl extends ContextTokenListGetterDecl {
+ public ContextTokenListIndexedGetterDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+
+ @Override
+ public String getArgType() {
+ return "int";
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/Decl.java b/tool/src/org/antlr/v4/codegen/model/decl/Decl.java
new file mode 100644
index 0000000..c956555
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/Decl.java
@@ -0,0 +1,67 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.SrcOp;
+
+/** */
+public class Decl extends SrcOp {
+ public String name;
+ public String decl; // whole thing if copied from action
+ public boolean isLocal; // if local var (not in RuleContext struct)
+ public StructDecl ctx; // which context contains us? set by addDecl
+
+ public Decl(OutputModelFactory factory, String name, String decl) {
+ this(factory, name);
+ this.decl = decl;
+ }
+
+ public Decl(OutputModelFactory factory, String name) {
+ super(factory);
+ this.name = name;
+ }
+
+ @Override
+ public int hashCode() {
+ return name.hashCode();
+ }
+
+ /** If same name, can't redefine, unless it's a getter */
+ @Override
+ public boolean equals(Object obj) {
+ if ( this==obj ) return true;
+ if ( !(obj instanceof Decl) ) return false;
+ // A() and label A are different
+ if ( obj instanceof ContextGetterDecl ) return false;
+ return name.equals(((Decl) obj).name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/ElementListDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/ElementListDecl.java
new file mode 100644
index 0000000..cf99a1a
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/ElementListDecl.java
@@ -0,0 +1,39 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class ElementListDecl extends Decl {
+ public ElementListDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/RuleContextDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/RuleContextDecl.java
new file mode 100644
index 0000000..6f97bfa
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/RuleContextDecl.java
@@ -0,0 +1,44 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** */
+public class RuleContextDecl extends Decl {
+ public String ctxName;
+ public boolean isImplicit;
+
+ public RuleContextDecl(OutputModelFactory factory, String name, String ctxName) {
+ super(factory, name);
+ this.ctxName = ctxName;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/RuleContextListDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/RuleContextListDecl.java
new file mode 100644
index 0000000..fbd22f6
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/RuleContextListDecl.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+public class RuleContextListDecl extends RuleContextDecl {
+ public RuleContextListDecl(OutputModelFactory factory, String name, String ctxName) {
+ super(factory, name, ctxName);
+ isImplicit = false;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java
new file mode 100644
index 0000000..309d6ca
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java
@@ -0,0 +1,112 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+import org.antlr.v4.codegen.model.DispatchMethod;
+import org.antlr.v4.codegen.model.ListenerDispatchMethod;
+import org.antlr.v4.codegen.model.ModelElement;
+import org.antlr.v4.codegen.model.OutputModelObject;
+import org.antlr.v4.codegen.model.VisitorDispatchMethod;
+import org.antlr.v4.runtime.misc.OrderedHashSet;
+import org.antlr.v4.tool.Attribute;
+import org.antlr.v4.tool.Rule;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/** This object models the structure holding all of the parameters,
+ * return values, local variables, and labels associated with a rule.
+ */
+public class StructDecl extends Decl {
+ public String derivedFromName; // rule name or label name
+ public boolean provideCopyFrom;
+ @ModelElement public OrderedHashSet attrs = new OrderedHashSet();
+ @ModelElement public OrderedHashSet getters = new OrderedHashSet();
+ @ModelElement public Collection ctorAttrs;
+ @ModelElement public List super DispatchMethod> dispatchMethods;
+ @ModelElement public List interfaces;
+ @ModelElement public List extensionMembers;
+
+ public StructDecl(OutputModelFactory factory, Rule r) {
+ super(factory, factory.getGenerator().getTarget().getRuleFunctionContextStructName(r));
+ addDispatchMethods(r);
+ derivedFromName = r.name;
+ provideCopyFrom = r.hasAltSpecificContexts();
+ }
+
+ public void addDispatchMethods(Rule r) {
+ dispatchMethods = new ArrayList();
+ if ( !r.hasAltSpecificContexts() ) {
+ // no enter/exit for this ruleContext if rule has labels
+ if ( factory.getGrammar().tool.gen_listener ) {
+ dispatchMethods.add(new ListenerDispatchMethod(factory, true));
+ dispatchMethods.add(new ListenerDispatchMethod(factory, false));
+ }
+ if ( factory.getGrammar().tool.gen_visitor ) {
+ dispatchMethods.add(new VisitorDispatchMethod(factory));
+ }
+ }
+ }
+
+ public void addDecl(Decl d) {
+ d.ctx = this;
+ if ( d instanceof ContextGetterDecl ) getters.add(d);
+ else attrs.add(d);
+ }
+
+ public void addDecl(Attribute a) {
+ addDecl(new AttributeDecl(factory, a));
+ }
+
+ public void addDecls(Collection attrList) {
+ for (Attribute a : attrList) addDecl(a);
+ }
+
+ public void implementInterface(OutputModelObject value) {
+ if (interfaces == null) {
+ interfaces = new ArrayList();
+ }
+
+ interfaces.add(value);
+ }
+
+ public void addExtensionMember(OutputModelObject member) {
+ if (extensionMembers == null) {
+ extensionMembers = new ArrayList();
+ }
+
+ extensionMembers.add(member);
+ }
+
+ public boolean isEmpty() { return attrs.isEmpty(); }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/TokenDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/TokenDecl.java
new file mode 100644
index 0000000..0971fb0
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/TokenDecl.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** x=ID or implicit _tID label */
+public class TokenDecl extends Decl {
+ public boolean isImplicit;
+
+ public TokenDecl(OutputModelFactory factory, String varName) {
+ super(factory, varName);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/TokenListDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/TokenListDecl.java
new file mode 100644
index 0000000..37b7b0e
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/TokenListDecl.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** */
+public class TokenListDecl extends TokenDecl {
+ public TokenListDecl(OutputModelFactory factory, String varName) {
+ super(factory, varName);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/model/decl/TokenTypeDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/TokenTypeDecl.java
new file mode 100644
index 0000000..2762ba3
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/model/decl/TokenTypeDecl.java
@@ -0,0 +1,40 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.model.decl;
+
+import org.antlr.v4.codegen.OutputModelFactory;
+
+/** */
+public class TokenTypeDecl extends Decl {
+ public TokenTypeDecl(OutputModelFactory factory, String name) {
+ super(factory, name);
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java
new file mode 100644
index 0000000..09df962
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java
@@ -0,0 +1,177 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2013 Terence Parr
+ * Copyright (c) 2013 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.codegen.target;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.Target;
+import org.antlr.v4.tool.ErrorType;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.stringtemplate.v4.NumberRenderer;
+import org.stringtemplate.v4.STErrorListener;
+import org.stringtemplate.v4.STGroup;
+import org.stringtemplate.v4.STGroupFile;
+import org.stringtemplate.v4.StringRenderer;
+import org.stringtemplate.v4.misc.STMessage;
+
+public class CSharpTarget extends Target {
+
+ public CSharpTarget(CodeGenerator gen) {
+ super(gen, "CSharp");
+ targetCharValueEscape[0] = "\\0";
+ targetCharValueEscape[0x0007] = "\\a";
+ targetCharValueEscape[0x000B] = "\\v";
+ }
+
+ @Override
+ public String getVersion() {
+ return "4.5.1"; // crossing fingers that it's close enough.
+ }
+
+ @Override
+ public String encodeIntAsCharEscape(int v) {
+ if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) {
+ throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v));
+ }
+
+ if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) {
+ return targetCharValueEscape[v];
+ }
+
+ if (v >= 0x20 && v < 127 && (v < '0' || v > '9') && (v < 'a' || v > 'f') && (v < 'A' || v > 'F')) {
+ return String.valueOf((char)v);
+ }
+
+ return String.format("\\x%X", v & 0xFFFF);
+ }
+
+ @Override
+ public String getTargetStringLiteralFromANTLRStringLiteral(
+ CodeGenerator generator,
+ String literal, boolean addQuotes)
+ {
+ StringBuilder sb = new StringBuilder();
+ String is = literal;
+
+ if ( addQuotes ) sb.append('"');
+
+ for (int i = 1; i < is.length() -1; i++) {
+ if (is.charAt(i) == '\\') {
+ // Anything escaped is what it is! We assume that
+ // people know how to escape characters correctly. However
+ // we catch anything that does not need an escape in Java (which
+ // is what the default implementation is dealing with and remove
+ // the escape. The C target does this for instance.
+ //
+ switch (is.charAt(i+1)) {
+ // Pass through any escapes that Java also needs
+ //
+ case '"':
+ case 'n':
+ case 'r':
+ case 't':
+ case 'b':
+ case 'f':
+ case '\\':
+ // Pass the escape through
+ sb.append('\\');
+ break;
+
+ case 'u': // Assume unnnn
+ // Pass the escape through as double \\
+ // so that Java leaves as \u0000 string not char
+ sb.append('\\');
+ sb.append('\\');
+ break;
+
+ default:
+ // Remove the escape by virtue of not adding it here
+ // Thus \' becomes ' and so on
+ break;
+ }
+
+ // Go past the \ character
+ i++;
+ } else {
+ // Characters that don't need \ in ANTLR 'strings' but do in Java
+ if (is.charAt(i) == '"') {
+ // We need to escape " in Java
+ sb.append('\\');
+ }
+ }
+ // Add in the next character, which may have been escaped
+ sb.append(is.charAt(i));
+ }
+
+ if ( addQuotes ) sb.append('"');
+
+ return sb.toString();
+ }
+
+ @Override
+ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ return false;
+ }
+
+ @Override
+ protected STGroup loadTemplates() {
+ // override the superclass behavior to put all C# templates in the same folder
+ STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT+"/CSharp/"+getLanguage()+STGroup.GROUP_FILE_EXTENSION);
+ result.registerRenderer(Integer.class, new NumberRenderer());
+ result.registerRenderer(String.class, new StringRenderer());
+ result.setListener(new STErrorListener() {
+ @Override
+ public void compileTimeError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void runTimeError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void IOError(STMessage msg) {
+ reportError(msg);
+ }
+
+ @Override
+ public void internalError(STMessage msg) {
+ reportError(msg);
+ }
+
+ private void reportError(STMessage msg) {
+ getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
+ }
+ });
+
+ return result;
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java
new file mode 100644
index 0000000..26fa4fe
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java
@@ -0,0 +1,234 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.target;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.Target;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.stringtemplate.v4.STGroup;
+import org.stringtemplate.v4.StringRenderer;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ *
+ * @author Eric Vergnaud
+ */
+public class JavaScriptTarget extends Target {
+
+ /** Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar */
+ protected static final String[] javaScriptKeywords = {
+ "break", "case", "class", "catch", "const", "continue", "debugger",
+ "default", "delete", "do", "else", "export", "extends", "finally", "for",
+ "function", "if", "import", "in", "instanceof", "let", "new", "return",
+ "super", "switch", "this", "throw", "try", "typeof", "var", "void",
+ "while", "with", "yield",
+ //future reserved
+ "enum", "await", "implements", "package", "protected", "static",
+ "interface", "private", "public",
+ //future reserved in older standards
+ "abstract", "boolean", "byte", "char", "double", "final", "float",
+ "goto", "int", "long", "native", "short", "synchronized", "transient",
+ "volatile",
+ //literals
+ "null", "true", "false"
+ };
+
+ /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
+ protected final Set badWords = new HashSet();
+
+ public JavaScriptTarget(CodeGenerator gen) {
+ super(gen, "JavaScript");
+ }
+
+ @Override
+ public String getVersion() {
+ return "4.5.1";
+ }
+
+ public Set getBadWords() {
+ if (badWords.isEmpty()) {
+ addBadWords();
+ }
+
+ return badWords;
+ }
+
+ protected void addBadWords() {
+ badWords.addAll(Arrays.asList(javaScriptKeywords));
+ badWords.add("rule");
+ badWords.add("parserRule");
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * 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 \"}.
+ *
+ * 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
+ * around.
+ */
+ @Override
+ public String getTargetStringLiteralFromANTLRStringLiteral(
+ CodeGenerator generator,
+ String literal, boolean addQuotes)
+ {
+ StringBuilder sb = new StringBuilder();
+ String is = literal;
+
+ if ( addQuotes ) sb.append('"');
+
+ for (int i = 1; i < is.length() -1; i++) {
+ if (is.charAt(i) == '\\') {
+ // Anything escaped is what it is! We assume that
+ // people know how to escape characters correctly. However
+ // we catch anything that does not need an escape in Java (which
+ // is what the default implementation is dealing with and remove
+ // the escape. The C target does this for instance.
+ //
+ switch (is.charAt(i+1)) {
+ // Pass through any escapes that Java also needs
+ //
+ case '"':
+ case 'n':
+ case 'r':
+ case 't':
+ case 'b':
+ case 'f':
+ case '\\':
+ // Pass the escape through
+ sb.append('\\');
+ break;
+
+ case 'u': // Assume unnnn
+ // Pass the escape through as double \\
+ // so that Java leaves as \u0000 string not char
+ sb.append('\\');
+ sb.append('\\');
+ break;
+
+ default:
+ // Remove the escape by virtue of not adding it here
+ // Thus \' becomes ' and so on
+ break;
+ }
+
+ // Go past the \ character
+ i++;
+ } else {
+ // Characters that don't need \ in ANTLR 'strings' but do in Java
+ if (is.charAt(i) == '"') {
+ // We need to escape " in Java
+ sb.append('\\');
+ }
+ }
+ // Add in the next character, which may have been escaped
+ sb.append(is.charAt(i));
+ }
+
+ if ( addQuotes ) sb.append('"');
+
+ return sb.toString();
+ }
+
+ @Override
+ public String encodeIntAsCharEscape(int v) {
+ if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) {
+ throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v));
+ }
+
+ if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) {
+ return targetCharValueEscape[v];
+ }
+
+ if (v >= 0x20 && v < 127) {
+ return String.valueOf((char)v);
+ }
+
+ String hex = Integer.toHexString(v|0x10000).substring(1,5);
+ return "\\u"+hex;
+ }
+
+ @Override
+ public int getSerializedATNSegmentLimit() {
+ return 2 ^ 31;
+ }
+
+ @Override
+ public int getInlineTestSetWordSize() {
+ return 32;
+ }
+
+ @Override
+ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ return getBadWords().contains(idNode.getText());
+ }
+
+ @Override
+ protected STGroup loadTemplates() {
+ STGroup result = super.loadTemplates();
+ result.registerRenderer(String.class, new JavaStringRenderer(), true);
+ return result;
+ }
+
+ protected static class JavaStringRenderer extends StringRenderer {
+
+ @Override
+ public String toString(Object o, String formatString, Locale locale) {
+ if ("java-escape".equals(formatString)) {
+ // 5C is the hex code for the \ itself
+ return ((String)o).replace("\\u", "\\u005Cu");
+ }
+
+ return super.toString(o, formatString, locale);
+ }
+
+ }
+
+ public boolean wantsBaseListener() {
+ return false;
+ }
+
+ public boolean wantsBaseVisitor() {
+ return false;
+ }
+
+ public boolean supportsOverloadedMethods() {
+ return false;
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/target/JavaTarget.java b/tool/src/org/antlr/v4/codegen/target/JavaTarget.java
new file mode 100644
index 0000000..6ee18da
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/target/JavaTarget.java
@@ -0,0 +1,126 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.target;
+
+import org.antlr.v4.Tool;
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.Target;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.stringtemplate.v4.STGroup;
+import org.stringtemplate.v4.StringRenderer;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+public class JavaTarget extends Target {
+
+ /**
+ * The Java target can cache the code generation templates.
+ */
+ private static final ThreadLocal targetTemplates = new ThreadLocal();
+
+ protected static final String[] javaKeywords = {
+ "abstract", "assert", "boolean", "break", "byte", "case", "catch",
+ "char", "class", "const", "continue", "default", "do", "double", "else",
+ "enum", "extends", "false", "final", "finally", "float", "for", "goto",
+ "if", "implements", "import", "instanceof", "int", "interface",
+ "long", "native", "new", "null", "package", "private", "protected",
+ "public", "return", "short", "static", "strictfp", "super", "switch",
+ "synchronized", "this", "throw", "throws", "transient", "true", "try",
+ "void", "volatile", "while"
+ };
+
+ /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
+ protected final Set badWords = new HashSet();
+
+ public JavaTarget(CodeGenerator gen) {
+ super(gen, "Java");
+ }
+
+ @Override
+ public String getVersion() {
+ return Tool.VERSION; // Java and tool versions move in lock step
+ }
+
+ public Set getBadWords() {
+ if (badWords.isEmpty()) {
+ addBadWords();
+ }
+
+ return badWords;
+ }
+
+ protected void addBadWords() {
+ badWords.addAll(Arrays.asList(javaKeywords));
+ badWords.add("rule");
+ badWords.add("parserRule");
+ }
+
+ @Override
+ public int getSerializedATNSegmentLimit() {
+ // 65535 is the class file format byte limit for a UTF-8 encoded string literal
+ // 3 is the maximum number of bytes it takes to encode a value in the range 0-0xFFFF
+ return 65535 / 3;
+ }
+
+ @Override
+ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ return getBadWords().contains(idNode.getText());
+ }
+
+ @Override
+ protected STGroup loadTemplates() {
+ STGroup result = targetTemplates.get();
+ if (result == null) {
+ result = super.loadTemplates();
+ result.registerRenderer(String.class, new JavaStringRenderer(), true);
+ targetTemplates.set(result);
+ }
+
+ return result;
+ }
+
+ protected static class JavaStringRenderer extends StringRenderer {
+
+ @Override
+ public String toString(Object o, String formatString, Locale locale) {
+ if ("java-escape".equals(formatString)) {
+ // 5C is the hex code for the \ itself
+ return ((String)o).replace("\\u", "\\u005Cu");
+ }
+
+ return super.toString(o, formatString, locale);
+ }
+
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/target/Python2Target.java b/tool/src/org/antlr/v4/codegen/target/Python2Target.java
new file mode 100644
index 0000000..36249d5
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/target/Python2Target.java
@@ -0,0 +1,136 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.target;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.Target;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.stringtemplate.v4.STGroup;
+import org.stringtemplate.v4.StringRenderer;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ *
+ * @author Eric Vergnaud
+ */
+public class Python2Target extends Target {
+ protected static final String[] python2Keywords = {
+ "abs", "all", "any", "apply", "as",
+ "bin", "bool", "buffer", "bytearray",
+ "callable", "chr", "classmethod", "coerce", "compile", "complex",
+ "delattr", "dict", "dir", "divmod",
+ "enumerate", "eval", "execfile",
+ "file", "filter", "float", "format", "frozenset",
+ "getattr", "globals",
+ "hasattr", "hash", "help", "hex",
+ "id", "input", "int", "intern", "isinstance", "issubclass", "iter",
+ "len", "list", "locals",
+ "map", "max", "min", "next",
+ "memoryview",
+ "object", "oct", "open", "ord",
+ "pow", "print", "property",
+ "range", "raw_input", "reduce", "reload", "repr", "reversed", "round",
+ "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super",
+ "tuple", "type",
+ "unichr", "unicode",
+ "vars",
+ "with",
+ "xrange",
+ "zip",
+ "__import__",
+ "True", "False", "None"
+ };
+
+ /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
+ protected final Set badWords = new HashSet();
+
+ public Python2Target(CodeGenerator gen) {
+ super(gen, "Python2");
+ }
+
+ @Override
+ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ return getBadWords().contains(idNode.getText());
+ }
+
+ @Override
+ protected STGroup loadTemplates() {
+ STGroup result = super.loadTemplates();
+ result.registerRenderer(String.class, new PythonStringRenderer(), true);
+ return result;
+ }
+
+ protected static class PythonStringRenderer extends StringRenderer {
+
+ @Override
+ public String toString(Object o, String formatString, Locale locale) {
+ return super.toString(o, formatString, locale);
+ }
+ }
+
+ @Override
+ public boolean wantsBaseListener() {
+ return false;
+ }
+
+ @Override
+ public boolean wantsBaseVisitor() {
+ return false;
+ }
+
+ @Override
+ public boolean supportsOverloadedMethods() {
+ return false;
+ }
+
+ @Override
+ public String getVersion() {
+ return "4.5.2";
+ }
+
+ public Set getBadWords() {
+ if (badWords.isEmpty()) {
+ addBadWords();
+ }
+
+ return badWords;
+ }
+
+ protected void addBadWords() {
+ badWords.addAll(Arrays.asList(python2Keywords));
+ badWords.add("rule");
+ badWords.add("parserRule");
+ }
+}
diff --git a/tool/src/org/antlr/v4/codegen/target/Python3Target.java b/tool/src/org/antlr/v4/codegen/target/Python3Target.java
new file mode 100644
index 0000000..d07247f
--- /dev/null
+++ b/tool/src/org/antlr/v4/codegen/target/Python3Target.java
@@ -0,0 +1,143 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.codegen.target;
+
+import org.antlr.v4.codegen.CodeGenerator;
+import org.antlr.v4.codegen.Target;
+import org.antlr.v4.tool.ast.GrammarAST;
+import org.stringtemplate.v4.STGroup;
+import org.stringtemplate.v4.StringRenderer;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ *
+ * @author Eric Vergnaud
+ */
+public class Python3Target extends Target {
+ protected static final String[] python3Keywords = {
+ "abs", "all", "any", "apply", "as",
+ "bin", "bool", "buffer", "bytearray",
+ "callable", "chr", "classmethod", "coerce", "compile", "complex",
+ "delattr", "dict", "dir", "divmod",
+ "enumerate", "eval", "execfile",
+ "file", "filter", "float", "format", "frozenset",
+ "getattr", "globals",
+ "hasattr", "hash", "help", "hex",
+ "id", "input", "int", "intern", "isinstance", "issubclass", "iter",
+ "len", "list", "locals",
+ "map", "max", "min", "next",
+ "memoryview",
+ "object", "oct", "open", "ord",
+ "pow", "print", "property",
+ "range", "raw_input", "reduce", "reload", "repr", "reversed", "round",
+ "set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super",
+ "tuple", "type",
+ "unichr", "unicode",
+ "vars",
+ "with",
+ "zip",
+ "__import__",
+ "True", "False", "None"
+ };
+
+ public Python3Target(CodeGenerator gen) {
+ super(gen, "Python3");
+ }
+
+ @Override
+ public int getSerializedATNSegmentLimit() {
+ // set to something stupid to avoid segmentation
+ return 2 ^ 31;
+ }
+
+ @Override
+ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) {
+ return getBadWords().contains(idNode.getText());
+ }
+
+ @Override
+ protected STGroup loadTemplates() {
+ STGroup result = super.loadTemplates();
+ result.registerRenderer(String.class, new PythonStringRenderer(), true);
+ return result;
+ }
+
+ protected static class PythonStringRenderer extends StringRenderer {
+
+ @Override
+ public String toString(Object o, String formatString, Locale locale) {
+ return super.toString(o, formatString, locale);
+ }
+ }
+
+ @Override
+ public boolean wantsBaseListener() {
+ return false;
+ }
+
+ @Override
+ public boolean wantsBaseVisitor() {
+ return false;
+ }
+
+ @Override
+ public boolean supportsOverloadedMethods() {
+ return false;
+ }
+
+ @Override
+ public String getVersion() {
+ return "4.5.2";
+ }
+
+ /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
+ protected final Set badWords = new HashSet();
+
+ public Set getBadWords() {
+ if (badWords.isEmpty()) {
+ addBadWords();
+ }
+
+ return badWords;
+ }
+
+ protected void addBadWords() {
+ badWords.addAll(Arrays.asList(python3Keywords));
+ badWords.add("rule");
+ badWords.add("parserRule");
+ }
+
+
+}
diff --git a/tool/src/org/antlr/v4/gui/BasicFontMetrics.java b/tool/src/org/antlr/v4/gui/BasicFontMetrics.java
new file mode 100644
index 0000000..b424a00
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/BasicFontMetrics.java
@@ -0,0 +1,95 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+/** Font metrics. The only way to generate accurate images
+ * in any format that contain text is to know the font metrics.
+ * Specifically, we need to know the width of every character and the
+ * maximum height (since we want all characters to fit within same line height).
+ * I used ttf2tfm to dump the font metrics from Mac TrueType fonts and
+ * then converted that to a Java class for use in a PostScript generator
+ * for trees. Commands:
+ *
+ *
+ *
+ * Then run metrics into python code after stripping header/footer:
+ *
+ *
+ * #
+ * # Process lines from ttf2tfm that look like this:
+ * # Glyph Code Glyph Name Width llx lly urx ury
+ * # ------------------------------------------------------------------------
+ * # 3 00020 space 333 0, 0 -- 0, 0
+ * #
+ * lines = open("metrics").read().split('\n')
+ * print "public class FontName {"
+ * print " {"
+ * maxh = 0;
+ * for line in lines[4:]: # skip header 0..3
+ * all = line.split(' ')
+ * words = [x for x in all if len(x)>0]
+ * ascii = int(words[1], 16)
+ * height = int(words[8])
+ * if height>maxh: maxh = height
+ * if ascii>=128: break
+ * print " widths[%d] = %s; // %s" % (ascii, words[3], words[2])
+ *
+ * print " maxCharHeight = "+str(maxh)+";"
+ * print " }"
+ * print "}"
+ *
+ *
+ * Units are 1000th of an 'em'.
+ */
+public abstract class BasicFontMetrics {
+ public static final int MAX_CHAR = '\u00FF';
+ protected int maxCharHeight;
+ protected int[] widths = new int[MAX_CHAR+1];
+
+ public double getWidth(String s, int fontSize) {
+ double w = 0;
+ for (char c : s.toCharArray()) {
+ w += getWidth(c, fontSize);
+ }
+ return w;
+ }
+
+ public double getWidth(char c, int fontSize) {
+ if ( c > MAX_CHAR || widths[c]==0 ) return widths['m']/1000.0; // return width('m')
+ return widths[c]/1000.0 * fontSize;
+ }
+
+ public double getLineHeight(int fontSize) {
+ return maxCharHeight / 1000.0 * fontSize;
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/GraphicsSupport.java b/tool/src/org/antlr/v4/gui/GraphicsSupport.java
new file mode 100644
index 0000000..eb9546d
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/GraphicsSupport.java
@@ -0,0 +1,138 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import javax.imageio.ImageIO;
+import javax.print.DocFlavor;
+import javax.print.DocPrintJob;
+import javax.print.PrintException;
+import javax.print.PrintService;
+import javax.print.SimpleDoc;
+import javax.print.StreamPrintServiceFactory;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.awt.print.PageFormat;
+import java.awt.print.Printable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+public class GraphicsSupport {
+ /**
+ [The "BSD license"]
+ Copyright (c) 2011 Cay Horstmann
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+ public static void saveImage(final JComponent comp, String fileName)
+ throws IOException, PrintException
+ {
+ if ( fileName.endsWith(".ps") || fileName.endsWith(".eps") ) {
+ DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
+ String mimeType = "application/postscript";
+ StreamPrintServiceFactory[] factories =
+ StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType);
+ System.out.println(Arrays.toString(factories));
+ if (factories.length > 0) {
+ FileOutputStream out = new FileOutputStream(fileName);
+ PrintService service = factories[0].getPrintService(out);
+ SimpleDoc doc = new SimpleDoc(new Printable() {
+ @Override
+ public int print(Graphics g, PageFormat pf, int page) {
+ if (page >= 1) return Printable.NO_SUCH_PAGE;
+ else {
+ Graphics2D g2 = (Graphics2D) g;
+ g2.translate((pf.getWidth() - pf.getImageableWidth()) / 2,
+ (pf.getHeight() - pf.getImageableHeight()) / 2);
+ if ( comp.getWidth() > pf.getImageableWidth() ||
+ comp.getHeight() > pf.getImageableHeight() )
+ {
+ double sf1 = pf.getImageableWidth() / (comp.getWidth() + 1);
+ double sf2 = pf.getImageableHeight() / (comp.getHeight() + 1);
+ double s = Math.min(sf1, sf2);
+ g2.scale(s, s);
+ }
+
+ comp.paint(g);
+ return Printable.PAGE_EXISTS;
+ }
+ }
+ }, flavor, null);
+ DocPrintJob job = service.createPrintJob();
+ PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
+ job.print(doc, attributes);
+ out.close();
+ }
+ } else {
+ // parrt: works with [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
+ Rectangle rect = comp.getBounds();
+ BufferedImage image = new BufferedImage(rect.width, rect.height,
+ BufferedImage.TYPE_INT_RGB);
+ Graphics2D g = (Graphics2D) image.getGraphics();
+ g.setColor(Color.WHITE);
+ g.fill(rect);
+// g.setColor(Color.BLACK);
+ comp.paint(g);
+ String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
+ boolean result = ImageIO.write(image, extension, new File(fileName));
+ if ( !result ) {
+ System.err.println("Now imager for " + extension);
+ }
+ g.dispose();
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/JFileChooserConfirmOverwrite.java b/tool/src/org/antlr/v4/gui/JFileChooserConfirmOverwrite.java
new file mode 100644
index 0000000..59cbb69
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/JFileChooserConfirmOverwrite.java
@@ -0,0 +1,63 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2013 Terence Parr
+ * Copyright (c) 2013 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.gui;
+
+import javax.swing.*;
+import java.io.File;
+
+/**
+ *
+ * @author Sam Harwell
+ */
+public class JFileChooserConfirmOverwrite extends JFileChooser {
+
+ public JFileChooserConfirmOverwrite() {
+ setMultiSelectionEnabled(false);
+ }
+
+ @Override
+ public void approveSelection() {
+ File selectedFile = getSelectedFile();
+
+ if (selectedFile.exists()) {
+ int answer = JOptionPane.showConfirmDialog(this,
+ "Overwrite existing file?",
+ "Overwrite?",
+ JOptionPane.YES_NO_OPTION);
+ if (answer != JOptionPane.YES_OPTION) {
+ // do not call super.approveSelection
+ return;
+ }
+ }
+
+ super.approveSelection();
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/gui/PostScriptDocument.java b/tool/src/org/antlr/v4/gui/PostScriptDocument.java
new file mode 100644
index 0000000..7faf2db
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/PostScriptDocument.java
@@ -0,0 +1,212 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import java.awt.*;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class PostScriptDocument {
+ public static final String DEFAULT_FONT = "CourierNew";
+
+ public static final Map POSTSCRIPT_FONT_NAMES;
+ static {
+ POSTSCRIPT_FONT_NAMES = new HashMap();
+ POSTSCRIPT_FONT_NAMES.put(Font.SANS_SERIF + ".plain", "ArialMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SANS_SERIF + ".bold", "Arial-BoldMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SANS_SERIF + ".italic", "Arial-ItalicMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SANS_SERIF + ".bolditalic", "Arial-BoldItalicMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SERIF + ".plain", "TimesNewRomanPSMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SERIF + ".bold", "TimesNewRomanPS-BoldMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SERIF + ".italic", "TimesNewRomanPS-ItalicMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.SERIF + ".bolditalic", "TimesNewRomanPS-BoldItalicMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.MONOSPACED + ".plain", "CourierNewPSMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.MONOSPACED + ".bold", "CourierNewPS-BoldMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.MONOSPACED + ".italic", "CourierNewPS-ItalicMT");
+ POSTSCRIPT_FONT_NAMES.put(Font.MONOSPACED + ".bolditalic", "CourierNewPS-BoldItalicMT");
+ }
+
+ protected int boundingBoxWidth;
+ protected int boundingBoxHeight;
+
+ protected SystemFontMetrics fontMetrics;
+ protected String fontName;
+ protected int fontSize = 12;
+ protected double lineWidth = 0.3;
+ protected String boundingBox;
+
+ protected StringBuilder ps = new StringBuilder();
+ protected boolean closed = false;
+
+ public PostScriptDocument() {
+ this(DEFAULT_FONT, 12);
+ }
+
+ public PostScriptDocument(String fontName, int fontSize) {
+ header();
+ setFont(fontName, fontSize);
+ }
+
+ public String getPS() {
+ close();
+ return header()+ps.toString();
+ }
+
+ public void boundingBox(int w, int h) {
+ boundingBoxWidth = w;
+ boundingBoxHeight = h;
+ boundingBox = String.format(Locale.US, "%%%%BoundingBox: %d %d %d %d\n", 0,0,
+ boundingBoxWidth,boundingBoxHeight);
+ }
+
+ public void close() {
+ if ( closed ) return;
+// ps.append("showpage\n");
+ ps.append("%%Trailer\n");
+ closed = true;
+ }
+
+ /** Compute the header separately because we need to wait for the bounding box */
+ protected StringBuilder header() {
+ StringBuilder b = new StringBuilder();
+ b.append("%!PS-Adobe-3.0 EPSF-3.0\n");
+ b.append(boundingBox).append("\n");
+ b.append("0.3 setlinewidth\n");
+ b.append("%% x y w h highlight\n" +
+ "/highlight {\n" +
+ " 4 dict begin\n" +
+ " /h exch def\n" +
+ " /w exch def\n" +
+ " /y exch def\n" +
+ " /x exch def\n" +
+ " gsave\n" +
+ " newpath\n" +
+ " x y moveto\n" +
+ " 0 h rlineto % up to left corner\n" +
+ " w 0 rlineto % to upper right corner\n" +
+ " 0 h neg rlineto % to lower right corner\n" +
+ " w neg 0 rlineto % back home to lower left corner\n" +
+ " closepath\n" +
+ " .95 .83 .82 setrgbcolor\n" +
+ " fill\n" +
+ " grestore\n" +
+ " end\n" +
+ "} def\n");
+
+ return b;
+ }
+
+ public void setFont(String fontName, int fontSize) {
+ this.fontMetrics = new SystemFontMetrics(fontName);
+ this.fontName = fontMetrics.getFont().getPSName();
+ this.fontSize = fontSize;
+
+ String psname = POSTSCRIPT_FONT_NAMES.get(this.fontName);
+ if (psname == null) {
+ psname = this.fontName;
+ }
+
+ ps.append(String.format(Locale.US, "/%s findfont %d scalefont setfont\n", psname, fontSize));
+ }
+
+ public void lineWidth(double w) {
+ lineWidth = w;
+ ps.append(w).append(" setlinewidth\n");
+ }
+
+ public void move(double x, double y) {
+ ps.append(String.format(Locale.US, "%1.3f %1.3f moveto\n", x, y));
+ }
+
+ public void lineto(double x, double y) {
+ ps.append(String.format(Locale.US, "%1.3f %1.3f lineto\n", x, y));
+ }
+
+ public void line(double x1, double y1, double x2, double y2) {
+ move(x1, y1);
+ lineto(x2, y2);
+ }
+
+ public void rect(double x, double y, double width, double height) {
+ line(x, y, x, y + height);
+ line(x, y + height, x + width, y + height);
+ line(x + width, y + height, x + width, y);
+ line(x + width, y, x, y);
+ }
+
+ /** Make red box */
+ public void highlight(double x, double y, double width, double height) {
+ ps.append(String.format(Locale.US, "%1.3f %1.3f %1.3f %1.3f highlight\n", x, y, width, height));
+ }
+
+ public void stroke() {
+ ps.append("stroke\n");
+ }
+
+// public void rarrow(double x, double y) {
+// ps.append(String.format(Locale.US, "%1.3f %1.3f rarrow\n", x,y));
+// }
+//
+// public void darrow(double x, double y) {
+// ps.append(String.format(Locale.US, "%1.3f %1.3f darrow\n", x,y));
+// }
+
+ public void text(String s, double x, double y) {
+ StringBuilder buf = new StringBuilder();
+ // escape \, (, ): \\, \(, \)
+ for (char c : s.toCharArray()) {
+ switch ( c ) {
+ case '\\' :
+ case '(' :
+ case ')' :
+ buf.append('\\');
+ buf.append(c);
+ break;
+ default :
+ buf.append(c);
+ break;
+ }
+ }
+ s = buf.toString();
+ move(x,y);
+ ps.append(String.format(Locale.US, "(%s) show\n", s));
+ stroke();
+ }
+
+ // courier new: wid/hei 7.611979 10.0625
+ /** All chars are 600 thousands of an 'em' wide if courier */
+ public double getWidth(char c) { return fontMetrics.getWidth(c, fontSize); }
+ public double getWidth(String s) { return fontMetrics.getWidth(s, fontSize); }
+ public double getLineHeight() { return fontMetrics.getLineHeight(fontSize); }
+
+ public int getFontSize() { return fontSize; }
+}
diff --git a/tool/src/org/antlr/v4/gui/SystemFontMetrics.java b/tool/src/org/antlr/v4/gui/SystemFontMetrics.java
new file mode 100644
index 0000000..fb2b17a
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/SystemFontMetrics.java
@@ -0,0 +1,63 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import java.awt.*;
+import java.awt.font.FontRenderContext;
+import java.awt.font.TextLayout;
+import java.awt.image.BufferedImage;
+
+/**
+ *
+ * @author Sam Harwell
+ */
+public class SystemFontMetrics extends BasicFontMetrics {
+ protected final Font font;
+
+ public SystemFontMetrics(String fontName) {
+ BufferedImage img = new BufferedImage(40, 40, BufferedImage.TYPE_4BYTE_ABGR);
+ Graphics2D graphics = GraphicsEnvironment.getLocalGraphicsEnvironment().createGraphics(img);
+ FontRenderContext fontRenderContext = graphics.getFontRenderContext();
+ this.font = new Font(fontName, Font.PLAIN, 1000);
+ double maxHeight = 0;
+ for (int i = 0; i < 255; i++) {
+ TextLayout layout = new TextLayout(Character.toString((char)i), font, fontRenderContext);
+ maxHeight = Math.max(maxHeight, layout.getBounds().getHeight());
+ super.widths[i] = (int)layout.getAdvance();
+ }
+
+ super.maxCharHeight = (int)Math.round(maxHeight);
+ }
+
+ public Font getFont() {
+ return font;
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/TestRig.java b/tool/src/org/antlr/v4/gui/TestRig.java
new file mode 100644
index 0000000..4812e5c
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/TestRig.java
@@ -0,0 +1,270 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.DiagnosticErrorListener;
+import org.antlr.v4.runtime.Lexer;
+import org.antlr.v4.runtime.Parser;
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.antlr.v4.runtime.TokenStream;
+import org.antlr.v4.runtime.atn.PredictionMode;
+
+import javax.print.PrintException;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+/** Run a lexer/parser combo, optionally printing tree string or generating
+ * postscript file. Optionally taking input file.
+ *
+ * $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName
+ * [-tree]
+ * [-tokens] [-gui] [-ps file.ps]
+ * [-trace]
+ * [-diagnostics]
+ * [-SLL]
+ * [input-filename(s)]
+ */
+public class TestRig {
+ public static final String LEXER_START_RULE_NAME = "tokens";
+
+ protected String grammarName;
+ protected String startRuleName;
+ protected final List inputFiles = new ArrayList();
+ protected boolean printTree = false;
+ protected boolean gui = false;
+ protected String psFile = null;
+ protected boolean showTokens = false;
+ protected boolean trace = false;
+ protected boolean diagnostics = false;
+ protected String encoding = null;
+ protected boolean SLL = false;
+
+ public TestRig(String[] args) throws Exception {
+ if ( args.length < 2 ) {
+ System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n" +
+ " [-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]\n" +
+ " [-trace] [-diagnostics] [-SLL]\n"+
+ " [input-filename(s)]");
+ System.err.println("Use startRuleName='tokens' if GrammarName is a lexer grammar.");
+ System.err.println("Omitting input-filename makes rig read from stdin.");
+ return;
+ }
+ int i=0;
+ grammarName = args[i];
+ i++;
+ startRuleName = args[i];
+ i++;
+ while ( i=args.length ) {
+ System.err.println("missing encoding on -encoding");
+ return;
+ }
+ encoding = args[i];
+ i++;
+ }
+ else if ( arg.equals("-ps") ) {
+ if ( i>=args.length ) {
+ System.err.println("missing filename on -ps");
+ return;
+ }
+ psFile = args[i];
+ i++;
+ }
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ TestRig testRig = new TestRig(args);
+ if(args.length >= 2) {
+ testRig.process();
+ }
+ }
+
+ public void process() throws Exception {
+// System.out.println("exec "+grammarName+"."+startRuleName);
+ String lexerName = grammarName+"Lexer";
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ Class extends Lexer> lexerClass = null;
+ try {
+ lexerClass = cl.loadClass(lexerName).asSubclass(Lexer.class);
+ }
+ catch (java.lang.ClassNotFoundException cnfe) {
+ // might be pure lexer grammar; no Lexer suffix then
+ lexerName = grammarName;
+ try {
+ lexerClass = cl.loadClass(lexerName).asSubclass(Lexer.class);
+ }
+ catch (ClassNotFoundException cnfe2) {
+ System.err.println("Can't load "+lexerName+" as lexer or parser");
+ return;
+ }
+ }
+
+ Constructor extends Lexer> lexerCtor = lexerClass.getConstructor(CharStream.class);
+ Lexer lexer = lexerCtor.newInstance((CharStream)null);
+
+ Class extends Parser> parserClass = null;
+ Parser parser = null;
+ if ( !startRuleName.equals(LEXER_START_RULE_NAME) ) {
+ String parserName = grammarName+"Parser";
+ parserClass = cl.loadClass(parserName).asSubclass(Parser.class);
+ if ( parserClass==null ) {
+ System.err.println("Can't load "+parserName);
+ }
+ Constructor extends Parser> parserCtor = parserClass.getConstructor(TokenStream.class);
+ parser = parserCtor.newInstance((TokenStream)null);
+ }
+
+ if ( inputFiles.size()==0 ) {
+ InputStream is = System.in;
+ Reader r;
+ if ( encoding!=null ) {
+ r = new InputStreamReader(is, encoding);
+ }
+ else {
+ r = new InputStreamReader(is);
+ }
+
+ process(lexer, parserClass, parser, is, r);
+ return;
+ }
+ for (String inputFile : inputFiles) {
+ InputStream is = System.in;
+ if ( inputFile!=null ) {
+ is = new FileInputStream(inputFile);
+ }
+ Reader r;
+ if ( encoding!=null ) {
+ r = new InputStreamReader(is, encoding);
+ }
+ else {
+ r = new InputStreamReader(is);
+ }
+
+ if ( inputFiles.size()>1 ) {
+ System.err.println(inputFile);
+ }
+ process(lexer, parserClass, parser, is, r);
+ }
+ }
+
+ protected void process(Lexer lexer, Class extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
+ try {
+ ANTLRInputStream input = new ANTLRInputStream(r);
+ lexer.setInputStream(input);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+
+ tokens.fill();
+
+ if ( showTokens ) {
+ for (Object tok : tokens.getTokens()) {
+ System.out.println(tok);
+ }
+ }
+
+ if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return;
+
+ if ( diagnostics ) {
+ parser.addErrorListener(new DiagnosticErrorListener());
+ parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
+ }
+
+ if ( printTree || gui || psFile!=null ) {
+ parser.setBuildParseTree(true);
+ }
+
+ if ( SLL ) { // overrides diagnostics
+ parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
+ }
+
+ parser.setTokenStream(tokens);
+ parser.setTrace(trace);
+
+ try {
+ Method startRule = parserClass.getMethod(startRuleName);
+ ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null);
+
+ if ( printTree ) {
+ System.out.println(tree.toStringTree(parser));
+ }
+ if ( gui ) {
+ Trees.inspect(tree, parser);
+ }
+ if ( psFile!=null ) {
+ Trees.save(tree, parser, psFile); // Generate postscript
+ }
+ }
+ catch (NoSuchMethodException nsme) {
+ System.err.println("No method for rule "+startRuleName+" or it has arguments");
+ }
+ }
+ finally {
+ if ( r!=null ) r.close();
+ if ( is!=null ) is.close();
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/TreeLayoutAdaptor.java b/tool/src/org/antlr/v4/gui/TreeLayoutAdaptor.java
new file mode 100644
index 0000000..6024e3b
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/TreeLayoutAdaptor.java
@@ -0,0 +1,149 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import org.abego.treelayout.TreeForTreeLayout;
+import org.antlr.v4.runtime.tree.Tree;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/** Adaptor ANTLR trees to {@link TreeForTreeLayout}. */
+public class TreeLayoutAdaptor implements TreeForTreeLayout {
+ private static class AntlrTreeChildrenIterable implements Iterable {
+ private final Tree tree;
+
+ public AntlrTreeChildrenIterable(Tree tree) {
+ this.tree = tree;
+ }
+
+ @Override
+ public Iterator iterator() {
+ return new Iterator() {
+ private int i = 0;
+
+ @Override
+ public boolean hasNext() {
+ return tree.getChildCount() > i;
+ }
+
+ @Override
+ public Tree next() {
+ if (!hasNext())
+ throw new NoSuchElementException();
+
+ return tree.getChild(i++);
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ }
+
+ private static class AntlrTreeChildrenReverseIterable implements
+ Iterable
+ {
+ private final Tree tree;
+
+ public AntlrTreeChildrenReverseIterable(Tree tree) {
+ this.tree = tree;
+ }
+
+ @Override
+ public Iterator iterator() {
+ return new Iterator() {
+ private int i = tree.getChildCount();
+
+ @Override
+ public boolean hasNext() {
+ return i > 0;
+ }
+
+ @Override
+ public Tree next() {
+ if (!hasNext())
+ throw new NoSuchElementException();
+
+ return tree.getChild(--i);
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ }
+
+ private Tree root;
+
+ public TreeLayoutAdaptor(Tree root) {
+ this.root = root;
+ }
+
+ @Override
+ public boolean isLeaf(Tree node) {
+ return node.getChildCount() == 0;
+ }
+
+ @Override
+ public boolean isChildOfParent(Tree node, Tree parentNode) {
+ return node.getParent() == parentNode;
+ }
+
+ @Override
+ public Tree getRoot() {
+ return root;
+ }
+
+ @Override
+ public Tree getLastChild(Tree parentNode) {
+ return parentNode.getChild(parentNode.getChildCount() - 1);
+ }
+
+ @Override
+ public Tree getFirstChild(Tree parentNode) {
+ return parentNode.getChild(0);
+ }
+
+ @Override
+ public Iterable getChildrenReverse(Tree node) {
+ return new AntlrTreeChildrenReverseIterable(node);
+ }
+
+ @Override
+ public Iterable getChildren(Tree node) {
+ return new AntlrTreeChildrenIterable(node);
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/TreePostScriptGenerator.java b/tool/src/org/antlr/v4/gui/TreePostScriptGenerator.java
new file mode 100644
index 0000000..d78a1c8
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/TreePostScriptGenerator.java
@@ -0,0 +1,176 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import org.abego.treelayout.Configuration;
+import org.abego.treelayout.NodeExtentProvider;
+import org.abego.treelayout.TreeForTreeLayout;
+import org.abego.treelayout.TreeLayout;
+import org.abego.treelayout.util.DefaultConfiguration;
+import org.antlr.v4.runtime.misc.Utils;
+import org.antlr.v4.runtime.tree.ErrorNode;
+import org.antlr.v4.runtime.tree.Tree;
+
+import java.awt.*;
+import java.awt.geom.Rectangle2D;
+import java.util.List;
+
+public class TreePostScriptGenerator {
+ public class VariableExtentProvide implements NodeExtentProvider {
+ @Override
+ public double getWidth(Tree tree) {
+ String s = getText(tree);
+ return doc.getWidth(s) + nodeWidthPadding*2;
+ }
+
+ @Override
+ public double getHeight(Tree tree) {
+ String s = getText(tree);
+ double h =
+ doc.getLineHeight() + nodeHeightPaddingAbove + nodeHeightPaddingBelow;
+ String[] lines = s.split("\n");
+ return h * lines.length;
+ }
+ }
+
+ protected double gapBetweenLevels = 17;
+ protected double gapBetweenNodes = 7;
+ protected int nodeWidthPadding = 1; // added to left/right
+ protected int nodeHeightPaddingAbove = 0;
+ protected int nodeHeightPaddingBelow = 5;
+
+ protected Tree root;
+ protected TreeTextProvider treeTextProvider;
+ protected TreeLayout treeLayout;
+
+ protected PostScriptDocument doc;
+
+ public TreePostScriptGenerator(List ruleNames, Tree root) {
+ this(ruleNames, root, PostScriptDocument.DEFAULT_FONT, 11);
+ }
+
+ public TreePostScriptGenerator(List ruleNames, Tree root,
+ String fontName, int fontSize)
+ {
+ this.root = root;
+ setTreeTextProvider(new TreeViewer.DefaultTreeTextProvider(ruleNames));
+ doc = new PostScriptDocument(fontName, fontSize);
+ boolean compareNodeIdentities = true;
+ this.treeLayout =
+ new TreeLayout(getTreeLayoutAdaptor(root),
+ new VariableExtentProvide(),
+ new DefaultConfiguration(gapBetweenLevels,
+ gapBetweenNodes,
+ Configuration.Location.Bottom),
+ compareNodeIdentities);
+ }
+
+ /** Get an adaptor for root that indicates how to walk ANTLR trees.
+ * Override to change the adapter from the default of {@link TreeLayoutAdaptor} */
+ public TreeForTreeLayout getTreeLayoutAdaptor(Tree root) {
+ return new TreeLayoutAdaptor(root);
+ }
+
+ public String getPS() {
+ // generate the edges and boxes (with text)
+ generateEdges(getTree().getRoot());
+ for (Tree node : treeLayout.getNodeBounds().keySet()) {
+ generateNode(node);
+ }
+
+ Dimension size = treeLayout.getBounds().getBounds().getSize();
+ doc.boundingBox(size.width, size.height);
+ doc.close();
+ return doc.getPS();
+ }
+
+ protected void generateEdges(Tree parent) {
+ if (!getTree().isLeaf(parent)) {
+ Rectangle2D.Double parentBounds = getBoundsOfNode(parent);
+// System.out.println("%% parent("+getText(parent)+")="+parentBounds);
+ double x1 = parentBounds.getCenterX();
+ double y1 = parentBounds.y;
+ for (Tree child : getChildren(parent)) {
+ Rectangle2D.Double childBounds = getBoundsOfNode(child);
+// System.out.println("%% child("+getText(child)+")="+childBounds);
+ double x2 = childBounds.getCenterX();
+ double y2 = childBounds.getMaxY();
+ doc.line(x1, y1, x2, y2);
+ generateEdges(child);
+ }
+ }
+ }
+
+ protected void generateNode(Tree t) {
+ // draw the text on top of the box (possibly multiple lines)
+ String[] lines = getText(t).split("\n");
+ Rectangle2D.Double box = getBoundsOfNode(t);
+ // for debugging, turn this on to see boundingbox of nodes
+ //doc.rect(box.x, box.y, box.width, box.height);
+ // make error nodes from parse tree red by default
+ if ( t instanceof ErrorNode ) {
+ doc.highlight(box.x, box.y, box.width, box.height);
+ }
+ double x = box.x+nodeWidthPadding;
+ double y = box.y+nodeHeightPaddingBelow;
+ for (int i = 0; i < lines.length; i++) {
+ doc.text(lines[i], x, y);
+ y += doc.getLineHeight();
+ }
+ }
+
+ protected TreeForTreeLayout getTree() {
+ return treeLayout.getTree();
+ }
+
+ protected Iterable getChildren(Tree parent) {
+ return getTree().getChildren(parent);
+ }
+
+ protected Rectangle2D.Double getBoundsOfNode(Tree node) {
+ return treeLayout.getNodeBounds().get(node);
+ }
+
+ protected String getText(Tree tree) {
+ String s = treeTextProvider.getText(tree);
+ s = Utils.escapeWhitespace(s, false);
+ return s;
+ }
+
+ public TreeTextProvider getTreeTextProvider() {
+ return treeTextProvider;
+ }
+
+ public void setTreeTextProvider(TreeTextProvider treeTextProvider) {
+ this.treeTextProvider = treeTextProvider;
+ }
+
+}
diff --git a/tool/src/org/antlr/v4/gui/TreeTextProvider.java b/tool/src/org/antlr/v4/gui/TreeTextProvider.java
new file mode 100644
index 0000000..9a88fc0
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/TreeTextProvider.java
@@ -0,0 +1,37 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import org.antlr.v4.runtime.tree.Tree;
+
+public interface TreeTextProvider {
+ String getText(Tree node);
+}
diff --git a/tool/src/org/antlr/v4/gui/TreeViewer.java b/tool/src/org/antlr/v4/gui/TreeViewer.java
new file mode 100644
index 0000000..0ca15f3
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/TreeViewer.java
@@ -0,0 +1,764 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.gui;
+
+import org.abego.treelayout.NodeExtentProvider;
+import org.abego.treelayout.TreeForTreeLayout;
+import org.abego.treelayout.TreeLayout;
+import org.abego.treelayout.util.DefaultConfiguration;
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.antlr.v4.runtime.misc.Utils;
+import org.antlr.v4.runtime.tree.ErrorNode;
+import org.antlr.v4.runtime.tree.Tree;
+import org.antlr.v4.runtime.tree.Trees;
+
+import javax.imageio.ImageIO;
+import javax.print.PrintException;
+import javax.swing.*;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.TreeSelectionEvent;
+import javax.swing.event.TreeSelectionListener;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.TreePath;
+import javax.swing.tree.TreeSelectionModel;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
+import java.awt.geom.CubicCurve2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.prefs.Preferences;
+
+public class TreeViewer extends JComponent {
+ public static final Color LIGHT_RED = new Color(244, 213, 211);
+
+ public static class DefaultTreeTextProvider implements TreeTextProvider {
+ private final List ruleNames;
+
+ public DefaultTreeTextProvider(List ruleNames) {
+ this.ruleNames = ruleNames;
+ }
+
+ @Override
+ public String getText(Tree node) {
+ return String.valueOf(Trees.getNodeText(node, ruleNames));
+ }
+ }
+
+ public static class VariableExtentProvide implements NodeExtentProvider {
+ TreeViewer viewer;
+ public VariableExtentProvide(TreeViewer viewer) {
+ this.viewer = viewer;
+ }
+ @Override
+ public double getWidth(Tree tree) {
+ FontMetrics fontMetrics = viewer.getFontMetrics(viewer.font);
+ String s = viewer.getText(tree);
+ int w = fontMetrics.stringWidth(s) + viewer.nodeWidthPadding*2;
+ return w;
+ }
+
+ @Override
+ public double getHeight(Tree tree) {
+ FontMetrics fontMetrics = viewer.getFontMetrics(viewer.font);
+ int h = fontMetrics.getHeight() + viewer.nodeHeightPadding*2;
+ String s = viewer.getText(tree);
+ String[] lines = s.split("\n");
+ return h * lines.length;
+ }
+ }
+
+ protected TreeTextProvider treeTextProvider;
+ protected TreeLayout treeLayout;
+ protected java.util.List highlightedNodes;
+
+ protected String fontName = "Helvetica"; //Font.SANS_SERIF;
+ protected int fontStyle = Font.PLAIN;
+ protected int fontSize = 11;
+ protected Font font = new Font(fontName, fontStyle, fontSize);
+
+ protected double gapBetweenLevels = 17;
+ protected double gapBetweenNodes = 7;
+ protected int nodeWidthPadding = 2; // added to left/right
+ protected int nodeHeightPadding = 0; // added above/below
+ protected int arcSize = 0; // make an arc in node outline?
+
+ protected double scale = 1.0;
+
+ protected Color boxColor = null; // set to a color to make it draw background
+
+ protected Color highlightedBoxColor = Color.lightGray;
+ protected Color borderColor = null;
+ protected Color textColor = Color.black;
+
+ public TreeViewer(List ruleNames, Tree tree) {
+ setRuleNames(ruleNames);
+ if ( tree!=null ) {
+ setTree(tree);
+ }
+ setFont(font);
+ }
+
+ private void updatePreferredSize() {
+ setPreferredSize(getScaledTreeSize());
+ invalidate();
+ if (getParent() != null) {
+ getParent().validate();
+ }
+ repaint();
+ }
+
+ // ---------------- PAINT -----------------------------------------------
+
+ private boolean useCurvedEdges = false;
+
+ public boolean getUseCurvedEdges() {
+ return useCurvedEdges;
+ }
+
+ public void setUseCurvedEdges(boolean useCurvedEdges) {
+ this.useCurvedEdges = useCurvedEdges;
+ }
+
+ protected void paintEdges(Graphics g, Tree parent) {
+ if (!getTree().isLeaf(parent)) {
+ BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
+ BasicStroke.JOIN_ROUND);
+ ((Graphics2D)g).setStroke(stroke);
+
+ Rectangle2D.Double parentBounds = getBoundsOfNode(parent);
+ double x1 = parentBounds.getCenterX();
+ double y1 = parentBounds.getMaxY();
+ for (Tree child : getTree().getChildren(parent)) {
+ Rectangle2D.Double childBounds = getBoundsOfNode(child);
+ double x2 = childBounds.getCenterX();
+ double y2 = childBounds.getMinY();
+ if (getUseCurvedEdges()) {
+ CubicCurve2D c = new CubicCurve2D.Double();
+ double ctrlx1 = x1;
+ double ctrly1 = (y1+y2)/2;
+ double ctrlx2 = x2;
+ double ctrly2 = y1;
+ c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
+ ((Graphics2D) g).draw(c);
+ } else {
+ g.drawLine((int) x1, (int) y1,
+ (int) x2, (int) y2);
+ }
+ paintEdges(g, child);
+ }
+ }
+ }
+
+ protected void paintBox(Graphics g, Tree tree) {
+ Rectangle2D.Double box = getBoundsOfNode(tree);
+ // draw the box in the background
+ boolean ruleFailedAndMatchedNothing = false;
+ if ( tree instanceof ParserRuleContext ) {
+ ParserRuleContext ctx = (ParserRuleContext) tree;
+ ruleFailedAndMatchedNothing = ctx.exception != null &&
+ ctx.stop != null && ctx.stop.getTokenIndex() < ctx.start.getTokenIndex();
+ }
+ if ( isHighlighted(tree) || boxColor!=null ||
+ tree instanceof ErrorNode ||
+ ruleFailedAndMatchedNothing)
+ {
+ if ( isHighlighted(tree) ) g.setColor(highlightedBoxColor);
+ else if ( tree instanceof ErrorNode || ruleFailedAndMatchedNothing ) g.setColor(LIGHT_RED);
+ else g.setColor(boxColor);
+ g.fillRoundRect((int) box.x, (int) box.y, (int) box.width - 1,
+ (int) box.height - 1, arcSize, arcSize);
+ }
+ if ( borderColor!=null ) {
+ g.setColor(borderColor);
+ g.drawRoundRect((int) box.x, (int) box.y, (int) box.width - 1,
+ (int) box.height - 1, arcSize, arcSize);
+ }
+
+ // draw the text on top of the box (possibly multiple lines)
+ g.setColor(textColor);
+ String s = getText(tree);
+ String[] lines = s.split("\n");
+ FontMetrics m = getFontMetrics(font);
+ int x = (int) box.x + arcSize / 2 + nodeWidthPadding;
+ int y = (int) box.y + m.getAscent() + m.getLeading() + 1 + nodeHeightPadding;
+ for (int i = 0; i < lines.length; i++) {
+ text(g, lines[i], x, y);
+ y += m.getHeight();
+ }
+ }
+
+ public void text(Graphics g, String s, int x, int y) {
+// System.out.println("drawing '"+s+"' @ "+x+","+y);
+ s = Utils.escapeWhitespace(s, true);
+ g.drawString(s, x, y);
+ }
+
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+
+ if ( treeLayout==null ) {
+ return;
+ }
+
+ Graphics2D g2 = (Graphics2D)g;
+ // anti-alias the lines
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+
+ // Anti-alias the text
+ g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
+ RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+// AffineTransform at = g2.getTransform();
+// g2.scale(
+// (double) this.getWidth() / 400,
+// (double) this.getHeight() / 400);
+//
+// g2.setTransform(at);
+
+ paintEdges(g, getTree().getRoot());
+
+ // paint the boxes
+ for (Tree Tree : treeLayout.getNodeBounds().keySet()) {
+ paintBox(g, Tree);
+ }
+ }
+
+ @Override
+ protected Graphics getComponentGraphics(Graphics g) {
+ Graphics2D g2d=(Graphics2D)g;
+ g2d.scale(scale, scale);
+ return super.getComponentGraphics(g2d);
+ }
+
+ // ----------------------------------------------------------------------
+
+
+ private static final String DIALOG_WIDTH_PREFS_KEY = "dialog_width";
+ private static final String DIALOG_HEIGHT_PREFS_KEY = "dialog_height";
+ private static final String DIALOG_X_PREFS_KEY = "dialog_x";
+ private static final String DIALOG_Y_PREFS_KEY = "dialog_y";
+ private static final String DIALOG_DIVIDER_LOC_PREFS_KEY = "dialog_divider_location";
+ private static final String DIALOG_VIEWER_SCALE_PREFS_KEY = "dialog_viewer_scale";
+
+ protected static JDialog showInDialog(final TreeViewer viewer) {
+ final JDialog dialog = new JDialog();
+ dialog.setTitle("Parse Tree Inspector");
+
+ final Preferences prefs = Preferences.userNodeForPackage(TreeViewer.class);
+
+ // Make new content panes
+ final Container mainPane = new JPanel(new BorderLayout(5,5));
+ final Container contentPane = new JPanel(new BorderLayout(0,0));
+ contentPane.setBackground(Color.white);
+
+ // Wrap viewer in scroll pane
+ JScrollPane scrollPane = new JScrollPane(viewer);
+ // Make the scrollpane (containing the viewer) the center component
+ contentPane.add(scrollPane, BorderLayout.CENTER);
+
+ JPanel wrapper = new JPanel(new FlowLayout());
+
+ // Add button to bottom
+ JPanel bottomPanel = new JPanel(new BorderLayout(0,0));
+ contentPane.add(bottomPanel, BorderLayout.SOUTH);
+
+ JButton ok = new JButton("OK");
+ ok.addActionListener(
+ new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
+ }
+ }
+ );
+ wrapper.add(ok);
+
+ // Add an export-to-png button right of the "OK" button
+ JButton png = new JButton("Export as PNG");
+ png.addActionListener(
+ new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ generatePNGFile(viewer, dialog);
+ }
+ }
+ );
+ wrapper.add(png);
+
+ bottomPanel.add(wrapper, BorderLayout.SOUTH);
+
+ // Add scale slider
+ double lastKnownViewerScale = prefs.getDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
+ viewer.setScale(lastKnownViewerScale);
+
+ int sliderValue = (int) ((lastKnownViewerScale - 1.0) * 1000);
+ final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL, -999, 1000, sliderValue);
+
+ scaleSlider.addChangeListener(
+ new ChangeListener() {
+ @Override
+ public void stateChanged(ChangeEvent e) {
+ int v = scaleSlider.getValue();
+ viewer.setScale(v / 1000.0 + 1.0);
+ }
+ }
+ );
+ bottomPanel.add(scaleSlider, BorderLayout.CENTER);
+
+ // Add a JTree representing the parser tree of the input.
+ JPanel treePanel = new JPanel(new BorderLayout(5, 5));
+
+ // An "empty" icon that will be used for the JTree's nodes.
+ Icon empty = new EmptyIcon();
+
+ UIManager.put("Tree.closedIcon", empty);
+ UIManager.put("Tree.openIcon", empty);
+ UIManager.put("Tree.leafIcon", empty);
+
+ Tree parseTreeRoot = viewer.getTree().getRoot();
+ TreeNodeWrapper nodeRoot = new TreeNodeWrapper(parseTreeRoot, viewer);
+ fillTree(nodeRoot, parseTreeRoot, viewer);
+ final JTree tree = new JTree(nodeRoot);
+ tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+
+ tree.addTreeSelectionListener(new TreeSelectionListener() {
+ @Override
+ public void valueChanged(TreeSelectionEvent e) {
+
+ JTree selectedTree = (JTree) e.getSource();
+ TreePath path = selectedTree.getSelectionPath();
+ TreeNodeWrapper treeNode = (TreeNodeWrapper) path.getLastPathComponent();
+
+ // Set the clicked AST.
+ viewer.setTree((Tree) treeNode.getUserObject());
+ }
+ });
+
+ treePanel.add(new JScrollPane(tree));
+
+ // Create the pane for both the JTree and the AST
+ final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
+ treePanel, contentPane);
+
+ mainPane.add(splitPane, BorderLayout.CENTER);
+
+ dialog.setContentPane(mainPane);
+
+ // make viz
+ WindowListener exitListener = new WindowAdapter() {
+ public void windowClosing(WindowEvent e) {
+ prefs.putInt(DIALOG_WIDTH_PREFS_KEY, (int) dialog.getSize().getWidth());
+ prefs.putInt(DIALOG_HEIGHT_PREFS_KEY, (int) dialog.getSize().getHeight());
+ prefs.putDouble(DIALOG_X_PREFS_KEY, dialog.getLocationOnScreen().getX());
+ prefs.putDouble(DIALOG_Y_PREFS_KEY, dialog.getLocationOnScreen().getY());
+ prefs.putInt(DIALOG_DIVIDER_LOC_PREFS_KEY, splitPane.getDividerLocation());
+ prefs.putDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
+
+ dialog.setVisible(false);
+ dialog.dispose();
+ }
+ };
+ dialog.addWindowListener(exitListener);
+ dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+
+ int width = prefs.getInt(DIALOG_WIDTH_PREFS_KEY, 600);
+ int height = prefs.getInt(DIALOG_HEIGHT_PREFS_KEY, 500);
+ dialog.setPreferredSize(new Dimension(width, height));
+ dialog.pack();
+
+ // After pack(): set the divider at 1/3 (200/600) of the frame.
+ int dividerLocation = prefs.getInt(DIALOG_DIVIDER_LOC_PREFS_KEY, 200);
+ splitPane.setDividerLocation(dividerLocation);
+
+ if (prefs.getDouble(DIALOG_X_PREFS_KEY, -1) != -1) {
+ dialog.setLocation(
+ (int)prefs.getDouble(DIALOG_X_PREFS_KEY, 100),
+ (int)prefs.getDouble(DIALOG_Y_PREFS_KEY, 100)
+ );
+ }
+ else {
+ dialog.setLocationRelativeTo(null);
+ }
+
+ dialog.setVisible(true);
+ return dialog;
+ }
+
+ private static void generatePNGFile(TreeViewer viewer, JDialog dialog) {
+ BufferedImage bi = new BufferedImage(viewer.getSize().width,
+ viewer.getSize().height,
+ BufferedImage.TYPE_INT_ARGB);
+ Graphics g = bi.createGraphics();
+ viewer.paint(g);
+ g.dispose();
+
+ try {
+ File suggestedFile = generateNonExistingPngFile();
+ JFileChooser fileChooser = new JFileChooserConfirmOverwrite();
+ fileChooser.setCurrentDirectory(suggestedFile.getParentFile());
+ fileChooser.setSelectedFile(suggestedFile);
+ FileFilter pngFilter = new FileFilter() {
+
+ @Override
+ public boolean accept(File pathname) {
+ if (pathname.isFile()) {
+ return pathname.getName().toLowerCase().endsWith(".png");
+ }
+
+ return true;
+ }
+
+ @Override
+ public String getDescription() {
+ return "PNG Files (*.png)";
+ }
+ };
+
+ fileChooser.addChoosableFileFilter(pngFilter);
+ fileChooser.setFileFilter(pngFilter);
+
+ int returnValue = fileChooser.showSaveDialog(dialog);
+ if (returnValue == JFileChooser.APPROVE_OPTION) {
+ File pngFile = fileChooser.getSelectedFile();
+ ImageIO.write(bi, "png", pngFile);
+
+ try {
+ // Try to open the parent folder using the OS' native file manager.
+ Desktop.getDesktop().open(pngFile.getParentFile());
+ }
+ catch (Exception ex) {
+ // We could not launch the file manager: just show a popup that we
+ // succeeded in saving the PNG file.
+ JOptionPane.showMessageDialog(dialog, "Saved PNG to: " +
+ pngFile.getAbsolutePath());
+ ex.printStackTrace();
+ }
+ }
+ }
+ catch (Exception ex) {
+ JOptionPane.showMessageDialog(dialog,
+ "Could not export to PNG: " + ex.getMessage(),
+ "Error",
+ JOptionPane.ERROR_MESSAGE);
+ ex.printStackTrace();
+ }
+ }
+
+ private static File generateNonExistingPngFile() {
+
+ final String parent = ".";
+ final String name = "antlr4_parse_tree";
+ final String extension = ".png";
+
+ File pngFile = new File(parent, name + extension);
+
+ int counter = 1;
+
+ // Keep looping until we create a File that does not yet exist.
+ while (pngFile.exists()) {
+ pngFile = new File(parent, name + "_"+ counter + extension);
+ counter++;
+ }
+
+ return pngFile;
+ }
+
+ private static void fillTree(TreeNodeWrapper node, Tree tree, TreeViewer viewer) {
+
+ if (tree == null) {
+ return;
+ }
+
+ for (int i = 0; i < tree.getChildCount(); i++) {
+
+ Tree childTree = tree.getChild(i);
+ TreeNodeWrapper childNode = new TreeNodeWrapper(childTree, viewer);
+
+ node.add(childNode);
+
+ fillTree(childNode, childTree, viewer);
+ }
+ }
+
+ private Dimension getScaledTreeSize() {
+ Dimension scaledTreeSize =
+ treeLayout.getBounds().getBounds().getSize();
+ scaledTreeSize = new Dimension((int)(scaledTreeSize.width*scale),
+ (int)(scaledTreeSize.height*scale));
+ return scaledTreeSize;
+ }
+
+
+ public Future open() {
+ final TreeViewer viewer = this;
+ viewer.setScale(1.5);
+ Callable callable = new Callable() {
+ JDialog result;
+
+ @Override
+ public JDialog call() throws Exception {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ result = showInDialog(viewer);
+ }
+ });
+
+ return result;
+ }
+ };
+
+ ExecutorService executor = Executors.newSingleThreadExecutor();
+
+ try {
+ return executor.submit(callable);
+ }
+ finally {
+ executor.shutdown();
+ }
+ }
+
+ public void save(String fileName) throws IOException, PrintException {
+ JDialog dialog = new JDialog();
+ Container contentPane = dialog.getContentPane();
+ ((JComponent) contentPane).setBorder(BorderFactory.createEmptyBorder(
+ 10, 10, 10, 10));
+ contentPane.add(this);
+ contentPane.setBackground(Color.white);
+ dialog.pack();
+ dialog.setLocationRelativeTo(null);
+ dialog.dispose();
+ GraphicsSupport.saveImage(this, fileName);
+ }
+
+ // ---------------------------------------------------
+
+ protected Rectangle2D.Double getBoundsOfNode(Tree node) {
+ return treeLayout.getNodeBounds().get(node);
+ }
+
+ protected String getText(Tree tree) {
+ String s = treeTextProvider.getText(tree);
+ s = Utils.escapeWhitespace(s, true);
+ return s;
+ }
+
+ public TreeTextProvider getTreeTextProvider() {
+ return treeTextProvider;
+ }
+
+ public void setTreeTextProvider(TreeTextProvider treeTextProvider) {
+ this.treeTextProvider = treeTextProvider;
+ }
+
+ public void setFontSize(int sz) {
+ fontSize = sz;
+ font = new Font(fontName, fontStyle, fontSize);
+ }
+
+ public void setFontName(String name) {
+ fontName = name;
+ font = new Font(fontName, fontStyle, fontSize);
+ }
+
+ /** Slow for big lists of highlighted nodes */
+ public void addHighlightedNodes(Collection nodes) {
+ highlightedNodes = new ArrayList();
+ highlightedNodes.addAll(nodes);
+ }
+
+ public void removeHighlightedNodes(Collection nodes) {
+ if ( highlightedNodes!=null ) {
+ // only remove exact objects defined by ==, not equals()
+ for (Tree t : nodes) {
+ int i = getHighlightedNodeIndex(t);
+ if ( i>=0 ) highlightedNodes.remove(i);
+ }
+ }
+ }
+
+ protected boolean isHighlighted(Tree node) {
+ return getHighlightedNodeIndex(node) >= 0;
+ }
+
+ protected int getHighlightedNodeIndex(Tree node) {
+ if ( highlightedNodes==null ) return -1;
+ for (int i = 0; i < highlightedNodes.size(); i++) {
+ Tree t = highlightedNodes.get(i);
+ if ( t == node ) return i;
+ }
+ return -1;
+ }
+
+ @Override
+ public Font getFont() {
+ return font;
+ }
+
+ @Override
+ public void setFont(Font font) {
+ this.font = font;
+ }
+
+ public int getArcSize() {
+ return arcSize;
+ }
+
+ public void setArcSize(int arcSize) {
+ this.arcSize = arcSize;
+ }
+
+ public Color getBoxColor() {
+ return boxColor;
+ }
+
+ public void setBoxColor(Color boxColor) {
+ this.boxColor = boxColor;
+ }
+
+ public Color getHighlightedBoxColor() {
+ return highlightedBoxColor;
+ }
+
+ public void setHighlightedBoxColor(Color highlightedBoxColor) {
+ this.highlightedBoxColor = highlightedBoxColor;
+ }
+
+ public Color getBorderColor() {
+ return borderColor;
+ }
+
+ public void setBorderColor(Color borderColor) {
+ this.borderColor = borderColor;
+ }
+
+ public Color getTextColor() {
+ return textColor;
+ }
+
+ public void setTextColor(Color textColor) {
+ this.textColor = textColor;
+ }
+
+ protected TreeForTreeLayout getTree() {
+ return treeLayout.getTree();
+ }
+
+ public void setTree(Tree root) {
+ if ( root!=null ) {
+ boolean useIdentity = true; // compare node identity
+ this.treeLayout =
+ new TreeLayout(getTreeLayoutAdaptor(root),
+ new TreeViewer.VariableExtentProvide(this),
+ new DefaultConfiguration(gapBetweenLevels,
+ gapBetweenNodes),
+ useIdentity);
+ // Let the UI display this new AST.
+ updatePreferredSize();
+ }
+ else {
+ this.treeLayout = null;
+ repaint();
+ }
+ }
+
+ /** Get an adaptor for root that indicates how to walk ANTLR trees.
+ * Override to change the adapter from the default of {@link TreeLayoutAdaptor} */
+ public TreeForTreeLayout getTreeLayoutAdaptor(Tree root) {
+ return new TreeLayoutAdaptor(root);
+ }
+
+ public double getScale() {
+ return scale;
+ }
+
+ public void setScale(double scale) {
+ if(scale <= 0) {
+ scale = 1;
+ }
+ this.scale = scale;
+ updatePreferredSize();
+ }
+
+ public void setRuleNames(List ruleNames) {
+ setTreeTextProvider(new DefaultTreeTextProvider(ruleNames));
+ }
+
+ private static class TreeNodeWrapper extends DefaultMutableTreeNode {
+
+ final TreeViewer viewer;
+
+ TreeNodeWrapper(Tree tree, TreeViewer viewer) {
+ super(tree);
+ this.viewer = viewer;
+ }
+
+ @Override
+ public String toString() {
+ return viewer.getText((Tree) this.getUserObject());
+ }
+ }
+
+ private static class EmptyIcon implements Icon {
+
+ @Override
+ public int getIconWidth() {
+ return 0;
+ }
+
+ @Override
+ public int getIconHeight() {
+ return 0;
+ }
+
+ @Override
+ public void paintIcon(Component c, Graphics g, int x, int y) {
+ /* Do nothing. */
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/gui/Trees.java b/tool/src/org/antlr/v4/gui/Trees.java
new file mode 100644
index 0000000..53796ab
--- /dev/null
+++ b/tool/src/org/antlr/v4/gui/Trees.java
@@ -0,0 +1,120 @@
+package org.antlr.v4.gui;
+
+import org.antlr.v4.runtime.Parser;
+import org.antlr.v4.runtime.misc.Utils;
+import org.antlr.v4.runtime.tree.Tree;
+
+import javax.print.PrintException;
+import javax.swing.*;
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Future;
+
+public class Trees {
+ /** Call this method to view a parse tree in a dialog box visually. */
+ public static Future inspect(Tree t, List ruleNames) {
+ TreeViewer viewer = new TreeViewer(ruleNames, t);
+ return viewer.open();
+ }
+
+ /** Call this method to view a parse tree in a dialog box visually. */
+ public static Future inspect(Tree t, Parser parser) {
+ List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
+ return inspect(t, ruleNames);
+ }
+
+ /** Save this tree in a postscript file */
+ public static void save(Tree t, Parser parser, String fileName)
+ throws IOException, PrintException
+ {
+ List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
+ save(t, ruleNames, fileName);
+ }
+
+ /** Save this tree in a postscript file using a particular font name and size */
+ public static void save(Tree t, Parser parser, String fileName,
+ String fontName, int fontSize)
+ throws IOException
+ {
+ List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
+ save(t, ruleNames, fileName, fontName, fontSize);
+ }
+
+ /** Save this tree in a postscript file */
+ public static void save(Tree t, List ruleNames, String fileName)
+ throws IOException, PrintException
+ {
+ writePS(t, ruleNames, fileName);
+ }
+
+ /** Save this tree in a postscript file using a particular font name and size */
+ public static void save(Tree t,
+ List ruleNames, String fileName,
+ String fontName, int fontSize)
+ throws IOException
+ {
+ writePS(t, ruleNames, fileName, fontName, fontSize);
+ }
+
+ public static String getPS(Tree t, List ruleNames,
+ String fontName, int fontSize)
+ {
+ TreePostScriptGenerator psgen =
+ new TreePostScriptGenerator(ruleNames, t, fontName, fontSize);
+ return psgen.getPS();
+ }
+
+ public static String getPS(Tree t, List ruleNames) {
+ return getPS(t, ruleNames, "Helvetica", 11);
+ }
+
+ public static void writePS(Tree t, List ruleNames,
+ String fileName,
+ String fontName, int fontSize)
+ throws IOException
+ {
+ String ps = getPS(t, ruleNames, fontName, fontSize);
+ FileWriter f = new FileWriter(fileName);
+ BufferedWriter bw = new BufferedWriter(f);
+ try {
+ bw.write(ps);
+ }
+ finally {
+ bw.close();
+ }
+ }
+
+ public static void writePS(Tree t, List ruleNames, String fileName)
+ throws IOException
+ {
+ writePS(t, ruleNames, fileName, "Helvetica", 11);
+ }
+
+ /** Print out a whole tree in LISP form. Arg nodeTextProvider is used on the
+ * node payloads to get the text for the nodes.
+ *
+ * @since 4.5.1
+ */
+ public static String toStringTree(Tree t, TreeTextProvider nodeTextProvider) {
+ if ( t==null ) return "null";
+ String s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false);
+ if ( t.getChildCount()==0 ) return s;
+ StringBuilder buf = new StringBuilder();
+ buf.append("(");
+ s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false);
+ buf.append(s);
+ buf.append(' ');
+ for (int i = 0; i0 ) buf.append(' ');
+ buf.append(toStringTree(t.getChild(i), nodeTextProvider));
+ }
+ buf.append(")");
+ return buf.toString();
+ }
+
+ private Trees() {
+ }
+}
diff --git a/tool/src/org/antlr/v4/misc/CharSupport.java b/tool/src/org/antlr/v4/misc/CharSupport.java
new file mode 100644
index 0000000..22e9648
--- /dev/null
+++ b/tool/src/org/antlr/v4/misc/CharSupport.java
@@ -0,0 +1,153 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.misc;
+
+import org.antlr.v4.runtime.Lexer;
+
+/** */
+public class CharSupport {
+ /** When converting ANTLR char and string literals, here is the
+ * value set of escape chars.
+ */
+ public static int ANTLRLiteralEscapedCharValue[] = new int[255];
+
+ /** Given a char, we need to be able to show as an ANTLR literal.
+ */
+ public static String ANTLRLiteralCharValueEscape[] = new String[255];
+
+ static {
+ ANTLRLiteralEscapedCharValue['n'] = '\n';
+ ANTLRLiteralEscapedCharValue['r'] = '\r';
+ ANTLRLiteralEscapedCharValue['t'] = '\t';
+ ANTLRLiteralEscapedCharValue['b'] = '\b';
+ ANTLRLiteralEscapedCharValue['f'] = '\f';
+ ANTLRLiteralEscapedCharValue['\\'] = '\\';
+ ANTLRLiteralEscapedCharValue['\''] = '\'';
+ ANTLRLiteralEscapedCharValue['"'] = '"';
+ ANTLRLiteralCharValueEscape['\n'] = "\\n";
+ ANTLRLiteralCharValueEscape['\r'] = "\\r";
+ ANTLRLiteralCharValueEscape['\t'] = "\\t";
+ ANTLRLiteralCharValueEscape['\b'] = "\\b";
+ ANTLRLiteralCharValueEscape['\f'] = "\\f";
+ ANTLRLiteralCharValueEscape['\\'] = "\\\\";
+ ANTLRLiteralCharValueEscape['\''] = "\\'";
+ }
+
+ /** Return a string representing the escaped char for code c. E.g., If c
+ * has value 0x100, you will get "\u0100". ASCII gets the usual
+ * char (non-hex) representation. Control characters are spit out
+ * as unicode. While this is specially set up for returning Java strings,
+ * it can be used by any language target that has the same syntax. :)
+ */
+ public static String getANTLRCharLiteralForChar(int c) {
+ if ( c< Lexer.MIN_CHAR_VALUE ) {
+ return "''";
+ }
+ if ( c=n ) break; // ignore spurious \ on end
+ if ( literal.charAt(i+1) == 'u' ) end = i+6;
+ }
+ if ( end>n ) break;
+ String esc = literal.substring(i, end);
+ int c = getCharValueFromCharInGrammarLiteral(esc);
+ if ( c==-1 ) { buf.append(esc); }
+ else buf.append((char)c);
+ i = end;
+ }
+ return buf.toString();
+ }
+
+ public static String capitalize(String s) {
+ return Character.toUpperCase(s.charAt(0)) + s.substring(1);
+ }
+}
diff --git a/tool/src/org/antlr/v4/misc/FrequencySet.java b/tool/src/org/antlr/v4/misc/FrequencySet.java
new file mode 100644
index 0000000..94e467c
--- /dev/null
+++ b/tool/src/org/antlr/v4/misc/FrequencySet.java
@@ -0,0 +1,52 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.v4.misc;
+
+import java.util.HashMap;
+
+/** Count how many of each key we have; not thread safe */
+public class FrequencySet extends HashMap {
+ public int count(T key) {
+ MutableInt value = get(key);
+ if (value == null) return 0;
+ return value.v;
+ }
+ public void add(T key) {
+ MutableInt value = get(key);
+ if (value == null) {
+ value = new MutableInt(1);
+ put(key, value);
+ }
+ else {
+ value.v++;
+ }
+ }
+}
diff --git a/tool/src/org/antlr/v4/misc/Graph.java b/tool/src/org/antlr/v4/misc/Graph.java
new file mode 100644
index 0000000..2509c81
--- /dev/null
+++ b/tool/src/org/antlr/v4/misc/Graph.java
@@ -0,0 +1,117 @@
+/*
+ * [The "BSD license"]
+ * Copyright (c) 2012 Terence Parr
+ * Copyright (c) 2012 Sam Harwell
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.v4.misc;
+
+import org.antlr.v4.runtime.misc.OrderedHashSet;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** A generic graph with edges; Each node as a single Object payload.
+ * This is only used to topologically sort a list of file dependencies
+ * at the moment.
+ */
+public class Graph {
+
+ public static class Node {
+ T payload;
+ List> edges; // points at which nodes?
+
+ public Node(T payload) { this.payload = payload; }
+
+ public void addEdge(Node n) {
+ if ( edges==null ) edges = new ArrayList>();
+ if ( !edges.contains(n) ) edges.add(n);
+ }
+
+ @Override
+ public String toString() { return payload.toString(); }
+ }
+
+ /** Map from node payload to node containing it */
+ protected Map> nodes = new LinkedHashMap>();
+
+ public void addEdge(T a, T b) {
+ //System.out.println("add edge "+a+" to "+b);
+ Node a_node = getNode(a);
+ Node b_node = getNode(b);
+ a_node.addEdge(b_node);
+ }
+
+ protected Node