1/*
2Language: Perl
3Author: Peter Leonov <gojpeg@yandex.ru>
4*/
5
6hljs.LANGUAGES.perl = function(){
7  var PERL_DEFAULT_CONTAINS = ['comment', 'string', 'number', 'regexp', 'sub', 'variable', 'operator', 'pod'];
8  var PERL_KEYWORDS = {'getpwent': 1, 'getservent': 1, 'quotemeta': 1, 'msgrcv': 1, 'scalar': 1, 'kill': 1, 'dbmclose': 1, 'undef': 1, 'lc': 1, 'ma': 1, 'syswrite': 1, 'tr': 1, 'send': 1, 'umask': 1, 'sysopen': 1, 'shmwrite': 1, 'vec': 1, 'qx': 1, 'utime': 1, 'local': 1, 'oct': 1, 'semctl': 1, 'localtime': 1, 'readpipe': 1, 'do': 1, 'return': 1, 'format': 1, 'read': 1, 'sprintf': 1, 'dbmopen': 1, 'pop': 1, 'getpgrp': 1, 'not': 1, 'getpwnam': 1, 'rewinddir': 1, 'qq': 1, 'fileno': 1, 'qw': 1, 'endprotoent': 1, 'wait': 1, 'sethostent': 1, 'bless': 1, 's': 1, 'opendir': 1, 'continue': 1, 'each': 1, 'sleep': 1, 'endgrent': 1, 'shutdown': 1, 'dump': 1, 'chomp': 1, 'connect': 1, 'getsockname': 1, 'die': 1, 'socketpair': 1, 'close': 1, 'flock': 1, 'exists': 1, 'index': 1, 'shmget': 1, 'sub': 1, 'for': 1, 'endpwent': 1, 'redo': 1, 'lstat': 1, 'msgctl': 1, 'setpgrp': 1, 'abs': 1, 'exit': 1, 'select': 1, 'print': 1, 'ref': 1, 'gethostbyaddr': 1, 'unshift': 1, 'fcntl': 1, 'syscall': 1, 'goto': 1, 'getnetbyaddr': 1, 'join': 1, 'gmtime': 1, 'symlink': 1, 'semget': 1, 'splice': 1, 'x': 1, 'getpeername': 1, 'recv': 1, 'log': 1, 'setsockopt': 1, 'cos': 1, 'last': 1, 'reverse': 1, 'gethostbyname': 1, 'getgrnam': 1, 'study': 1, 'formline': 1, 'endhostent': 1, 'times': 1, 'chop': 1, 'length': 1, 'gethostent': 1, 'getnetent': 1, 'pack': 1, 'getprotoent': 1, 'getservbyname': 1, 'rand': 1, 'mkdir': 1, 'pos': 1, 'chmod': 1, 'y': 1, 'substr': 1, 'endnetent': 1, 'printf': 1, 'next': 1, 'open': 1, 'msgsnd': 1, 'readdir': 1, 'use': 1, 'unlink': 1, 'getsockopt': 1, 'getpriority': 1, 'rindex': 1, 'wantarray': 1, 'hex': 1, 'system': 1, 'getservbyport': 1, 'endservent': 1, 'int': 1, 'chr': 1, 'untie': 1, 'rmdir': 1, 'prototype': 1, 'tell': 1, 'listen': 1, 'fork': 1, 'shmread': 1, 'ucfirst': 1, 'setprotoent': 1, 'else': 1, 'sysseek': 1, 'link': 1, 'getgrgid': 1, 'shmctl': 1, 'waitpid': 1, 'unpack': 1, 'getnetbyname': 1, 'reset': 1, 'chdir': 1, 'grep': 1, 'split': 1, 'require': 1, 'caller': 1, 'lcfirst': 1, 'until': 1, 'warn': 1, 'while': 1, 'values': 1, 'shift': 1, 'telldir': 1, 'getpwuid': 1, 'my': 1, 'getprotobynumber': 1, 'delete': 1, 'and': 1, 'sort': 1, 'uc': 1, 'defined': 1, 'srand': 1, 'accept': 1, 'package': 1, 'seekdir': 1, 'getprotobyname': 1, 'semop': 1, 'our': 1, 'rename': 1, 'seek': 1, 'if': 1, 'q': 1, 'chroot': 1, 'sysread': 1, 'setpwent': 1, 'no': 1, 'crypt': 1, 'getc': 1, 'chown': 1, 'sqrt': 1, 'write': 1, 'setnetent': 1, 'setpriority': 1, 'foreach': 1, 'tie': 1, 'sin': 1, 'msgget': 1, 'map': 1, 'stat': 1, 'getlogin': 1, 'unless': 1, 'elsif': 1, 'truncate': 1, 'exec': 1, 'keys': 1, 'glob': 1, 'tied': 1, 'closedir': 1, 'ioctl': 1, 'socket': 1, 'readlink': 1, 'eval': 1, 'xor': 1, 'readline': 1, 'binmode': 1, 'setservent': 1, 'eof': 1, 'ord': 1, 'bind': 1, 'alarm': 1, 'pipe': 1, 'atan2': 1, 'getgrent': 1, 'exp': 1, 'time': 1, 'push': 1, 'setgrent': 1, 'gt': 1, 'lt': 1, 'or': 1, 'ne': 1, 'm': 1};
9  return {
10    defaultMode: {
11      lexems: [hljs.IDENT_RE],
12      contains: PERL_DEFAULT_CONTAINS,
13      keywords: PERL_KEYWORDS
14    },
15    modes: [
16
17      // variables
18      {
19        className: 'variable',
20        begin: '\\$\\d', end: '^'
21      },
22      {
23        className: 'variable',
24        begin: '[\\$\\%\\@\\*](\\^\\w\\b|#\\w+(\\:\\:\\w+)*|[^\\s\\w{]|{\\w+}|\\w+(\\:\\:\\w*)*)', end: '^'
25      },
26
27      // numbers and strings
28      {
29        className: 'subst',
30        begin: '[$@]\\{', end: '\}',
31        lexems: [hljs.IDENT_RE],
32        keywords: PERL_KEYWORDS,
33        contains: PERL_DEFAULT_CONTAINS,
34        relevance: 10
35      },
36      {
37        className: 'number',
38        begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b', end: '^',
39        relevance: 0
40      },
41      {
42        className: 'string',
43        begin: 'q[qwxr]?\\s*\\(', end: '\\)',
44        contains: ['escape', 'subst', 'variable'],
45        relevance: 5
46      },
47      {
48        className: 'string',
49        begin: 'q[qwxr]?\\s*\\[', end: '\\]',
50        contains: ['escape', 'subst', 'variable'],
51        relevance: 5
52      },
53      {
54        className: 'string',
55        begin: 'q[qwxr]?\\s*\\{', end: '\\}',
56        contains: ['escape', 'subst', 'variable'],
57        relevance: 5
58      },
59      {
60        className: 'string',
61        begin: 'q[qwxr]?\\s*\\|', end: '\\|',
62        contains: ['escape', 'subst', 'variable'],
63        relevance: 5
64      },
65      {
66        className: 'string',
67        begin: 'q[qwxr]?\\s*\\<', end: '\\>',
68        contains: ['escape', 'subst', 'variable'],
69        relevance: 5
70      },
71      {
72        className: 'string',
73        begin: 'qw\\s+q', end: 'q',
74        contains: ['escape', 'subst', 'variable'],
75        relevance: 5
76      },
77      {
78        className: 'string',
79        begin: '\'', end: '\'',
80        contains: ['escape'],
81        relevance: 0
82      },
83      {
84        className: 'string',
85        begin: '"', end: '"',
86        contains: ['escape','subst','variable'],
87        relevance: 0
88      },
89      hljs.BACKSLASH_ESCAPE,
90      {
91        className: 'string',
92        begin: '`', end: '`',
93        contains: ['escape']
94      },
95
96      // regexps
97      {
98        className: 'regexp',
99        begin: '(s|tr|y)/(\\\\.|[^/])*/(\\\\.|[^/])*/[a-z]*', end: '^',
100        relevance: 10
101      },
102      {
103        className: 'regexp',
104        begin: '(m|qr)?/', end: '/[a-z]*',
105        contains: ['escape'],
106        relevance: 0 // allows empty "//" which is a common comment delimiter in other languages
107      },
108
109      // bareword context
110      {
111        className: 'string',
112        begin: '{\\w+}', end: '^',
113        relevance: 0
114      },
115      {
116        className: 'string',
117        begin: '\-?\\w+\\s*\\=\\>', end: '^',
118        relevance: 0
119      },
120
121      // subroutines
122      {
123        className: 'sub',
124        begin: '\\bsub\\b', end: '(\\s*\\(.*?\\))?[;{]',
125        lexems: [hljs.IDENT_RE],
126        keywords: {'sub':1},
127        relevance: 5
128      },
129
130      // operators
131      {
132        className: 'operator',
133        begin: '-\\w\\b', end: '^',
134        relevance: 0
135      },
136
137      // comments
138      hljs.HASH_COMMENT_MODE,
139      {
140        className: 'comment',
141        begin: '^(__END__|__DATA__)', end: '\\n$',
142        relevance: 5
143      },
144      // pod
145      {
146        className: 'pod',
147        begin: '\\=\\w', end: '\\=cut'
148      }
149
150    ]
151  };
152}();