288 Commits

Author SHA1 Message Date
Paul "LeoNerd" Evans
da6c78e212 Allow multivariable foreach to use refaliases
As noted by https://github.com/Perl/perl5/issues/24027, while multivar
foreach and refalias foreach have each been available separately for
some time now, it wasn't possible to combine the two until now.

This current implementation does have limits on the number of variables
in total are allowed in a multivariable foreach loop if any are
refaliases (no more than 256), and which ones can be refaliases (none
past the 24th) that are all to do with the way I am abusing the U32
`op_targ` field to store the variable count in the lower 8 bits, and a
bitmask of which vars are refaliases in the upper 24 bits. I decided to
do that as it means I don't have to make the `OP_ITER` op any larger -
such as by expanding it to an UNOP_AUX to give it that extra storage
space. In practice I don't imagine those limits will be a problem for
any practical use-case.

If in a future version we did want to expand on those limits, I think it
would be possible by moving the `refalias_mask` storage to a PV stored
in the UNOP_AUX vector, or somesuch.
2026-01-22 00:07:41 +00:00
Paul "LeoNerd" Evans
e53cc76fc6 Rename perly.y's "list_of_scalars" to "list_of_itervars"
Currently these are all scalar variables, but soon I will add the
ability for iteration variables to be refaliases, so they won't
necessarily all be scalars any more.
2026-01-22 00:07:41 +00:00
Paul "LeoNerd" Evans
980fade0ba Rename THING token when used for attributes
Prior to this, toke.c and perly.y used the token name `THING` for a
number of apparently totally unrelated reasons. While I haven't
attempted to fix most of them, I did find that the one used for
attribute lists is unrelated to the others, so I have renamed that to
something more sensible and meaningful.
2025-12-04 10:22:54 +00:00
Paul "LeoNerd" Evans
2b89da05e5 Don't treat class attributes as sub attributes in perly.y
We need to use a dedicated grammar type; or at the very least, not use
`subattrlist` for doing this. The previous use of `subattrlist` had the
side-effect of passing every attribute list for a class via the
`apply_builtin_cv_attributes()` function. That was pointlessly
redundant, though currently harmless. It may stop being harmless in a
future improvement to the way attributes are handled, so best not to do
that any more.

The new structure repurposes the previous `myattrlist` to be a
non-optional attribute list, and defines `optattrlist` to use for both
class and field attributes.
2025-11-16 23:59:51 +00:00
Paul "LeoNerd" Evans
f1a8d7d883 Implement named parameters in signatures (PPC0024)
This adds a major new ability to subroutine signatures, allowing callers
to pass parameters by name/value pairs rather than by position.

  sub f ($x, $y, :$alpha, :$beta = undef) { ... }

  f( 123, 456, alpha => 789 );

Originally specified in

  https://github.com/Perl/PPCs/blob/main/ppcs/ppc0024-signature-named-parameters.md

This feature is currently considered experimental.
2025-10-31 11:31:29 +00:00
Branislav Zahradník
147d5f1b9e [parser] new_block_statement - deduplicate "a block is a loop that happens once" 2025-10-22 17:23:56 +01:00
Branislav Zahradník
e6a443b294 [parser] package - deduplicate coupled call sequence
Function combines call of original `package` and `package_version` when
new namespace statement is detected.

Instead of required three statements usage now consists of single function call.
2025-10-22 17:23:56 +01:00
Branislav Zahradník
412b9d48fc [parser] Decompose barestmt -> bare_statement_yadayada 2025-10-07 15:23:22 +02:00
Branislav Zahradník
37f6bc88dc [parser] Decompose barestmt -> bare_statement_while 2025-10-07 15:23:22 +02:00
Branislav Zahradník
4810f9b72f [parser] Decompose barestmt -> bare_statement_when 2025-10-07 15:23:22 +02:00
Branislav Zahradník
192a22b2ce [parser] Decompose barestmt -> bare_statement_utilize 2025-10-07 15:23:22 +02:00
Branislav Zahradník
703db877f5 [parser] Decompose barestmt -> bare_statement_until 2025-10-07 15:23:22 +02:00
Branislav Zahradník
7708014280 [parser] Decompose barestmt -> bare_statement_unless 2025-10-07 15:23:22 +02:00
Branislav Zahradník
aa96f7534c [parser] Decompose barestmt -> bare_statement_try_catch 2025-10-07 15:23:22 +02:00
Branislav Zahradník
42969c5a35 [parser] Decompose barestmt -> bare_statement_sub_signature 2025-10-07 15:23:22 +02:00
Branislav Zahradník
440ad229a3 [parser] Decompose barestmt -> bare_statement_sub_traditional 2025-10-07 15:23:22 +02:00
Branislav Zahradník
229019db60 [parser] Decompose barestmt -> bare_statement_phaser 2025-10-07 15:23:22 +02:00
Branislav Zahradník
062bd6602e [parser] Decompose barestmt -> bare_statement_package_definition 2025-10-07 15:23:22 +02:00
Branislav Zahradník
f020aaeb57 [parser] Decompose barestmt -> bare_statement_package_declaration 2025-10-07 15:23:22 +02:00
Branislav Zahradník
8eb216ec1a [parser] Decompose barestmt -> bare_statement_null 2025-10-07 15:23:22 +02:00
Branislav Zahradník
28b5a7fa6a [parser] Decompose barestmt -> `bare_statement_if 2025-10-07 15:23:22 +02:00
Branislav Zahradník
bb69c94bb6 [parser] Decompose barestmt -> bare_statement_given 2025-10-07 15:23:22 +02:00
Branislav Zahradník
12c56bb182 [parser] Decompose barestmt -> bare_statement_format 2025-10-07 15:23:22 +02:00
Branislav Zahradník
2dc1c48245 [parser] Decompose barestmt -> bare_statement_for 2025-10-07 15:23:22 +02:00
Branislav Zahradník
d666e956c8 [parser] Decompose barestmt -> bare_statement_field_declaration 2025-10-07 15:23:22 +02:00
Branislav Zahradník
6111a9cae2 [parser] Decompose barestmt -> bare_statement_expression 2025-10-07 15:23:22 +02:00
Branislav Zahradník
c891c9a55b [parser] Decompose barestmt -> bare_statement_defer 2025-10-07 15:23:22 +02:00
Branislav Zahradník
58d94eda11 [parser] Decompose barestmt -> bare_statement_default 2025-10-07 15:23:22 +02:00
Branislav Zahradník
168ac44ddc [parser] Decompose barestmt -> bare_statement_class_definition 2025-10-07 15:23:22 +02:00
Branislav Zahradník
234ef9e87d [parser] Decompose barestmt -> bare_statement_class_declaration 2025-10-07 15:23:22 +02:00
Branislav Zahradník
825f28de1e [parser] Decompose barestmt -> bare_statement_block 2025-10-07 15:23:22 +02:00
Paul "LeoNerd" Evans
1f5204da4e Ensure that module files ending with an ADJUST phaser don't upset feature 'module_true'
Fixes https://github.com/Perl/perl5/issues/23758
2025-09-24 11:35:42 +01:00
Paul "LeoNerd" Evans
e25a4696dd perly.y: Reduce repetition of signature scalar element grammar
By defining a helper term for optional defaulting expression we can
remove one of the three alternate cases down to just two. This will help
when adding more code to these action blocks in future,
2025-08-25 14:06:05 +01:00
Paul "LeoNerd" Evans
0aba7db885 Pass PADOFFSET integers in to subsignature_append_*() functions
This simplifies potential callsites from other locations, avoiding their
need to create the special weird OP_ARGELEM fragments. Additionally this
decouples the knowledge that these operations are run on OP_ARGELEM ops
in the first place, allowing for a future version that operates
differently.
2025-08-12 12:54:46 +01:00
Lukas Mai
0e87955cfc move "illegal anon sub" error from toke.c to perly.y
This fixes the TODO test in t/op/anonsub.t, but breaks two other tests:

- t/op/anonsub.t

        not ok 12 - '{ $x = sub }' is illegal
        # Failed test 12 - '{ $x = sub }' is illegal at t/op/anonsub.t line 15
        #      got 'Global symbol \"$x\" requires explicit package name (did you forget to declare \"my $x\"?) at (eval 12) line 1, <DATA> line 87.\nIllegal declaration of anonymous subroutine at (eval 12) line 1, at EOF\n'
        # expected /(?^:^Illegal declaration of anonymous subroutine at)/

- t/lib/croak.t

        FILE: lib/croak/toke ; line 644
        PROG:
        no feature 'apostrophe_as_package_separator';
        sub 'Hello'_he_said (_);
        EXPECTED:
        Illegal declaration of anonymous subroutine at - line 2.
        EXIT STATUS: != 0
        GOT:
        Bareword found where operator expected (Missing operator before "_he_said"?) at - line 2, near "'Hello'_he_said"
        Illegal declaration of anonymous subroutine at - line 2, near "sub 'Hello'"
        Execution of - aborted due to compilation errors.
        EXIT STATUS: 255
        not ok 247 - tick in names: initial character of sub name (no feature)
        # Failed test 247 - tick in names: initial character of sub name (no feature) at lib/croak/toke line 644

Since we don't immediately error out from the lexer anymore, the parser
may accumulate more errors before failing with "Illegal declaration of
anonymous subroutine".

The tests have been changed to accomodate the new diagnostics.

Fixes #5959.
2025-08-04 09:55:34 +02:00
Lukas Mai
9e492c255d perly.y, op.c: PL_in_my is an enum (kind of), not a bool 2025-07-29 00:21:25 +02:00
Leon Timmermans
06c3a62f12 Dont call Perl_croak manually in core
Just call croak instead, we've been able to do that for vararg functions
since d933027ef0a56c99aee8cc3c88ff4f9981ac9fc2
2025-03-18 04:16:51 +01:00
Tony Cook
cd80cda9a1 Revert "switch removal: remove given/when/break/continue"
This reverts commit dff5181acc0f5386556522c0ae4e59f672162f6e.
2025-02-16 14:51:17 +01:00
Paul "LeoNerd" Evans
548c2bc900 Support lexical method invocation syntax of ->& (PPC0033) 2025-02-12 10:38:05 +00:00
Paul "LeoNerd" Evans
8d1931e529 Move all the signature param parsing logic out of perly.y into a helper API
Provide a subsignature_*() API

Added:
 * subsignature_start()
 * subsignature_append_slurpy()
 * subsignature_append_positional()
 * subsignature_finish()

Call these from code blocks in perly.y

Make the actual parser signature struct opaque, hidden in toke.c. This
gives it much more robustness against future modifications.
2025-02-07 13:22:55 +00:00
Paul "LeoNerd" Evans
6f04944602 Implement PPC0027: Create all and any operators
As these only permit `any BLOCK LIST` and not the deferred-expression
form like `grep EXPR, LIST`, we create a whole new token type, BLKLSTOP,
to represent these. The parser then only accepts the block form and not
the expression form when parsing these.
2024-12-04 16:28:06 +00:00
Lukas Mai
6169482089 regen_perly.pl: remove mostly dead gather_tokens code
The idea behind this code was to find the big enum block in perlytmp.h
that looks like:

    enum yytokentype {
      YYEMPTY = -2,
      YYEOF = 0,                     /* "end of file"  */
      YYerror = 256,                 /* error  */
      YYUNDEF = 257,                 /* "invalid token"  */
      GRAMPROG = 258,                /* GRAMPROG  */
      GRAMEXPR = 259,                /* GRAMEXPR  */
      GRAMBLOCK = 260,               /* GRAMBLOCK  */
      ...
    };

... and append to it (in perly.h) a series of equivalent macro
definitions, one for each (non-negative) enum symbol:

    #define YYEOF 0
    #define YYerror 256
    #define YYUNDEF 257
    #define GRAMPROG 258
    #define GRAMEXPR 259
    #define GRAMBLOCK 260
    ...

However, due to slight formatting changes in the code generated by bison
3+, this code has been essentially dead because the starting regex
didn't match anymore, so $gather_tokens was never set to a true value.

The last time we had token macros in perly.h was before commit
a9f5ab8de628 (in 2016), in which I generated perly.h using bison 3 for
the first time (without noticing the disappearance of the token macros).

We could try to fix the regex logic in regen_perly.pl, but given that no
one has complained about the missing token macros in 8 years (as far as
I know), let's just remove the code. (Yay, accidental scream tests?)
2024-09-07 05:36:35 +02:00
James E Keenan
bdafeebb08 Permit use of bison 3.8.* in regen_perly.pl
Then run that program to regenerate perly.[act|h|tab].
2024-09-02 20:40:30 -04:00
Tony Cook
dff5181acc switch removal: remove given/when/break/continue
continue blocks are not removed
2024-08-15 10:56:18 +10:00
Tony Cook
0d9e3bc6db perly LSTOPSUB: prevent a double op free between parse stack and CV
The reeproducer resulted in the "block" OP being both on the parser
stack and attacked to the CV.  If an error occurred while parsing the
rest of the list operator clean up would release the OP as attached
to the CV, and the same OP on the parse stack, resulting in a double
free.

It's unclear to me whether bison is intended to support modifying
the parse stack entry like this, but it appears to work here.

Fixes #21724
2024-05-02 16:28:18 +01:00
Martijn Lievaart
d6e802ac91 Create a new logical xor operator, spelled ^^
Previously, the low-precedence `xor` operator did not have a
higher-precedence logical variant, as compared `or` vs `||` and
`and` vs `&&`. This PR adds such an operator syntax, completing the
set.
2024-02-29 01:35:59 +00:00
Paul "LeoNerd" Evans
03dbf5c297 Permit attributed variables inside signatured subs (Fixes GH #21158) 2023-08-16 18:07:57 -04:00
Lukas Mai
b7eedb0895 fix incorrect vi filetype declarations in generated files
Vim's filetype declarations are case sensitive. The correct types for
Perl, C, and Pod are perl, c, and pod, respectively.
2023-03-24 06:10:05 +08:00
Paul "LeoNerd" Evans
7a993362d9 Have start_subparse() call class_prepare_method_parse() if CVf_IsMETHOD 2023-03-06 10:41:20 +00:00
Paul "LeoNerd" Evans
75ea41ae51 Have newATTRSUB() call class_wrap_method_body() if CVf_IsMETHOD 2023-03-06 10:41:20 +00:00