Skip to content

Commit

Permalink
Add token types for string literals in LuaType tokenizer
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
YetAnotherClown committed Oct 1, 2024
1 parent 1362d34 commit ab4157e
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions docusaurus-plugin-moonwave/src/components/LuaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,31 @@ function tokenize(code, isGroup) {
}

const readBalanced = (left, right) => {
const leftChars = left.split("")
const rightChars = right.split("")

let leftDepth = 0
let rightDepth = 0

let buffer = ""

let depth = 0
while (peek()) {
if (peek() === left) {
if (peek() === leftChars[leftDepth]) {
leftDepth++
depth++
} else if (peek() === right) {
if (depth === 0) {
break
} else if (peek() === rightChars[rightDepth]) {
rightDepth++

if (rightDepth === rightChars.length) {
if (depth === 0) {
break
} else {
depth--
}
} else {
depth--
next()
continue
}
}

Expand Down Expand Up @@ -66,10 +80,21 @@ function tokenize(code, isGroup) {

if (peek() === "[") {
next()
tokens.push({
type: "indexer",
tokens: tokenize(readBalanced("[", "]")),
})

if (peek() === "[") {
next()
tokens.push({
type: "stringLiteral",
luaType: `[[${readBalanced("[[", "]]")}]]`,
})
next()
} else {
tokens.push({
type: "indexer",
tokens: tokenize(readBalanced("[", "]")),
})
}

next()
continue
}
Expand Down Expand Up @@ -122,12 +147,24 @@ function tokenize(code, isGroup) {
if (atom) {
if (atom.endsWith(":")) {
tokens.push({ type: "identifier", identifier: atom.slice(0, -1) })
} else {
continue
}

if (
(atom.startsWith("'") && atom.endsWith("'")) ||
(atom.startsWith('"') && atom.endsWith('"'))
) {
tokens.push({
type: "luaType",
type: "stringLiteral",
luaType: atom,
})
continue
}

tokens.push({
type: "luaType",
luaType: atom,
})
continue
}

Expand Down Expand Up @@ -233,6 +270,8 @@ function Token({ token, depth }) {
<PrOp>]</PrOp>
</span>
)
case "stringLiteral":
return <code className={styles.blue}>{token.luaType}</code>
case "luaType":
const sanitizedToken = token.luaType.replace(/\W/g, "")
if (sanitizedToken in typeLinks) {
Expand Down

0 comments on commit ab4157e

Please sign in to comment.