rollback and do a real fix for whitespace escaping

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9906]
This commit is contained in:
parrt 2012-01-26 14:18:57 -08:00
parent adcf72b9a2
commit 7498908d62
3 changed files with 9 additions and 10 deletions

View File

@ -59,10 +59,10 @@ public class Utils {
while ( data.contains(value) ) data.remove(value);
}
public static String escapeWhitespace(String s) {
public static String escapeWhitespace(String s, boolean escapeSpaces) {
StringBuilder buf = new StringBuilder();
for (char c : s.toCharArray()) {
if ( c==' ' ) buf.append('\u00B7');
if ( c==' ' && escapeSpaces ) buf.append('\u00B7');
else if ( c=='\t' ) buf.append("\\t");
else if ( c=='\n' ) buf.append("\\n");
else if ( c=='\r' ) buf.append("\\r");

View File

@ -80,11 +80,12 @@ public class Trees {
* parse trees and extract data appropriately.
*/
public static String toStringTree(Tree t, Parser recog) {
boolean escapeWhitespace = false;
if ( t.getChildCount()==0 ) return getNodeText(t, recog, escapeWhitespace);
String s = Utils.escapeWhitespace(getNodeText(t, recog), false);
if ( t.getChildCount()==0 ) return s;
StringBuilder buf = new StringBuilder();
buf.append("(");
buf.append(getNodeText(t, recog, escapeWhitespace));
s = Utils.escapeWhitespace(getNodeText(t, recog), false);
buf.append(s);
buf.append(' ');
for (int i = 0; i<t.getChildCount(); i++) {
if ( i>0 ) buf.append(' ');
@ -94,7 +95,7 @@ public class Trees {
return buf.toString();
}
public static <Symbol> String getNodeText(Tree t, Parser recog, boolean escapeWhitespace) {
public static <Symbol> String getNodeText(Tree t, Parser recog) {
if ( recog!=null ) {
if ( t instanceof ParseTree.RuleNode ) {
int ruleIndex = ((ParseTree.RuleNode)t).getRuleContext().getRuleIndex();
@ -108,7 +109,6 @@ public class Trees {
Object symbol = ((ParseTree.TerminalNode<?>)t).getSymbol();
if (symbol instanceof Token) {
String s = ((Token)symbol).getText();
if ( escapeWhitespace ) s = Utils.escapeWhitespace(s);
return s;
}
}

View File

@ -62,8 +62,7 @@ public class TreeViewer extends JComponent {
@Override
public String getText(Tree node) {
boolean escapeWhitespace = true;
return String.valueOf(Trees.getNodeText(node, parser, escapeWhitespace));
return String.valueOf(Trees.getNodeText(node, parser));
}
}
@ -213,7 +212,7 @@ public class TreeViewer extends JComponent {
public void text(Graphics g, String s, int x, int y) {
// System.out.println("drawing '"+s+"' @ "+x+","+y);
s = Utils.escapeWhitespace(s);
s = Utils.escapeWhitespace(s, true);
g.drawString(s, x, y);
}