; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: This file contains procedures and data, that form the appearance of the
;               TAsmEdit components.
;
;  Target OS: Any
;
;  Dependencies:
;_________________________________________________________________________________________

module "FASM syntax highlighter for TAsmEdit"


ttText = 0
ttRem  = 1
ttStr  = 2
ttNum  = 3
ttChar = 4


ciText         = 0
ciComment      = 1
ciString       = 2
ciNumber       = 3
ciSymbol       = 4
ciRegister     = 5
ciInstruction  = 6
ciDerective    = 7
ciPreprocessor = 8
ciMacro        = 9


;_________________________________________________________________________________________
;
; Arguments:
;   .pLineInfo - pointer to TLineInfo structure.
;   .pAttr   - pointer to the array of TCharAttributes for the characters of the string.
;   .SynContext - input context of the highligher.
;
; Returns:
;       edx - pointer to TArray of TTextChunk
;       eax - output context of the highligher.
;
proc SyntaxFASM, .pLineInfo, .SynContext
.Quo   rb 4
.start dd ?
begin

        mov     esi, [.pLineInfo]
        mov     [.start], esi

        mov     edx, [esi+TLineInfo.syn_array]
        test    edx, edx
        jnz     .array_ok

        stdcall CreateArray, sizeof.TTextChunk
        mov     edx, eax
        mov     [esi+TLineInfo.syn_array], edx

.array_ok:


.loop:
        mov     al, [esi]
        test    al,al
        jz      .end_of_line

        cmp     [.SynContext], ttRem
        je      .endif

        cmp     [.SynContext], ttStr
        jne     .ifnotstr

        cmp     al,[.Quo]
        jne     .endif

        mov     [.SynContext], ttText
        mov     eax, ttStr
        jmp     .setcolor

.ifnotstr:
        cmp     al, ';'
        jne     .notrem

        mov     [.SynContext], ttRem
        jmp     .endif

.notrem:
        cmp     al, '"'
        je      .str
        cmp     al, "'"
        jne     .notstr

.str:
        mov     [.SynContext], ttStr
        mov     [.Quo], al
        jmp     .endif

.notstr:
        call    .CharInSymbols
        jnc     .notsym

        mov     [.SynContext], ttChar
        jmp     .endif

.notsym:
        cmp     [.SynContext], ttNum
        je      .endif

        cmp     esi, [.start]
        je      .maybenum

        cmp     [.SynContext], ttChar
        jne     .endif

; The number begins only after ttChar or at the begining of the line.
.maybenum:
        call    .CharInNum
        jnc     .notnum

        mov      [.SynContext], ttNum
        jmp      .endif

.notnum:
        mov     [.SynContext], ttText

.endif:
        mov     eax, [.SynContext]
        cmp     eax, ttText
        je      .next

.setcolor:
        cmp     [edi+TCharAttr.flags], 0
        jne     .next                           ; the char have protected attributes.

        mov     ecx, [ebx+4*eax-4]
        mov     [edi+TCharAttr.color], ecx

.next:
        add     edi, sizeof.TCharAttr
        inc     esi
        stdcall ScanForwardUtf8
        jmp     .loop

.end_of_line:
        mov     eax, ttText
        return


.CharInSymbols:
        push    edi ecx

        mov     edi, .symbol_characters+1
        movzx   ecx, byte [edi-1]

        repne scasb
        jne   @f
        stc
        pop     ecx edi
        ret
@@:
        clc
        pop     ecx edi
        retn


.CharInNum:
        cmp     al, '$'
        jne     @f
        stc
        retn

@@:
        cmp     al, '0'
        jb      @f
        cmp     al, '9'
        ja      @f
        stc
        retn
@@:
        clc
        retn


.symbol_characters db 27
                  db 9,0Ah,0Dh,1Ah,20h,'+-/*=<>()[]{}:,|&~#`;\'

endp

endmodule