Skip to content

Instantly share code, notes, and snippets.

@CherryDT
Created April 10, 2017 15:06
Show Gist options
  • Save CherryDT/3b3537940bf7dced05bd0d6a1607c7d5 to your computer and use it in GitHub Desktop.
Save CherryDT/3b3537940bf7dced05bd0d6a1607c7d5 to your computer and use it in GitHub Desktop.
DynRPG Comment Parser (v0.20)
' For reference: The DynRPG comment parsing routine.
' It is optimized for speed more than for readability. (It even has a GoTo, yikes.)
' From DynRPG version 0.20
' (C) David "Cherry" Trapp 2013
' cherry@cherrytree.at
Sub ParseComment(text As ZString Ptr)
With CommentData
.command[0] = 0
.parametersCount = 0
If text = NULL Then Return
If text[0] <> Asc("@") Then Return
Dim p As Integer = 0
Dim strend As Boolean = FALSE
.command[SizeOf(.command) - 1] = 0
Do
p += 1
Select Case (*text)[p]
Case 0
strend = TRUE
Exit Do
Case Asc(" ")
Exit Do
Case Asc("A") To Asc("Z")
If p < SizeOf(.command) - 1 Then
.command[p - 1] = (*text)[p] + 32
EndIf
Case Else
If p < SizeOf(.command) - 1 Then
.command[p - 1] = (*text)[p]
EndIf
End Select
Loop
If p < SizeOf(.command) - 1 Then
.command[p - 1] = 0
EndIf
If strend = FALSE Then
Dim v_deref As Integer = 0
Dim n_deref As Boolean = FALSE
Do Until strend
If .parametersCount >= UBound(.parameters) Then
strend = TRUE
Exit Do
EndIf
p += 1
Select Case (*text)[p]
Case 0
strend = TRUE
Exit Do
Case Asc(" "), 13, 10
Continue Do
Case Asc("0") To Asc("9"), Asc("."), Asc("-")
' Number
Static n As String * 20 = ""
n = ""
p -= 1
Do
p += 1
Select Case (*text)[p]
Case Asc(" "), 13, 10
Continue Do
Case 0
strend = TRUE
Exit Do
Case Asc(",")
Exit Do
Case Else
n += Chr((*text)[p])
End Select
Loop
Var d = Val(n)
Do Until v_deref = 0
d = GetVar(d)
v_deref -= 1
Loop
If n_deref Then
n_deref = FALSE
With .parameters(.parametersCount)
.ptype = PARAM_STRING
.text = GetHeroName(d)
End With
Else
With .parameters(.parametersCount)
.ptype = PARAM_NUMBER
.number = d
End With
EndIf
.parametersCount += 1
Case Asc("""")
' String
With .parameters(.parametersCount)
.ptype = PARAM_STRING
Dim sp As Integer = 0
Dim paramend As Boolean = FALSE
Do Until paramend OrElse strend ' ADDED: OrElse strend
p += 1
Select Case (*text)[p]
Case 0
strend = TRUE
Exit Do
Case 13, 10
Continue Do
Case Asc("""")
If text[p + 1] = Asc("""") Then
If sp < SizeOf(.text) - 1 Then
.text[sp] = Asc("""")
sp += 1
EndIf
p += 1
Continue Do
Else
Do
p += 1
Select Case (*text)[p]
Case 0
strend = TRUE
Exit Do
Case Asc(",")
paramend = TRUE
Exit Do
End Select
Loop
EndIf
Case Else
If sp < SizeOf(.text) - 1 Then
.text[sp] = (*text)[p]
sp += 1
EndIf
End Select
Loop
.text[sp] = 0
End With
.parametersCount += 1
v_deref = 0
n_deref = FALSE
Case Asc("v"), Asc("V")
' Variable
v_deref += 1
Continue Do
Case Asc("n"), Asc("N")
' Hero name
If v_deref = 0 AndAlso n_deref = FALSE Then
n_deref = TRUE
Continue Do
Else
GoTo IsToken
EndIf
Case Else
' Token
IsToken:
With .parameters(.parametersCount)
.ptype = PARAM_TOKEN
Dim sp As Integer = v_deref
If n_deref Then
sp += 1
.text = "n" + String(v_deref, Asc("v"))
Else
.text = String(v_deref, Asc("v"))
EndIf
p -= 1
Do
p += 1
Select Case (*text)[p]
Case 0
strend = TRUE
Exit Do
Case Asc(" "), 13, 10
Continue Do
Case Asc(",")
Exit Do
Case Asc("A") To Asc("Z")
If sp < SizeOf(.text) - 1 Then
.text[sp] = (*text)[p] + 32
sp += 1
EndIf
Case Else
If sp < SizeOf(.text) - 1 Then
.text[sp] = (*text)[p]
sp += 1
EndIf
End Select
Loop
If sp < SizeOf(.text) Then ' ADDED!
.text[sp] = 0
EndIf
End With
.parametersCount += 1
v_deref = 0
n_deref = FALSE
End Select
Loop
EndIf
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment