1 11 package org.eclipse.jdt.internal.compiler.ast; 12 13 import org.eclipse.jdt.internal.compiler.ASTVisitor; 14 import org.eclipse.jdt.internal.compiler.codegen.CodeStream; 15 import org.eclipse.jdt.internal.compiler.impl.StringConstant; 16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope; 17 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 18 19 public class StringLiteral extends Literal { 20 21 char[] source; 22 int lineNumber; 23 24 public StringLiteral(char[] token, int start, int end, int lineNumber) { 25 26 this(start,end); 27 this.source = token; 28 this.lineNumber = lineNumber - 1; } 30 31 public StringLiteral(int s, int e) { 32 33 super(s,e); 34 } 35 36 public void computeConstant() { 37 38 constant = StringConstant.fromValue(String.valueOf(source)); 39 } 40 41 public ExtendedStringLiteral extendWith(CharLiteral lit){ 42 43 return new ExtendedStringLiteral(this,lit); 45 } 46 47 public ExtendedStringLiteral extendWith(StringLiteral lit){ 48 49 return new ExtendedStringLiteral(this,lit); 51 } 52 53 56 public StringLiteralConcatenation extendsWith(StringLiteral lit) { 57 return new StringLiteralConcatenation(this, lit); 58 } 59 62 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { 63 64 int pc = codeStream.position; 65 if (valueRequired) 66 codeStream.ldc(constant.stringValue()); 67 codeStream.recordPositionsFrom(pc, this.sourceStart); 68 } 69 70 public TypeBinding literalType(BlockScope scope) { 71 72 return scope.getJavaLangString(); 73 } 74 75 public StringBuffer printExpression(int indent, StringBuffer output) { 76 77 output.append('\"'); 79 for (int i = 0; i < source.length; i++) { 80 switch (source[i]) { 81 case '\b' : 82 output.append("\\b"); break; 84 case '\t' : 85 output.append("\\t"); break; 87 case '\n' : 88 output.append("\\n"); break; 90 case '\f' : 91 output.append("\\f"); break; 93 case '\r' : 94 output.append("\\r"); break; 96 case '\"' : 97 output.append("\\\""); break; 99 case '\'' : 100 output.append("\\'"); break; 102 case '\\' : output.append("\\\\"); break; 105 default : 106 output.append(source[i]); 107 } 108 } 109 output.append('\"'); 110 return output; 111 } 112 113 public char[] source() { 114 115 return source; 116 } 117 118 public void traverse(ASTVisitor visitor, BlockScope scope) { 119 visitor.visit(this, scope); 120 visitor.endVisit(this, scope); 121 } 122 } 123 | Popular Tags |