File: perl-test-sub-prototypes.pl

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (239 lines) | stat: -rw-r--r-- 8,865 bytes parent folder | download | duplicates (11)
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
# -*- coding: utf-8 -*-
#--------------------------------------------------------------------------
# perl-test-sub-prototypes.pl
#--------------------------------------------------------------------------
# compiled all relevant subroutine prototype test cases
#
#--------------------------------------------------------------------------
# Kein-Hong Man <keinhong@gmail.com> Public Domain
#--------------------------------------------------------------------------
# 20151227	initial document
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
# test cases for sub syntax scanner
#--------------------------------------------------------------------------
# sub syntax: simple and with added module notation
#--------------------------------------------------------------------------

sub fish($) { 123; }
sub fish::chips($) { 123; }			# module syntax
sub fish::chips::sauce($) { 123; }		# multiple module syntax

sub fish :: chips  ::		sauce ($) { 123; }	# added whitespace

sub fish :: # embedded comment
chips 	# embedded comment
 :: sauce ($) { 123; }

sub fish :: ($) { 123; }	# incomplete or bad syntax examples
sub fish :: 123 ($) { 123; }
sub fish :: chips 123 ($) { 123; }
sub 123 ($) { 123; }

#--------------------------------------------------------------------------
# sub syntax: prototype attributes
#--------------------------------------------------------------------------

sub fish:prototype($) { 123; }
sub fish : prototype	($) { 123; }		# added whitespace

sub fish:salted($) { 123; }	# wrong attribute example (must use 'prototype')
sub fish :  123($) { 123; }	# illegal attribute
sub fish:prototype:salted($) { 123; }	# wrong 'prototype' position
sub fish:salted salt:prototype($) { 123; }	# wrong attribute syntax

sub fish:const:prototype($) { 123; }		# extra attributes
sub fish:const:lvalue:prototype($) { 123; }
sub fish:const:prototype($):lvalue{ 123; }	# might be legal too
sub fish  :const	:prototype($) { 123; }	# extra whitespace

sub fish  :const	# embedded comment: a constant sub
:prototype		# embedded comment
($) { 123; }

#--------------------------------------------------------------------------
# sub syntax: mixed
#--------------------------------------------------------------------------

sub fish::chips:prototype($) { 123; }
sub fish::chips::sauce:prototype($) { 123; }
sub fish  ::chips  ::sauce	:prototype($) { 123; }	# +whitespace

sub fish::chips::sauce:const:prototype($) { 123; }
sub fish::chips::sauce	:const	:prototype($) { 123; }	# +whitespace

sub fish		# embedded comment
::chips	::sauce		# embedded comment
  : const		# embedded comment
	: prototype ($) { 123; }

# wrong syntax examples, parentheses must follow ':prototype'
sub fish :prototype :const ($) { 123;}
sub fish :prototype ::chips ($) { 123;}

#--------------------------------------------------------------------------
# perl-test-5200delta.pl
#--------------------------------------------------------------------------
# More consistent prototype parsing
#--------------------------------------------------------------------------
# - whitespace now allowed, lexer now allows spaces or tabs

sub foo ( $ $ ) {}
sub foo ( 			 ) {}		# spaces/tabs empty
sub foo (  *  ) {}
sub foo (@	) {}
sub foo (	%) {}

# untested, should probably be \[ but scanner does not check this for now
sub foo ( \ [ $ @ % & * ] ) {}

#--------------------------------------------------------------------------
# perl-test-5140delta.pl
#--------------------------------------------------------------------------
# new + prototype character, acts like (\[@%])
#--------------------------------------------------------------------------

# these samples work as before
sub mylink ($$)          # mylink $old, $new
sub myvec ($$$)          # myvec $var, $offset, 1
sub myindex ($$;$)       # myindex &getstring, "substr"
sub mysyswrite ($$$;$)   # mysyswrite $buf, 0, length($buf) - $off, $off
sub myreverse (@)        # myreverse $a, $b, $c
sub myjoin ($@)          # myjoin ":", $a, $b, $c
sub myopen (*;$)         # myopen HANDLE, $name
sub mypipe (**)          # mypipe READHANDLE, WRITEHANDLE
sub mygrep (&@)          # mygrep { /foo/ } $a, $b, $c
sub myrand (;$)          # myrand 42
sub mytime ()            # mytime

# backslash group notation to specify more than one allowed argument type
sub myref (\[$@%&*]) {}

sub mysub (_)            # underscore can be optionally used FIXED 20151211

# these uses the new '+' prototype character
sub mypop (+)            # mypop @array
sub mysplice (+$$@)      # mysplice @array, 0, 2, @pushme
sub mykeys (+)           # mykeys %{$hashref}

#--------------------------------------------------------------------------
# perl-test-5200delta.pl
#--------------------------------------------------------------------------
# Experimental Subroutine signatures (mostly works)
#--------------------------------------------------------------------------
# INCLUDED FOR COMPLETENESS ONLY
# IMPORTANT NOTE the subroutine prototypes lexing implementation has
# no effect on subroutine signature syntax highlighting

# subroutine signatures mostly looks fine except for the @ and % slurpy
# notation which are highlighted as operators (all other parameters are
# highlighted as vars of some sort), a minor aesthetic issue

use feature 'signatures';

sub foo ($left, $right) {		# mandatory positional parameters
    return $left + $right;
}
sub foo ($first, $, $third) {		# ignore second argument
    return "first=$first, third=$third";
}
sub foo ($left, $right = 0) {		# optional parameter with default value
    return $left + $right;
}
my $auto_id = 0;			# default value expression, evaluated if default used only
sub foo ($thing, $id = $auto_id++) {
    print "$thing has ID $id";
}
sub foo ($first_name, $surname, $nickname = $first_name) {	# 3rd parm may depend on 1st parm
    print "$first_name $surname is known as \"$nickname\"";
}
sub foo ($thing, $ = 1) {		# nameless default parameter
    print $thing;
}
sub foo ($thing, $=) {			# (this does something, I'm not sure what...)
    print $thing;
}
sub foo ($filter, @inputs) {		# additional arguments (slurpy parameter)
    print $filter->($_) foreach @inputs;
}
sub foo ($thing, @) {			# nameless slurpy parameter FAILS for now
    print $thing;
}
sub foo ($filter, %inputs) {		# slurpy parameter, hash type
    print $filter->($_, $inputs{$_}) foreach sort keys %inputs;
}
sub foo ($thing, %) {			# nameless slurpy parm, hash type FAILS for now
    print $thing;
}
sub foo () {				# empty signature no arguments (styled as prototype)
    return 123;
}

#--------------------------------------------------------------------------
# perl-test-5200delta.pl
#--------------------------------------------------------------------------
# subs now take a prototype attribute
#--------------------------------------------------------------------------

sub foo :prototype($) { $_[0] }

sub foo :prototype($$) ($left, $right) {
    return $left + $right;
}

sub foo : prototype($$){}		# whitespace allowed

# additional samples from perl-test-cases.pl with ':prototype' added:
sub mylink :prototype($$) {}		sub myvec :prototype($$$) {}
sub myindex :prototype($$;$) {}		sub mysyswrite :prototype($$$;$) {}
sub myreverse :prototype(@) {}		sub myjoin :prototype($@) {}
sub mypop :prototype(\@) {}		sub mysplice :prototype(\@$$@) {}
sub mykeys :prototype(\%) {}		sub myopen :prototype(*;$) {}
sub mypipe :prototype(**) {}		sub mygrep :prototype(&@) {}
sub myrand :prototype($) {}		sub mytime :prototype() {}
# backslash group notation to specify more than one allowed argument type
sub myref :prototype(\[$@%&*]) {}

# additional attributes may complicate scanning for prototype syntax,
# for example (from https://metacpan.org/pod/perlsub):
# Lvalue subroutines

my $val;
sub canmod : lvalue {
    $val;  # or:  return $val;
}
canmod() = 5;   # assigns to $val

#--------------------------------------------------------------------------
# perl-test-5220delta.pl
#--------------------------------------------------------------------------
# New :const subroutine attribute
#--------------------------------------------------------------------------

my $x = 54321;
*INLINED = sub : const { $x };
$x++;

# more examples of attributes
# (not 5.22 stuff, but some general examples for study, useful for
#  handling subroutine signature and subroutine prototype highlighting)

sub foo : lvalue ;

package X;
sub Y::z : lvalue { 1 }

package X;
sub foo { 1 }
package Y;
BEGIN { *bar = \&X::foo; }
package Z;
sub Y::bar : lvalue ;

# built-in attributes for subroutines:
lvalue method prototype(..) locked const

#--------------------------------------------------------------------------
# end of test file
#--------------------------------------------------------------------------