Skip to content

Commit a725d9a

Browse files
committed
feat: add parse lua string
1 parent e217c29 commit a725d9a

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mavenPublishing {
8585

8686
signAllPublications()
8787

88-
coordinates("io.github.dingyi222666", "luaparser", "1.0.0")
88+
coordinates("io.github.dingyi222666", "luaparser", "1.0.1")
8989

9090
pom {
9191
name.set("luaparser")

src/commonMain/kotlin/io/github/dingyi222666/luaparser/parser/ast/node/expressionNode.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.dingyi222666.luaparser.parser.ast.node
22

33
import io.github.dingyi222666.luaparser.parser.ast.visitor.ASTVisitor
4+
import io.github.dingyi222666.luaparser.util.parseLuaString
45
import kotlin.jvm.Transient
56
import kotlin.properties.Delegates
67

@@ -68,7 +69,6 @@ class ConstantNode(
6869

6970
TYPE.NIL -> "nil"
7071

71-
//TODO: STRING/LONG STRING
7272
else -> newValue
7373
}
7474
}
@@ -82,6 +82,10 @@ class ConstantNode(
8282
FLOAT, INTERGER, BOOLEAN, STRING, NIL, UNKNOWN
8383
}
8484

85+
fun stringOf(): String {
86+
return parseLuaString(rawValue.toString())
87+
}
88+
8589
fun intOf(): Int {
8690
return _value as Int
8791
}

src/commonMain/kotlin/io/github/dingyi222666/luaparser/util/default.kt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,111 @@ fun <T> equalsMore(origin: T, vararg arg: T): Boolean {
1919
return false
2020
}
2121

22+
23+
fun parseLuaString(text: String): String {
24+
//char or default
25+
26+
val buffer = StringBuilder()
27+
var currentIndex = 0
28+
var isLongStringStart = false
29+
var isShortString = false
30+
var isLongStringEnd = false
31+
32+
while (currentIndex < text.length) {
33+
val currentChar = text[currentIndex]
34+
35+
if (isShortString && currentChar == '\\') {
36+
val nextChar = text.getOrNull(currentIndex + 1)
37+
if (nextChar == 'n') {
38+
buffer.append('\n')
39+
} else if (nextChar == 't') {
40+
buffer.append('\t')
41+
} else if (nextChar == 'r') {
42+
buffer.append('\r')
43+
} else if (nextChar == 'n') {
44+
buffer.append('\n')
45+
} else if (nextChar == 'f') {
46+
buffer.append('\u000c')
47+
} else if (nextChar == '\\') {
48+
buffer.append('\\')
49+
} else if (nextChar == '\'') {
50+
buffer.append('\'')
51+
} else if (nextChar == '"') {
52+
buffer.append('"')
53+
} else if (nextChar == ']') {
54+
buffer.append(']')
55+
} else if (nextChar == '[') {
56+
buffer.append('[')
57+
} else if (nextChar == '0') {
58+
buffer.append('\u0000')
59+
} else if (nextChar == 'x') {
60+
buffer.append(text.substring(currentIndex + 2, currentIndex + 4).toInt(16).toChar())
61+
currentIndex += 4
62+
continue
63+
} else if (nextChar == 'd') {
64+
buffer.append(text.substring(currentIndex + 2, currentIndex + 4).toInt(8).toChar())
65+
currentIndex += 4
66+
continue
67+
} else if (nextChar == 'u') {
68+
val charCode = text.substring(currentIndex + 2, currentIndex + 6).toInt(16)
69+
buffer.append(charCode.toChar())
70+
currentIndex += 6
71+
continue
72+
} else {
73+
throw IllegalArgumentException("Illegal escape character: $nextChar")
74+
}
75+
76+
currentIndex += 2
77+
78+
continue
79+
}
80+
81+
if (currentChar == '\'' || currentChar == '"') {
82+
if (!isShortString && !isLongStringStart) {
83+
isShortString = true
84+
} else {
85+
break
86+
}
87+
88+
currentIndex++
89+
continue
90+
}
91+
92+
if (currentChar == '[' && !isShortString) {
93+
if (!isLongStringStart && text.getOrNull(currentIndex + 1) == '=') {
94+
isLongStringStart = true
95+
} else if (isLongStringStart && text.getOrNull(currentIndex - 1) == '=') {
96+
isLongStringStart = false
97+
}
98+
99+
currentIndex++
100+
continue
101+
}
102+
103+
if ((currentChar == '=') and !isShortString and (isLongStringStart or isLongStringEnd)) {
104+
currentIndex++
105+
continue
106+
}
107+
108+
if (currentChar == ']' && !isShortString) {
109+
if (!isLongStringEnd && text.getOrNull(currentIndex + 1) == '=') {
110+
isLongStringEnd = true
111+
} else if (isLongStringEnd && text.getOrNull(currentIndex - 1) == '=') {
112+
break
113+
}
114+
115+
currentIndex++
116+
continue
117+
}
118+
119+
120+
121+
buffer.append(currentChar)
122+
123+
currentIndex++
124+
125+
}
126+
127+
return buffer.toString()
128+
129+
}

src/commonTest/kotlin/parser.common.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import io.github.dingyi222666.luaparser.parser.LuaParser
22
import io.github.dingyi222666.luaparser.source.AST2Lua
3+
import io.github.dingyi222666.luaparser.util.parseLuaString
34
import kotlin.test.Test
45

56
class ParserTest {
67

78
@Test
89
fun parse() {
10+
println(parseLuaString("""[==[xxx]==]"""))
11+
println(parseLuaString(""" "xxxx" """))
12+
println(parseLuaString("""'xxxx'"""))
13+
println(parseLuaString("""'\u6578\x01\n\r\t'"""))
14+
println(parseLuaString("""[[\u6578]]"""))
915
val parser = LuaParser()
1016

1117
val root = parser.parse(source)

0 commit comments

Comments
 (0)