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