Пример #1
0
  /**
   * Given the parsing context and what numerical data type is expected convert a string to the
   * correct type. Note no attempt is made to let the magnitude of the number influence our choice.
   *
   * @param string The string to convert to a number. E.g. "123.3e2". If it contains a '.' or an 'e'
   *     then the type must either be f or F.
   * @param requiredType Either z, Z, f, F or any.
   * @param tb The source. The cursor will be at the end of the number but any type specifier will
   *     not have been consumed. If there is one then we'll eat it.
   * @return The derived type.
   * @throws ParsingException If there is a clash of types.
   */
  private static Object deriveType(String string, DataType requiredType, Text t, int radix) {
    final Object result;

    // Figure out the correct type...
    final DataType derivedType;
    if (t.isEof()) {
      if (requiredType == DataType.any) {
        if (string.indexOf('.') >= 0 || string.indexOf('e') >= 0) {
          derivedType = DataType.f;
        } else {
          derivedType = DataType.z;
        }
      } else {
        derivedType = requiredType;
      }
    } else {
      final char c = t.peek();
      if (c == 'z' || c == 'Z' || c == 'f' || c == 'F') {
        t.consume(c);
        derivedType = DataType.valueOf(String.valueOf(c));
        if (!(requiredType == DataType.any || requiredType == derivedType)) {
          throw new ParsingException("Incompatible type: " + string + c);
        }
      } else {
        if (requiredType == DataType.any) {
          if (string.indexOf('.') >= 0 || string.indexOf('e') >= 0) {
            derivedType = DataType.f;
          } else {
            derivedType = DataType.z;
          }
        } else {
          derivedType = requiredType;
        }
      }
    }

    switch (derivedType) {
      case z:
        result = new Long(Long.parseLong(string, radix));
        break;
      case Z:
        result = new BigInteger(string, radix);
        break;
      case f:
        result = new Double(string);
        break;
      case F:
        result = new BigDecimal(string);
        break;
        // $CASES-OMITTED$
      default:
        throw new UnexpectedException("toType: " + derivedType);
    }

    return result;
  }
Пример #2
0
  private static boolean any(Text t, List<Object> valueList) {
    final boolean result;

    // value
    // : Text
    // | Path
    // | Date | Time | DateTime
    // | 'null'
    // | 'true' | 'false'
    // | z | Z | f | F
    // | Identifier
    // | Cardinality
    // ;

    /*
     * Try and parse a value, we don't know the type so use the first
     * character as an indicator to jump to the right type.
     *
     * We know we're not EOF and "null" has been dealt with by the caller.
     */
    final char c = t.peek();
    if (c == '"') { // Text
      result = text(t, valueList);
    } else if (c == '`') { // Path
      result = path(t, valueList);
    } else if (c == '@') { // Temporal
      result =
          datetime(t, valueList) // Must be first
              || date(t, valueList)
              || time(t, valueList);
    } else if (c >= '0' && c <= '9' || c == '.' || c == '-') {
      result = cardinality(t, valueList) || number(t, DataType.any, valueList);
    } else {
      result =
          bool(t, valueList) // Must be first
              || identifier(t, valueList);
    }

    return result;
  }