Commit graph

95 commits

Author SHA1 Message Date
Jean Boussier
025832c385 [ruby/prism] Use a locale-insensitive version of tolower
[Bug #21161]

The `tolower` function provided by the libc is locale dependent
and can behave in ways you wouldn't expect for some value
of `LC_CTYPE`.

e3488256b4

Co-Authored-By: Nobuyoshi Nakada <nobu@ruby-lang.org>
2025-02-28 00:28:24 +00:00
HASUMI Hitoshi
31162bf426 [ruby/prism] Handle zero-sized allocation in pm_constant_id_list_init_capacity
According to the calloc(3) man page, when nmemb or size is 0, `calloc()` can either return NULL or a unique pointer that can be passed to `free()`.
While gcc and clang typically return a unique pointer, mruby's `mrb_calloc()` returns NULL in this case.

Since `pm_constant_pool_init()` is commonly called with capacity=0 during normal operation of Prism, explicitly handle this case by setting `list->ids` to NULL when capacity is 0.
This approach is portable across different calloc implementations and avoids potential issues with mruby's allocation behavior.

This maintains compatibility with `free()` and `realloc()`, as passing NULL pointers to these functions is explicitly allowed by their specifications.

1c32252df7
2025-02-13 20:12:43 +00:00
Kevin Newton
c4534c9fe8 [ruby/prism] Handle escapes in named capture names
b4b7a69ce7
2025-01-08 20:36:06 +00:00
Yusuke Endoh
29e90a598a [ruby/prism] Fix a possible leak of a file descriptor
When mmap fails for any reason, the fd must be closed.

Coverity Scan found this issue.

c06e9c400f
2024-11-28 02:15:25 +00:00
Aaron Patterson
3c9be02af1 [ruby/prism] Only read from buffer if size is greater than 0
It looks like we can possibly do an out of bounds read if size is equal
to 0.  This commit adds a conditional to ensure size is actually greater
than 0 before looking backwards in the buffer

2031b626e6
2024-10-28 20:38:20 +00:00
Kevin Newton
ddbd644001 [ruby/prism] Stat file first to check directory
4ed7de537b
2024-09-13 19:30:57 +00:00
Kevin Newton
38ba15beed [ruby/prism] Check errno for parsing directory
d68ea29d04
2024-09-12 13:43:04 -04:00
Kevin Newton
d4d6f1de83 [ruby/prism] UTF-8 characters in file name
487f0ffe78
2024-09-11 19:17:12 +00:00
Kevin Newton
886fc69b1c [ruby/prism] Parse tempfile
31154a389a
2024-09-11 15:39:22 +00:00
Alexander Momchilov
2b0b68fa46 Shrink pm_integer_t (32 bytes → 24) 2024-08-26 12:38:58 +00:00
Yuta Saito
a65c205a1b [ruby/prism] Add explicit check for PRISM_HAS_NO_FILESYSTEM
89c22f0e6c
2024-07-26 17:07:50 +00:00
Yuta Saito
1992bd31a5 [ruby/prism] Fallback to pm_string_file_init on platforms without memory-mapped files
> ..., and on other POSIX systems we'll use `read`.

As `pm_string_mapped_init`'s doc comment says, it should fall back to
`read(2)`-based implementation on platforms without memory-mapped files
like WASI, but it didn't. This commit fixes it by calling `pm_string_file_init`
in the fallback case.
Also `defined(_POSIX_MAPPED_FILES)` check for `read(2)`-based path is
unnecessary, and it prevents the fallback from being executed, so this
change removes it.

b3d9064b71
2024-07-26 17:07:50 +00:00
Kevin Newton
8e5ac5a87d [PRISM] Ensure not opening directories 2024-07-18 13:03:25 -04:00
Kevin Newton
c1df15c3e6 [PRISM] Use node ids for error highlight 2024-07-11 14:25:54 -04:00
Kevin Newton
ac70dd07e6 [ruby/prism] Remove unused string list struct
36c6851c85
2024-06-05 14:40:03 -04:00
Herwin
61e2916d1c [ruby/prism] Typo fix: poitive => positive
d13a05252d
2024-05-28 15:28:02 +00:00
Kevin Newton
b04c959621 [ruby/prism] Remove various unused memsize infra
283938ed1f
2024-05-24 17:19:34 +00:00
Kevin Newton
b8681c2e37 [ruby/prism] Remove Debug::integer_parse
14e397598b
2024-05-24 17:19:33 +00:00
Kevin Newton
870350253e [ruby/prism] Remove Debug::static_inspect
486c71c426
2024-05-24 17:19:33 +00:00
Kevin Newton
89efb94fec [ruby/prism] Reconfigure rationals
This eliminates the subnode on RationalNode and replaces it with two
integer fields, which represent the ratio for the rational. It also
reduces those two integers if they both fit into 32 bits.

Importantly, this PR does not implement bignum reduction. That's something
I'd like to consider for the future, but it's simple enough for now to
leave them unreduced, which makes it more useful than it used to be.

86e06c7068
2024-05-21 14:27:46 -04:00
Kevin Newton
286d3032a7 [ruby/prism] More mixed encoding errors
2a43b4f55c
2024-05-16 17:22:14 +00:00
Kevin Newton
0defbc11a5 [ruby/prism] Fix recursive multiply when values are switched in karatsuba_multiply
4dc6ea960d
2024-04-23 18:17:29 +00:00
Kevin Newton
f72436f835 [ruby/prism] Inline pm_state_stack 2024-04-17 10:36:52 -04:00
Kevin Newton
f34409bf87 [ruby/prism] Fix up more clang-analyzer failures
f9a1abbc64
2024-04-17 01:15:21 +00:00
Kevin Newton
646a00892e [ruby/prism] Fix up clang-analyzer violations
259aef2acd
2024-04-16 14:30:34 +00:00
Kevin Newton
540cc886b9 [ruby/prism] Make the locals set switch from list to hash dynamically
c977c4c98a
2024-04-05 19:23:57 +00:00
Kevin Newton
358aeb103b [ruby/prism] Switch locals to use a hash
f38946021e
2024-04-05 19:23:57 +00:00
Kevin Newton
35ff302893 [ruby/prism] Various cleanup with new -x option
020756fb11
2024-03-28 12:04:35 -04:00
Kevin Newton
fcc06fa82a [ruby/prism] CLI -x flag
2068e3c30a
2024-03-28 12:04:35 -04:00
Koichi ITO
56a2fad2a4 [ruby/prism] Fix incorrect paring when using invalid regexp options
Fixes https://github.com/ruby/prism/pull/2617.

There was an issue with the lexer as follows.
The following are valid regexp options:

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.lex("/x/io").value.map {|token| token[0].type }'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[:REGEXP_BEGIN, :STRING_CONTENT, :REGEXP_END, :EOF]
```

The following are invalid regexp options. Unnecessary the `IDENTIFIER` token is appearing:

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.lex("/x/az").value.map {|token| token[0].type }'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[:REGEXP_BEGIN, :STRING_CONTENT, :REGEXP_END, :IDENTIFIER, :EOF]
```

As a behavior of Ruby, when given `A` to `Z` and `a` to `z`, they act as invalid regexp options. e.g.,

```console
$ ruby -e '/regexp/az'
-e:1: unknown regexp options - az
/regexp/az
-e: compile error (SyntaxError)
```

Thus, it should probably not be construed as `IDENTIFIER` token.

Therefore, `pm_byte_table` has been adapted to accept those invalid regexp option values.
Whether it is a valid regexp option or not is checked by `pm_regular_expression_flags_create`.
For invalid regexp options, `PM_ERR_REGEXP_UNKNOWN_OPTIONS` is added to diagnostics.

d2a6096fcf
2024-03-25 12:16:32 +00:00
Kevin Newton
a05dfbd405
[PRISM] Remove ssize_t definition from prism 2024-03-13 12:06:48 -04:00
Kevin Newton
4dd9602c6f [ruby/prism] Remove ssize_t usage
64c4f1268b
2024-03-13 16:06:13 +00:00
Nobuyoshi Nakada
f42164e037
Define ssize_t on mswin build 2024-03-12 14:49:25 +09:00
Kevin Newton
21ea290b34 [ruby/prism] Static literals inspect
4913d112da
2024-03-12 03:30:50 +00:00
Kevin Newton
6242a82c8f [ruby/prism] Provide more documentation for pm_integer_parse_digit_values
c3fcb5031f
2024-03-11 15:12:14 +00:00
Kevin Newton
2dfa95a272 [ruby/prism] Stop crashing on invalid integers
afac2d6646
2024-03-11 15:11:42 +00:00
Kevin Newton
f5294ebbdb [ruby/prism] Shared integer parsing logic
a2594a23c1
2024-03-07 18:02:33 -05:00
Kevin Newton
c1462250b8 [ruby/prism] Style and allocation functions
97f838c323
2024-03-07 18:02:33 -05:00
tompng
81f02eb6ba [ruby/prism] Change pm_integer_t structure
588acf823f
2024-03-07 18:02:33 -05:00
tompng
5113d6b059 [ruby/prism] Faster pm_integer_parse pm_integer_string using karatsuba algorithm
ae4fb6b988
2024-03-07 18:02:33 -05:00
Kevin Newton
b88973165a [ruby/prism] Parse files from Ruby API using fread, not mmap
62d4376a53
2024-03-06 17:45:56 +00:00
cui fliter
226a889dc7
[DOC] fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 18:50:47 +09:00
HASUMI Hitoshi
b95e2cdca7 [ruby/prism] Additional fix of adding x prefix after rebase with main branch
08733438bd
2024-03-04 16:40:24 +00:00
HASUMI Hitoshi
c4bd6da298 [ruby/prism] Make alloc interface replaceable
- Add `x` prefix to malloc, calloc, realloc, and free
  (eg: malloc -> xmalloc)
- By default, they are replaced with stdlib's functions at build
- You can use custom functions by defining `PRISM_CUSTOM_ALLOCATOR` macro

7a878af619
2024-03-04 16:40:23 +00:00
Kevin Newton
96907f9495 [ruby/prism] Convert pm_integer_t to strings
fa9a30ad91
2024-02-23 21:54:01 +00:00
Kevin Newton
02b531a813 [ruby/prism] Factor in sign to integer comparison
377666a5df
2024-02-23 13:25:31 -05:00
Kevin Newton
a38cc177d2 [ruby/prism] Duplicated when clauses
865b0d5fbe
2024-02-23 13:25:31 -05:00
Kevin Newton
d1ce989829 [ruby/prism] Duplicated hash keys
3e10c46c14
2024-02-23 13:25:31 -05:00
Kevin Newton
5e0589cf52 [ruby/prism] Parse float values
9137226a52
2024-02-22 22:42:44 -05:00
Kevin Newton
3b3def5db7 [ruby/prism] Regenerate snapshots using integer values 2024-02-22 22:42:44 -05:00