# Escapes common to string and regexp
syntax .awk-esc

state special
    char 0-3 oct1
    char 4-7 oct2
    char x hex0
    # Anything but \n
    char -n "\n" END error
    # Don't eat \n
    noeat END

state oct1 special
    char 0-7 oct2
    noeat END

state oct2 special
    char 0-7 END special
    noeat END

state hex0 special
    char 0-9a-fA-F hex1
    noeat END

state hex1 special
    char 0-9a-fA-F hex2
    noeat END

# "\xaff" is an error but "\xafg" is not
state hex2 special
    char 0-9a-fA-F END error
    noeat END

syntax awk

state code
    char \" string
    char / regexp
    char # comment
    char -b a-zA-Z_ ident
    eat this

state string
    char \\ dqescape
    char \" code string
    # Avoid highlighting rest of the file again
    char "\n" code
    eat this

state regexp
    char \\ regexpescape
    char / code regexp
    # Avoid highlighting rest of the file again
    char "\n" code
    eat this

state dqescape special
    char abfnrtv\\\" string special
    noeat .awk-esc:string

state regexpescape special
    char abfnrtv.[({*+?|^/\\\$ regexp special
    noeat .awk-esc:regexp

state comment
    char "\n" code
    eat this

state ident
    char -b a-zA-Z0-9_ this
    inlist keyword code
    inlist builtin code
    inlist pattern code
    inlist variable code
    noeat code

# gawk reserved words are separated by an empty line

list keyword \
    break continue delete do else exit for function getline if next \
    print printf return while \
\
    func nextfile

list builtin \
    atan2 close cos exp fflush gsub index int length log match rand sin \
    split sprintf sqrt srand sub substr system \
\
    and asort bindtextdomain compl dcgettext gensub lshift mktime or \
    rshift strftime strtonum systime tolower toupper xor

list pattern BEGIN END

list variable \
    ARGC ARGV FILENAME FNR FS NF NR OFMT OFS ORS RLENGTH RS RSTART SUBSEP \
\
    ARGIND BINMODE CONVFMT ENVIRON ERRNO FIELDWIDTHS IGNORECASE LINT \
    PROCINFO RLENGTH RT TEXTDOMAIN

default string regexp
default keyword pattern
