Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[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, boolean valueRequired) {
51   //
52   //            int pc = codeStream.position;
53   //            if (valueRequired)
54   //                    codeStream.ldc(constant.stringValue());
55   //            codeStream.recordPositionsFrom(pc, this.sourceStart);
56   //    }
57   public TypeBinding literalType(BlockScope scope) {
58
59     return scope.getJavaLangString();
60   }
61
62   public StringBuffer printExpression(int indent, StringBuffer output) {
63
64     // handle some special char.....
65     output.append('\"');
66     for (int i = 0; i < source.length; i++) {
67       switch (source[i]) {
68       case '\b':
69         output.append("\\b"); //$NON-NLS-1$
70         break;
71       case '\t':
72         output.append("\\t"); //$NON-NLS-1$
73         break;
74       case '\n':
75         output.append("\\n"); //$NON-NLS-1$
76         break;
77       case '\f':
78         output.append("\\f"); //$NON-NLS-1$
79         break;
80       case '\r':
81         output.append("\\r"); //$NON-NLS-1$
82         break;
83       case '\"':
84         output.append("\\\""); //$NON-NLS-1$
85         break;
86       case '\'':
87         output.append("\\'"); //$NON-NLS-1$
88         break;
89       case '\\': //take care not to display the escape as a potential real char
90         output.append("\\\\"); //$NON-NLS-1$
91         break;
92       default:
93         output.append(source[i]);
94       }
95     }
96     output.append('\"');
97     return output;
98   }
99
100   public char[] source() {
101
102     return source;
103   }
104
105   public String toStringExpression() {
106
107     // handle some special char.....
108     StringBuffer result = new StringBuffer("\""); //$NON-NLS-1$
109     for (int i = 0; i < source.length; i++) {
110       switch (source[i]) {
111       case '\b':
112         result.append("\\b"); //$NON-NLS-1$
113         break;
114       case '\t':
115         result.append("\\t"); //$NON-NLS-1$
116         break;
117       case '\n':
118         result.append("\\n"); //$NON-NLS-1$
119         break;
120       case '\f':
121         result.append("\\f"); //$NON-NLS-1$
122         break;
123       case '\r':
124         result.append("\\r"); //$NON-NLS-1$
125         break;
126       case '\"':
127         result.append("\\\""); //$NON-NLS-1$
128         break;
129       case '\'':
130         result.append("\\'"); //$NON-NLS-1$
131         break;
132       case '\\': //take care not to display the escape as a potential real char
133         result.append("\\\\"); //$NON-NLS-1$
134         break;
135       default:
136         result.append(source[i]);
137       }
138     }
139     result.append("\""); //$NON-NLS-1$
140     return result.toString();
141   }
142
143   public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
144     visitor.visit(this, scope);
145     visitor.endVisit(this, scope);
146   }
147 }