PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/BSHLiteral.java

#
Java | 139 lines | 94 code | 12 blank | 33 comment | 2 complexity | 352ba3e6de74ab8c02b4867e947db4fd MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. class BSHLiteral extends SimpleNode
  35. {
  36. public Object value;
  37. BSHLiteral(int id) { super(id); }
  38. public Object eval( CallStack callstack, Interpreter interpreter )
  39. throws EvalError
  40. {
  41. if ( value == null )
  42. throw new InterpreterError("Null in bsh literal: "+value);
  43. return value;
  44. }
  45. private char getEscapeChar(char ch)
  46. {
  47. switch(ch)
  48. {
  49. case 'b':
  50. ch = '\b';
  51. break;
  52. case 't':
  53. ch = '\t';
  54. break;
  55. case 'n':
  56. ch = '\n';
  57. break;
  58. case 'f':
  59. ch = '\f';
  60. break;
  61. case 'r':
  62. ch = '\r';
  63. break;
  64. // do nothing - ch already contains correct character
  65. case '"':
  66. case '\'':
  67. case '\\':
  68. break;
  69. }
  70. return ch;
  71. }
  72. public void charSetup(String str)
  73. {
  74. char ch = str.charAt(0);
  75. if(ch == '\\')
  76. {
  77. // get next character
  78. ch = str.charAt(1);
  79. if(Character.isDigit(ch))
  80. ch = (char)Integer.parseInt(str.substring(1), 8);
  81. else
  82. ch = getEscapeChar(ch);
  83. }
  84. value = new Primitive(new Character(ch));
  85. }
  86. void stringSetup(String str)
  87. {
  88. StringBuffer buffer = new StringBuffer();
  89. for(int i = 0; i < str.length(); i++)
  90. {
  91. char ch = str.charAt(i);
  92. if(ch == '\\')
  93. {
  94. // get next character
  95. ch = str.charAt(++i);
  96. if(Character.isDigit(ch))
  97. {
  98. int endPos = i;
  99. // check the next two characters
  100. while(endPos < i + 2)
  101. {
  102. if(Character.isDigit(str.charAt(endPos + 1)))
  103. endPos++;
  104. else
  105. break;
  106. }
  107. ch = (char)Integer.parseInt(str.substring(i, endPos + 1), 8);
  108. i = endPos;
  109. }
  110. else
  111. ch = getEscapeChar(ch);
  112. }
  113. buffer.append(ch);
  114. }
  115. value = buffer.toString();
  116. }
  117. }