< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Lexer.java

Print this page




1114 
1115     /**
1116      * Is the given character a valid escape char after "\" ?
1117      *
1118      * @param ch character to be checked
1119      * @return if the given character is valid after "\"
1120      */
1121     protected boolean isEscapeCharacter(final char ch) {
1122         return true;
1123     }
1124 
1125     /**
1126      * Convert string to number.
1127      *
1128      * @param valueString  String to convert.
1129      * @param radix        Numeric base.
1130      * @return Converted number.
1131      */
1132     private static Number valueOf(final String valueString, final int radix) throws NumberFormatException {
1133         try {
1134             final long value = Long.parseLong(valueString, radix);
1135             if(value >= MIN_INT_L && value <= MAX_INT_L) {
1136                 return (int)value;
1137             }
1138             return value;
1139         } catch (final NumberFormatException e) {
1140             if (radix == 10) {
1141                 return Double.valueOf(valueString);
1142             }
1143 
1144             double value = 0.0;
1145 
1146             for (int i = 0; i < valueString.length(); i++) {
1147                 final char ch = valueString.charAt(i);
1148                 // Preverified, should always be a valid digit.
1149                 final int digit = convertDigit(ch, radix);
1150                 value *= radix;
1151                 value += digit;
1152             }
1153 
1154             return value;
1155         }
1156     }
1157 
1158     /**


1765         case OCTAL_LEGACY:
1766             return Lexer.valueOf(source.getString(start, len), 8); // number
1767         case OCTAL:
1768             return Lexer.valueOf(source.getString(start + 2, len - 2), 8); // number
1769         case BINARY_NUMBER:
1770             return Lexer.valueOf(source.getString(start + 2, len - 2), 2); // number
1771         case FLOATING:
1772             final String str   = source.getString(start, len);
1773             final double value = Double.valueOf(str);
1774             if (str.indexOf('.') != -1) {
1775                 return value; //number
1776             }
1777             //anything without an explicit decimal point is still subject to a
1778             //"representable as int or long" check. Then the programmer does not
1779             //explicitly code something as a double. For example new Color(int, int, int)
1780             //and new Color(float, float, float) will get ambiguous for cases like
1781             //new Color(1.0, 1.5, 1.5) if we don't respect the decimal point.
1782             //yet we don't want e.g. 1e6 to be a double unnecessarily
1783             if (JSType.isStrictlyRepresentableAsInt(value)) {
1784                 return (int)value;
1785             } else if (JSType.isStrictlyRepresentableAsLong(value)) {
1786                 return (long)value;
1787             }
1788             return value;
1789         case STRING:
1790             return source.getString(start, len); // String
1791         case ESCSTRING:
1792             return valueOfString(start, len, strict); // String
1793         case IDENT:
1794             return valueOfIdent(start, len); // String
1795         case REGEX:
1796             return valueOfPattern(start, len); // RegexToken::LexerToken
1797         case TEMPLATE:
1798         case TEMPLATE_HEAD:
1799         case TEMPLATE_MIDDLE:
1800         case TEMPLATE_TAIL:
1801             return valueOfString(start, len, true); // String
1802         case XML:
1803             return valueOfXML(start, len); // XMLToken::LexerToken
1804         case DIRECTIVE_COMMENT:
1805             return source.getString(start, len);
1806         default:




1114 
1115     /**
1116      * Is the given character a valid escape char after "\" ?
1117      *
1118      * @param ch character to be checked
1119      * @return if the given character is valid after "\"
1120      */
1121     protected boolean isEscapeCharacter(final char ch) {
1122         return true;
1123     }
1124 
1125     /**
1126      * Convert string to number.
1127      *
1128      * @param valueString  String to convert.
1129      * @param radix        Numeric base.
1130      * @return Converted number.
1131      */
1132     private static Number valueOf(final String valueString, final int radix) throws NumberFormatException {
1133         try {
1134             return Integer.parseInt(valueString, radix);




1135         } catch (final NumberFormatException e) {
1136             if (radix == 10) {
1137                 return Double.valueOf(valueString);
1138             }
1139 
1140             double value = 0.0;
1141 
1142             for (int i = 0; i < valueString.length(); i++) {
1143                 final char ch = valueString.charAt(i);
1144                 // Preverified, should always be a valid digit.
1145                 final int digit = convertDigit(ch, radix);
1146                 value *= radix;
1147                 value += digit;
1148             }
1149 
1150             return value;
1151         }
1152     }
1153 
1154     /**


1761         case OCTAL_LEGACY:
1762             return Lexer.valueOf(source.getString(start, len), 8); // number
1763         case OCTAL:
1764             return Lexer.valueOf(source.getString(start + 2, len - 2), 8); // number
1765         case BINARY_NUMBER:
1766             return Lexer.valueOf(source.getString(start + 2, len - 2), 2); // number
1767         case FLOATING:
1768             final String str   = source.getString(start, len);
1769             final double value = Double.valueOf(str);
1770             if (str.indexOf('.') != -1) {
1771                 return value; //number
1772             }
1773             //anything without an explicit decimal point is still subject to a
1774             //"representable as int or long" check. Then the programmer does not
1775             //explicitly code something as a double. For example new Color(int, int, int)
1776             //and new Color(float, float, float) will get ambiguous for cases like
1777             //new Color(1.0, 1.5, 1.5) if we don't respect the decimal point.
1778             //yet we don't want e.g. 1e6 to be a double unnecessarily
1779             if (JSType.isStrictlyRepresentableAsInt(value)) {
1780                 return (int)value;


1781             }
1782             return value;
1783         case STRING:
1784             return source.getString(start, len); // String
1785         case ESCSTRING:
1786             return valueOfString(start, len, strict); // String
1787         case IDENT:
1788             return valueOfIdent(start, len); // String
1789         case REGEX:
1790             return valueOfPattern(start, len); // RegexToken::LexerToken
1791         case TEMPLATE:
1792         case TEMPLATE_HEAD:
1793         case TEMPLATE_MIDDLE:
1794         case TEMPLATE_TAIL:
1795             return valueOfString(start, len, true); // String
1796         case XML:
1797             return valueOfXML(start, len); // XMLToken::LexerToken
1798         case DIRECTIVE_COMMENT:
1799             return source.getString(start, len);
1800         default:


< prev index next >