preproc = "import"

# these might be unreadable but I don't know how else to do that...
(keyword,regexp,keyword) = 
        `(s)(\{(?:\\\}|[^}])*\}\{(?:\\\}|[^}])*\})([ixsmogce]*)`
(keyword,regexp,keyword) = 
        `(s)(\((?:\\\)|[^)])*\)\((?:\\\)|[^)])*\))([ixsmogce]*)`
(keyword,regexp,keyword) = 
        `(s)(\[(?:\\\]|[^\]])*\]\[(?:\\\]|[^\]])*\])([ixsmogce]*)`
(keyword,regexp,keyword) = `(s)(<.*><.*>)([ixsmogce]*)`

(keyword,string) = `(q(?:q?))(\{(?:\\\}|[^}])*\})`
(keyword,string) = `(q(?:q?))(\((?:\\\)|[^)])*\))`
(keyword,string) = `(q(?:q?))(\[(?:\\\]|[^\]])*\])`
(keyword,string) = `(q(?:q?))(<.*>)`

(keyword,string,string) = `(q(?:q?))([^[:alnum:][:blank:]])(.*\2)`

# the last (lookahead) expression is used to deal with # used
# as a delimiter.
# otherwise, with a line such as
# s#foo\###;      # my comment
# the # of the comment would be match as the closing delimiter
(keyword,regexp,regexp,keyword) = 
    `(s)([^[:alnum:][:blank:]])(.*\2.*\2)([ixsmogce]*(?=[[:blank:]]*(?:\)|;)))`

# this is to deal with cases where the delimiters for the first and the
# second part are not the same (and spaces are allowed between the first
# closing and the second opening)
(keyword,regexp,regexp,regexp,regexp,keyword) = 
    `(s)([^[:alnum:][:blank:]])(.*\2[[:blank:]]*)([^[:alnum:][:blank:]])(.*\4)([ixsmogce]*(?=[[:blank:]]*(?:\)|;)))`

include "script_comment.lang"

include "number.lang"

# this won't work if # has something (non blank) before
vardef comment_in_exp = '[[:blank:]]+#.*'

vardef var_in_exp = '\$([[:word:]]+|\{[[:word:]]+\})'

# this is to highlight correctly regular expressions
# (and don't mix them with { } code blocks
environment keyword = '(m|qr)(?=\{)' begin
  environment regexp = '\{' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\\{|\\\}'
    regexp = "}" exitall
  end
end

# repeat for other non alpha numerical chars
environment keyword = '(m|qr)(?=#)' begin
  environment regexp = '#' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\#'
    regexp = "#" exitall
  end
end

environment keyword = '(m|qr)(?=\|)' begin
  environment regexp = '\|' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\\|'
    regexp = "\|" exitall
  end
end

environment keyword = '(m|qr)(?=@)' begin
  environment regexp = '@' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\@'
    regexp = "@" exitall
  end
end

environment keyword = '(m|qr)(?=<)' begin
  environment regexp = '<' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\<|\\>'
    regexp = ">" exitall
  end
end

environment keyword = '(m|qr)(?=\[)' begin
  environment regexp = '\[' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\]'
    regexp = "]" exitall
  end
end

environment keyword = '(m|qr)(?=\\)' begin
  environment regexp = '\\' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\\\'
    regexp = "\\" exitall
  end
end

environment keyword = '(m|qr)(?=/)' begin
  environment regexp = '/' begin
    comment = $comment_in_exp
    variable = $var_in_exp
    regexp = '\\/'
    regexp = "/" exitall
  end
end

string delim "\"" "\"" escape "\\"
string delim "'" "'"  escape "\\"
string delim "<" ">"
#string = '[[:word:]]*/[^\n]*/[[:word:]]*'
string = '/[^\n]*/'

keyword = "chomp|chop|chr|crypt|hex|i|index|lc|lcfirst|length|oct|ord|pack|q|qq|reverse|rindex|sprintf|substr|tr|uc|ucfirst|m|s|g|qw|abs|atan2|cos|exp|hex|int|log|oct|rand|sin|sqrt|srand|my|local|our|delete|each|exists|keys|values|pack|read|syscall|sysread|syswrite|unpack|vec|undef|unless|return|length|grep|sort|caller|continue|dump|eval|exit|goto|last|next|redo|sub|wantarray|pop|push|shift|splice|unshift|split|switch|join|defined|foreach|last|chop|chomp|bless|dbmclose|dbmopen|ref|tie|tied|untie|while|next|map|eq|die|cmp|lc|uc|and|do|if|else|elsif|for|use|require|package|import|chdir|chmod|chown|chroot|fcntl|glob|ioctl|link|lstat|mkdir|open|opendir|readlink|rename|rmdir|stat|symlink|umask|unlink|utime|binmode|close|closedir|dbmclose|dbmopen|die|eof|fileno|flock|format|getc|print|printf|read|readdir|rewinddir|seek|seekdir|select|syscall|sysread|sysseek|syswrite|tell|telldir|truncate|warn|write|alarm|exec|fork|getpgrp|getppid|getpriority|kill|pipe|qx|setpgrp|setpriority|sleep|system|times|x|wait|waitpid"

comment delim '^\=(?:head1|head2|item)' '\=cut' multiline

variable = '(?:\$[#]?|@|%)[/[:word:]]+'

include "symbols.lang"

cbracket = "{|}"

include "function.lang"

