Analyzer of brackets (?)
Posted: Tue Dec 19, 2023 1:43 pm
My brain has broken. How to do it?
Code: Select all
EnableExplicit
Procedure Brackets(*c.Character, Array ArrBrackets(2))
Protected c, i, Pos = -1, Open, Close, flgBracket
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
While *c\c
Pos + 1
Select *c\c
Case '('
Open + 1
ReDim ArrBrackets(1, Open)
If flgBracket
c + 1
EndIf
ArrBrackets(0, Open) = Pos
flgBracket = 1
Case ')'
Close + 1
If c
ArrBrackets(1, Close + c - 1) = Pos
c - 1
Else
ArrBrackets(1, Close) = Pos
EndIf
flgBracket = 0
EndSelect
*c + SizeOf(Character)
Wend
EndProcedure
Define MyStr$, i
Define Dim ArrBr(1, 0)
MyStr$ = "word (word) (word\(f\)word) (word(f)word)"
Brackets(@MyStr$, ArrBr())
Debug "Count = " + Str(ArraySize(ArrBr(), 2))
For i = 0 To ArraySize(ArrBr(), 2) - 1
Debug Str(ArrBr(0, i)) + " - " + Str(ArrBr(1, i))
Next
Enumeration
#Brackets1
#Brackets2
EndEnumeration
Define *Text
Define Toggle
If Not InitScintilla()
End
EndIf
If OpenWindow(0, 0, 0, 630, 190, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ScintillaGadget(0, 10, 10, 620, 170, 0)
*Text = UTF8(MyStr$)
ScintillaSendMessage(0, #SCI_SETTEXT, 0, *Text)
FreeMemory(*Text)
ScintillaSendMessage(0, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 16)
ScintillaSendMessage(0, #SCI_INDICSETSTYLE, #Brackets1, #INDIC_STRAIGHTBOX)
ScintillaSendMessage(0, #SCI_INDICSETFORE, #Brackets1, $9900FF)
ScintillaSendMessage(0, #SCI_INDICSETUNDER, #Brackets1, 1)
ScintillaSendMessage(0, #SCI_INDICSETALPHA, #Brackets1, 127)
ScintillaSendMessage(0, #SCI_INDICSETSTYLE, #Brackets2, #INDIC_STRAIGHTBOX)
ScintillaSendMessage(0, #SCI_INDICSETFORE, #Brackets2, $0099FF)
ScintillaSendMessage(0, #SCI_INDICSETUNDER, #Brackets2, 1)
ScintillaSendMessage(0, #SCI_INDICSETALPHA, #Brackets2, 127)
For i = 0 To ArraySize(ArrBr(), 2) - 1
ScintillaSendMessage(0, #SCI_SETINDICATORCURRENT, Toggle, #INDIC_STRAIGHTBOX)
ScintillaSendMessage(0, #SCI_INDICATORFILLRANGE, ArrBr(0, i), 1)
ScintillaSendMessage(0, #SCI_INDICATORFILLRANGE, ArrBr(1, i), 1)
Toggle = Bool(Not Toggle)
Next
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf