Refactor while loops to use iterator
This commit is contained in:
parent
19d7284633
commit
1df72e7704
|
@ -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()
|
||||
|
|
|
@ -2,10 +2,10 @@ package scanner
|
|||
|
||||
import error.CriticalLispException
|
||||
|
||||
interface LispInputStream {
|
||||
abstract class LispInputStream: AbstractIterator<Int>() {
|
||||
|
||||
fun read(): Int
|
||||
fun unreadLastCharacter()
|
||||
abstract fun read(): Int
|
||||
abstract fun unreadLastCharacter()
|
||||
|
||||
class MaximumUnreadsExceededException : CriticalLispException()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue