diff --git a/src/main/kotlin/scanner/LispScanner.kt b/src/main/kotlin/scanner/LispScanner.kt index e3cce53..489f154 100644 --- a/src/main/kotlin/scanner/LispScanner.kt +++ b/src/main/kotlin/scanner/LispScanner.kt @@ -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) { diff --git a/src/main/kotlin/token/TokenFactoryImpl.kt b/src/main/kotlin/token/TokenFactoryImpl.kt index 217bb96..a60c705 100644 --- a/src/main/kotlin/token/TokenFactoryImpl.kt +++ b/src/main/kotlin/token/TokenFactoryImpl.kt @@ -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) } } } diff --git a/src/main/kotlin/util/Characters.kt b/src/main/kotlin/util/Characters.kt index c5a19a8..8ed4b38 100644 --- a/src/main/kotlin/util/Characters.kt +++ b/src/main/kotlin/util/Characters.kt @@ -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 }