Clean up converted code
This commit is contained in:
parent
72f283c60a
commit
19d7284633
|
@ -10,6 +10,8 @@ import util.Characters.BACKSLASH
|
|||
import util.Characters.DOUBLE_QUOTE
|
||||
import util.Characters.EOF
|
||||
import util.Characters.NEWLINE
|
||||
import util.Characters.isIdentifierCharacter
|
||||
import util.Characters.isNumberPrefix
|
||||
import java.io.InputStream
|
||||
import java.lang.Character.isDigit
|
||||
import java.lang.Character.isWhitespace
|
||||
|
@ -41,31 +43,23 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
|||
return tokenFactory.createEofToken(positionTracker.currentPosition())
|
||||
}
|
||||
|
||||
private fun createTokenFromCharacter(c: Char): Token {
|
||||
val currentPosition = positionTracker.currentPosition()
|
||||
val tokenText = retrieveTokenText(c)
|
||||
|
||||
return tokenFactory.createToken(tokenText, currentPosition)
|
||||
private fun createTokenFromCharacter(c: Char) = positionTracker.currentPosition().let { position ->
|
||||
tokenFactory.createToken(retrieveTokenText(c), position)
|
||||
}
|
||||
|
||||
private fun retrieveTokenText(firstCharacter: Char): String {
|
||||
var tokenText = "" + firstCharacter
|
||||
|
||||
if (firstCharacter == DOUBLE_QUOTE)
|
||||
tokenText = retrieveStringTokenText(firstCharacter)
|
||||
else if (Characters.isNumberPrefix(firstCharacter))
|
||||
tokenText = retrieveNumberOrIdentifierTokenText(firstCharacter)
|
||||
else if (isDigit(firstCharacter))
|
||||
tokenText = retrieveNumberTokenText(firstCharacter)
|
||||
else if (Characters.isLegalIdentifierCharacter(firstCharacter))
|
||||
tokenText = retrieveIdentifierTokenText(firstCharacter)
|
||||
when {
|
||||
firstCharacter == DOUBLE_QUOTE -> tokenText = retrieveStringTokenText(firstCharacter)
|
||||
isNumberPrefix(firstCharacter) -> tokenText = retrieveNumberOrIdentifierTokenText(firstCharacter)
|
||||
firstCharacter.isDigit() -> tokenText = retrieveNumberTokenText(firstCharacter)
|
||||
isIdentifierCharacter(firstCharacter) -> tokenText = retrieveIdentifierTokenText(firstCharacter)
|
||||
}
|
||||
|
||||
return tokenText
|
||||
}
|
||||
|
||||
private fun retrieveStringTokenText(firstDoubleQuote: Char) =
|
||||
ComplexTokenTextRetriever(firstDoubleQuote, { Characters.isLegalStringCharacter(it) }).retrieveToken()
|
||||
|
||||
private fun retrieveNumberOrIdentifierTokenText(firstCharacter: Char): String {
|
||||
val nextCharacter = inputStream.read().toChar()
|
||||
inputStream.unreadLastCharacter()
|
||||
|
@ -76,11 +70,14 @@ class LispScanner(inputStream: InputStream, fileName: String) {
|
|||
retrieveIdentifierTokenText(firstCharacter)
|
||||
}
|
||||
|
||||
private fun retrieveStringTokenText(firstDoubleQuote: Char) =
|
||||
ComplexTokenTextRetriever(firstDoubleQuote) { true }.retrieveToken()
|
||||
|
||||
private fun retrieveNumberTokenText(firstCharacter: Char) =
|
||||
ComplexTokenTextRetriever(firstCharacter, { Character.isDigit(it) }).retrieveToken()
|
||||
ComplexTokenTextRetriever(firstCharacter, Character::isDigit).retrieveToken()
|
||||
|
||||
private fun retrieveIdentifierTokenText(firstCharacter: Char) =
|
||||
ComplexTokenTextRetriever(firstCharacter, { Characters.isLegalIdentifierCharacter(it) }).retrieveToken()
|
||||
ComplexTokenTextRetriever(firstCharacter, Characters::isIdentifierCharacter).retrieveToken()
|
||||
|
||||
private inner class ComplexTokenTextRetriever(internal var firstCharacter: Char,
|
||||
internal var isPartOfToken: (Char) -> Boolean) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import util.Characters.DOUBLE_QUOTE
|
|||
import util.Characters.LEFT_PARENTHESIS
|
||||
import util.Characters.RIGHT_PARENTHESIS
|
||||
import util.Characters.SINGLE_QUOTE
|
||||
import util.Characters.isLegalIdentifierCharacter
|
||||
import util.Characters.isIdentifierCharacter
|
||||
import util.Characters.isNumberPrefix
|
||||
import java.lang.Character.isDigit
|
||||
|
||||
|
@ -31,9 +31,9 @@ class TokenFactoryImpl : TokenFactory {
|
|||
AT_SIGN -> AtSign(text, position)
|
||||
COMMA -> Comma(text, position)
|
||||
else -> when {
|
||||
isNumeric(firstCharacter, text) -> Number(text, position)
|
||||
isLegalIdentifierCharacter(firstCharacter) -> Identifier(text, position)
|
||||
else -> throw BadCharacterException(text, position)
|
||||
isNumeric(firstCharacter, text) -> Number(text, position)
|
||||
isIdentifierCharacter(firstCharacter) -> Identifier(text, position)
|
||||
else -> throw BadCharacterException(text, position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ object Characters {
|
|||
illegalIdentifierCharacters.add(SINGLE_QUOTE)
|
||||
}
|
||||
|
||||
fun isLegalIdentifierCharacter(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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue