packages feed

zeolite-lang-0.18.0.0: lib/util/parse.0rp

/* -----------------------------------------------------------------------------
Copyright 2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- */

// Author: Kevin P. Barry [ta0kira@gmail.com]

// Helpers to parse a full sequence of Char.
concrete ParseChars {
  // Parses a Float.
  //
  // Args:
  // - DefaultOrder<Char>: Decimal-format floating-point.
  //
  // Returns:
  // - ErrorOr<Float>: The parsed value, or an error on parse failure.
  //
  // Notes:
  // - At least one digit is required.
  // - Leading +/- signs may be used.
  // - A successful parse must consume the entire sequence.
  // - Scientific notation is allowed, e.g., "10E-5".
  // - This function does not check for overflow/underflow.
  //
  // Example:
  //
  //   ErrorOr<Float> value <- ParseChars.float("123.456E5")
  //   if (value.isError()) {
  //     fail(value.getError())
  //   }
  @type float (DefaultOrder<Char>) -> (ErrorOr<Float>)

  // Parses an Int.
  //
  // Args:
  // - DefaultOrder<Char>: Decimal-format integer.
  //
  // Returns:
  // - ErrorOr<Int>: The parsed value, or an error on parse failure.
  //
  // Notes:
  // - At least one digit is required.
  // - Leading +/- signs may be used.
  // - A successful parse must consume the entire sequence.
  // - This function does not check for overflow/underflow.
  //
  // Example:
  //
  //   ErrorOr<Int> value <- ParseChars.int("123")
  //   if (value.isError()) {
  //     fail(value.getError())
  //   }
  @type int (DefaultOrder<Char>) -> (ErrorOr<Int>)

  // Parses a hex-formatted Int.
  //
  // Args:
  // - DefaultOrder<Char>: Hex-format integer.
  //
  // Returns:
  // - ErrorOr<Int>: The parsed value, or an error on parse failure.
  //
  // Notes:
  // - At least one digit is required.
  // - Leading +/- signs may be used.
  // - A successful parse must consume the entire sequence.
  // - This function does not check for overflow/underflow.
  //
  // Example:
  //
  //   ErrorOr<Int> value <- ParseChars.hexInt("123ABC")
  //   if (value.isError()) {
  //     fail(value.getError())
  //   }
  @type hexInt (DefaultOrder<Char>) -> (ErrorOr<Int>)

  // Parses a octal-formatted Int.
  //
  // Args:
  // - DefaultOrder<Char>: Octal-format integer.
  //
  // Returns:
  // - ErrorOr<Int>: The parsed value, or an error on parse failure.
  //
  // Notes:
  // - At least one digit is required.
  // - Leading +/- signs may be used.
  // - A successful parse must consume the entire sequence.
  // - This function does not check for overflow/underflow.
  //
  // Example:
  //
  //   ErrorOr<Int> value <- ParseChars.octInt("755")
  //   if (value.isError()) {
  //     fail(value.getError())
  //   }
  @type octInt (DefaultOrder<Char>) -> (ErrorOr<Int>)

  // Parses a binary-formatted Int.
  //
  // Args:
  // - DefaultOrder<Char>: Binary-format integer.
  //
  // Returns:
  // - ErrorOr<Int>: The parsed value, or an error on parse failure.
  //
  // Notes:
  // - At least one digit is required.
  // - Leading +/- signs may be used.
  // - A successful parse must consume the entire sequence.
  // - This function does not check for overflow/underflow.
  //
  // Example:
  //
  //   ErrorOr<Int> value <- ParseChars.binInt("10010")
  //   if (value.isError()) {
  //     fail(value.getError())
  //   }
  @type binInt (DefaultOrder<Char>) -> (ErrorOr<Int>)
}