001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: class BSHLiteral extends SimpleNode {
035: public Object value;
036:
037: BSHLiteral(int id) {
038: super (id);
039: }
040:
041: public Object eval(CallStack callstack, Interpreter interpreter)
042: throws EvalError {
043: if (value == null)
044: throw new InterpreterError("Null in bsh literal: " + value);
045:
046: return value;
047: }
048:
049: private char getEscapeChar(char ch) {
050: switch (ch) {
051: case 'b':
052: ch = '\b';
053: break;
054:
055: case 't':
056: ch = '\t';
057: break;
058:
059: case 'n':
060: ch = '\n';
061: break;
062:
063: case 'f':
064: ch = '\f';
065: break;
066:
067: case 'r':
068: ch = '\r';
069: break;
070:
071: // do nothing - ch already contains correct character
072: case '"':
073: case '\'':
074: case '\\':
075: break;
076: }
077:
078: return ch;
079: }
080:
081: public void charSetup(String str) {
082: char ch = str.charAt(0);
083: if (ch == '\\') {
084: // get next character
085: ch = str.charAt(1);
086:
087: if (Character.isDigit(ch))
088: ch = (char) Integer.parseInt(str.substring(1), 8);
089: else
090: ch = getEscapeChar(ch);
091: }
092:
093: value = new Primitive(new Character(ch).charValue());
094: }
095:
096: void stringSetup(String str) {
097: StringBuffer buffer = new StringBuffer();
098: for (int i = 0; i < str.length(); i++) {
099: char ch = str.charAt(i);
100: if (ch == '\\') {
101: // get next character
102: ch = str.charAt(++i);
103:
104: if (Character.isDigit(ch)) {
105: int endPos = i;
106:
107: // check the next two characters
108: while (endPos < i + 2) {
109: if (Character.isDigit(str.charAt(endPos + 1)))
110: endPos++;
111: else
112: break;
113: }
114:
115: ch = (char) Integer.parseInt(str.substring(i,
116: endPos + 1), 8);
117: i = endPos;
118: } else
119: ch = getEscapeChar(ch);
120: }
121:
122: buffer.append(ch);
123: }
124:
125: value = buffer.toString().intern();
126: }
127: }
|