/* ----------------------------------------------------------------------------- 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] define ParseChars { float (data) { Float total <- 0.0 Float sign <- 0.0 Bool hasDigit <- false Bool needExpDigits <- false optional Order curr <- data.defaultOrder() // Parse to the left of '.'. while (present(curr)) { Char c <- require(curr).get() curr <- require(curr).next() $ReadOnly[c]$ $Hidden[curr]$ if (sign == 0.0) { if (c == '-') { sign <- -1.0 continue } elif (c == '+') { sign <- 1.0 continue } else { sign <- 1.0 } } if (c >= '0' && c <= '9') { hasDigit <- true total <- 10.0*total+(c.asInt() - '0'.asInt()).asFloat() continue } if (c == '.') { break } if (c == 'E' || c == 'e') { if (!hasDigit) { return autoFail(c) } else { needExpDigits <- true break } } return autoFail(c) } if (!needExpDigits) { Float decimal <- 0.1 // Parse to the right of '.'. while (present(curr)) { Char c <- require(curr).get() curr <- require(curr).next() $ReadOnly[c]$ $Hidden[curr]$ if (c >= '0' && c <= '9') { hasDigit <- true total <- total+decimal*(c.asInt() - '0'.asInt()).asFloat() decimal <- decimal/10.0 continue } if (c == 'E' || c == 'e') { if (!hasDigit) { return autoFail(c) } else { needExpDigits <- true break } } return autoFail(c) } } if (!hasDigit) { return ErrorOr:error("missing digits in Float value") } total <- sign*total $Hidden[sign, hasDigit]$ Int expSign <- 0 Int exp <- 0 if (needExpDigits) { // Parse exponent. while (present(curr)) { Char c <- require(curr).get() curr <- require(curr).next() $ReadOnly[c]$ $Hidden[curr, total]$ if (expSign == 0) { if (c == '-') { expSign <- -1 continue } elif (c == '+') { expSign <- 1 continue } else { expSign <- 1 } } if (c >= '0' && c <= '9') { needExpDigits <- false exp <- 10*exp+(c.asInt() - '0'.asInt()) continue } return autoFail(c) } } if (needExpDigits) { return ErrorOr:error("missing digits in Float exponent") } traverse (Counter.zeroIndexed(exp) -> _) { if (expSign > 0) { total <- 10.0*total } else { total <- total/10.0 } } return ErrorOr:value(total) } int (data) { Int total <- 0 Int sign <- 0 Bool hasDigit <- false traverse (data.defaultOrder() -> Char c) { if (sign == 0) { if (c == '-') { sign <- -1 continue } elif (c == '+') { sign <- 1 continue } else { sign <- 1 } } if (c >= '0' && c <= '9') { hasDigit <- true total <- 10*total+(c.asInt() - '0'.asInt()) continue } return autoFail(c) } if (!hasDigit) { return ErrorOr:error("missing digits in Int value") } return ErrorOr:value(sign*total) } hexInt (data) { Int total <- 0 Int sign <- 0 Bool hasDigit <- false traverse (data.defaultOrder() -> Char c) { if (sign == 0) { if (c == '-') { sign <- -1 continue } elif (c == '+') { sign <- 1 continue } else { sign <- 1 } } if (c >= '0' && c <= '9') { hasDigit <- true total <- 16*total+(c.asInt() - '0'.asInt()) continue } if (c >= 'a' && c <= 'f') { hasDigit <- true total <- 16*total+(c.asInt() - 'a'.asInt() + 10) continue } if (c >= 'A' && c <= 'F') { hasDigit <- true total <- 16*total+(c.asInt() - 'A'.asInt() + 10) continue } return autoFail(c) } if (!hasDigit) { return ErrorOr:error("missing digits in Int value") } return ErrorOr:value(sign*total) } octInt (data) { Int total <- 0 Int sign <- 0 Bool hasDigit <- false traverse (data.defaultOrder() -> Char c) { if (sign == 0) { if (c == '-') { sign <- -1 continue } elif (c == '+') { sign <- 1 continue } else { sign <- 1 } } if (c >= '0' && c <= '7') { hasDigit <- true total <- 8*total+(c.asInt() - '0'.asInt()) continue } return autoFail(c) } if (!hasDigit) { return ErrorOr:error("missing digits in Int value") } return ErrorOr:value(sign*total) } binInt (data) { Int total <- 0 Int sign <- 0 Bool hasDigit <- false traverse (data.defaultOrder() -> Char c) { if (sign == 0) { if (c == '-') { sign <- -1 continue } elif (c == '+') { sign <- 1 continue } else { sign <- 1 } } if (c >= '0' && c <= '1') { hasDigit <- true total <- 2*total+(c.asInt() - '0'.asInt()) continue } return autoFail(c) } if (!hasDigit) { return ErrorOr:error("missing digits in Int value") } return ErrorOr:value(sign*total) } @type autoFail<#x> (Char) -> (ErrorOr) autoFail (c) { return ErrorOr:error(String.builder() .append("failed to parse ") .append(typename<#x>()) .append(" at '") .append(c) .append("'") .build()) } }