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