|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CharacterLiteral.java | 25% | 16% | 66.7% | 22.2% |
|
||||||||||||||
| 1 | /* | |
| 2 | * DynamicJava - Copyright (C) 1999-2001 | |
| 3 | * | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 | * copy of this software and associated documentation files | |
| 6 | * (the "Software"), to deal in the Software without restriction, including | |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, | |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 | * persons to whom the Software is furnished to do so, subject to the | |
| 10 | * following conditions: | |
| 11 | * The above copyright notice and this permission notice shall be included | |
| 12 | * in all copies or substantial portions of the Software. | |
| 13 | * | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| 17 | * IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| 20 | * DEALINGS IN THE SOFTWARE. | |
| 21 | * | |
| 22 | * Except as contained in this notice, the name of Dyade shall not be | |
| 23 | * used in advertising or otherwise to promote the sale, use or other | |
| 24 | * dealings in this Software without prior written authorization from | |
| 25 | * Dyade. | |
| 26 | * | |
| 27 | */ | |
| 28 | ||
| 29 | package koala.dynamicjava.tree; | |
| 30 | ||
| 31 | /** | |
| 32 | * This class represents the character literal nodes of the syntax tree | |
| 33 | * | |
| 34 | * @author Stephane Hillion | |
| 35 | * @version 1.0 - 1999/04/24 | |
| 36 | */ | |
| 37 | ||
| 38 | public class CharacterLiteral extends Literal { | |
| 39 | /** | |
| 40 | * Initializes a literal | |
| 41 | * @param rep the representation of the literal | |
| 42 | */ | |
| 43 | 0 | public CharacterLiteral(String rep) { |
| 44 | 0 | this(rep, SourceInfo.NONE); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Initializes a literal | |
| 49 | * @param rep the representation of the literal | |
| 50 | */ | |
| 51 | 8 | public CharacterLiteral(String rep, |
| 52 | SourceInfo si) { | |
| 53 | 8 | super(rep, |
| 54 | new Character(decodeCharacter(rep)), | |
| 55 | char.class, | |
| 56 | si); | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Decodes the representation of a Java literal character. | |
| 61 | * @param rep the representation of the character | |
| 62 | * @return the character represented by the given string | |
| 63 | */ | |
| 64 | 8 | private static char decodeCharacter(String rep) { |
| 65 | 8 | if (rep.charAt(0) != '\'' || rep.charAt(rep.length()-1) != '\'') { |
| 66 | 0 | throw new IllegalArgumentException("Malformed character literal"); |
| 67 | } | |
| 68 | 8 | if (rep.length() == 3) { |
| 69 | 8 | return rep.charAt(1); |
| 70 | } | |
| 71 | 0 | char c; |
| 72 | // Assume that charAt(1) == '\\' and length > 3 | |
| 73 | 0 | switch (c = rep.charAt(2)) { |
| 74 | 0 | case 'n' : return '\n'; |
| 75 | 0 | case 't' : return '\t'; |
| 76 | 0 | case 'b' : return '\b'; |
| 77 | 0 | case 'r' : return '\r'; |
| 78 | 0 | case 'f' : return '\f'; |
| 79 | 0 | default : |
| 80 | 0 | if (Character.isDigit(c)) { |
| 81 | 0 | int v = 0; |
| 82 | 0 | for (int i = 2; i < rep.length()-1; i++) { |
| 83 | 0 | v = (v * 7) + Integer.parseInt(""+rep.charAt(i)); |
| 84 | } | |
| 85 | 0 | return (char)v; |
| 86 | } else { | |
| 87 | 0 | return c; |
| 88 | } | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | } |
|
||||||||||