merge sam's pulls
This commit is contained in:
commit
201db8b6d0
|
@ -28,10 +28,7 @@
|
|||
*/
|
||||
package org.antlr.v4.runtime;
|
||||
|
||||
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.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||
import org.antlr.v4.runtime.tree.ErrorNodeImpl;
|
||||
|
@ -297,24 +294,6 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
|
|||
public Symbol getStart() { return start; }
|
||||
public Symbol getStop() { return stop; }
|
||||
|
||||
@Override
|
||||
public String toString(@NotNull Recognizer<?,?> recog, RuleContext stop) {
|
||||
if ( recog==null ) return super.toString(recog, stop);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
RuleContext p = this;
|
||||
buf.append("[");
|
||||
String[] ruleNames = recog.getRuleNames();
|
||||
while ( p != null && p != stop ) {
|
||||
int ruleIndex = p.getRuleIndex();
|
||||
String ruleName = ruleIndex >= 0 && ruleIndex < ruleNames.length ? ruleNames[ruleIndex] : Integer.toString(ruleIndex);
|
||||
buf.append(ruleName);
|
||||
if ( p.parent != null ) buf.append(" ");
|
||||
p = p.parent;
|
||||
}
|
||||
buf.append("]");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/** Used for rule context info debugging during parse-time, not so much for ATN debugging */
|
||||
public String toInfoString(Parser recognizer) {
|
||||
List<String> rules = recognizer.getRuleInvocationStack(this);
|
||||
|
|
|
@ -38,6 +38,8 @@ import org.antlr.v4.runtime.tree.gui.TreeViewer;
|
|||
|
||||
import javax.print.PrintException;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/** A rule context is a record of a single rule invocation. It knows
|
||||
* which context invoked it, if any. If there is no parent context, then
|
||||
|
@ -290,32 +292,65 @@ public class RuleContext implements RuleNode {
|
|||
* (root child1 .. childN). Print just a node if this is a leaf.
|
||||
* We have to know the recognizer so we can get rule names.
|
||||
*/
|
||||
public String toStringTree(Parser recog) {
|
||||
public String toStringTree(@Nullable Parser recog) {
|
||||
return Trees.toStringTree(this, recog);
|
||||
}
|
||||
|
||||
/** Print out a whole tree, not just a node, in LISP format
|
||||
* (root child1 .. childN). Print just a node if this is a leaf.
|
||||
*/
|
||||
public String toStringTree(@Nullable List<String> ruleNames) {
|
||||
return Trees.toStringTree(this, ruleNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toStringTree() { return toStringTree(null); }
|
||||
public String toStringTree() {
|
||||
return toStringTree((List<String>)null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(null);
|
||||
return toString((List<String>)null, (RuleContext)null);
|
||||
}
|
||||
|
||||
public String toString(@Nullable Recognizer<?,?> recog) {
|
||||
public final String toString(@Nullable Recognizer<?,?> recog) {
|
||||
return toString(recog, ParserRuleContext.EMPTY);
|
||||
}
|
||||
|
||||
public final String toString(@Nullable List<String> ruleNames) {
|
||||
return toString(ruleNames, null);
|
||||
}
|
||||
|
||||
// recog null unless ParserRuleContext, in which case we use subclass toString(...)
|
||||
public String toString(@Nullable Recognizer<?,?> recog, RuleContext stop) {
|
||||
public String toString(@Nullable Recognizer<?,?> recog, @Nullable RuleContext stop) {
|
||||
String[] ruleNames = recog != null ? recog.getRuleNames() : null;
|
||||
List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;
|
||||
return toString(ruleNamesList, stop);
|
||||
}
|
||||
|
||||
public String toString(@Nullable List<String> ruleNames, @Nullable RuleContext stop) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
RuleContext p = this;
|
||||
buf.append("[");
|
||||
while (p != null && p != stop) {
|
||||
if ( !p.isEmpty() ) buf.append(p.invokingState);
|
||||
if ( p.parent != null && !p.parent.isEmpty() ) buf.append(" ");
|
||||
if (ruleNames == null) {
|
||||
if (!p.isEmpty()) {
|
||||
buf.append(p.invokingState);
|
||||
}
|
||||
}
|
||||
else {
|
||||
int ruleIndex = p.getRuleIndex();
|
||||
String ruleName = ruleIndex >= 0 && ruleIndex < ruleNames.size() ? ruleNames.get(ruleIndex) : Integer.toString(ruleIndex);
|
||||
buf.append(ruleName);
|
||||
}
|
||||
|
||||
if (p.parent != null && (ruleNames != null || !p.parent.isEmpty())) {
|
||||
buf.append(" ");
|
||||
}
|
||||
|
||||
p = p.parent;
|
||||
}
|
||||
|
||||
buf.append("]");
|
||||
return buf.toString();
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ package org.antlr.v4.runtime.tree;
|
|||
import org.antlr.v4.runtime.Parser;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import org.antlr.v4.runtime.misc.Utils;
|
||||
import org.antlr.v4.runtime.tree.gui.TreePostScriptGenerator;
|
||||
|
||||
|
@ -39,6 +40,7 @@ import java.io.BufferedWriter;
|
|||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -75,31 +77,55 @@ public class Trees {
|
|||
writePS(t, recog, fileName, "Helvetica", 11);
|
||||
}
|
||||
|
||||
/** Print out a whole tree in LISP form. 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
|
||||
* parse trees and extract data appropriately.
|
||||
*/
|
||||
public static String toStringTree(Tree t, Parser recog) {
|
||||
String s = Utils.escapeWhitespace(getNodeText(t, recog), false);
|
||||
public static String toStringTree(@NotNull Tree t) {
|
||||
return toStringTree(t, (List<String>)null);
|
||||
}
|
||||
|
||||
/** 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.
|
||||
*/
|
||||
public static String toStringTree(@NotNull Tree t, @Nullable Parser recog) {
|
||||
String[] ruleNames = recog != null ? recog.getRuleNames() : null;
|
||||
List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;
|
||||
return toStringTree(t, ruleNamesList);
|
||||
}
|
||||
|
||||
/** 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.
|
||||
*/
|
||||
public static String toStringTree(@NotNull Tree t, @Nullable List<String> 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, recog), false);
|
||||
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), recog));
|
||||
buf.append(toStringTree(t.getChild(i), ruleNames));
|
||||
}
|
||||
buf.append(")");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String getNodeText(Tree t, Parser recog) {
|
||||
if ( recog!=null ) {
|
||||
public static String getNodeText(@NotNull Tree t, @Nullable Parser recog) {
|
||||
String[] ruleNames = recog != null ? recog.getRuleNames() : null;
|
||||
List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;
|
||||
return getNodeText(t, ruleNamesList);
|
||||
}
|
||||
|
||||
public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
|
||||
if ( ruleNames!=null ) {
|
||||
if ( t instanceof RuleNode ) {
|
||||
int ruleIndex = ((RuleNode)t).getRuleContext().getRuleIndex();
|
||||
String ruleName = recog.getRuleNames()[ruleIndex];
|
||||
String ruleName = ruleNames.get(ruleIndex);
|
||||
return ruleName;
|
||||
}
|
||||
else if ( t instanceof ErrorNode) {
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
grammar T;
|
||||
|
||||
s : e ';' ;
|
||||
options
|
||||
{
|
||||
output=AST;
|
||||
backtrack=true;
|
||||
}
|
||||
|
||||
e : e '+' e
|
||||
| INT
|
||||
;
|
||||
Integer : '0' .. '9';
|
||||
|
||||
INT : [0-9]+ ;
|
||||
WS : [ \r\n\t]+ -> skip ;
|
||||
|
||||
myID : Integer*;
|
||||
|
||||
public json : myID+ -> ^(myID);
|
Loading…
Reference in New Issue