prism 0.30.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/README.md +3 -1
  4. data/config.yml +185 -126
  5. data/docs/serialization.md +3 -0
  6. data/ext/prism/api_node.c +2843 -2085
  7. data/ext/prism/extconf.rb +1 -1
  8. data/ext/prism/extension.c +35 -25
  9. data/ext/prism/extension.h +2 -2
  10. data/include/prism/ast.h +1048 -69
  11. data/include/prism/defines.h +9 -0
  12. data/include/prism/diagnostic.h +11 -3
  13. data/include/prism/options.h +55 -1
  14. data/include/prism/parser.h +27 -3
  15. data/include/prism/regexp.h +2 -1
  16. data/include/prism/util/pm_integer.h +6 -6
  17. data/include/prism/util/pm_newline_list.h +11 -0
  18. data/include/prism/util/pm_string.h +1 -0
  19. data/include/prism/version.h +3 -3
  20. data/lib/prism/desugar_compiler.rb +111 -74
  21. data/lib/prism/dispatcher.rb +2 -1
  22. data/lib/prism/dot_visitor.rb +21 -31
  23. data/lib/prism/dsl.rb +656 -471
  24. data/lib/prism/ffi.rb +3 -0
  25. data/lib/prism/inspect_visitor.rb +285 -57
  26. data/lib/prism/mutation_compiler.rb +5 -5
  27. data/lib/prism/node.rb +2282 -4754
  28. data/lib/prism/node_ext.rb +72 -11
  29. data/lib/prism/parse_result/errors.rb +65 -0
  30. data/lib/prism/parse_result/newlines.rb +28 -28
  31. data/lib/prism/parse_result.rb +25 -2
  32. data/lib/prism/reflection.rb +7 -7
  33. data/lib/prism/serialize.rb +468 -610
  34. data/lib/prism/translation/parser/compiler.rb +18 -18
  35. data/lib/prism/translation/parser/lexer.rb +1 -1
  36. data/lib/prism/translation/parser.rb +3 -3
  37. data/lib/prism/translation/ripper.rb +14 -14
  38. data/lib/prism/translation/ruby_parser.rb +43 -7
  39. data/prism.gemspec +3 -1
  40. data/rbi/prism/dsl.rbi +521 -0
  41. data/rbi/prism/node.rbi +1456 -5616
  42. data/rbi/prism.rbi +16 -16
  43. data/sig/prism/dsl.rbs +189 -305
  44. data/sig/prism/node.rbs +702 -603
  45. data/sig/prism/parse_result.rbs +2 -0
  46. data/src/diagnostic.c +22 -6
  47. data/src/node.c +277 -284
  48. data/src/options.c +18 -0
  49. data/src/prettyprint.c +99 -108
  50. data/src/prism.c +1282 -760
  51. data/src/regexp.c +72 -4
  52. data/src/serialize.c +165 -50
  53. data/src/token_type.c +2 -2
  54. data/src/util/pm_integer.c +14 -14
  55. data/src/util/pm_newline_list.c +29 -0
  56. data/src/util/pm_string.c +9 -5
  57. metadata +4 -2
data/ext/prism/extconf.rb CHANGED
@@ -50,7 +50,7 @@ def make(env, target)
50
50
  Dir.chdir(File.expand_path("../..", __dir__)) do
51
51
  system(
52
52
  env,
53
- RUBY_PLATFORM.include?("openbsd") ? "gmake" : "make",
53
+ RUBY_PLATFORM.match?(/openbsd|freebsd/) ? "gmake" : "make",
54
54
  target,
55
55
  exception: true
56
56
  )
@@ -21,6 +21,7 @@ VALUE rb_cPrismParseError;
21
21
  VALUE rb_cPrismParseWarning;
22
22
  VALUE rb_cPrismResult;
23
23
  VALUE rb_cPrismParseResult;
24
+ VALUE rb_cPrismLexResult;
24
25
  VALUE rb_cPrismParseLexResult;
25
26
 
26
27
  VALUE rb_cPrismDebugEncoding;
@@ -138,7 +139,13 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {
138
139
  if (key_id == rb_id_option_filepath) {
139
140
  if (!NIL_P(value)) pm_options_filepath_set(options, check_string(value));
140
141
  } else if (key_id == rb_id_option_encoding) {
141
- if (!NIL_P(value)) pm_options_encoding_set(options, rb_enc_name(rb_to_encoding(value)));
142
+ if (!NIL_P(value)) {
143
+ if (value == Qfalse) {
144
+ pm_options_encoding_locked_set(options, true);
145
+ } else {
146
+ pm_options_encoding_set(options, rb_enc_name(rb_to_encoding(value)));
147
+ }
148
+ }
142
149
  } else if (key_id == rb_id_option_line) {
143
150
  if (!NIL_P(value)) pm_options_line_set(options, NUM2INT(value));
144
151
  } else if (key_id == rb_id_option_frozen_string_literal) {
@@ -206,6 +213,7 @@ build_options(VALUE argument) {
206
213
  static void
207
214
  extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) {
208
215
  options->line = 1; // default
216
+
209
217
  if (!NIL_P(keywords)) {
210
218
  struct build_options_data data = { .options = options, .keywords = keywords };
211
219
  struct build_options_data *argument = &data;
@@ -364,7 +372,7 @@ dump_file(int argc, VALUE *argv, VALUE self) {
364
372
  */
365
373
  static VALUE
366
374
  parser_comments(pm_parser_t *parser, VALUE source) {
367
- VALUE comments = rb_ary_new();
375
+ VALUE comments = rb_ary_new_capa(parser->comment_list.size);
368
376
 
369
377
  for (pm_comment_t *comment = (pm_comment_t *) parser->comment_list.head; comment != NULL; comment = (pm_comment_t *) comment->node.next) {
370
378
  VALUE location_argv[] = {
@@ -386,7 +394,7 @@ parser_comments(pm_parser_t *parser, VALUE source) {
386
394
  */
387
395
  static VALUE
388
396
  parser_magic_comments(pm_parser_t *parser, VALUE source) {
389
- VALUE magic_comments = rb_ary_new();
397
+ VALUE magic_comments = rb_ary_new_capa(parser->magic_comment_list.size);
390
398
 
391
399
  for (pm_magic_comment_t *magic_comment = (pm_magic_comment_t *) parser->magic_comment_list.head; magic_comment != NULL; magic_comment = (pm_magic_comment_t *) magic_comment->node.next) {
392
400
  VALUE key_loc_argv[] = {
@@ -436,7 +444,7 @@ parser_data_loc(const pm_parser_t *parser, VALUE source) {
436
444
  */
437
445
  static VALUE
438
446
  parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
439
- VALUE errors = rb_ary_new();
447
+ VALUE errors = rb_ary_new_capa(parser->error_list.size);
440
448
  pm_diagnostic_t *error;
441
449
 
442
450
  for (error = (pm_diagnostic_t *) parser->error_list.head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
@@ -479,7 +487,7 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
479
487
  */
480
488
  static VALUE
481
489
  parser_warnings(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
482
- VALUE warnings = rb_ary_new();
490
+ VALUE warnings = rb_ary_new_capa(parser->warning_list.size);
483
491
  pm_diagnostic_t *warning;
484
492
 
485
493
  for (warning = (pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (pm_diagnostic_t *) warning->node.next) {
@@ -556,9 +564,10 @@ static void
556
564
  parse_lex_token(void *data, pm_parser_t *parser, pm_token_t *token) {
557
565
  parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) parser->lex_callback->data;
558
566
 
559
- VALUE yields = rb_ary_new_capa(2);
560
- rb_ary_push(yields, pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source));
561
- rb_ary_push(yields, INT2FIX(parser->lex_state));
567
+ VALUE yields = rb_assoc_new(
568
+ pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source),
569
+ INT2FIX(parser->lex_state)
570
+ );
562
571
 
563
572
  rb_ary_push(parse_lex_data->tokens, yields);
564
573
  }
@@ -599,7 +608,7 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
599
608
  pm_parser_register_encoding_changed_callback(&parser, parse_lex_encoding_changed_callback);
600
609
 
601
610
  VALUE source_string = rb_str_new((const char *) pm_string_source(input), pm_string_length(input));
602
- VALUE offsets = rb_ary_new();
611
+ VALUE offsets = rb_ary_new_capa(parser.newline_list.size);
603
612
  VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 3, source_string, LONG2NUM(parser.start_line), offsets);
604
613
 
605
614
  parse_lex_data_t parse_lex_data = {
@@ -628,16 +637,16 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
628
637
  rb_ary_push(offsets, ULONG2NUM(parser.newline_list.offsets[index]));
629
638
  }
630
639
 
631
- VALUE value;
640
+ VALUE result;
632
641
  if (return_nodes) {
633
- value = rb_ary_new_capa(2);
642
+ VALUE value = rb_ary_new_capa(2);
634
643
  rb_ary_push(value, pm_ast_new(&parser, node, parse_lex_data.encoding, source));
635
644
  rb_ary_push(value, parse_lex_data.tokens);
645
+ result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source);
636
646
  } else {
637
- value = parse_lex_data.tokens;
647
+ result = parse_result_create(rb_cPrismLexResult, &parser, parse_lex_data.tokens, parse_lex_data.encoding, source);
638
648
  }
639
649
 
640
- VALUE result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source);
641
650
  pm_node_destroy(&parser, node);
642
651
  pm_parser_free(&parser);
643
652
 
@@ -646,10 +655,10 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
646
655
 
647
656
  /**
648
657
  * call-seq:
649
- * Prism::lex(source, **options) -> Array
658
+ * Prism::lex(source, **options) -> LexResult
650
659
  *
651
- * Return an array of Token instances corresponding to the given string. For
652
- * supported options, see Prism::parse.
660
+ * Return a LexResult instance that contains an array of Token instances
661
+ * corresponding to the given string. For supported options, see Prism::parse.
653
662
  */
654
663
  static VALUE
655
664
  lex(int argc, VALUE *argv, VALUE self) {
@@ -666,10 +675,10 @@ lex(int argc, VALUE *argv, VALUE self) {
666
675
 
667
676
  /**
668
677
  * call-seq:
669
- * Prism::lex_file(filepath, **options) -> Array
678
+ * Prism::lex_file(filepath, **options) -> LexResult
670
679
  *
671
- * Return an array of Token instances corresponding to the given file. For
672
- * supported options, see Prism::parse.
680
+ * Return a LexResult instance that contains an array of Token instances
681
+ * corresponding to the given file. For supported options, see Prism::parse.
673
682
  */
674
683
  static VALUE
675
684
  lex_file(int argc, VALUE *argv, VALUE self) {
@@ -849,8 +858,8 @@ parse_stream_fgets(char *string, int size, void *stream) {
849
858
  return NULL;
850
859
  }
851
860
 
852
- const char *cstr = StringValueCStr(line);
853
- size_t length = strlen(cstr);
861
+ const char *cstr = RSTRING_PTR(line);
862
+ long length = RSTRING_LEN(line);
854
863
 
855
864
  memcpy(string, cstr, length);
856
865
  string[length] = '\0';
@@ -954,9 +963,9 @@ parse_file_comments(int argc, VALUE *argv, VALUE self) {
954
963
 
955
964
  /**
956
965
  * call-seq:
957
- * Prism::parse_lex(source, **options) -> ParseResult
966
+ * Prism::parse_lex(source, **options) -> ParseLexResult
958
967
  *
959
- * Parse the given string and return a ParseResult instance that contains a
968
+ * Parse the given string and return a ParseLexResult instance that contains a
960
969
  * 2-element array, where the first element is the AST and the second element is
961
970
  * an array of Token instances.
962
971
  *
@@ -981,9 +990,9 @@ parse_lex(int argc, VALUE *argv, VALUE self) {
981
990
 
982
991
  /**
983
992
  * call-seq:
984
- * Prism::parse_lex_file(filepath, **options) -> ParseResult
993
+ * Prism::parse_lex_file(filepath, **options) -> ParseLexResult
985
994
  *
986
- * Parse the given file and return a ParseResult instance that contains a
995
+ * Parse the given file and return a ParseLexResult instance that contains a
987
996
  * 2-element array, where the first element is the AST and the second element is
988
997
  * an array of Token instances.
989
998
  *
@@ -1124,6 +1133,7 @@ Init_prism(void) {
1124
1133
  rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject);
1125
1134
  rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject);
1126
1135
  rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult);
1136
+ rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult);
1127
1137
  rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult);
1128
1138
 
1129
1139
  // Intern all of the IDs eagerly that we support so that we don't have to do
@@ -1,7 +1,7 @@
1
1
  #ifndef PRISM_EXT_NODE_H
2
2
  #define PRISM_EXT_NODE_H
3
3
 
4
- #define EXPECTED_PRISM_VERSION "0.30.0"
4
+ #define EXPECTED_PRISM_VERSION "1.0.0"
5
5
 
6
6
  #include <ruby.h>
7
7
  #include <ruby/encoding.h>
@@ -14,6 +14,6 @@ VALUE pm_integer_new(const pm_integer_t *integer);
14
14
 
15
15
  void Init_prism_api_node(void);
16
16
  void Init_prism_pack(void);
17
- PRISM_EXPORTED_FUNCTION void Init_prism(void);
17
+ RUBY_FUNC_EXPORTED void Init_prism(void);
18
18
 
19
19
  #endif