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>
|
<name>ANTLR 4 Runtime</name>
|
||||||
<description>The ANTLR 4 Runtime</description>
|
<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>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -38,7 +30,7 @@
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.felix</groupId>
|
<groupId>org.apache.felix</groupId>
|
||||||
<artifactId>maven-bundle-plugin</artifactId>
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
<version>2.5.4</version>
|
<version>2.5.4</version>
|
||||||
|
@ -46,7 +38,7 @@
|
||||||
<execution>
|
<execution>
|
||||||
<id>bundle-manifest</id>
|
<id>bundle-manifest</id>
|
||||||
<phase>process-classes</phase>
|
<phase>process-classes</phase>
|
||||||
<configuration>
|
<configuration>
|
||||||
<instructions>
|
<instructions>
|
||||||
<Bundle-SymbolicName>org.antlr.antlr4-runtime-osgi</Bundle-SymbolicName>
|
<Bundle-SymbolicName>org.antlr.antlr4-runtime-osgi</Bundle-SymbolicName>
|
||||||
<Implementation-Title>ANTLR 4 Runtime</Implementation-Title>
|
<Implementation-Title>ANTLR 4 Runtime</Implementation-Title>
|
||||||
|
@ -55,19 +47,19 @@
|
||||||
<Implementation-Version>${project.version}</Implementation-Version>
|
<Implementation-Version>${project.version}</Implementation-Version>
|
||||||
</instructions>
|
</instructions>
|
||||||
</configuration>
|
</configuration>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>manifest</goal>
|
<goal>manifest</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>2.4</version>
|
<version>2.4</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -78,14 +70,14 @@
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<configuration>
|
<configuration>
|
||||||
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
|
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
|
||||||
</configuration>
|
</configuration>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>shade</goal>
|
<goal>shade</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -34,7 +34,6 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||||
import org.antlr.v4.runtime.tree.RuleNode;
|
import org.antlr.v4.runtime.tree.RuleNode;
|
||||||
import org.antlr.v4.runtime.tree.Trees;
|
import org.antlr.v4.runtime.tree.Trees;
|
||||||
import org.antlr.v4.runtime.tree.gui.TreeViewer;
|
|
||||||
|
|
||||||
import javax.print.PrintException;
|
import javax.print.PrintException;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
@ -183,49 +182,6 @@ public class RuleContext implements RuleNode {
|
||||||
@Override
|
@Override
|
||||||
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { return visitor.visitChildren(this); }
|
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
|
/** Print out a whole tree, not just a node, in LISP format
|
||||||
* (root child1 .. childN). Print just a node if this is a leaf.
|
* (root child1 .. childN). Print just a node if this is a leaf.
|
||||||
* We have to know the recognizer so we can get rule names.
|
* 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.Interval;
|
||||||
import org.antlr.v4.runtime.misc.Predicate;
|
import org.antlr.v4.runtime.misc.Predicate;
|
||||||
import org.antlr.v4.runtime.misc.Utils;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -51,41 +46,6 @@ import java.util.List;
|
||||||
|
|
||||||
/** A set of utility routines useful for all kinds of ANTLR trees. */
|
/** A set of utility routines useful for all kinds of ANTLR trees. */
|
||||||
public class 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
|
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the
|
||||||
* node payloads to get the text for the nodes. Detect
|
* node payloads to get the text for the nodes. Detect
|
||||||
* parse trees and extract data appropriately.
|
* parse trees and extract data appropriately.
|
||||||
|
@ -104,38 +64,23 @@ public class Trees {
|
||||||
return toStringTree(t, ruleNamesList);
|
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
|
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the
|
||||||
* node payloads to get the text for the nodes.
|
* node payloads to get the text for the nodes.
|
||||||
*/
|
*/
|
||||||
public static String toStringTree(final Tree t, final List<String> ruleNames) {
|
public static String toStringTree(final Tree t, final List<String> ruleNames) {
|
||||||
return toStringTree(t, new TreeTextProvider() {
|
String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
|
||||||
@Override
|
if ( t.getChildCount()==0 ) return s;
|
||||||
public String getText(Tree node) {
|
StringBuilder buf = new StringBuilder();
|
||||||
return getNodeText(node, ruleNames);
|
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) {
|
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;
|
if ( t==null || u==null || t.getParent()==null ) return false;
|
||||||
Tree p = u.getParent();
|
Tree p = u.getParent();
|
||||||
while ( p!=null ) {
|
while ( p!=null ) {
|
||||||
if ( t == p ) return true;
|
if ( t==p ) return true;
|
||||||
p = p.getParent();
|
p = p.getParent();
|
||||||
}
|
}
|
||||||
return false;
|
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.ErrorNode;
|
||||||
import org.antlr.v4.runtime.tree.Tree;
|
import org.antlr.v4.runtime.tree.Tree;
|
||||||
import org.antlr.v4.runtime.tree.Trees;
|
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 org.antlr.v4.tool.GrammarInterpreterRuleContext;
|
||||||
|
|
||||||
import java.util.Arrays;
|
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.RuleStartState;
|
||||||
import org.antlr.v4.runtime.atn.Transition;
|
import org.antlr.v4.runtime.atn.Transition;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
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.Grammar;
|
||||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||||
import org.antlr.v4.tool.LexerGrammar;
|
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.InterpreterRuleContext;
|
||||||
import org.antlr.v4.runtime.LexerInterpreter;
|
import org.antlr.v4.runtime.LexerInterpreter;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
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.Grammar;
|
||||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||||
import org.antlr.v4.tool.LexerGrammar;
|
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.DecisionInfo;
|
||||||
import org.antlr.v4.runtime.atn.LookaheadEventInfo;
|
import org.antlr.v4.runtime.atn.LookaheadEventInfo;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
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.Grammar;
|
||||||
import org.antlr.v4.tool.GrammarParserInterpreter;
|
import org.antlr.v4.tool.GrammarParserInterpreter;
|
||||||
import org.antlr.v4.tool.LexerGrammar;
|
import org.antlr.v4.tool.LexerGrammar;
|
||||||
|
|
11
tool/pom.xml
11
tool/pom.xml
|
@ -46,6 +46,11 @@
|
||||||
<version>4.0.8</version>
|
<version>4.0.8</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.abego.treelayout</groupId>
|
||||||
|
<artifactId>org.abego.treelayout.core</artifactId>
|
||||||
|
<version>1.0.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -93,14 +98,14 @@
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<configuration>
|
<configuration>
|
||||||
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
|
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
|
||||||
</configuration>
|
</configuration>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>shade</goal>
|
<goal>shade</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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
|
/** Font metrics. The only way to generate accurate images
|
||||||
* in any format that contain text is to know the font metrics.
|
* 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.
|
* 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.imageio.ImageIO;
|
||||||
import javax.print.DocFlavor;
|
import javax.print.DocFlavor;
|
|
@ -27,7 +27,7 @@
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
* (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, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
package org.antlr.v4.runtime.misc;
|
package org.antlr.v4.gui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.*;
|
||||||
import java.awt.font.FontRenderContext;
|
import java.awt.font.FontRenderContext;
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.ANTLRInputStream;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
@ -252,10 +252,10 @@ public class TestRig {
|
||||||
System.out.println(tree.toStringTree(parser));
|
System.out.println(tree.toStringTree(parser));
|
||||||
}
|
}
|
||||||
if ( gui ) {
|
if ( gui ) {
|
||||||
tree.inspect(parser);
|
Trees.inspect(tree, parser);
|
||||||
}
|
}
|
||||||
if ( psFile!=null ) {
|
if ( psFile!=null ) {
|
||||||
tree.save(parser, psFile); // Generate postscript
|
Trees.save(tree, parser, psFile); // Generate postscript
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (NoSuchMethodException nsme) {
|
catch (NoSuchMethodException nsme) {
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.abego.treelayout.TreeForTreeLayout;
|
||||||
import org.antlr.v4.runtime.tree.Tree;
|
import org.antlr.v4.runtime.tree.Tree;
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.Configuration;
|
||||||
import org.abego.treelayout.NodeExtentProvider;
|
import org.abego.treelayout.NodeExtentProvider;
|
|
@ -28,7 +28,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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;
|
import org.antlr.v4.runtime.tree.Tree;
|
||||||
|
|
|
@ -28,15 +28,13 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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.NodeExtentProvider;
|
||||||
import org.abego.treelayout.TreeForTreeLayout;
|
import org.abego.treelayout.TreeForTreeLayout;
|
||||||
import org.abego.treelayout.TreeLayout;
|
import org.abego.treelayout.TreeLayout;
|
||||||
import org.abego.treelayout.util.DefaultConfiguration;
|
import org.abego.treelayout.util.DefaultConfiguration;
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
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.misc.Utils;
|
||||||
import org.antlr.v4.runtime.tree.ErrorNode;
|
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||||
import org.antlr.v4.runtime.tree.Tree;
|
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