Commit graph

2691 commits

Author SHA1 Message Date
Nobuyoshi Nakada
978c31f04a
ripper: Splat array patterns with pre_arg 2024-05-21 13:52:30 +09:00
Nobuyoshi Nakada
3e81bc3d53
ripper: Splat $:opt_args_tail for params! 2024-05-21 13:52:30 +09:00
Nobuyoshi Nakada
be0f2ab32d
ripper: Splat $:head for defs! 2024-05-21 13:52:30 +09:00
Nobuyoshi Nakada
5bba5fb739
ripper: Describe var_ref for user_variable in ripper DSL 2024-05-21 13:52:30 +09:00
Nobuyoshi Nakada
56d2c26c85
ripper: Move assign_error call to assignable
Prepare `lhs` as `$:$` before `assignable` and update it there.
Remove `ripper_assignable` which is no longer used.
2024-05-21 13:52:30 +09:00
Nobuyoshi Nakada
e61e5c3b84
ripper: Move assign_error call to const_decl
Prepare `path` as `$:$` before `const_decl` and update it there.
Remove `ripper_const_decl` which is no longer used.
2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
147134b474
ripper: Remove rb_ripper_none
Now it is used only for wheter `opt_paren_args` is `none`.  Introduce
a new special node to distinguish an empty parentheses from it .
2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
ee8bbbabe5
ripper: Show popped TOS in debug mode 2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
2e765c20db
ripper: Short hand for rb_ary_new_from_args 2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
2d92a4afba
ripper: Make $:n to refer each grammar values
Ripper DSL uses these values for callbacks, but does not need indexes.
2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
5fed63f7b0
ripper: Use ripper DSL in simple dispatch chain cases 2024-05-21 13:52:29 +09:00
Nobuyoshi Nakada
dbbaf871de
[DOC] Fix $< comment 2024-05-19 00:29:00 +09:00
Nobuyoshi Nakada
fd8e6e8c54
Replace cast tags for tSTRING_DVAR with typed midrule actions 2024-05-19 00:27:34 +09:00
Nobuyoshi Nakada
232f7b37cf
Replace cast tags with typed midrule actions
* Add types to `tLAMBDA` and `tSTRING_DBEG` to store corresponding
  information when returning these tokens.
* Add `enum lex_state_e state` to `%union` for `tSTRING_DBEG`.
2024-05-18 19:46:05 +09:00
yui-knk
55c62e676f No need to specify tags anymore
In the past, these codes were used by both parser and ripper.
On ripper, the type of LHS is `<val>` then type cast was needed.
However currently these are only used by parser then no need to
cast.
2024-05-18 11:26:17 +09:00
Nobuyoshi Nakada
5695c5df95
ripper: Fix opassign when assignment to backref variables 2024-05-12 15:38:22 +09:00
yui-knk
7e604a0263 Fix SEGV when ripper hits backref_error on command_asgn or arg 2024-05-11 20:47:15 +09:00
Nobuyoshi Nakada
5bb656e4f0
[Bug #20474] Keep spaces in leading blank line 2024-05-08 19:25:37 +09:00
yui-knk
cf74ff714a Change return value of gets function to be rb_parser_string_t * instead of VALUE
This change reduces parser's dependency on ruby object.
2024-05-04 11:59:10 +09:00
ydah
e1905ca180 Use user defined parameterizing rules f_optarg(value) 2024-05-03 12:05:21 +09:00
ydah
ed5a7a59c0 Use callee side tag specification of parameterizing rules 2024-05-02 15:04:20 +09:00
Peter Zhu
7ef8bb129f Fix memory leak in Ripper.sexp
rb_ast_dispose does not free the rb_ast_t causing it to be leaked. This
commit changes it to use rb_ast_free instead.

For example:

    require "ripper"

    10.times do
      100_000.times do
        Ripper.sexp("")
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    27648
    32512
    37376
    42240
    47232
    52224
    57344
    62208
    67072
    71936

After:

    22784
    22784
    22784
    22784
    22912
    22912
    22912
    22912
    22912
    22912
2024-05-01 11:09:54 -04:00
ydah
acc76c8c91 Use user defined parameterizing rules f_opt(value) 2024-05-01 15:53:08 +09:00
ydah
c28329817d Use user defined parameterizing rules f_kwarg(kw) 2024-05-01 14:50:10 +09:00
ydah
9f7e0d2bb4 Use user defined parameterizing rules opt_args_tail(tail) 2024-05-01 13:53:37 +09:00
yui-knk
528c4501f4 Use rb_parser_string_t * as ruby_sourcefile_string
This reduces dependency on VALUE.
2024-04-30 09:00:05 +09:00
ydah
f9cf923af2 Use user defined parameterizing rules 2024-04-29 08:38:23 +09:00
ydah
8ad0b2cd31 Use ' '+ instead of words_sep 2024-04-28 09:01:49 +09:00
HASUMI Hitoshi
55a402bb75 Add line_count field to rb_ast_body_t
This patch adds `int line_count` field to `rb_ast_body_t` structure.
Instead, we no longer cast `script_lines` to Fixnum.

## Background

Ref https://github.com/ruby/ruby/pull/10618

In the PR above, we have decoupled IMEMO from `rb_ast_t`.
This means we could lift the five-words-restriction of the structure
that forced us to unionize `rb_ast_t *` and `FIXNUM` in one field.

## Relating refactor

- Remove the second parameter of `rb_ruby_ast_new()` function

## Attention

I will remove a code that assigns -1 to line_count, in `rb_binding_add_dynavars()`
of vm.c, because I don't think it is necessary.
But I will make another PR for this so that we can atomically revert
in case I was wrong (See the comment on the code)
2024-04-27 12:08:26 +09:00
yui-knk
140c59c633 Set SCRIPT_LINES__ outside of parser
Parser should not depend on functions defiend on "ruby_parser.c".
2024-04-26 20:34:49 +09:00
HASUMI Hitoshi
2244c58b00 [Universal parser] Decouple IMEMO from rb_ast_t
This patch removes the `VALUE flags` member from the `rb_ast_t` structure making `rb_ast_t` no longer an IMEMO object.

## Background

We are trying to make the Ruby parser generated from parse.y a universal parser that can be used by other implementations such as mruby.
To achieve this, it is necessary to exclude VALUE and IMEMO from parse.y, AST, and NODE.

## Summary (file by file)

- `rubyparser.h`
  - Remove the `VALUE flags` member from `rb_ast_t`
- `ruby_parser.c` and `internal/ruby_parser.h`
  - Use TypedData_Make_Struct VALUE which wraps `rb_ast_t` `in ast_alloc()` so that GC can manage it
    - You can retrieve `rb_ast_t` from the VALUE by `rb_ruby_ast_data_get()`
  - Change the return type of `rb_parser_compile_XXXX()` functions from `rb_ast_t *` to `VALUE`
  - rb_ruby_ast_new() which internally `calls ast_alloc()` is to create VALUE vast outside ruby_parser.c
- `iseq.c` and `vm_core.h`
  - Amend the first parameter of `rb_iseq_new_XXXX()` functions from `rb_ast_body_t *` to `VALUE`
  - This keeps the VALUE of AST on the machine stack to prevent being removed by GC
- `ast.c`
  - Almost all change is replacement `rb_ast_t *ast` with `VALUE vast` (sorry for the big diff)
  - Fix `node_memsize()`
    - Now it includes `rb_ast_local_table_link`, `tokens` and script_lines
- `compile.c`, `load.c`, `node.c`, `parse.y`, `proc.c`, `ruby.c`, `template/prelude.c.tmpl`, `vm.c` and `vm_eval.c`
  - Follow-up due to the above changes
- `imemo.{c|h}`
  - If an object with `imemo_ast` appears, considers it a bug

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-04-26 11:21:08 +09:00
卜部昌平
64bd8e41df false is not a pointer
This function returned VALUE before.  False made sense back then.
Now that it returns a pointer.  NULL should be used instead.
2024-04-25 10:12:47 +02:00
yui-knk
33929ef995 Move encoding object conversion outside of parser
Reduce the parser's dependence on `VALUE` and `rb_enc_from_encoding`.
2024-04-23 13:11:46 +09:00
yui-knk
2992e1074a Refactor parser compile functions
Refactor parser compile functions to reduce the dependence
on ruby functions.
This commit includes these changes

1. Refactor `gets`, `input` and `gets_` of `parser_params`

Parser needs two different data structure to get next line, function (`gets`) and input data (`input`).
However `gets_` is used for both function (`call`) and input data (`ptr`).
`call` is used for managing general callback function when `rb_ruby_parser_compile_generic` is used.
`ptr` is used for managing the current pointer on String when `parser_compile_string` is used.
This commit changes parser to used only `gets` and `input` then removes `gets_`.

2. Move parser_compile functions and `gets` functions from parse.y to ruby_parser.c

This change reduces the dependence on ruby functions from parser.

3. Change ruby_parser and ripper to take care of `VALUE input` GC mark

Move the responsibility of calling `rb_gc_mark` for `VALUE input` from parser to ruby_parser and ripper.
`input` is arbitrary data pointer from the viewpoint of parser.

4. Introduce rb_parser_compile_array function

Caller of `rb_parser_compile_generic` needs to take care about GC because ruby_parser doesn’t know
about the detail of `lex_gets` and `input`.
Introduce `rb_parser_compile_array` to reduce the complexity of ast.c.
2024-04-23 07:20:22 +09:00
yui-knk
f77618c1fa Expand Qnone and Qnull macros
In the past, `Qnone` and `Qnull` had different values
in ripper context like below.
However 89cfc15207 removes the usage in ripper context,
then expand the macro.

```
#ifndef RIPPER
# define Qnone 0
# define Qnull 0
#else
# define Qnone Qnil
# define Qnull Qundef
#endif
```
2024-04-22 15:32:21 +09:00
yui-knk
d07df8567e Parser and universal parser share wrapper functions 2024-04-20 18:08:33 +09:00
HASUMI Hitoshi
9b1e97b211 [Universal parser] DeVALUE of p->debug_lines and ast->body.script_lines
This patch is part of universal parser work.

## Summary
- Decouple VALUE from members below:
  - `(struct parser_params *)->debug_lines`
  - `(rb_ast_t *)->body.script_lines`
- Instead, they are now `rb_parser_ary_t *`
  - They can also be a `(VALUE)FIXNUM` as before to hold line count
- `ISEQ_BODY(iseq)->variable.script_lines` remains VALUE
  - In order to do this,
  - Add `VALUE script_lines` param to `rb_iseq_new_with_opt()`
  - Introduce `rb_parser_build_script_lines_from()` to convert `rb_parser_ary_t *` into `VALUE`

## Other details
- Extend `rb_parser_ary_t *`. It previously could only store `rb_parser_ast_token *`, now can store script_lines, too
- Change tactics of building the top-level `SCRIPT_LINES__` in `yycompile0()`
  - Before: While parsing, each line of the script is added to `SCRIPT_LINES__[path]`
  - After: After `yyparse(p)`, `SCRIPT_LINES__[path]` will be built from `p->debug_lines`
- Remove the second parameter of `rb_parser_set_script_lines()` to make it simple
- Introduce `script_lines_free()` to be called from `rb_ast_free()` because the GC no longer takes care of the script_lines
- Introduce `rb_parser_string_deep_copy()` in parse.y to maintain script_lines when `rb_ruby_parser_free()` called
  - With regard to this, please see *Future tasks* below

## Future tasks
- Decouple IMEMO from `rb_ast_t *`
  - This lifts the five-members-restriction of Ruby object,
  - So we will be able to move the ownership of the `lex.string_buffer` from parser to AST
  - Then we remove `rb_parser_string_deep_copy()` to make the whole thing simple
2024-04-15 20:51:54 +09:00
yui-knk
515e52a0b1 Emit warn event for duplicated hash keys on ripper
Need to use `rb_warn` macro instead of calling `rb_compile_warn`
directly to emit `warn` event on ripper.
2024-04-15 06:29:25 +09:00
yui-knk
5970386a2e Use rb_parser_string_t * for delayed.token 2024-04-14 09:08:19 +09:00
Nobuyoshi Nakada
e36988450e
[Bug #20423] Disallow anonymous block within argument forwarding 2024-04-12 16:07:49 +09:00
Jean Boussier
1b830740ba compile.c: use rb_enc_interned_str to reduce allocations
The `rb_fstring(rb_enc_str_new())` pattern is inneficient because:

- It passes a mutable string to `rb_fstring` so if it has to be interned
  it will first be duped.
- It an equivalent interned string already exists, we allocated the string
  for nothing.

With `rb_enc_interned_str` we either directly get the pre-existing string
with 0 allocations, or efficiently directly intern the one we create
without first duping it.
2024-04-11 09:04:31 +02:00
Nobuyoshi Nakada
207788466e
[Bug #20417] Block local variables do not need to warn about unused 2024-04-11 12:03:55 +09:00
S-H-GAMELINKS
76732b3e7b Remove unused AREF macro 2024-04-11 11:32:09 +09:00
yui-knk
39be11a17a Fix segv when parsing command by ripper
89cfc15207 made this event dispatch to pass `Qundef`
to user defined callback method by mistake.
This commit fix it to be `nil`.
2024-04-11 10:28:58 +09:00
yui-knk
70a0dd4162 Remove unused macro 2024-04-07 11:15:09 +09:00
yui-knk
7767db2379 Fix ripper to dispatch warning event for duplicated when clause
Need to separate `check_literal_when` function for parser and
ripper otherwise warning event is not dispatched because
parser `rb_warning1` is used in ripper.
2024-04-07 11:15:09 +09:00
yui-knk
02f5e627a4 Remove redundant conversion between int and object 2024-04-06 22:03:26 +09:00
yui-knk
977672731f Fix a variable name
The first argument of `WARN_SPACE_CHAR` is always `c2` in caller
side, so `c` equals to `c2`.
2024-04-06 22:03:08 +09:00
yui-knk
9d1d2de2fe Make nd_plen to be int to reduce rb_long2int usage 2024-04-05 14:25:04 +09:00
yui-knk
bd728a625a Remove unused functions from struct rb_parser_config_struct 2024-04-05 10:04:48 +09:00