/* ----------------------------------------------------------------------------- Copyright 2019-2020 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 Void { @category Void singleton <- Void{ } formatted () { return "void" } equals (_,_) { return true } lessThan (_,_) { return true } void () { return singleton } } define ReadIterator { @value ReadPosition<#x> reader @value Int position fromReadPosition (reader) { return fromReadPositionAt<#y>(reader,0) } fromReadPositionAt (reader,position) { Int position2 <- position if (position < 0) { position2 <- -1 } if (position >= reader.readSize() || reader.readSize() == 0) { position2 <- reader.readSize() } return ReadIterator<#y>{ reader, position } } readCurrent () { if (pastForwardEnd() || pastReverseEnd()) { \ LazyStream$new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput$error()) } return reader.readPosition(position) } forward () { return fromReadPositionAt<#x>(reader,position+1) } pastForwardEnd () { return position >= reader.readSize() } reverse () { return fromReadPositionAt<#x>(reader,position-1) } pastReverseEnd () { return position < 0 || reader.readSize() == 0 } } define LazyStream { @value optional #x value @value optional LazyStream<#x> next new () { return LazyStream<#x>{ empty, empty } } append (value2) { return LazyStream<#x>{ value2, self } } @value recursive (Writer<#x>) -> () recursive (writer) { if (present(next)) { \ require(next).recursive(writer) } if (present(value)) { \ writer.write(require(value)) } } writeTo (writer) { \ recursive(writer) \ writer.flush() } } define TextReader { @value BlockReader reader @value String buffer fromBlockReader (reader) { return TextReader{ reader, "" } } readAll (reader) { Builder builder <- String$builder() while (!reader.pastEnd()) { \ builder.append(reader.readBlock($ExprLookup[BLOCK_READ_SIZE]$)) } return builder.build() } readNextLine () { while (true) { Int newline <- findNewline() if (newline < 0) { if (reader.pastEnd()) { cleanup { buffer <- "" } in return buffer } else { buffer <- buffer+reader.readBlock($ExprLookup[BLOCK_READ_SIZE]$) } } else { return takeLine(newline) } } return "" } pastEnd () { return buffer.readSize() == 0 && reader.pastEnd() } @value findNewline () -> (Int) findNewline () { scoped { Int position <- 0 } in while (position < buffer.readSize()) { Char c <- buffer.readPosition(position) // TODO: Maybe the line separator should be a factory argument. if (c == '\n' || c == '\r') { return position } } update { position <- position+1 } return -1 } @value takeLine (Int) -> (String) takeLine (position) { String data <- buffer.subSequence(0,position) buffer <- buffer.subSequence(position+1,buffer.readSize()-position-1) return data } } define ErrorOr { @value optional #x maybeValue @value optional Formatted maybeError value (x) { return ErrorOr<#x>{ x, empty } } error (message) { return ErrorOr{ empty, message } } isError () { return !present(maybeValue) && present(maybeError) } getValue () { if (present(maybeValue)) { return require(maybeValue) } else { fail(require(maybeError)) } } getError () { return require(maybeError) } convertError () { return ErrorOr{ empty, maybeError } } }