Skip to content

Commit

Permalink
fix: change invoke implementation in CodeStateMachine
Browse files Browse the repository at this point in the history
This commit can break the implementation

BREAKING CHANGE: Now states return Boolean?
  • Loading branch information
kevinah95 committed Nov 30, 2023
1 parent 315ae18 commit ae825a5
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ".") {
Expand All @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "::") {
Expand All @@ -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 ->
Expand All @@ -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") {
Expand Down Expand Up @@ -129,6 +137,8 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) {
_state = ::_stateOldCParams
_savedTokens = mutableListOf(token)
}

return null
}

val _stateThrow = readInsideBracketsThen("()") { _ ->
Expand All @@ -141,22 +151,24 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) {
_state(token)
}

fun _stateNoexcept(token: String) {
fun _stateNoexcept(token: String): Boolean? {
if (token == "(") {
_state = _stateThrow
} else {
_state = ::_stateDecToImp
}

_state(token)

return null
}

val _stateTrailingReturn = readUntilThen(";{") { token, _ ->
_state = ::_stateDecToImp
_state(token)
}

open fun _stateOldCParams(token: String) {
open fun _stateOldCParams(token: String): Boolean? {
_savedTokens.add(token)
if (token == ";") {
_savedTokens = mutableListOf<String>()
Expand All @@ -165,7 +177,7 @@ open class CLikeStates(context: FileInfoBuilder) : CodeStateMachine(context) {
if (_savedTokens.size == 2) {
_savedTokens = mutableListOf<String>()
_stateDecToImp(token)
return
return null
}

_state = ::_stateGlobal
Expand All @@ -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
}


Expand All @@ -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) {_ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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
var callback: (() -> Unit)? = null
var rutTokens: MutableList<String> = 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 }
Expand All @@ -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
}
Expand All @@ -66,21 +69,20 @@ 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)
}

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
Expand All @@ -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
Expand All @@ -111,22 +115,25 @@ 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<String>) -> Unit): (String) -> Unit {
fun decorator(func: ((String, List<String>) -> Unit)): (String) -> Unit {
fun readUntilThenToken(token: String): Unit {
fun readUntilThen(tokens: String, function: (String, List<String>) -> Unit): (String) -> Boolean? {
fun decorator(func: ((String, List<String>) -> Unit)): (String) -> Boolean? {
fun readUntilThenToken(token: String): Boolean? {

if (token in tokens){
func(token, rutTokens)
rutTokens = mutableListOf()
} else {
rutTokens.add(token)
}
return null
}
return ::readUntilThenToken
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class GoReader : CodeReader() {

override var ext: MutableList<String> = mutableListOf("go")

// TODO: Implement this
val languageNames: List<String> = listOf("go")

override operator fun invoke(_context: FileInfoBuilder) {
Expand Down
Loading

0 comments on commit ae825a5

Please sign in to comment.