1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.visit.*; 6 import polyglot.util.*; 7 import java.util.*; 8 9 13 public class StringLit_c extends Lit_c implements StringLit 14 { 15 protected String value; 16 17 public StringLit_c(Position pos, String value) { 18 super(pos); 19 this.value = value; 20 } 21 22 23 public String value() { 24 return this.value; 25 } 26 27 28 public StringLit value(String value) { 29 StringLit_c n = (StringLit_c) copy(); 30 n.value = value; 31 return n; 32 } 33 34 35 public Node typeCheck(TypeChecker tc) throws SemanticException { 36 return type(tc.typeSystem().String()); 37 } 38 39 public String toString() { 40 if (StringUtil.unicodeEscape(value).length() > 11) { 41 return "\"" + StringUtil.unicodeEscape(value).substring(0,8) + "...\""; 42 } 43 44 return "\"" + StringUtil.unicodeEscape(value) + "\""; 45 } 46 47 protected int MAX_LENGTH = 60; 48 49 50 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 51 List l = breakupString(); 52 53 if (l.size() > 1) { 55 w.write("("); 56 } 57 58 for (Iterator i = l.iterator(); i.hasNext(); ) { 59 String s = (String ) i.next(); 60 w.begin(0); 61 } 62 63 for (Iterator i = l.iterator(); i.hasNext(); ) { 64 String s = (String ) i.next(); 65 66 w.write("\""); 67 w.write(StringUtil.escape(s)); 68 w.write("\""); 69 w.end(); 70 71 if (i.hasNext()) { 72 w.write(" +"); 73 w.allowBreak(0, " "); 74 } 75 } 76 77 if (l.size() > 1) { 78 w.write(")"); 79 } 80 } 81 82 86 protected List breakupString() { 87 List result = new LinkedList(); 88 int n = value.length(); 89 int i = 0; 90 91 while (i < n) { 92 int j; 93 94 int len = 0; 97 98 for (j = i; j < n; j++) { 99 char c = value.charAt(j); 100 int k = StringUtil.unicodeEscape(c).length(); 101 if (len + k > MAX_LENGTH) break; 102 len += k; 103 } 104 105 result.add(value.substring(i, j)); 106 107 i = j; 108 } 109 110 if (result.isEmpty()) { 111 if (! value.equals("")) { 113 throw new InternalCompilerError("breakupString failed"); 114 } 115 result.add(value); 116 } 117 118 return result; 119 } 120 121 public Object constantValue() { 122 return value; 123 } 124 } 125 | Popular Tags |