Jump to content
Sign in to follow this  
Zisly

Highlight syntax help..

Recommended Posts

Zisly

I've looked thought Betapad to try to understand how colors are determined but I don't get it :D

So I'll give the code here..

You don't need to explain everything, just give me a push to the right direction ;)

Global $stringStart = '{\cf0\b ';153, 153, 204
Global $stringEnd = '}'
Global $commentStart = '{\cf2\i '; 0, 153, 51
Global $commentEnd = '}'
Global $keywordStart = '{\\cf1\\b ';0, 0, 255
Global $keywordEnd = '}'
Global $functionstart = '{\\cf3\\i\\b ';0, 0, 144
Global $functionend = '}'
Global $numberStart = '{\\cf4\\i\\b ';214, 58, 169
Global $numberEnd = '}'
Global $macroStart = '{\\cf7\\b ';255, 51, 255
Global $macroEnd = '}'
Global $variableStart = '{\\cf5\\b ';170, 0, 0
Global $variableEnd = '}'
Global $hashStart = '{\cf6\i '; 240, 0, 255
Global $hashEnd = '}'
Global $operatorStart = '{\cf8\b '; 240, 0, 255
Global $operatorEnd = '}'

Func _SyntaxHighlight($hiauto = True)
    Local $keywords[36] = ['If', 'Then', 'ElseIf', 'Else', 'EndIf', 'Select', 'Switch', 'Case', 'EndSwitch', 'EndSelect', _
            'For', 'In', 'Next', 'Step', 'With', 'EndWith', 'While', 'WEnd', 'Do', 'Until', 'Func', 'Endfunc', 'Const', 'ContinueCase', _
            'ContinueLoop', 'Default', 'Dim', 'Enum', 'Exit', 'ExitLoop', 'False', 'Global', 'Local', 'ReDim', 'Return', 'True']
    Local $operators[11] = ['+', '-', '/', '*', '[', ']', '=', '&', '^', '<', '>']
    Local $macros = StringSplit(IniRead(@ScriptDir & "\definitions.ini", "Definitions", "Macros", ""), " ")
    Local $d = $DocEd1.Text
    $d = StringReplace($d, "\", "\\") ;these chars have special meanings in rtf
    $d = StringReplace($d, "{", "\{")
    $d = StringReplace($d, "}", "\}")
    ; Split into lines...
    ; Windows line ending   --> Unix line ending.
    $sLine = StringSplit($d, @CRLF, 1)
;~  _ArrayDisplay($sLine, "")
    $sOutput = ''; The output.
    For $i = 1 To $sLine[0]
        If $hiauto Then
            $sParsed = ''; Current parsed line.
            $sText = ''; Text notation.
            ; For each character...
            For $j = 1 To StringLen($sLine[$i])
                ; Set the current char...
                $cchar = StringMid($sLine[$i], $j, 1);
                ; If we found the double quote
                If ($cchar = '"') Then
                    ; If no text opened then open the text.
                    If ($sText = '') Then
                        $sText = '"'
                        $sParsed &= $stringStart & '"'
                        ; If the text is opened using the " then close the text.
                    ElseIf ($sText = '"') Then
                        $sText = ''
                        $sParsed &= '"' & $stringEnd
                        ; Else (e.g. opened using the ', or comment mode), print as normal.
                    Else
                        $sParsed &= '"'
                    EndIf
                    ; But if we found the single quote
                ElseIf ($cchar = "'") Then
                    ; If no text is opened then open the text.
                    If ($sText = '') Then
                        $sText = "'"
                        $sParsed &= $stringStart & "'"
                        ; If the text if opened using the ' then close the text.
                    ElseIf ($sText = "'") Then
                        $sText = ''
                        $sParsed &= "'" & $stringEnd
                        ; Else, print as normal.
                    Else
                        $sParsed &= "'";
                    EndIf
                    ; If we found a ; and no text is opened, it is a comment.
                ElseIf ($cchar = ';' And $sText = '') Then
                    $sText = ';'
                    $sParsed &= $commentStart & ';'
                    ; If we found a # and no text is opened, it is a hash statement.
                ElseIf ($cchar = '#' And $sText = '') Then
                    $sText = '#'
                    $sParsed &= $hashStart & '#'
                    ; Other characters...
                Else
                    ; If current text is normal text, print normally.
                    ; This way we can hilight keywords correctly.
                    For $iOperator = 0 To UBound($operators) - 1
                        If $cchar = $operators[$iOperator] And $sText <> "'" And $sText <> '"' And $sText <> ';' Then
                            $sParsed &= $operatorStart & $cchar & $operatorEnd
                            $cchar = ''
                        EndIf
                    Next
                    $sParsed &= $cchar
                EndIf
            Next
            ; OKay, we finished the loop, we closed the unclosed string.
            If ($sText = '"' Or $sText = "'") Then
                $sParsed &= $stringEnd
                ; .. and we close the comment.
            ElseIf ($sText = ';') Then
                $sParsed &= $commentEnd
                ; .. and we close the hash.
            ElseIf ($sText = '#') Then
                $sParsed &= $hashEnd
            EndIf
            ; We indent the text, so it's easier to parse.
            $sParsed = ' ' & $sParsed & ' '
            ; Highlight the keyword. $sParsed is byref.
            For $iKeyword = 0 To UBound($keywords) - 1
                replaceKeywords($sParsed, $keywords[$iKeyword], $sText)
            Next
            ; Highlight the number.
            $sParsed = StringRegExpReplace($sParsed, '([^A-Za-z0-9_#])((?:[0-9]+)\.?(?:[0-9]+)?e?(?:[0-9]+)?)([^A-Za-z0-9_])', '\1' & $numberStart & '\2' & $numberEnd & '\3')
            ; .. and the hexadecimal too.
            $sParsed = StringRegExpReplace($sParsed, '([^A-Za-z0-9_#])(0x)([0-9a-fA-F]+)([^A-Za-z0-9_])', '\1' & $numberStart & '\2\3' & $numberEnd & '\4')
            ; Now, highlight the function.
            $sParsed = StringRegExpReplace($sParsed, '([A-Za-z0-9_]+)(\s*?)\(', $functionstart & '\1' & $functionend & '\2(')
            ; Then, highlight the macro.
            $sParsed = StringRegExpReplace($sParsed, '@([A-Za-z0-9_]+)', $macroStart & '@\1' & $macroEnd)
            ; And some variable.
            $sParsed = StringRegExpReplace($sParsed, '\$([A-Za-z0-9_]+)', $variableStart & '$\1' & $variableEnd)
            ;Correct macro case - e.g. @appdatacommondir > @AppDataCommonDir
            For $iMacro = 1 To $macros[0]
                $sParsed = StringReplace($sParsed, StringReplace($macroStart & $macros[$iMacro] & $macroEnd, "\\", "\"), StringReplace($macroStart & $macros[$iMacro] & $macroEnd, "\\", "\"))
            Next
            ; Finally, outdent the text.
            $sParsed = StringMid($sParsed, 2, StringLen($sParsed) - 2)
            $sOutput &= $sParsed & "\line "
        Else
            $sOutput &= $sLine[$i] & "\line "
        EndIf
    Next
    $sTemp = "{\rtf1\ansi\deff0{\fonttbl{\f0\froman Courier New;}}{\colortbl{"
    $sTemp &= _RTFColor(153, 153, 204) ;0 - String
    $sTemp &= _RTFColor(0, 0, 255)  ;1 - Keyword
    $sTemp &= _RTFColor(0, 153, 51)     ;2 - Comment
    $sTemp &= _RTFColor(0, 0, 144)  ;3 - Function
    $sTemp &= _RTFColor(214, 58, 169)   ;4 - Number
    $sTemp &= _RTFColor(170, 0, 0)  ;5 - Variable
    $sTemp &= _RTFColor(240, 0, 255)    ;6 - Hash
    $sTemp &= _RTFColor(255, 51, 255)   ;7 - Macro
    $sTemp &= _RTFColor(255, 0, 0)  ;8 - Operator
    $sTemp &= "}}\deflang1033\plain\fs20 " & $sOutput & "}"
    $DocEd1.TextRTF = $sTemp
EndFunc   ;==>_SyntaxHighlight

; Replaces the keywords
Func replaceKeywords(ByRef $p, $s, $sText)
    $p = StringRegExpReplace($p, '(?i)([^a-z0-9_])(' & $s & ')([^a-z0-9_])', '\1' & $keywordStart & $s & $keywordEnd & '\3');
EndFunc   ;==>replaceKeywords
Func _RTFColor($iRed, $iGreen, $iBlue)
    Return "\red" & $iRed & "\green" & $iGreen & "\blue" & $iBlue & ";"
EndFunc   ;==>_RTFColor

Share this post


Link to post
Share on other sites
Zisly

bump!!!! ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×