prism 0.15.1 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -1
  3. data/Makefile +6 -0
  4. data/README.md +2 -0
  5. data/config.yml +21 -20
  6. data/docs/configuration.md +2 -0
  7. data/docs/javascript.md +90 -0
  8. data/docs/releasing.md +27 -0
  9. data/docs/ruby_api.md +2 -0
  10. data/ext/prism/api_node.c +66 -68
  11. data/ext/prism/extension.c +73 -0
  12. data/ext/prism/extension.h +1 -1
  13. data/include/prism/ast.h +40 -40
  14. data/include/prism/defines.h +9 -0
  15. data/include/prism/enc/pm_encoding.h +1 -0
  16. data/include/prism/node.h +0 -17
  17. data/include/prism/parser.h +1 -0
  18. data/include/prism/prettyprint.h +15 -0
  19. data/include/prism/util/pm_buffer.h +10 -4
  20. data/include/prism/util/pm_constant_pool.h +1 -1
  21. data/include/prism/util/pm_newline_list.h +1 -1
  22. data/include/prism/version.h +3 -3
  23. data/include/prism.h +11 -11
  24. data/lib/prism/compiler.rb +0 -3
  25. data/lib/prism/debug.rb +20 -6
  26. data/lib/prism/desugar_compiler.rb +1 -1
  27. data/lib/prism/dispatcher.rb +0 -14
  28. data/lib/prism/dsl.rb +8 -13
  29. data/lib/prism/ffi.rb +25 -0
  30. data/lib/prism/lex_compat.rb +1 -1
  31. data/lib/prism/mutation_compiler.rb +3 -8
  32. data/lib/prism/node.rb +123 -159
  33. data/lib/prism/node_ext.rb +23 -16
  34. data/lib/prism/parse_result.rb +21 -5
  35. data/lib/prism/pattern.rb +3 -3
  36. data/lib/prism/serialize.rb +901 -305
  37. data/lib/prism/visitor.rb +0 -3
  38. data/prism.gemspec +8 -1
  39. data/rbi/prism.rbi +7261 -0
  40. data/rbi/prism_static.rbi +182 -0
  41. data/sig/prism.rbs +4439 -0
  42. data/sig/prism_static.rbs +110 -0
  43. data/src/enc/pm_unicode.c +1 -1
  44. data/src/node.c +28 -29
  45. data/src/prettyprint.c +7674 -1647
  46. data/src/prism.c +353 -300
  47. data/src/regexp.c +2 -0
  48. data/src/serialize.c +392 -381
  49. data/src/util/pm_buffer.c +47 -12
  50. data/src/util/pm_constant_pool.c +1 -1
  51. data/src/util/pm_newline_list.c +8 -54
  52. metadata +9 -2
data/src/util/pm_buffer.c CHANGED
@@ -40,9 +40,13 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
40
40
  size_t next_length = buffer->length + length;
41
41
 
42
42
  if (next_length > buffer->capacity) {
43
- do {
43
+ if (buffer->capacity == 0) {
44
+ buffer->capacity = 1;
45
+ }
46
+
47
+ while (next_length > buffer->capacity) {
44
48
  buffer->capacity *= 2;
45
- } while (next_length > buffer->capacity);
49
+ }
46
50
 
47
51
  buffer->value = realloc(buffer->value, buffer->capacity);
48
52
  }
@@ -53,20 +57,43 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
53
57
  // Append a generic pointer to memory to the buffer.
54
58
  static inline void
55
59
  pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
60
+ size_t cursor = buffer->length;
56
61
  pm_buffer_append_length(buffer, length);
57
- memcpy(buffer->value + (buffer->length - length), source, length);
62
+ memcpy(buffer->value + cursor, source, length);
58
63
  }
59
64
 
60
65
  // Append the given amount of space as zeroes to the buffer.
61
66
  void
62
67
  pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) {
68
+ size_t cursor = buffer->length;
63
69
  pm_buffer_append_length(buffer, length);
64
- memset(buffer->value + (buffer->length - length), 0, length);
70
+ memset(buffer->value + cursor, 0, length);
71
+ }
72
+
73
+ // Append a formatted string to the buffer.
74
+ void
75
+ pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) {
76
+ va_list arguments;
77
+ va_start(arguments, format);
78
+ int result = vsnprintf(NULL, 0, format, arguments);
79
+ va_end(arguments);
80
+
81
+ if (result < 0) return;
82
+ size_t length = (size_t) (result + 1);
83
+
84
+ size_t cursor = buffer->length;
85
+ pm_buffer_append_length(buffer, length);
86
+
87
+ va_start(arguments, format);
88
+ vsnprintf(buffer->value + cursor, length, format, arguments);
89
+ va_end(arguments);
90
+
91
+ buffer->length--;
65
92
  }
66
93
 
67
94
  // Append a string to the buffer.
68
95
  void
69
- pm_buffer_append_str(pm_buffer_t *buffer, const char *value, size_t length) {
96
+ pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length) {
70
97
  pm_buffer_append(buffer, value, length);
71
98
  }
72
99
 
@@ -78,27 +105,35 @@ pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length)
78
105
 
79
106
  // Append a single byte to the buffer.
80
107
  void
81
- pm_buffer_append_u8(pm_buffer_t *buffer, uint8_t value) {
108
+ pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value) {
82
109
  const void *source = &value;
83
110
  pm_buffer_append(buffer, source, sizeof(uint8_t));
84
111
  }
85
112
 
86
- // Append a 32-bit unsigned integer to the buffer.
113
+ // Append a 32-bit unsigned integer to the buffer as a variable-length integer.
87
114
  void
88
- pm_buffer_append_u32(pm_buffer_t *buffer, uint32_t value) {
115
+ pm_buffer_append_varint(pm_buffer_t *buffer, uint32_t value) {
89
116
  if (value < 128) {
90
- pm_buffer_append_u8(buffer, (uint8_t) value);
117
+ pm_buffer_append_byte(buffer, (uint8_t) value);
91
118
  } else {
92
119
  uint32_t n = value;
93
120
  while (n >= 128) {
94
- pm_buffer_append_u8(buffer, (uint8_t) (n | 128));
121
+ pm_buffer_append_byte(buffer, (uint8_t) (n | 128));
95
122
  n >>= 7;
96
123
  }
97
- pm_buffer_append_u8(buffer, (uint8_t) n);
124
+ pm_buffer_append_byte(buffer, (uint8_t) n);
125
+ }
126
+ }
127
+
128
+ // Concatenate one buffer onto another.
129
+ void
130
+ pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source) {
131
+ if (source->length > 0) {
132
+ pm_buffer_append(destination, source->value, source->length);
98
133
  }
99
134
  }
100
135
 
101
- // Free the memory associated with the buffer.
136
+ // Free the internal memory associated with the buffer.
102
137
  void
103
138
  pm_buffer_free(pm_buffer_t *buffer) {
104
139
  free(buffer->value);
@@ -156,7 +156,7 @@ pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
156
156
 
157
157
  // Return a pointer to the constant indicated by the given constant id.
158
158
  pm_constant_t *
159
- pm_constant_pool_id_to_constant(pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
159
+ pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
160
160
  assert(constant_id > 0 && constant_id <= pool->size);
161
161
  return &pool->constants[constant_id - 1];
162
162
  }
@@ -53,10 +53,14 @@ pm_newline_list_check_append(pm_newline_list_t *list, const uint8_t *cursor) {
53
53
  return pm_newline_list_append(list, cursor);
54
54
  }
55
55
 
56
- // Returns the line and column of the given offset, assuming we don't have any
57
- // information about the previous index that we found.
58
- static pm_line_column_t
59
- pm_newline_list_line_column_search(pm_newline_list_t *list, size_t offset) {
56
+ // Returns the line and column of the given offset. If the offset is not in the
57
+ // list, the line and column of the closest offset less than the given offset
58
+ // are returned.
59
+ pm_line_column_t
60
+ pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor) {
61
+ assert(cursor >= list->start);
62
+ size_t offset = (size_t) (cursor - list->start);
63
+
60
64
  size_t left = 0;
61
65
  size_t right = list->size - 1;
62
66
 
@@ -77,56 +81,6 @@ pm_newline_list_line_column_search(pm_newline_list_t *list, size_t offset) {
77
81
  return ((pm_line_column_t) { left - 1, offset - list->offsets[left - 1] });
78
82
  }
79
83
 
80
- // Returns the line and column of the given offset, assuming we know the last
81
- // index that we found.
82
- static pm_line_column_t
83
- pm_newline_list_line_column_scan(pm_newline_list_t *list, size_t offset) {
84
- if (offset > list->last_offset) {
85
- size_t index = list->last_index;
86
- while (index < list->size && list->offsets[index] < offset) {
87
- index++;
88
- }
89
-
90
- if (index == list->size) {
91
- return ((pm_line_column_t) { index - 1, offset - list->offsets[index - 1] });
92
- }
93
-
94
- return ((pm_line_column_t) { index, 0 });
95
- } else {
96
- size_t index = list->last_index;
97
- while (index > 0 && list->offsets[index] > offset) {
98
- index--;
99
- }
100
-
101
- if (index == 0) {
102
- return ((pm_line_column_t) { 0, offset });
103
- }
104
-
105
- return ((pm_line_column_t) { index, offset - list->offsets[index - 1] });
106
- }
107
- }
108
-
109
- // Returns the line and column of the given offset. If the offset is not in the
110
- // list, the line and column of the closest offset less than the given offset
111
- // are returned.
112
- pm_line_column_t
113
- pm_newline_list_line_column(pm_newline_list_t *list, const uint8_t *cursor) {
114
- assert(cursor >= list->start);
115
- size_t offset = (size_t) (cursor - list->start);
116
- pm_line_column_t result;
117
-
118
- if (list->last_offset == 0) {
119
- result = pm_newline_list_line_column_search(list, offset);
120
- } else {
121
- result = pm_newline_list_line_column_scan(list, offset);
122
- }
123
-
124
- list->last_index = result.line;
125
- list->last_offset = offset;
126
-
127
- return result;
128
- }
129
-
130
84
  // Free the internal memory allocated for the newline list.
131
85
  void
132
86
  pm_newline_list_free(pm_newline_list_t *list) {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.1
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-18 00:00:00.000000000 Z
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -32,8 +32,10 @@ files:
32
32
  - docs/encoding.md
33
33
  - docs/fuzzing.md
34
34
  - docs/heredocs.md
35
+ - docs/javascript.md
35
36
  - docs/mapping.md
36
37
  - docs/prism.png
38
+ - docs/releasing.md
37
39
  - docs/ripper.md
38
40
  - docs/ruby_api.md
39
41
  - docs/serialization.md
@@ -51,6 +53,7 @@ files:
51
53
  - include/prism/node.h
52
54
  - include/prism/pack.h
53
55
  - include/prism/parser.h
56
+ - include/prism/prettyprint.h
54
57
  - include/prism/regexp.h
55
58
  - include/prism/util/pm_buffer.h
56
59
  - include/prism/util/pm_char.h
@@ -84,6 +87,10 @@ files:
84
87
  - lib/prism/serialize.rb
85
88
  - lib/prism/visitor.rb
86
89
  - prism.gemspec
90
+ - rbi/prism.rbi
91
+ - rbi/prism_static.rbi
92
+ - sig/prism.rbs
93
+ - sig/prism_static.rbs
87
94
  - src/diagnostic.c
88
95
  - src/enc/pm_big5.c
89
96
  - src/enc/pm_euc_jp.c