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.
|
* Removes Lisp comments from an input stream.
|
||||||
*/
|
*/
|
||||||
class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispInputStream {
|
class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispInputStream() {
|
||||||
|
|
||||||
private val underlyingInputStream = SafeInputStream(underlyingInputStream)
|
private val underlyingInputStream = SafeInputStream(underlyingInputStream)
|
||||||
private var isInQuotedString = false
|
private var isInQuotedString = false
|
||||||
|
@ -19,6 +19,10 @@ class LispCommentRemovingInputStream(underlyingInputStream: InputStream) : LispI
|
||||||
private var previousCharacter = 0
|
private var previousCharacter = 0
|
||||||
private var currentCharacter = 0
|
private var currentCharacter = 0
|
||||||
|
|
||||||
|
override fun computeNext() = read().let {
|
||||||
|
if (it == EOF) done() else setNext(it)
|
||||||
|
}
|
||||||
|
|
||||||
override fun read(): Int {
|
override fun read(): Int {
|
||||||
if (!rereadLastCharacter)
|
if (!rereadLastCharacter)
|
||||||
return readFromUnderlyingInputStream()
|
return readFromUnderlyingInputStream()
|
||||||
|
|
|
@ -2,10 +2,10 @@ package scanner
|
||||||
|
|
||||||
import error.CriticalLispException
|
import error.CriticalLispException
|
||||||
|
|
||||||
interface LispInputStream {
|
abstract class LispInputStream: AbstractIterator<Int>() {
|
||||||
|
|
||||||
fun read(): Int
|
abstract fun read(): Int
|
||||||
fun unreadLastCharacter()
|
abstract fun unreadLastCharacter()
|
||||||
|
|
||||||
class MaximumUnreadsExceededException : CriticalLispException()
|
class MaximumUnreadsExceededException : CriticalLispException()
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,7 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
|
|
||||||
val nextToken: Token
|
val nextToken: Token
|
||||||
get() {
|
get() {
|
||||||
var c = inputStream.read()
|
for (c in inputStream) {
|
||||||
while (c != EOF) {
|
|
||||||
val currentCharacter = c.toChar()
|
val currentCharacter = c.toChar()
|
||||||
positionTracker.incrementColumn()
|
positionTracker.incrementColumn()
|
||||||
|
|
||||||
|
@ -36,8 +35,6 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
return createTokenFromCharacter(currentCharacter)
|
return createTokenFromCharacter(currentCharacter)
|
||||||
else if (currentCharacter == NEWLINE)
|
else if (currentCharacter == NEWLINE)
|
||||||
positionTracker.incrementLine()
|
positionTracker.incrementLine()
|
||||||
|
|
||||||
c = inputStream.read()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokenFactory.createEofToken(positionTracker.currentPosition())
|
return tokenFactory.createEofToken(positionTracker.currentPosition())
|
||||||
|
@ -64,7 +61,7 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
val nextCharacter = inputStream.read().toChar()
|
val nextCharacter = inputStream.read().toChar()
|
||||||
inputStream.unreadLastCharacter()
|
inputStream.unreadLastCharacter()
|
||||||
|
|
||||||
return if (isDigit(nextCharacter))
|
return if (nextCharacter.isDigit())
|
||||||
retrieveNumberTokenText(firstCharacter)
|
retrieveNumberTokenText(firstCharacter)
|
||||||
else
|
else
|
||||||
retrieveIdentifierTokenText(firstCharacter)
|
retrieveIdentifierTokenText(firstCharacter)
|
||||||
|
@ -79,12 +76,12 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
private fun retrieveIdentifierTokenText(firstCharacter: Char) =
|
private fun retrieveIdentifierTokenText(firstCharacter: Char) =
|
||||||
ComplexTokenTextRetriever(firstCharacter, Characters::isIdentifierCharacter).retrieveToken()
|
ComplexTokenTextRetriever(firstCharacter, Characters::isIdentifierCharacter).retrieveToken()
|
||||||
|
|
||||||
private inner class ComplexTokenTextRetriever(internal var firstCharacter: Char,
|
private inner class ComplexTokenTextRetriever(private val firstCharacter: Char,
|
||||||
internal var isPartOfToken: (Char) -> Boolean) {
|
private val isPartOfToken: (Char) -> Boolean) {
|
||||||
internal var text = StringBuilder()
|
private val text = StringBuilder()
|
||||||
internal var position = positionTracker.currentPosition()
|
private val position = positionTracker.currentPosition()
|
||||||
internal var currentCharacter = firstCharacter
|
private var currentCharacter = firstCharacter
|
||||||
internal var previousCharacter = firstCharacter
|
private var previousCharacter = firstCharacter
|
||||||
|
|
||||||
private fun isTerminatingCharacter() = isStringToken() && isTerminatingDoubleQuote()
|
private fun isTerminatingCharacter() = isStringToken() && isTerminatingDoubleQuote()
|
||||||
private fun isStringToken() = firstCharacter == DOUBLE_QUOTE
|
private fun isStringToken() = firstCharacter == DOUBLE_QUOTE
|
||||||
|
@ -93,8 +90,7 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
fun retrieveToken(): String {
|
fun retrieveToken(): String {
|
||||||
text.append(firstCharacter)
|
text.append(firstCharacter)
|
||||||
|
|
||||||
var c = inputStream.read()
|
for (c in inputStream) {
|
||||||
while (c != EOF) {
|
|
||||||
currentCharacter = c.toChar()
|
currentCharacter = c.toChar()
|
||||||
|
|
||||||
if (!isPartOfToken(currentCharacter)) {
|
if (!isPartOfToken(currentCharacter)) {
|
||||||
|
@ -109,7 +105,6 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
||||||
return text.toString()
|
return text.toString()
|
||||||
|
|
||||||
previousCharacter = currentCharacter
|
previousCharacter = currentCharacter
|
||||||
c = inputStream.read()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return terminateTokenWithEof()
|
return terminateTokenWithEof()
|
||||||
|
|
|
@ -43,6 +43,5 @@ object Characters {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isIdentifierCharacter(c: Char) = !Character.isWhitespace(c) && !illegalIdentifierCharacters.contains(c)
|
fun isIdentifierCharacter(c: Char) = !Character.isWhitespace(c) && !illegalIdentifierCharacters.contains(c)
|
||||||
fun isLegalStringCharacter(c: Char) = true
|
|
||||||
fun isNumberPrefix(c: Char) = c == DASH || c == PLUS
|
fun isNumberPrefix(c: Char) = c == DASH || c == PLUS
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue