diff --git a/src/main/kotlin/scanner/LispCommentRemovingInputStream.kt b/src/main/kotlin/scanner/LispCommentRemovingInputStream.kt index 7730058..18ec9e4 100644 --- a/src/main/kotlin/scanner/LispCommentRemovingInputStream.kt +++ b/src/main/kotlin/scanner/LispCommentRemovingInputStream.kt @@ -11,7 +11,7 @@ import java.io.InputStream /** * Removes Lisp comments from an input stream. */ -class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispInputStream { +class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispInputStream() { private val underlyingInputStream = SafeInputStream(underlyingInputStream) private var isInQuotedString = false @@ -19,6 +19,10 @@ class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispI private var previousCharacter = 0 private var currentCharacter = 0 + override fun computeNext() = read().let { + if (it == EOF) done() else setNext(it) + } + override fun read(): Int { if (!rereadLastCharacter) return readFromUnderlyingInputStream() diff --git a/src/main/kotlin/scanner/LispInputStream.kt b/src/main/kotlin/scanner/LispInputStream.kt index 24ac439..e0fe1ba 100644 --- a/src/main/kotlin/scanner/LispInputStream.kt +++ b/src/main/kotlin/scanner/LispInputStream.kt @@ -2,10 +2,10 @@ package scanner import error.CriticalLispException -interface LispInputStream { +abstract class LispInputStream: AbstractIterator() { - fun read(): Int - fun unreadLastCharacter() + abstract fun read(): Int + abstract fun unreadLastCharacter() class MaximumUnreadsExceededException : CriticalLispException() } diff --git a/src/main/kotlin/scanner/LispScanner.kt b/src/main/kotlin/scanner/LispScanner.kt index 489f154..7210aac 100644 --- a/src/main/kotlin/scanner/LispScanner.kt +++ b/src/main/kotlin/scanner/LispScanner.kt @@ -27,8 +27,7 @@ class LispScanner(inputStream: InputStream, fileName: String) { val nextToken: Token get() { - var c = inputStream.read() - while (c != EOF) { + for (c in inputStream) { val currentCharacter = c.toChar() positionTracker.incrementColumn() @@ -36,8 +35,6 @@ class LispScanner(inputStream: InputStream, fileName: String) { return createTokenFromCharacter(currentCharacter) else if (currentCharacter == NEWLINE) positionTracker.incrementLine() - - c = inputStream.read() } return tokenFactory.createEofToken(positionTracker.currentPosition()) @@ -64,7 +61,7 @@ class LispScanner(inputStream: InputStream, fileName: String) { val nextCharacter = inputStream.read().toChar() inputStream.unreadLastCharacter() - return if (isDigit(nextCharacter)) + return if (nextCharacter.isDigit()) retrieveNumberTokenText(firstCharacter) else retrieveIdentifierTokenText(firstCharacter) @@ -79,12 +76,12 @@ class LispScanner(inputStream: InputStream, fileName: String) { private fun retrieveIdentifierTokenText(firstCharacter: Char) = ComplexTokenTextRetriever(firstCharacter, Characters::isIdentifierCharacter).retrieveToken() - private inner class ComplexTokenTextRetriever(internal var firstCharacter: Char, - internal var isPartOfToken: (Char) -> Boolean) { - internal var text = StringBuilder() - internal var position = positionTracker.currentPosition() - internal var currentCharacter = firstCharacter - internal var previousCharacter = firstCharacter + private inner class ComplexTokenTextRetriever(private val firstCharacter: Char, + private val isPartOfToken: (Char) -> Boolean) { + private val text = StringBuilder() + private val position = positionTracker.currentPosition() + private var currentCharacter = firstCharacter + private var previousCharacter = firstCharacter private fun isTerminatingCharacter() = isStringToken() && isTerminatingDoubleQuote() private fun isStringToken() = firstCharacter == DOUBLE_QUOTE @@ -93,8 +90,7 @@ class LispScanner(inputStream: InputStream, fileName: String) { fun retrieveToken(): String { text.append(firstCharacter) - var c = inputStream.read() - while (c != EOF) { + for (c in inputStream) { currentCharacter = c.toChar() if (!isPartOfToken(currentCharacter)) { @@ -109,7 +105,6 @@ class LispScanner(inputStream: InputStream, fileName: String) { return text.toString() previousCharacter = currentCharacter - c = inputStream.read() } return terminateTokenWithEof() diff --git a/src/main/kotlin/util/Characters.kt b/src/main/kotlin/util/Characters.kt index 8ed4b38..40dabb0 100644 --- a/src/main/kotlin/util/Characters.kt +++ b/src/main/kotlin/util/Characters.kt @@ -43,6 +43,5 @@ object Characters { } fun isIdentifierCharacter(c: Char) = !Character.isWhitespace(c) && !illegalIdentifierCharacters.contains(c) - fun isLegalStringCharacter(c: Char) = true fun isNumberPrefix(c: Char) = c == DASH || c == PLUS }