示例#1
0
文件: DataType.java 项目: inexas/oak
  private static boolean bool(Text t, List<Object> valueList) {
    final boolean result;

    if (t.consume("true")) {
      valueList.add(Boolean.TRUE);
      result = true;
    } else if (t.consume("false")) {
      valueList.add(Boolean.FALSE);
      result = true;
    } else {
      result = false;
    }

    return result;
  }
示例#2
0
文件: DataType.java 项目: inexas/oak
  /**
   * Parse a value.
   *
   * @param tb Source.
   * @param type The type of the array, may be any..
   * @param valueList Result added here.
   * @return Return true if a value has been added to the valueList and the cursor advanced or false
   *     and the cursor is as it was.
   */
  private static boolean value(Text t, DataType type, List<Object> valueList) {
    boolean result;

    if (t.isEof()) {
      result = false;
    } else if (t.consume("null")) {
      valueList.add(null);
      result = true;
    } else {
      switch (type) {
        case z:
        case Z:
        case f:
        case F:
          result = number(t, type, valueList);
          break;

        case text:
          result = text(t, valueList);
          break;

        case identifier:
          result = identifier(t, valueList);
          break;

        case path:
          result = path(t, valueList);
          break;

        case datetime:
          result = datetime(t, valueList);
          break;

        case date:
          result = date(t, valueList);
          break;

        case time:
          result = time(t, valueList);
          break;

        case bool:
          result = bool(t, valueList);
          break;

        case cardinality:
          result = cardinality(t, valueList);
          break;

        case any:
          result = any(t, valueList);
          break;

        default:
          throw new UnexpectedException("value: " + type);
      }
    }

    return result;
  }
示例#3
0
文件: DataType.java 项目: inexas/oak
  private static boolean number(Text t, DataType requiredType, List<Object> valueList) {
    // posNumber
    // : cardinality
    // | '0b' BinaryDigits+ [zZ]?
    // | '0x' HexDigit+ [zZ]?
    // | '-'? Pint? ( '.' Digit+ ) ('e' Pint) [fF]
    // | '-'? Pint [zZfF]?
    // ;
    final boolean result;

    assert !t.isEof() : "Should have been checked by caller";

    if (binary(t, valueList, requiredType) || hex(t, valueList, requiredType)) {
      // ?todo I could allow negatives here
      // ( binary | hex | '0' )
      result = true;
    } else {
      // ( Int DecPart | Int | DecPart ) ( 'e' Int )? [zZfF]?

      final int start = t.cursor();

      if ((t.consumeInt() && (decPart(t) || true) && (exponent(t) || true))
          || (t.consume('-') || true) && decPart(t) && (exponent(t) || true)) {
        final String string = t.getString(start);
        final Object value = deriveType(string, requiredType, t, 10);
        valueList.add(value);
        result = true;
      } else {
        result = false;
      }
    }
    return result;
  }
示例#4
0
文件: DataType.java 项目: inexas/oak
  /**
   * Parse a string into a list of data values. White space is supported only in between the [square
   * brackets].
   *
   * @param string A string representation of an Oak property array, e.g. [ true, null, false ]
   * @param type The type of the array, may be 'any'.
   * @return A potentially empty but not null list of values.
   */
  @SuppressWarnings("unchecked")
  public static <T> List<T> parseArray(String string, DataType type) {
    final List<?> result;

    final Text t = new Text();
    t.append(string);
    if (t.consume('[')
        && (result = elementList(t, type)) != null
        && (t.ws() && t.consume(']'))
        && t.isEof()) {
      // OK
    } else {
      throw new ParsingException("Invalid array: " + string);
    }

    return (List<T>) result;
  }
示例#5
0
文件: DataType.java 项目: inexas/oak
  /**
   * 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;
  }
示例#6
0
文件: DataType.java 项目: inexas/oak
  private static boolean date(Text t) {
    final boolean result;

    // yyyy '/' MM '/' dd

    final int save = t.cursor();
    if (t.consumeAscii(Text.ASCII_0_9)
        && t.consume('/')
        && t.consumeAscii(Text.ASCII_0_9)
        && t.consume('/')
        && t.consumeAscii(Text.ASCII_0_9)) {
      result = true;
    } else {
      result = false;
      t.setCursor(save);
    }

    return result;
  }
示例#7
0
文件: DataType.java 项目: inexas/oak
  private static boolean decPart(Text t) {
    final boolean result;

    // '.' Digits+
    final int save = t.cursor();
    if (t.consume('.') && t.consumeAscii(Text.ASCII_0_9)) {
      result = true;
    } else {
      t.setCursor(save);
      result = false;
    }

    return result;
  }
示例#8
0
文件: DataType.java 项目: inexas/oak
  private static boolean exponent(Text t) {
    final boolean result;

    // 'e' Int
    final int save = t.cursor();
    if (t.consume('e') && t.consumeInt()) {
      result = true;
    } else {
      t.setCursor(save);
      result = false;
    }

    return result;
  }
示例#9
0
文件: DataType.java 项目: inexas/oak
  private static boolean time(Text t) {
    final boolean result;

    // HH ':' mm ( : ss )?

    // Try for HH:mm...
    int reset = t.cursor();
    if (t.consumeAscii(Text.ASCII_0_9) && t.consume(':') && t.consumeAscii(Text.ASCII_0_9)) {
      result = true;
      reset = t.cursor();

      // Try for :ss...
      if (t.consume(':') && t.consumeAscii(Text.ASCII_0_9)) {
        reset = t.cursor();
      }
    } else {
      result = false;
    }

    t.setCursor(reset);

    return result;
  }
示例#10
0
文件: DataType.java 项目: inexas/oak
  private static boolean date(Text t, List<Object> valueList) {
    final boolean result;

    final int start = t.cursor();
    if (t.consume('@') && date(t)) {
      final String string = t.getString(start + 1); // Jump the'@'
      final LocalDate value = DateU.parseStandardDate(string);
      valueList.add(value);
      result = true;
    } else {
      result = false;
    }

    return result;
  }
示例#11
0
文件: DataType.java 项目: inexas/oak
  private static List<Object> elementList(Text t, DataType type) {
    final List<Object> result = new ArrayList<>();

    // elementList: ( WS? value ( WS? ',' WS? value )* WS? )?
    if (t.ws() && value(t, type, result)) {
      while (true) {
        final int save = t.cursor();
        if (t.ws() && t.consume(',') && t.ws() && value(t, type, result)) {
          continue;
        }
        t.setCursor(save);
        break;
      }
    }

    return result;
  }
示例#12
0
文件: DataType.java 项目: inexas/oak
  private static boolean binary(Text t, List<Object> valueList, DataType requiredType) {
    final boolean result;

    // '0b' [01]+ [zZfF]?
    final int save = t.cursor();
    if (t.consume("0b") && t.consumeAscii(Text.ASCII_0_1)) {
      final String string = t.getString(save + 2);
      final Object value = deriveType(string, requiredType, t, 2);
      valueList.add(value);
      result = true;
    } else {
      t.setCursor(save);
      result = false;
    }

    return result;
  }
示例#13
0
文件: DataType.java 项目: inexas/oak
  /**
   * There's a bug in this as it requires seconds in the string whereas the time() function does
   * not. It seems to be in the Java parser.
   *
   * @param tb Source to parse.
   * @return A Temporal or null if none found.
   */
  @Nullable
  private static Temporal temporal(Text t) {
    Temporal result;

    final int save = t.cursor();
    t.consume('@');
    final int start = save + 1;

    // yyyy/MM/dd HH:mm:ss

    try {
      if (date(t)) {
        final int save2 = t.cursor();
        t.ws();
        if (time(t)) {
          final String string = t.getString(start);
          result = DateU.parseStandardDatetime(string);
        } else {
          t.setCursor(save2);
          final String string = t.getString(start);
          result = DateU.parseStandardDate(string);
        }
      } else if (time(t)) {
        final String string = t.getString(start);
        result = DateU.parseStandardTime(string);
      } else {
        result = null;
        t.setCursor(save);
      }
    } catch (final DateTimeParseException e) {
      result = null;
      t.setCursor(save);
    }

    return result;
  }