diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLike.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLike.kt index 05753f2..1340305 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLike.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLike.kt @@ -93,12 +93,13 @@ open class CLikeReader : CodeReader() { class CppRValueRefStates(context: FileInfoBuilder) : CodeStateMachine(context) { - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == "&&") { next(_rValueRef) } else if (token == "typedef") { next(_typedef) } + return null } val _rValueRef = readUntilThen("=;{})") { token: String, _: List -> diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeNestingStackStates.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeNestingStackStates.kt index 8e3c7d1..2ac67d6 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeNestingStackStates.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeNestingStackStates.kt @@ -25,7 +25,7 @@ class CLikeNestingStackStates(context: FileInfoBuilder) : CodeStateMachine(conte override var commandsByName = listOf(::_stateGlobal, ::_readNamespace).associateBy { it.name } - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == "template") { _state = _templateDeclaration } else if (token == ".") { @@ -37,19 +37,24 @@ class CLikeNestingStackStates(context: FileInfoBuilder) : CodeStateMachine(conte } else if (token == "}") { context.popNesting() } + + return null } - fun _dot(token: String) { + fun _dot(token: String): Boolean? { _state = ::_stateGlobal + return null } - fun _readNamespace(token: String) { + fun _readNamespace(token: String): Boolean? { if (token == "[") { _state = _readAttribute } else { _state = _readNamespaceName } _state(token) + + return null } diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeStates.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeStates.kt index a8d579c..3e5cbb4 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeStates.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CLikeStates.kt @@ -36,13 +36,15 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { } } - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token[0].isLetter() || token[0] in "_~") { tryNewFunction(token) } + + return null } - fun _stateFunction(token: String) { + fun _stateFunction(token: String): Boolean? { if (token == "(") { next(_stateDec, token) } else if (token == "::") { @@ -53,34 +55,40 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { } else { next(::_stateGlobal, token) } + return null } val _stateTemplateInName = readInsideBracketsThen("<>", "_stateFunction") {token -> context.addToFunctionName(token) } - fun _stateOperator(token: String) { + fun _stateOperator(token: String): Boolean? { if (token != "(") { _state = ::_stateOperatorNext } context.addToFunctionName(" $token") + + return null } - fun _stateOperatorNext(token: String) { + fun _stateOperatorNext(token: String): Boolean? { if (token == "(") { _stateFunction(token) } else { context.addToFunctionName(" $token") } + + return null } - fun _stateNameWithSpace(token: String) { + fun _stateNameWithSpace(token: String): Boolean? { _state = if (token == "operator") { ::_stateOperator } else { ::_stateFunction } context.addToFunctionName(token) + return null } val _stateDec = readInsideBracketsThen("()", "_stateDecToImp") { token -> @@ -100,7 +108,7 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { context.addToLongFunctionName(token) } - fun _stateDecToImp(token: String) { + fun _stateDecToImp(token: String): Boolean? { if (token in listOf("const", "&", "&&")) { context.addToLongFunctionName(" $token") } else if (token == "throw") { @@ -129,6 +137,8 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { _state = ::_stateOldCParams _savedTokens = mutableListOf(token) } + + return null } val _stateThrow = readInsideBracketsThen("()") { _ -> @@ -141,7 +151,7 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { _state(token) } - fun _stateNoexcept(token: String) { + fun _stateNoexcept(token: String): Boolean? { if (token == "(") { _state = _stateThrow } else { @@ -149,6 +159,8 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { } _state(token) + + return null } val _stateTrailingReturn = readUntilThen(";{") { token, _ -> @@ -156,7 +168,7 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { _state(token) } - open fun _stateOldCParams(token: String) { + open fun _stateOldCParams(token: String): Boolean? { _savedTokens.add(token) if (token == ";") { _savedTokens = mutableListOf() @@ -165,7 +177,7 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { if (_savedTokens.size == 2) { _savedTokens = mutableListOf() _stateDecToImp(token) - return + return null } _state = ::_stateGlobal @@ -179,13 +191,17 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { _state(tkn) } } + + return null } - fun _stateInitializationList(token: String) { + fun _stateInitializationList(token: String): Boolean? { _state = _stateOneInitialization if (token == "{") { next(::_stateEnteringImp, "{") } + + return null } @@ -207,9 +223,10 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) { _state = ::_stateInitializationList } - fun _stateEnteringImp(token: String) { + fun _stateEnteringImp(token: String): Boolean? { context.confirmNewFunction() next(_stateImp, token) + return null } val _stateImp = readInsideBracketsThen("{}", null) {_ -> diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CodeStateMachine.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CodeStateMachine.kt index 4c48ec5..cfc0ca0 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CodeStateMachine.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/CodeStateMachine.kt @@ -22,7 +22,7 @@ import io.github.kevinah95.klizard.FileInfoBuilder open class CodeStateMachine { lateinit var context: FileInfoBuilder - var savedState: (token: String) -> Unit + var savedState: (token: String) -> Boolean? var lastToken: String? = null var toExit: Boolean = false @@ -30,7 +30,7 @@ open class CodeStateMachine { var rutTokens: MutableList = mutableListOf() var brCount: Int = 0 - var _state: ((token: String) -> Unit) = ::_stateGlobal + var _state: ((token: String) -> Boolean?) = ::_stateGlobal open var commandsByName = listOf(::_stateGlobal).associateBy { it.name } @@ -49,12 +49,15 @@ open class CodeStateMachine { return CodeStateMachine(this.context) } - fun next(state: ((token: String) -> Unit), token: String? = null): Boolean? { + fun next(state: ((token: String) -> Boolean?), token: String? = null): Boolean? { _state = state - return token?.let { this(it) } + if (token != null){ + return this.invoke(token) + } + return null } - fun nextIf(state: ((token: String) -> Unit), token: String, expected: String) { + fun nextIf(state: ((token: String) -> Boolean?), token: String, expected: String) { if (token != expected) { return } @@ -66,7 +69,7 @@ open class CodeStateMachine { statemachineBeforeReturn() } - fun subState(state: ((token: String) -> Unit), callback: (() -> Unit)? = null, token: String? = null) { + fun subState(state: ((token: String) -> Boolean?), callback: (() -> Unit)? = null, token: String? = null) { savedState = _state this.callback = callback next(state, token) @@ -74,13 +77,12 @@ open class CodeStateMachine { operator fun invoke(token: String, reader: CodeReader? = null): Boolean? { //TODO: Check this != null -// if (_state(token) != null) { -// next(savedState) -// if (callback != null) { -// callback?.let { it() } -// } -// } - _state(token) + if (_state(token) != null) { + next(savedState) + if (callback != null) { + callback?.let { it() } + } + } lastToken = token if (toExit) { return true @@ -89,13 +91,15 @@ open class CodeStateMachine { return null } - open fun _stateGlobal(token: String) {} + open fun _stateGlobal(token: String): Boolean? { + return null + } fun statemachineBeforeReturn() {} - fun readInsideBracketsThen(brs: String, endState: String? = null, function: (String) -> Unit): (String) -> Unit { - fun decorator(func: ((String) -> Unit)): (String) -> Unit { - fun readUntilMatchingBrackets(token: String): Unit { + fun readInsideBracketsThen(brs: String, endState: String? = null, function: (String) -> Unit): (String) -> Boolean? { + fun decorator(func: ((String) -> Unit)): (String) -> Boolean? { + fun readUntilMatchingBrackets(token: String): Boolean? { brCount += when (token) { brs[0].toString() -> 1 @@ -111,15 +115,17 @@ open class CodeStateMachine { // TODO: Review this method: https://stackoverflow.com/questions/69622835/how-to-call-a-function-in-kotlin-from-a-string-name commandsByName[endState]?.let { next(it) } } + + return null } return ::readUntilMatchingBrackets } return decorator(function) } - fun readUntilThen(tokens: String, function: (String, List) -> Unit): (String) -> Unit { - fun decorator(func: ((String, List) -> Unit)): (String) -> Unit { - fun readUntilThenToken(token: String): Unit { + fun readUntilThen(tokens: String, function: (String, List) -> Unit): (String) -> Boolean? { + fun decorator(func: ((String, List) -> Unit)): (String) -> Boolean? { + fun readUntilThenToken(token: String): Boolean? { if (token in tokens){ func(token, rutTokens) @@ -127,6 +133,7 @@ open class CodeStateMachine { } else { rutTokens.add(token) } + return null } return ::readUntilThenToken } diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Go.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Go.kt index 96aedf4..ef377f9 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Go.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Go.kt @@ -24,7 +24,6 @@ class GoReader : CodeReader() { override var ext: MutableList = mutableListOf("go") - // TODO: Implement this val languageNames: List = listOf("go") override operator fun invoke(_context: FileInfoBuilder) { diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/GoLikeStates.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/GoLikeStates.kt index 70d9edb..bd15320 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/GoLikeStates.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/GoLikeStates.kt @@ -23,7 +23,7 @@ import io.github.kevinah95.klizard.FileInfoBuilder open class GoLikeStates(context: FileInfoBuilder): CodeStateMachine(context) { val FUNC_KEYWORD = "func" - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == FUNC_KEYWORD) { _state = ::_functionName context.pushNewFunction("") @@ -32,29 +32,29 @@ open class GoLikeStates(context: FileInfoBuilder): CodeStateMachine(context) { } else if (token in setOf("}")) { statemachineReturn() } + + return null } - fun _functionName(token: String) { + fun _functionName(token: String): Boolean? { if (token != "`") { if (token == "(") { if (context.stackedFunctions.size > 0 && context.stackedFunctions.last().name != "*global*") { - next(_functionDec, token) - return + return next(_functionDec, token) } else { - next(_memberFunction, token) - return + return next(_memberFunction, token) } } if (token == "{") { - next(::_expectFunctionImpl, token) - return + return next(::_expectFunctionImpl, token) } context.addToFunctionName(token) _state = ::_expectFunctionDec } + return null } - fun _expectFunctionDec(token: String) { + fun _expectFunctionDec(token: String): Boolean? { if (token == "(") { next(_functionDec, token) } else if (token == "<") { @@ -62,6 +62,8 @@ open class GoLikeStates(context: FileInfoBuilder): CodeStateMachine(context) { } else { _state = ::_stateGlobal } + + return null } val _generalize = readInsideBracketsThen("<>", "_expectFunctionDec") {token -> @@ -80,18 +82,23 @@ open class GoLikeStates(context: FileInfoBuilder): CodeStateMachine(context) { - fun _expectFunctionImpl(token: String) { + fun _expectFunctionImpl(token: String): Boolean? { if (token == "{" && lastToken != "interface") { next(::_functionImpl, token) } + + return null } - fun _functionImpl(token: String) { + fun _functionImpl(token: String): Boolean? { val callback = { _state = ::_stateGlobal context.endOfFunction() } + /////////////////////////////////////// subState(statemachineClone()._state, callback) + + return null } diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Java.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Java.kt index 1115b95..233ad46 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Java.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Java.kt @@ -36,10 +36,11 @@ class JavaReader : CLikeReader() { class JavaStates(context: FileInfoBuilder) : CLikeStates(context) { - override fun _stateOldCParams(token: String) { + override fun _stateOldCParams(token: String): Boolean? { if (token == "{"){ _stateDecToImp(token) } + return null } override fun tryNewFunction(name: String) { @@ -47,25 +48,29 @@ class JavaStates(context: FileInfoBuilder) : CLikeStates(context) { _state = ::_stateFunction } - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == "@"){ _state = ::_stateDecorator - return + return null } super._stateGlobal(token) + return null } - fun _stateDecorator(token: String) { + fun _stateDecorator(token: String): Boolean? { _state = ::_statePostDecorator + return null } - fun _statePostDecorator(token: String) { + fun _statePostDecorator(token: String): Boolean? { if (token == "."){ _state = ::_stateDecorator } else { _state = ::_stateGlobal _state(token) } + + return null } } \ No newline at end of file diff --git a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Python.kt b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Python.kt index aaed847..81e2c48 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Python.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/klizard_languages/Python.kt @@ -131,45 +131,53 @@ class PythonReader() : CodeReader() { class PythonStates(context: FileInfoBuilder, reader: CodeReader) : CodeStateMachine(context) { - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == "def") { _state = ::_function } + return null } - fun _function(token: String) { + fun _function(token: String): Boolean? { if (token != "(") { context.restartNewFunction(token) context.addToLongFunctionName("(") } else { _state = ::_dec } + + return null } - fun _dec(token: String) { + fun _dec(token: String): Boolean? { if (token == ")") { _state = ::_stateColon } else { context.parameter(token) - return + return null } context.addToLongFunctionName(" $token") + return null } - fun _stateColon(token: String) { + fun _stateColon(token: String): Boolean? { if (token == ":") { next(::_stateFirstLine) } else { next(::_stateGlobal) } + + return null } - fun _stateFirstLine(token: String) { + fun _stateFirstLine(token: String): Boolean? { _state = ::_stateGlobal if (token.startsWith(""""""""") || token.startsWith("'''")) { context.addNloc(-token.count { it == '\n' } - 1) } _stateGlobal(token) + + return null } } diff --git a/src/main/kotlin/io/github/kevinah95/klizard/lizard_ext/LizardMcCabe.kt b/src/main/kotlin/io/github/kevinah95/klizard/lizard_ext/LizardMcCabe.kt index ee1d63c..41d20a1 100644 --- a/src/main/kotlin/io/github/kevinah95/klizard/lizard_ext/LizardMcCabe.kt +++ b/src/main/kotlin/io/github/kevinah95/klizard/lizard_ext/LizardMcCabe.kt @@ -20,19 +20,22 @@ package io.github.kevinah95.klizard.lizard_ext class LizardMcCabe(): ExtensionBase() { - override fun _stateGlobal(token: String) { + override fun _stateGlobal(token: String): Boolean? { if (token == "case") { _state = ::_inCase } + + return null } - fun _inCase(token: String){ + fun _inCase(token: String): Boolean?{ if (token == ":"){ _state = ::_afterACase } + return null } - fun _afterACase(token: String) { + fun _afterACase(token: String): Boolean? { if (token == "case"){ context.addCondition(-1) if (context::class.members.any { it.name == "add_nd_condition" }){ @@ -43,6 +46,8 @@ class LizardMcCabe(): ExtensionBase() { } else { next(::_stateGlobal) } + + return null } } \ No newline at end of file