Commit graph

139 commits

Author SHA1 Message Date
Kevin Newton
746eede412 [ruby/yarp] Constant on block parameter node
2cd9a67150
2023-09-06 18:18:10 +00:00
Kevin Newton
dee383b262 [ruby/yarp] Constants on classes and modules
(https://github.com/ruby/yarp/pull/1409)

0a11ec579f
2023-09-06 16:19:43 +00:00
Kevin Newton
5537169ef0 [ruby/yarp] Temporarily add name_constant to replace name on ClassNode/ModuleNode
8f87686e9c
2023-09-06 14:20:13 +00:00
Kevin Newton
4c9a036606 [ruby/yarp] Add constants and constants
d7eaa89bc3
2023-09-06 13:00:23 +00:00
Benoit Daloze
6356f6d3cd [ruby/yarp] Rename YP_NODE_*_NODE to YP_*_NODE
d93b93f342
2023-09-06 12:30:36 +00:00
Kevin Newton
767f984017 [ruby/yarp] Flatten CallAndWriteNode, CallOrWriteNode, and CallOperatorWriteNode
8f26ffa0b2
2023-09-05 19:13:30 +00:00
Kevin Newton
95e4bdcd62 [ruby/yarp] Rename CallNode#operator_loc to CallNode#call_operator_loc
fbcd307a54
2023-09-05 19:13:29 +00:00
Kevin Newton
c384ef0799 [ruby/yarp] Introduce a BlockLocalVariableNode
This is a tradeoff that I think is worth it. Right now we have a
location list that tracks the location of each of the block locals.
Instead, I'd like to make that a node list that has a proper node
in each spot in the list. In doing so, we eliminate the need to have
a location list at all, making it simpler on all of the various
consumers as we have one fewer field type. There should be minimal
memory implications here since this syntax is exceedingly rare.

04d329ddf0
2023-09-05 18:41:51 +00:00
Kevin Newton
c666077182 [ruby/yarp] Add global variables to the constant pool
b48067b067
2023-09-01 22:52:14 +00:00
Mike Dalessio
512f8217cb [ruby/yarp] fix: double-counting of errors in parsing escaped strings
Essentially, this change updates `yp_unescape_calculate_difference` to
not create syntax errors, and we rely entirely on
`yp_unescape_manipulate_string` to report syntax errors.

To do that, this PR adds another (!) parameter to `unescape`:
`yp_list_t *error_list`. When present, `unescape` reports syntax
errors (and otherwise does not).

However, an edge case that needed to be addressed is reporting syntax
errors in this case:

    ?\u{1234 2345}

In a string context, it's possible to have multiple codepoints by
doing something like `"\u{1234 2345}"`; however, in the character
literal context, this is a syntax error -- only a single codepoint is
allowed.

Unfortunately, when `yp_unescape_manipulate_string` is called, there's
nothing to indicate that we are in a "character literal" context and
that only a single codepoint is valid.

To make this work, this PR:

- introduces a new static utility function in yarp.c,
  `yp_char_literal_node_create_and_unescape`, which is called when
  we're parsing `YP_TOKEN_CHARACTER_LITERAL`
- introduces a new (unexported) function,
  `yp_unescape_manipulate_char_literal` which does the same thing as
  `yp_unescape_manipulate_string` but tells `unescape` that only a
  single codepoint is expected

f6a65840b5
2023-09-01 17:04:37 +00:00
Mike Dalessio
df4c77608e [ruby/yarp] fix: octal, hex, and unicode strings at the end of a
file
(https://github.com/ruby/yarp/pull/1371)

* refactor: move EOF check into yp_unescape_calculate_difference

parser_lex is a bit more readable when we can rely on that behavior

* fix: octal and hex digits at the end of a file

Previously this resulted in invalid memory access.

* fix: unicode strings at the end of a file

Previously this resulted in invalid memory access.

* Unterminated curly-bracket unicode is a syntax error

21cf11acb5
2023-08-31 22:40:35 +00:00
Nathan Froyd
8470acc1ec [ruby/yarp] add some const qualifiers to local variables
eb3c6eb928
2023-08-31 17:43:50 +00:00
Kevin Newton
4aa98b2760 [ruby/yarp] Add a value to numbered references
5d9b048971
2023-08-30 21:31:36 +00:00
Mike Dalessio
bbaae3681c [ruby/yarp] fix: regular expression with start and end out of order
Also, a similar test and fix for interpolated regular expressions.

This snippet:

    <<-A.g//,
    A
    /{/, ''\

previously created a regular expression node with inverted start and
end:

    RegularExpressionNode(14...13)((14...15), (15...21), (12...13), ", ''", 0),

which failed an assertion during serialization.

After this change:

    RegularExpressionNode(12...15)((14...15), (15...21), (12...13), ", ''", 0),

Found by the fuzzer.

5fef572f95
2023-08-30 20:46:09 +00:00
Mike Dalessio
6beaf010a4 [ruby/yarp] fix: binary CallNode with out-of-order arg and receiver
The snippet added in this commit previously resulted in a CallNode
with inverted start and end locations:

    >   AssocNode(15...13)(
    >     CallNode(15...13)(
            StringNode(15...17)((15...16), (16...16), (16...17), ""),
            nil,
            (12...13),
            nil,
            ArgumentsNode(12...13)([MissingNode(12...13)()]),
            nil,
            nil,
            0,
            "/"
          ),
          MissingNode(13...13)(),
          (13...13)
        ),

which failed an assertion during serialization.

After this change, it looks better:

    >   AssocNode(12...13)(
    >     CallNode(12...17)(
            StringNode(15...17)((15...16), (16...16), (16...17), ""),
            nil,
            (12...13),
            nil,
            ArgumentsNode(12...13)([MissingNode(12...13)()]),
            nil,
            nil,
            0,
            "/"
          ),
          MissingNode(13...13)(),
          (13...13)
        ),

Found by the fuzzer.

040aa63ad6
2023-08-30 20:46:09 +00:00
Mike Dalessio
f80582cda8 [ruby/yarp] fix: StatementsNode with out-of-order body nodes
The presence of the heredocs in this snippet with invalid syntax:

    for <<A + <<B
    A
    B

causes the MissingNode to have a location after other nodes in the
list, resulting in a StatementsNode with inverted start and end
locations:

      [ForNode(0...14)(
         MultiWriteNode(4...7)([InterpolatedStringNode(4...7)((4...7), [], (14...16))], nil, nil, nil, nil),
         MissingNode(16...16)(),
    >    StatementsNode(16...14)(
           [MissingNode(16...16)(), InterpolatedStringNode(10...13)((10...13), [], (16...18)), MissingNode(13...14)()]
         ),
         (0...3),
         (16...16),
         nil,
         (14...14)
       )]

which failed an assertion during serialization.

With this fix, the node's locations are:

      [ForNode(0...14)(
         MultiWriteNode(4...7)([InterpolatedStringNode(4...7)((4...7), [], (14...16))], nil, nil, nil, nil),
         MissingNode(16...16)(),
    >    StatementsNode(10...16)(
           [MissingNode(16...16)(), InterpolatedStringNode(10...13)((10...13), [], (16...18)), MissingNode(13...14)()]
         ),
         (0...3),
         (16...16),
         nil,
         (14...14)
       )]

Found by the fuzzer.

09bcedc05e
2023-08-30 20:46:08 +00:00
Kevin Newton
00dbee94ac [ruby/yarp] Add class variables to the constant pool
be5cb60c83
2023-08-30 19:59:28 +00:00
Kevin Newton
151e94fee5 [ruby/yarp] Fix instance variable constant names
1f94f55fcb
2023-08-30 19:59:26 +00:00
Kevin Newton
7be08f3f58 [ruby/yarp] Switch from handling const char * to const uint8_t *
465e7bb0a9
2023-08-30 14:41:23 -04:00
Mike Dalessio
2d009805e7 [ruby/yarp] fix: comment followed by whitespace at end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

b248553dd6
2023-08-30 18:27:52 +00:00
Mike Dalessio
ae7f907559 [ruby/yarp] fix: heredoc with incomplete escape at end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

ec4abd87f4
2023-08-30 18:27:51 +00:00
Mike Dalessio
341f47a6dd [ruby/yarp] fix: incomplete escape in regex at the end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

55b9dfb41c
2023-08-30 18:27:51 +00:00
Mike Dalessio
7cebb9b737 [ruby/yarp] fix: incomplete escape in list at the end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

78ed75ed75
2023-08-30 18:27:51 +00:00
Mike Dalessio
46e47404a8 [ruby/yarp] fix: trailing decimal, binary, octal, and hex numbers at end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

af5b85a27a
2023-08-30 18:27:50 +00:00
Mike Dalessio
c83552a596 [ruby/yarp] fix: trailing asterisk at end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

c86b4907b4
2023-08-30 18:27:49 +00:00
Mike Dalessio
bd0268372e [ruby/yarp] fix: trailing comment at end of file
Previously this resulted in invalid memory access.

Found by the fuzzer.

a1c9404906
2023-08-30 18:27:49 +00:00
Mike Dalessio
6f8126faeb [ruby/yarp] fix: string escape char "\" at the end of a file
Previously this resulted in invalid memory access.

Found by the fuzzer.

178862e2ca
2023-08-30 18:27:48 +00:00
Mike Dalessio
476f38d62d [ruby/yarp] fix: ":" at the end of a file
Previously this resulted in invalid memory access.

Found by the fuzzer.

c781c9fcd2
2023-08-30 18:27:48 +00:00
Mike Dalessio
3da139d284 [ruby/yarp] fix: "$" at the end of a file
Previously this resulted in invalid memory access as well as a
cascading failed assertion:

    src/enc/yp_unicode.c:2224: yp_utf_8_codepoint: Assertion `n >= 1' failed.

Found by the fuzzer.

a34c534440
2023-08-30 18:27:47 +00:00
Kevin Newton
b435161404 [ruby/yarp] Add instance variable names to the constant pool
f049932c44
2023-08-29 20:12:57 +00:00
Kevin Newton
5161c6c4cd [ruby/yarp] Statements inside ensure blocks can accept blocks
be84ea5343
2023-08-29 19:33:31 +00:00
Nathan Froyd
36e210718c [ruby/yarp] simplify context_pop
fe85b595b6
2023-08-29 15:36:21 +00:00
Nathan Froyd
f726ad9740 [ruby/yarp] use memcmp for block memory comparison
3563e5c5d5
2023-08-28 20:00:25 +00:00
Mike Dalessio
9b87518ea0 [ruby/yarp] fix: %I list spanning a heredoc
Similar to the previous %W fix, we accept a symbol node and
concatenate it onto an interpolated symbol.

6b5911b95e
2023-08-28 12:37:31 +00:00
Mike Dalessio
29c5b85128 [ruby/yarp] fix: %i list spanning a heredoc
The fix here is similar to what we did in a previous commit for %w, to
accept two consecutive string tokens without a separator.

f869fbdbe5
2023-08-28 12:37:30 +00:00
Mike Dalessio
74812df496 [ruby/yarp] fix: %W list spanning a heredoc
Primarily this fix is to accept a string node and concatenate it onto
an interpolated string.

6df729fe72
2023-08-28 12:37:30 +00:00
Mike Dalessio
77e971b6ec [ruby/yarp] fix: %w list spanning a heredoc
Two fixes were necessary:

- ensure we are handling newlines correctly
- accept two consecutive string tokens without a separator

4e707937cb

Co-authored-by: Kevin Newton <kddnewton@gmail.com>
2023-08-28 12:37:29 +00:00
Mike Dalessio
2b9a053740 [ruby/yarp] fix: yp_interpolated_symbol_node_append
Made this function's behavior match the interpolated_string
implementation.

Previously, the start location was not set and left as 0.

87f348889f
2023-08-28 12:37:28 +00:00
Benoit Daloze
5937d01f7f [ruby/yarp] Rename constant pool fields to name or operator
* `constant_id` and `operator_id` are confusing.
* See https://github.com/ruby/yarp/issues/1296

09d0a144df
2023-08-27 16:18:15 +00:00
Kevin Newton
ca9a44795b Remove version templating in YARP 2023-08-25 18:20:51 -04:00
Kevin Newton
9b8602dd90 [ruby/yarp] Introduce parse_lex instead of asking for a block
7e70339fe1
2023-08-25 21:10:19 +00:00
Kevin Newton
4813887694 [ruby/yarp] Accept a block to parse and parse_file to get lexer output as well
40fbf61a8d
2023-08-25 21:10:17 +00:00
Kevin Newton
0c1a749eef [ruby/yarp] Fix nested multi assignment locations
9a65f002dc
2023-08-25 21:10:16 +00:00
Kevin Newton
a31b069a8a [ruby/yarp] Track block opening and closing locations
7984e4ddc7
2023-08-25 21:10:13 +00:00
Kevin Newton
b9a2c96747 [ruby/yarp] Ensure interpolated symbols converted to regular symbols get opening and closing
386655d54f
2023-08-25 21:10:13 +00:00
Kevin Newton
b112e89bb1 [ruby/yarp] Add closing_loc to WhileNode
b4132b876d
2023-08-25 21:10:12 +00:00
Kevin Newton
df11a08d93 [ruby/yarp] Add closing_loc to UntilNode
4362cecc2c
2023-08-25 21:10:11 +00:00
Kevin Newton
74780c3e7f [ruby/yarp] Call shorthand should not result in a message location
ad0f9d35e3
2023-08-25 21:10:10 +00:00
Kevin Newton
0df515c095 [ruby/yarp] Mark local variable targets in pattern matching
6c6700a001
2023-08-25 19:31:33 +00:00
Kevin Newton
7898b8e1ea [ruby/yarp] Provide target node versions
a026564d38
2023-08-25 19:31:31 +00:00