forked from jasder/antlr
move swing related stuff out of runtime package into org.antlr.v4.gui
This commit is contained in:
parent
53678867ca
commit
b395127e73
|
@ -11,14 +11,6 @@
|
|||
<name>ANTLR 4 Runtime</name>
|
||||
<description>The ANTLR 4 Runtime</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.abego.treelayout</groupId>
|
||||
<artifactId>org.abego.treelayout.core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
|
|
|
@ -34,7 +34,6 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
|||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
import org.antlr.v4.runtime.tree.RuleNode;
|
||||
import org.antlr.v4.runtime.tree.Trees;
|
||||
import org.antlr.v4.runtime.tree.gui.TreeViewer;
|
||||
|
||||
import javax.print.PrintException;
|
||||
import javax.swing.*;
|
||||
|
@ -183,49 +182,6 @@ public class RuleContext implements RuleNode {
|
|||
@Override
|
||||
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { return visitor.visitChildren(this); }
|
||||
|
||||
/** Call this method to view a parse tree in a dialog box visually. */
|
||||
public Future<JDialog> inspect(Parser parser) {
|
||||
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
||||
return inspect(ruleNames);
|
||||
}
|
||||
|
||||
public Future<JDialog> inspect(List<String> ruleNames) {
|
||||
TreeViewer viewer = new TreeViewer(ruleNames, this);
|
||||
return viewer.open();
|
||||
}
|
||||
|
||||
/** Save this tree in a postscript file */
|
||||
public void save(Parser parser, String fileName)
|
||||
throws IOException, PrintException
|
||||
{
|
||||
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
||||
save(ruleNames, fileName);
|
||||
}
|
||||
|
||||
/** Save this tree in a postscript file using a particular font name and size */
|
||||
public void save(Parser parser, String fileName,
|
||||
String fontName, int fontSize)
|
||||
throws IOException
|
||||
{
|
||||
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
||||
save(ruleNames, fileName, fontName, fontSize);
|
||||
}
|
||||
|
||||
/** Save this tree in a postscript file */
|
||||
public void save(List<String> ruleNames, String fileName)
|
||||
throws IOException, PrintException
|
||||
{
|
||||
Trees.writePS(this, ruleNames, fileName);
|
||||
}
|
||||
|
||||
/** Save this tree in a postscript file using a particular font name and size */
|
||||
public void save(List<String> ruleNames, String fileName,
|
||||
String fontName, int fontSize)
|
||||
throws IOException
|
||||
{
|
||||
Trees.writePS(this, ruleNames, fileName, fontName, fontSize);
|
||||
}
|
||||
|
||||
/** 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.
|
||||
|
|
|
@ -37,12 +37,7 @@ import org.antlr.v4.runtime.Token;
|
|||
import org.antlr.v4.runtime.misc.Interval;
|
||||
import org.antlr.v4.runtime.misc.Predicate;
|
||||
import org.antlr.v4.runtime.misc.Utils;
|
||||
import org.antlr.v4.runtime.tree.gui.TreePostScriptGenerator;
|
||||
import org.antlr.v4.runtime.tree.gui.TreeTextProvider;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
@ -51,41 +46,6 @@ import java.util.List;
|
|||
|
||||
/** A set of utility routines useful for all kinds of ANTLR trees. */
|
||||
public class Trees {
|
||||
|
||||
public static String getPS(Tree t, List<String> ruleNames,
|
||||
String fontName, int fontSize)
|
||||
{
|
||||
TreePostScriptGenerator psgen =
|
||||
new TreePostScriptGenerator(ruleNames, t, fontName, fontSize);
|
||||
return psgen.getPS();
|
||||
}
|
||||
|
||||
public static String getPS(Tree t, List<String> ruleNames) {
|
||||
return getPS(t, ruleNames, "Helvetica", 11);
|
||||
}
|
||||
|
||||
public static void writePS(Tree t, List<String> 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<String> ruleNames, String fileName)
|
||||
throws IOException
|
||||
{
|
||||
writePS(t, ruleNames, fileName, "Helvetica", 11);
|
||||
}
|
||||
|
||||
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the
|
||||
* node payloads to get the text for the nodes. Detect
|
||||
* parse trees and extract data appropriately.
|
||||
|
@ -104,38 +64,23 @@ public class Trees {
|
|||
return toStringTree(t, ruleNamesList);
|
||||
}
|
||||
|
||||
/** 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; i<t.getChildCount(); i++) {
|
||||
if ( i>0 ) buf.append(' ');
|
||||
buf.append(toStringTree(t.getChild(i), nodeTextProvider));
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the
|
||||
* node payloads to get the text for the nodes.
|
||||
*/
|
||||
public static String toStringTree(final Tree t, final List<String> ruleNames) {
|
||||
return toStringTree(t, new TreeTextProvider() {
|
||||
@Override
|
||||
public String getText(Tree node) {
|
||||
return getNodeText(node, ruleNames);
|
||||
}
|
||||
});
|
||||
String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
|
||||
if ( t.getChildCount()==0 ) return s;
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("(");
|
||||
s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
|
||||
buf.append(s);
|
||||
buf.append(' ');
|
||||
for (int i = 0; i<t.getChildCount(); i++) {
|
||||
if ( i>0 ) buf.append(' ');
|
||||
buf.append(toStringTree(t.getChild(i), ruleNames));
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String getNodeText(Tree t, Parser recog) {
|
||||
|
@ -204,7 +149,7 @@ public class Trees {
|
|||
if ( t==null || u==null || t.getParent()==null ) return false;
|
||||
Tree p = u.getParent();
|
||||
while ( p!=null ) {
|
||||
if ( t == p ) return true;
|
||||
if ( t==p ) return true;
|
||||
p = p.getParent();
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -3,7 +3,7 @@ package org.antlr.v4.test.tool;
|
|||
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||
import org.antlr.v4.runtime.tree.Tree;
|
||||
import org.antlr.v4.runtime.tree.Trees;
|
||||
import org.antlr.v4.runtime.tree.gui.TreeTextProvider;
|
||||
import org.antlr.v4.gui.TreeTextProvider;
|
||||
import org.antlr.v4.tool.GrammarInterpreterRuleContext;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
|
|
@ -14,7 +14,7 @@ import org.antlr.v4.runtime.atn.PredictionMode;
|
|||
import org.antlr.v4.runtime.atn.RuleStartState;
|
||||
import org.antlr.v4.runtime.atn.Transition;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.Trees;
|
||||
import org.antlr.v4.gui.Trees;
|
||||
import org.antlr.v4.tool.Grammar;
|
||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||
import org.antlr.v4.tool.LexerGrammar;
|
||||
|
|
|
@ -34,7 +34,7 @@ import org.antlr.v4.runtime.CommonTokenStream;
|
|||
import org.antlr.v4.runtime.InterpreterRuleContext;
|
||||
import org.antlr.v4.runtime.LexerInterpreter;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.Trees;
|
||||
import org.antlr.v4.gui.Trees;
|
||||
import org.antlr.v4.tool.Grammar;
|
||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||
import org.antlr.v4.tool.LexerGrammar;
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
|
|||
import org.antlr.v4.runtime.atn.DecisionInfo;
|
||||
import org.antlr.v4.runtime.atn.LookaheadEventInfo;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.Trees;
|
||||
import org.antlr.v4.gui.Trees;
|
||||
import org.antlr.v4.tool.Grammar;
|
||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||
import org.antlr.v4.tool.LexerGrammar;
|
||||
|
|
|
@ -46,6 +46,11 @@
|
|||
<version>4.0.8</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.abego.treelayout</groupId>
|
||||
<artifactId>org.abego.treelayout.core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
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.
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.print.DocFlavor;
|
|
@ -27,7 +27,7 @@
|
|||
* (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.misc;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.font.FontRenderContext;
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import org.antlr.v4.runtime.ANTLRInputStream;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
|
@ -252,10 +252,10 @@ public class TestRig {
|
|||
System.out.println(tree.toStringTree(parser));
|
||||
}
|
||||
if ( gui ) {
|
||||
tree.inspect(parser);
|
||||
Trees.inspect(tree, parser);
|
||||
}
|
||||
if ( psFile!=null ) {
|
||||
tree.save(parser, psFile); // Generate postscript
|
||||
Trees.save(tree, parser, psFile); // Generate postscript
|
||||
}
|
||||
}
|
||||
catch (NoSuchMethodException nsme) {
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import org.abego.treelayout.TreeForTreeLayout;
|
||||
import org.antlr.v4.runtime.tree.Tree;
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import org.abego.treelayout.Configuration;
|
||||
import org.abego.treelayout.NodeExtentProvider;
|
|
@ -28,7 +28,7 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
package org.antlr.v4.gui;
|
||||
|
||||
import org.antlr.v4.runtime.tree.Tree;
|
||||
|
|
@ -28,15 +28,13 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.tree.gui;
|
||||
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.GraphicsSupport;
|
||||
import org.antlr.v4.runtime.misc.JFileChooserConfirmOverwrite;
|
||||
import org.antlr.v4.runtime.misc.Utils;
|
||||
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||
import org.antlr.v4.runtime.tree.Tree;
|
|
@ -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<JDialog> inspect(Tree t, List<String> 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<JDialog> inspect(Tree t, Parser parser) {
|
||||
List<String> 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<String> 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<String> 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<String> 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<String> ruleNames, String fileName,
|
||||
String fontName, int fontSize)
|
||||
throws IOException
|
||||
{
|
||||
writePS(t, ruleNames, fileName, fontName, fontSize);
|
||||
}
|
||||
|
||||
public static String getPS(Tree t, List<String> ruleNames,
|
||||
String fontName, int fontSize)
|
||||
{
|
||||
TreePostScriptGenerator psgen =
|
||||
new TreePostScriptGenerator(ruleNames, t, fontName, fontSize);
|
||||
return psgen.getPS();
|
||||
}
|
||||
|
||||
public static String getPS(Tree t, List<String> ruleNames) {
|
||||
return getPS(t, ruleNames, "Helvetica", 11);
|
||||
}
|
||||
|
||||
public static void writePS(Tree t, List<String> 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<String> 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; i<t.getChildCount(); i++) {
|
||||
if ( i>0 ) buf.append(' ');
|
||||
buf.append(toStringTree(t.getChild(i), nodeTextProvider));
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private Trees() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue