-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimxs.pl
341 lines (292 loc) · 8.18 KB
/
vimxs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/perl
use strict;
use warnings;
use Fatal qw/open/;
use File::Spec;
our $VERSION = '0.201008171';
my $root = $ARGV[0] || '.';
die 'Wrong directory' unless -e $root and -r _ and -x _;
sub perlfile {
my $path = File::Spec->catfile($root, $_[0]);
return $_[1] ? do { open my $fh, '<', $path; $fh } : $path;
}
my (%macros, %functions, %private, %superseded);
my %skip_functions = map { $_ => 1 } qw{
lop
};
my %types = map { $_ => 1 } qw{
pTHX _pTHX pTHX_
pMY_CXT _pMY_CXT pMY_CXT_
my_cxt_t
OPCODE
};
my %skip_types = map { $_ => 1 } qw{
sv av hv he hek cv gv gp io
op unop binop listop svop pvop pmop padop loop logop
cop block subst context stackinfo
token nexttoken
interpreter expectation
jmpenv
any
};
sub maybe_type {
local $_ = $_[0];
return if $skip_types{$_};
++$types{$_};
}
my %variables = map { $_ => 1 } qw{
aTHX _aTHX aTHX_
aMY_CXT _aMY_CXT aMY_CXT_
MY_CXT
SP TARG MARK ORIGMARK
RETVAL items
};
my %constants = map { $_ => 1 } qw{
SVt_PVBM SVt_RV
};
my %strings = map { $_ => 1 } qw{
IVdf UVuf UVof UVxf NVef NVff NVgf
SVf SVf_ SVf32 SVf256
};
my %exceptions = map { $_ => 1 } qw{
dXCPT XCPT_TRY_START XCPT_TRY_END XCPT_CATCH XCPT_RETHROW
};
my %keywords = map { $_ => 1 } qw{
MODULE PACKAGE PREFIX
IN OUTLIST IN_OUTLIST OUT IN_OUT
ENABLE DISABLE
length
OUTPUT: NO_OUTPUT: CODE: INIT: NO_INIT: PREINIT: SCOPE: INPUT: C_ARGS:
PPCODE: REQUIRE: CLEANUP: POSTCALL: BOOT: VERSIONCHECK:
PROTOTYPES: PROTOTYPE: ALIAS: OVERLOAD: FALLBACK:
INTERFACE: INTERFACE_MACRO: INCLUDE: CASE:
};
my %skip_macro = map { $_ => 1 } qw{
void const register volatile NULL
};
sub maybe_macro {
local $_ = $_[0];
my $value = $_[1];
return if $skip_macro{$_}
or $functions{$_} or $private{$_} or $superseded{$_}
or $types{$_} or $variables{$_} or $constants{$_}
or $strings{$_} or $exceptions{$_} or $keywords{$_};
if (/^(?:SV[sfp]|OPf_|OPp[A-Z]+|OA_|CXt_|G_|PERL_MAGIC_)/) {
++$constants{$_};
return;
} elsif (/^\w+_t$/) {
++$types{$_};
return;
}
++$macros{$_};
}
my %clib = (
map( { 'std' . $_ => 'PerlIO_std' . $_ } qw/in out err/ ),
map( { 'f' . $_ => 'PerlIO_' . $_ } qw/open reopen flush close read write puts eof seek getpos setpos error/ ),
map( { my $p = 'PerlIO_' . $_; $_ => $p, "f$_" => $p } qw/printf getc putc/ ),
map( { $_ => 'PerlIO_' . $_ } qw/ungetc rewind clearerr/ ),
'fgets' => 'sv_gets',
'malloc' => 'Newx',
'calloc' => 'Newxz',
'realloc' => 'Renew',
'memcpy' => 'Copy',
'memmove' => 'Move',
'memset' => 'Zero',
'free' => 'Safefree',
'strdup' => 'savepv',
'strstr' => 'instr',
'strcmp' => 'strEQ',
'strncmp' => 'strnEQ',
'strlen' => 'sv_len',
'strcpy' => 'sv_setpv',
'strncpy' => 'sv_setpvn',
'strcat' => 'sv_catpv',
'strncat' => 'sv_catpvn',
'sprintf' => 'sv_setpvf',
map( { my $u = uc; "is$_" => "is$u" } qw/alnum alpha cntrl digit graph lower print punct space upper xdigit/),
map( { my $u = uc; "to$_" => "to$u" } qw/lower upper/),
map( { $_ => ucfirst } qw/atof atol strtol strtoul/),
'strtod' => 'croak', # Dummy
'rand' => 'Drand01',
'srand' => 'seedDrand01',
'exit' => 'my_exit',
'system' => 'croak', # Dummy
'getenv' => 'PerlEnv_getenv',
'setenv' => 'my_putenv'
);
%superseded = map { $_ => 1 } keys %clib;
{
my $intrpvar = perlfile('intrpvar.h', 1);
while (<$intrpvar>) {
if (/^\s*PERLVARI?\s*\(\s*I?(\w+)/) {
++$variables{"PL_$1"}
}
}
}
{
my $pp_proto = perlfile('pp_proto.h', 1);
while (<$pp_proto>) {
if (/^\s*PERL_(?:CK|PP)DEF\s*\((\w+)\)/) {
next if $skip_functions{$1};
++$functions{$1};
}
}
}
{
my $embed = perlfile('embed.fnc', 1);
while (<$embed>) {
next if /^[\s:#]/;
(my $flags, my $name) = /^(\w+)\s*\|.*?\|\s*([^\|\s]+)/;
next unless $flags and $name;
next if $skip_functions{$name};
if ($flags =~ /A/ and $flags !~ /D/) {
if ($flags =~ /m/) {
++$macros{$name} unless $flags =~ /o/;
} else {
++$functions{$name} unless $flags =~ /o/;
++$functions{"Perl_$name"} if $flags =~ /p/;
}
} else {
if ($flags =~ /m/) {
++$private{$name};
} elsif ($flags =~ /[sED]/) {
++$private{$name} unless $flags =~ /o/;
++$private{"Perl_$name"} if $flags =~ /p/;
++$private{"S_$name"} if $flags =~ /s/;
}
}
}
}
my %skip_header = (
'embed.h' => 1,
'pp_proto.h' => 1,
'intrpvar.h' => 1,
);
for my $header (glob perlfile('*.h')) {
next if $skip_header{ (File::Spec->splitpath($header))[2] };
open my $header_fh, '<', $header;
my ($comment, $enum);
while (<$header_fh>) {
s[/\*.*\*/][];
if (s[/\*.*][]) {
$comment = 1;
# Process the beginning of the line
} elsif ($comment) {
$comment = not s[.*\*/][];
next if $comment;
}
if ($enum || s/^\s*typedef\s*enum\s*\w*\s*\{//) {
$enum = !/\}\s*(\w*)\s*;/;
++$types{$1} if $1;
++$constants{$_} for map { /^\s*(\w+)/ ? $1 : () } split /,/, $_;
next;
}
if (/^\s*\#\s*undef\s*(\w+)/) {
delete $macros{$1};
} elsif (my ($macro, $value) = /^\s*\#\s*define\s*(\w+)\s*(\S*)/) {
maybe_macro($macro, $value);
} elsif ( /^\s*typedef.*?(\w+)\s*;/
or /^\s*(?:struct|enum|union)\s+(\w+)\s+[\{;]/) {
maybe_type($1);
} elsif (/\b(?:extern|EXT(?:|ERN(?:_C)?|CONST))\b.*?\b(PL_\w+)\b/) {
++$variables{$1};
}
}
}
{
my $toke = perlfile('toke.c', 1);
while (<$toke>) {
if (/^\s*#\s*define\s*(PL_\w+)/) {
++$variables{$1};
}
}
}
print STDERR "Found " . (keys %functions) . " functions, "
. (keys %macros) . " macros, "
. (keys %variables) . " variables, "
. (keys %constants) . " constants and "
. (keys %types) . " types\n";
my $len = 78;
sub output {
my ($data, $type, $fh) = @_;
$fh = *STDOUT unless $fh;
my $head = "syn keyword xs$type";
my $line = $head;
for (sort keys %$data) {
if (length() + length($line) + 1 >= $len) {
print $fh "$line\n";
$line = $head;
}
$line .= " $_";
}
print $fh "$line\n" if $line;
}
my $vim = \*STDOUT;
my ( undef, undef, undef, $day, $month, $year ) = gmtime();
$year += 1900;
$month++;
my $date = sprintf '%04d-%02d-%02d', $year, $month, $day;
print $vim <<_VIM_;
" Vim syntax file
" Language: XS (Perl extension interface language)
" Author: Autogenerated from perl headers, on an original basis of Michael W. Dodge <sarge\@pobox.com>
" Maintainer: vim-perl <vim-perl\@googlegroups.com>
" Previous: Vincent Pit <perl\@profvince.com>
" Last Change: $date
" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
" Read the C syntax to start with
if version < 600
source <sfile>:p:h/c.vim
else
runtime! syntax/c.vim
endif
" Configuration:
" let xs_superseded = 0 " mark C functions superseded by Perl replacements (ex. memcpy vs Copy)
" let xs_not_core = 0 " mark private core functions
_VIM_
print $vim "if get(g:, 'xs_superseded', 0)\n";
output \%superseded, 'Superseded' => $vim;
print $vim "endif\n";
print $vim "if get(g:, 'xs_not_core', 0)\n";
output \%private, 'Private' => $vim;
print $vim "endif\n";
output \%types, 'Type' => $vim;
output \%strings, 'String' => $vim;
output \%constants, 'Constant' => $vim;
output \%exceptions, 'Exception' => $vim;
output \%keywords, 'Keyword' => $vim;
output \%functions, 'Function' => $vim;
output \%variables, 'Variable' => $vim;
output \%macros, 'Macro' => $vim;
print $vim <<'_VIM_';
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_xs_syntax_inits")
if version < 508
let did_xs_syntax_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink xsPrivate Error
HiLink xsSuperseded Error
HiLink xsType Type
HiLink xsString String
HiLink xsConstant Constant
HiLink xsException Exception
HiLink xsKeyword Keyword
HiLink xsFunction Function
HiLink xsVariable Identifier
HiLink xsMacro Macro
delcommand HiLink
endif
let b:current_syntax = "xs"
" vim: ts=8
_VIM_