Clean up converted code

This commit is contained in:
Mike Cifelli 2018-07-21 12:17:52 -04:00
parent 72f283c60a
commit 19d7284633
3 changed files with 20 additions and 23 deletions

View File

@ -10,6 +10,8 @@ import util.Characters.BACKSLASH
import util.Characters.DOUBLE_QUOTE import util.Characters.DOUBLE_QUOTE
import util.Characters.EOF import util.Characters.EOF
import util.Characters.NEWLINE import util.Characters.NEWLINE
import util.Characters.isIdentifierCharacter
import util.Characters.isNumberPrefix
import java.io.InputStream import java.io.InputStream
import java.lang.Character.isDigit import java.lang.Character.isDigit
import java.lang.Character.isWhitespace import java.lang.Character.isWhitespace
@ -41,31 +43,23 @@ class LispScanner(inputStream: InputStream, fileName: String) {
return tokenFactory.createEofToken(positionTracker.currentPosition()) return tokenFactory.createEofToken(positionTracker.currentPosition())
} }
private fun createTokenFromCharacter(c: Char): Token { private fun createTokenFromCharacter(c: Char) = positionTracker.currentPosition().let { position ->
val currentPosition = positionTracker.currentPosition() tokenFactory.createToken(retrieveTokenText(c), position)
val tokenText = retrieveTokenText(c)
return tokenFactory.createToken(tokenText, currentPosition)
} }
private fun retrieveTokenText(firstCharacter: Char): String { private fun retrieveTokenText(firstCharacter: Char): String {
var tokenText = "" + firstCharacter var tokenText = "" + firstCharacter
if (firstCharacter == DOUBLE_QUOTE) when {
tokenText = retrieveStringTokenText(firstCharacter) firstCharacter == DOUBLE_QUOTE -> tokenText = retrieveStringTokenText(firstCharacter)
else if (Characters.isNumberPrefix(firstCharacter)) isNumberPrefix(firstCharacter) -> tokenText = retrieveNumberOrIdentifierTokenText(firstCharacter)
tokenText = retrieveNumberOrIdentifierTokenText(firstCharacter) firstCharacter.isDigit() -> tokenText = retrieveNumberTokenText(firstCharacter)
else if (isDigit(firstCharacter)) isIdentifierCharacter(firstCharacter) -> tokenText = retrieveIdentifierTokenText(firstCharacter)
tokenText = retrieveNumberTokenText(firstCharacter) }
else if (Characters.isLegalIdentifierCharacter(firstCharacter))
tokenText = retrieveIdentifierTokenText(firstCharacter)
return tokenText return tokenText
} }
private fun retrieveStringTokenText(firstDoubleQuote: Char) =
ComplexTokenTextRetriever(firstDoubleQuote, { Characters.isLegalStringCharacter(it) }).retrieveToken()
private fun retrieveNumberOrIdentifierTokenText(firstCharacter: Char): String { private fun retrieveNumberOrIdentifierTokenText(firstCharacter: Char): String {
val nextCharacter = inputStream.read().toChar() val nextCharacter = inputStream.read().toChar()
inputStream.unreadLastCharacter() inputStream.unreadLastCharacter()
@ -76,11 +70,14 @@ class LispScanner(inputStream: InputStream, fileName: String) {
retrieveIdentifierTokenText(firstCharacter) retrieveIdentifierTokenText(firstCharacter)
} }
private fun retrieveStringTokenText(firstDoubleQuote: Char) =
ComplexTokenTextRetriever(firstDoubleQuote) { true }.retrieveToken()
private fun retrieveNumberTokenText(firstCharacter: Char) = private fun retrieveNumberTokenText(firstCharacter: Char) =
ComplexTokenTextRetriever(firstCharacter, { Character.isDigit(it) }).retrieveToken() ComplexTokenTextRetriever(firstCharacter, Character::isDigit).retrieveToken()
private fun retrieveIdentifierTokenText(firstCharacter: Char) = private fun retrieveIdentifierTokenText(firstCharacter: Char) =
ComplexTokenTextRetriever(firstCharacter, { Characters.isLegalIdentifierCharacter(it) }).retrieveToken() ComplexTokenTextRetriever(firstCharacter, Characters::isIdentifierCharacter).retrieveToken()
private inner class ComplexTokenTextRetriever(internal var firstCharacter: Char, private inner class ComplexTokenTextRetriever(internal var firstCharacter: Char,
internal var isPartOfToken: (Char) -> Boolean) { internal var isPartOfToken: (Char) -> Boolean) {

View File

@ -10,7 +10,7 @@ import util.Characters.DOUBLE_QUOTE
import util.Characters.LEFT_PARENTHESIS import util.Characters.LEFT_PARENTHESIS
import util.Characters.RIGHT_PARENTHESIS import util.Characters.RIGHT_PARENTHESIS
import util.Characters.SINGLE_QUOTE import util.Characters.SINGLE_QUOTE
import util.Characters.isLegalIdentifierCharacter import util.Characters.isIdentifierCharacter
import util.Characters.isNumberPrefix import util.Characters.isNumberPrefix
import java.lang.Character.isDigit import java.lang.Character.isDigit
@ -31,9 +31,9 @@ class TokenFactoryImpl : TokenFactory {
AT_SIGN -> AtSign(text, position) AT_SIGN -> AtSign(text, position)
COMMA -> Comma(text, position) COMMA -> Comma(text, position)
else -> when { else -> when {
isNumeric(firstCharacter, text) -> Number(text, position) isNumeric(firstCharacter, text) -> Number(text, position)
isLegalIdentifierCharacter(firstCharacter) -> Identifier(text, position) isIdentifierCharacter(firstCharacter) -> Identifier(text, position)
else -> throw BadCharacterException(text, position) else -> throw BadCharacterException(text, position)
} }
} }
} }

View File

@ -42,7 +42,7 @@ object Characters {
illegalIdentifierCharacters.add(SINGLE_QUOTE) 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 isLegalStringCharacter(c: Char) = true
fun isNumberPrefix(c: Char) = c == DASH || c == PLUS fun isNumberPrefix(c: Char) = c == DASH || c == PLUS
} }