Initial import of the Guide contributed to the project byRobert Kraske. Was originall...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / StringLiteral.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.ast;
9
10 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
11 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
12 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
13 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
14
15 public class StringLiteral extends Literal {
16
17         char[] source;
18
19         public StringLiteral(char[] token, int s, int e) {
20
21                 this(s, e);
22                 source = token;
23         }
24
25         public StringLiteral(int s, int e) {
26
27                 super(s, e);
28         }
29
30         public void computeConstant() {
31
32                 constant = Constant.fromValue(String.valueOf(source));
33         }
34
35         // public ExtendedStringLiteral extendWith(CharLiteral lit) {
36         //
37         // //add the lit source to mine, just as if it was mine
38         // return new ExtendedStringLiteral(this, lit);
39         // }
40
41         public ExtendedStringLiteral extendWith(StringLiteral lit) {
42
43                 // add the lit source to mine, just as if it was mine
44                 return new ExtendedStringLiteral(this, lit);
45         }
46
47         /**
48          * Code generation for string literal
49          */
50         // public void generateCode(BlockScope currentScope, CodeStream codeStream,
51         // boolean valueRequired) {
52         //
53         // int pc = codeStream.position;
54         // if (valueRequired)
55         // codeStream.ldc(constant.stringValue());
56         // codeStream.recordPositionsFrom(pc, this.sourceStart);
57         // }
58         public TypeBinding literalType(BlockScope scope) {
59
60                 return scope.getJavaLangString();
61         }
62
63         public StringBuffer printExpression(int indent, StringBuffer output) {
64
65                 // handle some special char.....
66                 output.append('\"');
67                 for (int i = 0; i < source.length; i++) {
68                         switch (source[i]) {
69                         case '\b':
70                                 output.append("\\b"); //$NON-NLS-1$
71                                 break;
72                         case '\t':
73                                 output.append("\\t"); //$NON-NLS-1$
74                                 break;
75                         case '\n':
76                                 output.append("\\n"); //$NON-NLS-1$
77                                 break;
78                         case '\f':
79                                 output.append("\\f"); //$NON-NLS-1$
80                                 break;
81                         case '\r':
82                                 output.append("\\r"); //$NON-NLS-1$
83                                 break;
84                         case '\"':
85                                 output.append("\\\""); //$NON-NLS-1$
86                                 break;
87                         case '\'':
88                                 output.append("\\'"); //$NON-NLS-1$
89                                 break;
90                         case '\\': // take care not to display the escape as a potential
91                                                 // real char
92                                 output.append("\\\\"); //$NON-NLS-1$
93                                 break;
94                         default:
95                                 output.append(source[i]);
96                         }
97                 }
98                 output.append('\"');
99                 return output;
100         }
101
102         public char[] source() {
103
104                 return source;
105         }
106
107         public String toStringExpression() {
108
109                 // handle some special char.....
110                 StringBuffer result = new StringBuffer("\""); //$NON-NLS-1$
111                 for (int i = 0; i < source.length; i++) {
112                         switch (source[i]) {
113                         case '\b':
114                                 result.append("\\b"); //$NON-NLS-1$
115                                 break;
116                         case '\t':
117                                 result.append("\\t"); //$NON-NLS-1$
118                                 break;
119                         case '\n':
120                                 result.append("\\n"); //$NON-NLS-1$
121                                 break;
122                         case '\f':
123                                 result.append("\\f"); //$NON-NLS-1$
124                                 break;
125                         case '\r':
126                                 result.append("\\r"); //$NON-NLS-1$
127                                 break;
128                         case '\"':
129                                 result.append("\\\""); //$NON-NLS-1$
130                                 break;
131                         case '\'':
132                                 result.append("\\'"); //$NON-NLS-1$
133                                 break;
134                         case '\\': // take care not to display the escape as a potential
135                                                 // real char
136                                 result.append("\\\\"); //$NON-NLS-1$
137                                 break;
138                         default:
139                                 result.append(source[i]);
140                         }
141                 }
142                 result.append("\""); //$NON-NLS-1$
143                 return result.toString();
144         }
145
146         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
147                 visitor.visit(this, scope);
148                 visitor.endVisit(this, scope);
149         }
150 }