package xm::code::awk;

use xm::o;
use xm::code::vars;
use xm::code::func;

sub DESC{"
  highlight sourcecode - AWK mode
"}

# BEGIN {
# written by JA
$LANGUAGE{'awk'}       =  {
                            'filename'   => '(?i)\\.awk$',
                            'regex'      => '^\\s*#\\s*![^\\s]*awk',
                            'patterns'   => [
                                              {
                                                'name'       => 'comment',
                                                'regex'      => '#.*?$',
                                                'style'      => 'comment',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'string',
                                                'regex'      => '""|".*?([^\\\\](\\\\\\\\)*)"|"\\\\\\\\"',
#                                                'regex'      => '""|"\\\\\\\\"|"[^"\\\\]"|"[^"].*?[^\\\\]"',
                                                'style'      => 'string',
                                                'childregex' => [
                                                                  {
                                                                    'name'       => 'esc character',
                                                                    'regex'      => '\\\\.',
                                                                    'style'      => 'esc character',
                                                                    'childregex' => []
                                                                  }
                                                                ]
                                              },
                                              {
                                                'name'       => 'string',
                                                'regex'      => '\'\'|\'.*?([^\\\\](\\\\\\\\)*)\'|\'\\\\\\\\\'',
#                                                'regex'      => '\'\'|\'\\\\\\\\\'|\'[^\'\\\\]\'|\'[^\'].*?[^\\\\]\'',
                                                'style'      => 'string',
                                                'childregex' => [
                                                                  {
                                                                    'name'       => 'esc character',
                                                                    'regex'      => '\\\\.',
                                                                    'style'      => 'esc character',
                                                                    'childregex' => []
                                                                  }
                                                                ]
                                              },
                                              {
                                                'name'       => 'function header',
                                                'regex'      => 'function[\\t ]+([a-zA-Z0-9_]+)[\\t \\n]*(\\{|\\n)',
                                                'style'      => 'function header',
                                                'childregex' => [
                                                                  {
                                                                    'name'       => 'function coloring',
                                                                    'regex'      => '[\\t ]([a-zA-Z0-9_]+)',
                                                                    'style'      => 'function header name',
                                                                    'childregex' => []
                                                                  }
                                                                ]
                                              },
                                              {
                                                'name'       => 'regex matching I 1',
                                                'regex'      => '(\\b| )?(/)(\\\\/|[^/\\n])*(/[gimesox]*)',
                                                'style'      => 'regex',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'regex matching I 2',
                                                'regex'      => '(?:\\b| )(?:(?:m|q|qq)([!"#$%&\'*+-/]))(\\\\\\2|[^\\2\\n])*(\\2[gimesox]*)',
                                                'style'      => 'regex',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'regex matching II',
                                                'regex'      => '(?:\\b| )?(?:s([!"#$%&\'*+-/]))(?:\\\\\\2|[^\\2\\n])*?(\\2)[^(\\2)\\n]*?(\\2[gimesox]*)',
                                                'style'      => 'regex',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'translate',
                                                'regex'      => '(?:\\b| )(?:(?:tr|y)([^\w\s]))(?:\\\\\\2|[^\\2\\n])*?(\\2)[^(\\2)\\n]*?(\\2[gimesox]*)',
                                                'style'      => 'regex',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'keywords',
                                                'regex'      => '\\b(BEGIN|END|ARGC|ARGIND|ARGV|CONVFMT|ENVIRON|ERRNO|FIELDWIDTHS|FILENAME|FNR|FS|IGNORECASE|NF|NR|OFMT|OFS|ORS|RS|RT|RSTART|RLENGTH|SUBSEP)\\b',
                                                'style'      => 'reserved word',                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'keywords 2',
                                                'regex'      => '\\b(if|while|do|for|in|break|continue|delete|exit|next|nextfile|function)\\b',
                                                'style'      => 'reserved word',                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'library fns',
                                                'regex'      => '\\b(close|getline|print|printf|system|fflush|atan2|cos|exp|int|log|rand|sin|sqrt|srand|gensub|gsub|index|length|split|sprintf|sub|substr|tolower|toupper|systime|strftime)\\b',                                                'style'      => 'library function',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => 'braces and parens',
                                                'regex'      => '[\\[\\]\\{\\}\\(\\)]',
                                                'style'      => 'braces',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => '<< stuff',
                                                'regex'      => '<<\'([^\\n]*)\';.*?^\\2$',
                                                'style'      => 'text',
                                                'childregex' => []
                                              },
                                              {
                                                'name'       => '<< stuff',
                                                'regex'      => '<<([^\\n]*).*?^\\2$',
                                                'style'      => 'text',
                                                'childregex' => []
                                              }
                                            ]
                           };
 
# }1;

sub DO
{
    return xm::code::func::DO($_[0],$LANGUAGE{awk}{patterns});
}

sub ARGS { return    xm::o::args_stdin(@_, DESC); }
sub main { return DO(xm::o::args_stdin(@_, DESC)); }

1;