1 33 34 35 package bsh; 36 37 class BSHLiteral extends SimpleNode 38 { 39 public Object value; 40 41 BSHLiteral(int id) { super(id); } 42 43 public Object eval( CallStack callstack, Interpreter interpreter ) 44 throws EvalError 45 { 46 if ( value == null ) 47 throw new InterpreterError("Null in bsh literal: "+value); 48 49 return value; 50 } 51 52 private char getEscapeChar(char ch) 53 { 54 switch(ch) 55 { 56 case 'b': 57 ch = '\b'; 58 break; 59 60 case 't': 61 ch = '\t'; 62 break; 63 64 case 'n': 65 ch = '\n'; 66 break; 67 68 case 'f': 69 ch = '\f'; 70 break; 71 72 case 'r': 73 ch = '\r'; 74 break; 75 76 case '"': 78 case '\'': 79 case '\\': 80 break; 81 } 82 83 return ch; 84 } 85 86 public void charSetup(String str) 87 { 88 char ch = str.charAt(0); 89 if(ch == '\\') 90 { 91 ch = str.charAt(1); 93 94 if(Character.isDigit(ch)) 95 ch = (char)Integer.parseInt(str.substring(1), 8); 96 else 97 ch = getEscapeChar(ch); 98 } 99 100 value = new Primitive(new Character (ch).charValue()); 101 } 102 103 void stringSetup(String str) 104 { 105 StringBuffer buffer = new StringBuffer (); 106 for(int i = 0; i < str.length(); i++) 107 { 108 char ch = str.charAt(i); 109 if(ch == '\\') 110 { 111 ch = str.charAt(++i); 113 114 if(Character.isDigit(ch)) 115 { 116 int endPos = i; 117 118 while(endPos < i + 2) 120 { 121 if(Character.isDigit(str.charAt(endPos + 1))) 122 endPos++; 123 else 124 break; 125 } 126 127 ch = (char)Integer.parseInt(str.substring(i, endPos + 1), 8); 128 i = endPos; 129 } 130 else 131 ch = getEscapeChar(ch); 132 } 133 134 buffer.append(ch); 135 } 136 137 value = buffer.toString().intern(); 138 } 139 } 140 | Popular Tags |