KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > StringLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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; // line number is 1 based
29
}
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         //add the lit source to mine, just as if it was mine
44
return new ExtendedStringLiteral(this,lit);
45     }
46
47     public ExtendedStringLiteral extendWith(StringLiteral lit){
48
49         //add the lit source to mine, just as if it was mine
50
return new ExtendedStringLiteral(this,lit);
51     }
52
53     /**
54      * Add the lit source to mine, just as if it was mine
55      */

56     public StringLiteralConcatenation extendsWith(StringLiteral lit) {
57         return new StringLiteralConcatenation(this, lit);
58     }
59     /**
60      * Code generation for string literal
61      */

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 JavaDoc printExpression(int indent, StringBuffer JavaDoc output) {
76     
77         // handle some special char.....
78
output.append('\"');
79         for (int i = 0; i < source.length; i++) {
80             switch (source[i]) {
81                 case '\b' :
82                     output.append("\\b"); //$NON-NLS-1$
83
break;
84                 case '\t' :
85                     output.append("\\t"); //$NON-NLS-1$
86
break;
87                 case '\n' :
88                     output.append("\\n"); //$NON-NLS-1$
89
break;
90                 case '\f' :
91                     output.append("\\f"); //$NON-NLS-1$
92
break;
93                 case '\r' :
94                     output.append("\\r"); //$NON-NLS-1$
95
break;
96                 case '\"' :
97                     output.append("\\\""); //$NON-NLS-1$
98
break;
99                 case '\'' :
100                     output.append("\\'"); //$NON-NLS-1$
101
break;
102                 case '\\' : //take care not to display the escape as a potential real char
103
output.append("\\\\"); //$NON-NLS-1$
104
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