KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > CharLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
18
19 public class CharLiteral extends NumberLiteral {
20     char value;
21 public CharLiteral(char[] token, int s, int e) {
22     super(token, s, e);
23     computeValue();
24 }
25 public void computeConstant() {
26     //The source is a char[3] first and last char are '
27
//This is true for both regular char AND unicode char
28
//BUT not for escape char like '\b' which are char[4]....
29

30     constant = CharConstant.fromValue(value);
31 }
32 private void computeValue() {
33     //The source is a char[3] first and last char are '
34
//This is true for both regular char AND unicode char
35
//BUT not for escape char like '\b' which are char[4]....
36

37     if ((value = source[1]) != '\\')
38         return;
39     char digit;
40     switch (digit = source[2]) {
41         case 'b' :
42             value = '\b';
43             break;
44         case 't' :
45             value = '\t';
46             break;
47         case 'n' :
48             value = '\n';
49             break;
50         case 'f' :
51             value = '\f';
52             break;
53         case 'r' :
54             value = '\r';
55             break;
56         case '\"' :
57             value = '\"';
58             break;
59         case '\'' :
60             value = '\'';
61             break;
62         case '\\' :
63             value = '\\';
64             break;
65         default : //octal (well-formed: ended by a ' )
66
int number = ScannerHelper.getNumericValue(digit);
67             if ((digit = source[3]) != '\'')
68                 number = (number * 8) + ScannerHelper.getNumericValue(digit);
69             else {
70                 constant = CharConstant.fromValue(value = (char) number);
71                 break;
72             }
73             if ((digit = source[4]) != '\'')
74                 number = (number * 8) + ScannerHelper.getNumericValue(digit);
75             value = (char) number;
76             break;
77     }
78 }
79 /**
80  * CharLiteral code generation
81  *
82  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
83  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
84  * @param valueRequired boolean
85  */

86 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
87     int pc = codeStream.position;
88     if (valueRequired) {
89         codeStream.generateConstant(constant, implicitConversion);
90     }
91     codeStream.recordPositionsFrom(pc, this.sourceStart);
92 }
93 public TypeBinding literalType(BlockScope scope) {
94     return TypeBinding.CHAR;
95 }
96 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
97     visitor.visit(this, blockScope);
98     visitor.endVisit(this, blockScope);
99 }
100 }
101
Popular Tags