001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.ast;
011:
012: import org.eclipse.jdt.internal.compiler.ASTVisitor;
013: import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
014: import org.eclipse.jdt.internal.compiler.impl.StringConstant;
015: import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
016: import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
017:
018: public class StringLiteral extends Literal {
019:
020: char[] source;
021: int lineNumber;
022:
023: public StringLiteral(char[] token, int start, int end,
024: int lineNumber) {
025:
026: this (start, end);
027: this .source = token;
028: this .lineNumber = lineNumber - 1; // line number is 1 based
029: }
030:
031: public StringLiteral(int s, int e) {
032:
033: super (s, e);
034: }
035:
036: public void computeConstant() {
037:
038: constant = StringConstant.fromValue(String.valueOf(source));
039: }
040:
041: public ExtendedStringLiteral extendWith(CharLiteral lit) {
042:
043: //add the lit source to mine, just as if it was mine
044: return new ExtendedStringLiteral(this , lit);
045: }
046:
047: public ExtendedStringLiteral extendWith(StringLiteral lit) {
048:
049: //add the lit source to mine, just as if it was mine
050: return new ExtendedStringLiteral(this , lit);
051: }
052:
053: /**
054: * Add the lit source to mine, just as if it was mine
055: */
056: public StringLiteralConcatenation extendsWith(StringLiteral lit) {
057: return new StringLiteralConcatenation(this , lit);
058: }
059:
060: /**
061: * Code generation for string literal
062: */
063: public void generateCode(BlockScope currentScope,
064: CodeStream codeStream, boolean valueRequired) {
065:
066: int pc = codeStream.position;
067: if (valueRequired)
068: codeStream.ldc(constant.stringValue());
069: codeStream.recordPositionsFrom(pc, this .sourceStart);
070: }
071:
072: public TypeBinding literalType(BlockScope scope) {
073:
074: return scope.getJavaLangString();
075: }
076:
077: public StringBuffer printExpression(int indent, StringBuffer output) {
078:
079: // handle some special char.....
080: output.append('\"');
081: for (int i = 0; i < source.length; i++) {
082: switch (source[i]) {
083: case '\b':
084: output.append("\\b"); //$NON-NLS-1$
085: break;
086: case '\t':
087: output.append("\\t"); //$NON-NLS-1$
088: break;
089: case '\n':
090: output.append("\\n"); //$NON-NLS-1$
091: break;
092: case '\f':
093: output.append("\\f"); //$NON-NLS-1$
094: break;
095: case '\r':
096: output.append("\\r"); //$NON-NLS-1$
097: break;
098: case '\"':
099: output.append("\\\""); //$NON-NLS-1$
100: break;
101: case '\'':
102: output.append("\\'"); //$NON-NLS-1$
103: break;
104: case '\\': //take care not to display the escape as a potential real char
105: output.append("\\\\"); //$NON-NLS-1$
106: break;
107: default:
108: output.append(source[i]);
109: }
110: }
111: output.append('\"');
112: return output;
113: }
114:
115: public char[] source() {
116:
117: return source;
118: }
119:
120: public void traverse(ASTVisitor visitor, BlockScope scope) {
121: visitor.visit(this, scope);
122: visitor.endVisit(this, scope);
123: }
124: }
|