diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java
index 46518dc63..d3d428602 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java
@@ -1,6 +1,37 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2011 Terence Parr
+ 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.runtime;
-import org.antlr.v4.runtime.atn.*;
+import org.antlr.v4.runtime.atn.ATN;
+import org.antlr.v4.runtime.atn.ATNState;
+import org.antlr.v4.runtime.atn.RuleTransition;
import org.antlr.v4.runtime.misc.IntervalSet;
/** This is the default error handling mechanism for ANTLR parsers
@@ -100,6 +131,9 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
/** Make sure that the current lookahead symbol is consistent with
* what were expecting at this point in the ATN.
+ *
+ * TODO: see if we can merge sync() and recoverInline(). are they same?
+ * does one call the other?
*/
@Override
public void sync(BaseRecognizer recognizer) {
diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java
index 130e75336..5428ff2e1 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java
@@ -1,38 +1,43 @@
/*
[The "BSD license"]
- Copyright (c) 2011 Terence Parr
- All rights reserved.
+ Copyright (c) 2011 Terence Parr
+ All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
+ 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.
+ 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.
+ 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.runtime;
-import org.antlr.v4.runtime.atn.*;
+import org.antlr.v4.runtime.atn.ATN;
+import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.Interval;
-import org.antlr.v4.runtime.tree.*;
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.antlr.v4.runtime.tree.ParseTreeListener;
+import org.antlr.v4.runtime.tree.Trees;
+import org.antlr.v4.runtime.tree.gui.TreeViewer;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
/** Rules can return start/stop info as well as possible trees and templates.
* Each context knows about invoking context and pointer into ATN so we
@@ -297,6 +302,11 @@ public class RuleContext implements ParseTree.RuleNode {
return new Interval(start, stop);
}
+ public void inspect(BaseRecognizer parser) {
+ TreeViewer viewer = new TreeViewer(parser, this);
+ viewer.open();
+ }
+
/** Print out a whole tree, not just a node, in LISP format
* (root child1 .. childN). Print just a node if this is a leaf.
* We have to know the recognizer so we can get rule names.
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java
index 41c747e99..2478b2ad9 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java
@@ -1,35 +1,39 @@
/*
[The "BSD license"]
- Copyright (c) 2011 Terence Parr
- All rights reserved.
+ Copyright (c) 2011 Terence Parr
+ All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
+ 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.
+ 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.
+ 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.runtime.tree;
-import java.util.*;
+import org.antlr.v4.runtime.BaseRecognizer;
+import org.antlr.v4.runtime.tree.gui.TreeViewer;
+
+import java.util.ArrayList;
+import java.util.List;
/** A generic AST implementation with no payload. You must subclass to
* actually have any user data. ANTLR v3 uses a list of children approach
@@ -250,6 +254,11 @@ public abstract class BaseAST implements AST {
*/
public List extends Tree> getAncestors() { return Trees.getAncestors(this); }
+ public void inspect(BaseRecognizer parser) {
+ TreeViewer viewer = new TreeViewer(parser, this);
+ viewer.open();
+ }
+
/** Don't use standard tree printing mechanism since ASTs can have nil
* root nodes.
*/
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java
index 07886bd49..4216828d6 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java
@@ -1,37 +1,36 @@
/*
[The "BSD license"]
- Copyright (c) 2011 Terence Parr
- All rights reserved.
+ Copyright (c) 2011 Terence Parr
+ All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
+ 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.
+ 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.
+ 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.runtime.tree;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
-import org.antlr.v4.runtime.tree.gui.ASTViewer;
/** A tree node that is wrapper for a Token object. */
public class CommonAST extends BaseAST {
@@ -128,12 +127,6 @@ public class CommonAST extends BaseAST {
stopIndex = index;
}
- // TODO: move to basetree when i settle on how runtime works
- public void inspect() {
- ASTViewer viewer = new ASTViewer(this);
- viewer.open();
- }
-
public String toString() {
if ( isNil() ) {
return "nil";
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ANTLRASTViewer.jfdproj b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ANTLRASTViewer.jfdproj
deleted file mode 100644
index 8b06df3b6..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ANTLRASTViewer.jfdproj
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.java
deleted file mode 100644
index 2b239db81..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- [The "BSD license"]
- Copyright (c) 2011 Terence Parr
- 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.runtime.tree.gui;
-
-import javax.swing.*;
-import java.awt.*;
-/*
- * Created by JFormDesigner on Mon Jan 18 14:54:16 PST 2010
- */
-
-/**
- * @author Terence Parr
- */
-public class ASTViewFrame extends JFrame {
- public ASTViewFrame() {
- initComponents();
- }
-
- private void initComponents() {
- // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
- // Generated using JFormDesigner non-commercial license
- scrollPane1 = new JScrollPane();
- tree = new JTree();
-
- //======== this ========
- setTitle("ANTLR AST Viewer");
- Container contentPane = getContentPane();
- contentPane.setLayout(new GridLayout(1, 1));
-
- //======== scrollPane1 ========
- {
- scrollPane1.setViewportView(tree);
- }
- contentPane.add(scrollPane1);
- pack();
- setLocationRelativeTo(getOwner());
- // JFormDesigner - End of component initialization //GEN-END:initComponents
- }
-
- // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
- // Generated using JFormDesigner non-commercial license
- private JScrollPane scrollPane1;
- public JTree tree;
- // JFormDesigner - End of variables declaration //GEN-END:variables
-}
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.jfd b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.jfd
deleted file mode 100644
index 5cddad9ac..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewFrame.jfd
+++ /dev/null
@@ -1,73 +0,0 @@
-
-
-
-
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewer.java
deleted file mode 100644
index aead3c869..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/ASTViewer.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- [The "BSD license"]
- Copyright (c) 2011 Terence Parr
- 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.runtime.tree.gui;
-
-import org.antlr.v4.runtime.tree.*;
-
-/** */
-public class ASTViewer {
- ASTAdaptor adaptor;
- Object root;
-
- public ASTViewer(ASTAdaptor adaptor, Object root) {
- this.adaptor = adaptor;
- this.root = root;
- }
-
- public ASTViewer(Object root) {
- this.adaptor = new CommonASTAdaptor();
- this.root = root;
- }
-
- public void open() {
- ASTViewFrame m = new ASTViewFrame();
- m.tree.setModel(new JTreeASTModel(adaptor, root));
- m.pack();
- m.setSize(800,600);
- m.setVisible(true);
- }
-}
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/AntlrTreeLayouter.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/AntlrTreeLayouter.java
deleted file mode 100644
index ab92f29de..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/AntlrTreeLayouter.java
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- [The "BSD license"]
- Copyright (c) 2011 Terence Parr
- 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.runtime.tree.gui;
-
-import org.abego.treelayout.TreeForTreeLayout;
-import org.abego.treelayout.util.*;
-import org.antlr.v4.runtime.tree.Tree;
-
-import java.util.*;
-
-public class AntlrTreeLayouter {
- // TODO: provide public interface to the configuration/nodeExtent
- private double gapBetweenLevels = 50;
- private double gapBetweenNodes = 10;
- private double nodeWidth = 60;
- private double nodeHeight = 20;
-
- public TreeViewer.AntlrTreeLayout layout(Tree tree) {
- return new TreeViewer.AntlrTreeLayout(new AntlrTreeForTreeLayout(tree),
- new FixedNodeExtentProvider(nodeWidth, nodeHeight),
-// new TreeViewer.VariableExtentProvide(),
- new DefaultConfiguration(gapBetweenLevels,
- gapBetweenNodes));
- }
-
- private static class AntlrTreeForTreeLayout 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 AntlrTreeForTreeLayout(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 (Tree) parentNode
- .getChild(parentNode.getChildCount() - 1);
- }
-
- @Override
- public Tree getFirstChild(Tree parentNode) {
- return (Tree) 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/runtime/Java/src/org/antlr/v4/runtime/tree/gui/DefaultTreeTextProvider.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/DefaultTreeTextProvider.java
deleted file mode 100644
index 9db880152..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/DefaultTreeTextProvider.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.antlr.v4.runtime.tree.gui;
-
-import org.antlr.v4.runtime.tree.Tree;
-
-public class DefaultTreeTextProvider implements TreeTextProvider {
-
- @Override
- public String getText(Tree node) {
- return String.valueOf(node.getPayload());
- }
-}
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/JTreeASTModel.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/JTreeASTModel.java
deleted file mode 100644
index d8af13e31..000000000
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/JTreeASTModel.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- [The "BSD license"]
- Copyright (c) 2011 Terence Parr
- 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.runtime.tree.gui;
-
-import org.antlr.v4.runtime.tree.*;
-
-import javax.swing.event.TreeModelListener;
-import javax.swing.tree.*;
-
-public class JTreeASTModel implements TreeModel {
- ASTAdaptor adaptor;
- Object root;
-
- public JTreeASTModel(ASTAdaptor adaptor, Object root) {
- this.adaptor = adaptor;
- this.root = root;
- }
-
- public JTreeASTModel(Object root) {
- this.adaptor = new CommonASTAdaptor();
- this.root = root;
- }
-
- public int getChildCount(Object parent) {
- return adaptor.getChildCount(parent);
- }
-
- public int getIndexOfChild(Object parent, Object child){
- if ( parent==null ) return -1;
- return adaptor.getChildIndex(child);
- }
-
- public Object getChild(Object parent, int index){
- return adaptor.getChild(parent, index);
- }
-
- public boolean isLeaf(Object node) {
- return getChildCount(node)==0;
- }
-
- public Object getRoot() { return root; }
-
- public void valueForPathChanged(TreePath treePath, Object o) {
- }
-
- public void addTreeModelListener(TreeModelListener treeModelListener) {
- }
-
- public void removeTreeModelListener(TreeModelListener treeModelListener) {
- }
-}
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.java
new file mode 100644
index 000000000..4c5263649
--- /dev/null
+++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.java
@@ -0,0 +1,149 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2011 Udo Borkowski and Terence Parr
+ 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.runtime.tree.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 abego.treelayout.TreeForTreeLayout */
+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 (Tree) parentNode
+ .getChild(parentNode.getChildCount() - 1);
+ }
+
+ @Override
+ public Tree getFirstChild(Tree parentNode) {
+ return (Tree) 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/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeTextProvider.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeTextProvider.java
index 994541f7f..c17763cbd 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeTextProvider.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeTextProvider.java
@@ -1,3 +1,32 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2011 Udo Borkowski and Terence Parr
+ 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.runtime.tree.gui;
import org.antlr.v4.runtime.tree.Tree;
diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java
index 57f3fb939..84cb877d8 100644
--- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java
+++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java
@@ -1,95 +1,123 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2011 Udo Borkowski and Terence Parr
+ 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.runtime.tree.gui;
-import org.abego.treelayout.*;
+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.BaseRecognizer;
import org.antlr.v4.runtime.tree.Tree;
+import org.antlr.v4.runtime.tree.Trees;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class TreeViewer extends JComponent {
- private Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
+ public class DefaultTreeTextProvider implements TreeTextProvider {
+ BaseRecognizer parser;
+ public DefaultTreeTextProvider(BaseRecognizer parser) {
+ this.parser = parser;
+ }
- private int arcSize = 10;
-
- public int getArcSize() {
- return arcSize;
+ @Override
+ public String getText(Tree node) {
+ return String.valueOf(Trees.getNodeText(node, parser));
+ }
}
- public void setArcSize(int arcSize) {
- this.arcSize = arcSize;
+ 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;
- private Color boxColor = Color.orange;
+ protected String fontName = Font.MONOSPACED;
+ protected int fontStyle = Font.PLAIN;
+ protected int fontSize = 12;
+ protected Font font = new Font(fontName, fontStyle, fontSize);
- public Color getBoxColor() {
- return boxColor;
- }
+ protected double gapBetweenLevels = 30;
+ protected double gapBetweenNodes = 10;
+ protected int nodeWidthPadding = 2; // added to left/right
+ protected int nodeHeightPadding = 1; // added above/below
+ protected int arcSize = 0; // make an arc in node outline?
- public void setBoxColor(Color boxColor) {
- this.boxColor = boxColor;
- }
+ protected Color boxColor = Color.white;
+ protected Color borderColor = Color.white;
+ protected Color textColor = Color.black;
- // ----------------------------------------------------------------------
+ protected BaseRecognizer parser;
- private Color borderColor = Color.darkGray;
-
- public Color getBorderColor() {
- return borderColor;
- }
-
- public void setBorderColor(Color borderColor) {
- this.borderColor = borderColor;
- }
-
- // ----------------------------------------------------------------------
-
- private Color textColor = Color.black;
-
- public Color getTextColor() {
- return textColor;
- }
-
- public void setTextColor(Color textColor) {
- this.textColor = textColor;
- }
-
- private TreeViewer.AntlrTreeLayout treeLayout;
-
- private TreeForTreeLayout getTree() {
- return treeLayout.getTree();
- }
-
- private Iterable getChildren(Tree parent) {
- return getTree().getChildren(parent);
- }
-
- private Rectangle2D.Double getBoundsOfNode(Tree node) {
- return treeLayout.getNodeBounds().get(node);
- }
-
- private String getText(Tree tree) {
- return treeTextProvider.getText(tree);
- }
-
- public TreeViewer(Tree tree) {
- this.treeLayout = layouter.layout(tree);
+ public TreeViewer(BaseRecognizer parser, Tree tree) {
+ this.parser = parser;
+ setTreeTextProvider(new DefaultTreeTextProvider(parser));
+ this.treeLayout =
+ new TreeLayout(new TreeLayoutAdaptor(tree),
+ new TreeViewer.VariableExtentProvide(this),
+ new DefaultConfiguration(gapBetweenLevels,
+ gapBetweenNodes));
Dimension size = treeLayout.getBounds().getBounds().getSize();
setPreferredSize(size);
setFont(font);
}
- // -------------------------------------------------------------------
- // painting
+ // ---------------- PAINT -----------------------------------------------
- private void paintEdges(Graphics g, Tree parent) {
+ protected void paintEdges(Graphics g, Tree parent) {
if (!getTree().isLeaf(parent)) {
Rectangle2D.Double b1 = getBoundsOfNode(parent);
double x1 = b1.getCenterX();
double y1 = b1.getCenterY();
- for (Tree child : getChildren(parent)) {
+ for (Tree child : getTree().getChildren(parent)) {
Rectangle2D.Double b2 = getBoundsOfNode(child);
g.drawLine((int) x1, (int) y1, (int) b2.getCenterX(),
(int) b2.getCenterY());
@@ -99,7 +127,7 @@ public class TreeViewer extends JComponent {
}
}
- private void paintBox(Graphics g, Tree tree) {
+ protected void paintBox(Graphics g, Tree tree) {
// draw the box in the background
g.setColor(boxColor);
Rectangle2D.Double box = getBoundsOfNode(tree);
@@ -114,8 +142,8 @@ public class TreeViewer extends JComponent {
String s = getText(tree);
String[] lines = s.split("\n");
FontMetrics m = getFontMetrics(font);
- int x = (int) box.x + arcSize / 2;
- int y = (int) box.y + m.getAscent() + m.getLeading() + 1;
+ 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++) {
g.drawString(lines[i], x, y);
y += m.getHeight();
@@ -136,35 +164,13 @@ public class TreeViewer extends JComponent {
// ----------------------------------------------------------------------
- public static class VariableExtentProvide implements NodeExtentProvider {
- @Override
- public double getHeight(Tree tree) {
-// FontMetrics m = getFontMetrics(font);
-// String text = treeTextProvider.getText(tree);
-// int x = (int) box.x + arcSize / 2;
-// int y = (int) box.y + m.getAscent() + m.getLeading() + 1;
-// int hgt = metrics.getHeight();
-// // get the advance of my text in this font and render context
-// int adv = metrics.stringWidth(text);
- return 0;
- }
-
- @Override
- public double getWidth(Tree tree) {
- return 0;
- }
- }
- public static class AntlrTreeLayout extends TreeLayout {
- public AntlrTreeLayout(TreeForTreeLayout tree,
- NodeExtentProvider nodeExtentProvider,
- Configuration configuration) {
- super(tree, nodeExtentProvider, configuration);
- }
+ protected Rectangle2D.Double getBoundsOfNode(Tree node) {
+ return treeLayout.getNodeBounds().get(node);
}
- // ----------------------------------------------------------------------
-
- private TreeTextProvider treeTextProvider = new DefaultTreeTextProvider();
+ protected String getText(Tree tree) {
+ return treeTextProvider.getText(tree);
+ }
public TreeTextProvider getTreeTextProvider() {
return treeTextProvider;
@@ -174,18 +180,13 @@ public class TreeViewer extends JComponent {
this.treeTextProvider = treeTextProvider;
}
- // ----------------------------------------------------------------------
-
- private AntlrTreeLayouter layouter = new AntlrTreeLayouter();
-
- // ----------------------------------------------------------------------
-
- private static void showInDialog(JComponent panel) {
+ protected static void showInDialog(TreeViewer viewer) {
JDialog dialog = new JDialog();
Container contentPane = dialog.getContentPane();
((JComponent) contentPane).setBorder(BorderFactory.createEmptyBorder(
10, 10, 10, 10));
- contentPane.add(panel);
+ contentPane.add(viewer);
+ contentPane.setBackground(Color.white);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
@@ -195,4 +196,61 @@ public class TreeViewer extends JComponent {
showInDialog(this);
}
+ // ---------------------------------------------------
+
+ 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);
+ }
+
+ @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 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();
+ }
}
diff --git a/tool/playground/TestT.java b/tool/playground/TestT.java
index 63461566a..518d50be3 100644
--- a/tool/playground/TestT.java
+++ b/tool/playground/TestT.java
@@ -1,23 +1,36 @@
-import org.antlr.v4.runtime.*;
-import org.antlr.v4.runtime.tree.*;
-import org.antlr.v4.runtime.tree.gui.*;
+/*
+ [The "BSD license"]
+ Copyright (c) 2011 Terence Parr
+ All rights reserved.
-import java.awt.*;
+ 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.
+ */
+
+import org.antlr.v4.runtime.ANTLRFileStream;
+import org.antlr.v4.runtime.CommonTokenStream;
public class TestT {
-
- public static class MyTreeTextProvider implements TreeTextProvider {
- BaseRecognizer parser;
- public MyTreeTextProvider(BaseRecognizer parser) {
- this.parser = parser;
- }
-
- @Override
- public String getText(Tree node) {
- return String.valueOf(Trees.getNodeText(node, parser));
- }
- }
-
public static void main(String[] args) throws Exception {
TLexer t = new TLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
@@ -30,11 +43,7 @@ public class TestT {
TParser.sContext tree = p.s();
System.out.println(tree.toStringTree(p));
- TreeViewer tv = new TreeViewer(tree);
- tv.setTreeTextProvider(new MyTreeTextProvider(p));
- tv.setBoxColor(Color.lightGray);
- tv.setBorderColor(Color.darkGray);
- tv.open();
+ tree.inspect(p);
//
// ParseTreeWalker walker = new ParseTreeWalker();
// TListener listener = new BlankTListener() {