prism 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -1
  3. data/Makefile +5 -0
  4. data/README.md +8 -6
  5. data/config.yml +236 -38
  6. data/docs/build_system.md +19 -2
  7. data/docs/cruby_compilation.md +27 -0
  8. data/docs/parser_translation.md +34 -0
  9. data/docs/parsing_rules.md +19 -0
  10. data/docs/releasing.md +3 -3
  11. data/docs/ruby_api.md +1 -1
  12. data/docs/serialization.md +17 -5
  13. data/ext/prism/api_node.c +101 -81
  14. data/ext/prism/extension.c +74 -11
  15. data/ext/prism/extension.h +1 -1
  16. data/include/prism/ast.h +1699 -504
  17. data/include/prism/defines.h +8 -0
  18. data/include/prism/diagnostic.h +39 -2
  19. data/include/prism/encoding.h +10 -0
  20. data/include/prism/options.h +40 -14
  21. data/include/prism/parser.h +33 -17
  22. data/include/prism/util/pm_buffer.h +9 -0
  23. data/include/prism/util/pm_constant_pool.h +7 -0
  24. data/include/prism/util/pm_newline_list.h +0 -11
  25. data/include/prism/version.h +2 -2
  26. data/include/prism.h +19 -2
  27. data/lib/prism/debug.rb +11 -5
  28. data/lib/prism/dot_visitor.rb +36 -14
  29. data/lib/prism/dsl.rb +22 -22
  30. data/lib/prism/ffi.rb +2 -2
  31. data/lib/prism/node.rb +1020 -737
  32. data/lib/prism/node_ext.rb +2 -2
  33. data/lib/prism/parse_result.rb +17 -9
  34. data/lib/prism/serialize.rb +53 -29
  35. data/lib/prism/translation/parser/compiler.rb +1831 -0
  36. data/lib/prism/translation/parser/lexer.rb +335 -0
  37. data/lib/prism/translation/parser/rubocop.rb +37 -0
  38. data/lib/prism/translation/parser.rb +163 -0
  39. data/lib/prism/translation.rb +11 -0
  40. data/lib/prism.rb +1 -0
  41. data/prism.gemspec +12 -5
  42. data/rbi/prism.rbi +150 -88
  43. data/rbi/prism_static.rbi +15 -3
  44. data/sig/prism.rbs +996 -961
  45. data/sig/prism_static.rbs +123 -46
  46. data/src/diagnostic.c +259 -219
  47. data/src/encoding.c +4 -8
  48. data/src/node.c +2 -6
  49. data/src/options.c +24 -5
  50. data/src/prettyprint.c +174 -42
  51. data/src/prism.c +1136 -328
  52. data/src/serialize.c +12 -9
  53. data/src/token_type.c +353 -4
  54. data/src/util/pm_buffer.c +11 -0
  55. data/src/util/pm_constant_pool.c +12 -11
  56. data/src/util/pm_newline_list.c +2 -14
  57. metadata +10 -3
  58. data/docs/building.md +0 -29
@@ -81,7 +81,7 @@ module Prism
81
81
  class RationalNode < Node
82
82
  # Returns the value of the node as a Ruby Rational.
83
83
  def value
84
- Rational(slice.chomp("r"))
84
+ Rational(numeric.is_a?(IntegerNode) ? numeric.value : slice.chomp("r"))
85
85
  end
86
86
  end
87
87
 
@@ -118,7 +118,7 @@ module Prism
118
118
  current = current.parent
119
119
  end
120
120
 
121
- unless current.is_a?(ConstantReadNode)
121
+ if !current.is_a?(ConstantReadNode) && !current.nil?
122
122
  raise DynamicPartsInConstantPathError, "Constant path contains dynamic parts. Cannot compute full name"
123
123
  end
124
124
 
@@ -312,20 +312,24 @@ module Prism
312
312
  # A Location object representing the location of this error in the source.
313
313
  attr_reader :location
314
314
 
315
+ # The level of this error.
316
+ attr_reader :level
317
+
315
318
  # Create a new error object with the given message and location.
316
- def initialize(message, location)
319
+ def initialize(message, location, level)
317
320
  @message = message
318
321
  @location = location
322
+ @level = level
319
323
  end
320
324
 
321
325
  # Implement the hash pattern matching interface for ParseError.
322
326
  def deconstruct_keys(keys)
323
- { message: message, location: location }
327
+ { message: message, location: location, level: level }
324
328
  end
325
329
 
326
330
  # Returns a string representation of this error.
327
331
  def inspect
328
- "#<Prism::ParseError @message=#{@message.inspect} @location=#{@location.inspect}>"
332
+ "#<Prism::ParseError @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level.inspect}>"
329
333
  end
330
334
  end
331
335
 
@@ -337,20 +341,24 @@ module Prism
337
341
  # A Location object representing the location of this warning in the source.
338
342
  attr_reader :location
339
343
 
344
+ # The level of this warning.
345
+ attr_reader :level
346
+
340
347
  # Create a new warning object with the given message and location.
341
- def initialize(message, location)
348
+ def initialize(message, location, level)
342
349
  @message = message
343
350
  @location = location
351
+ @level = level
344
352
  end
345
353
 
346
354
  # Implement the hash pattern matching interface for ParseWarning.
347
355
  def deconstruct_keys(keys)
348
- { message: message, location: location }
356
+ { message: message, location: location, level: level }
349
357
  end
350
358
 
351
359
  # Returns a string representation of this warning.
352
360
  def inspect
353
- "#<Prism::ParseWarning @message=#{@message.inspect} @location=#{@location.inspect}>"
361
+ "#<Prism::ParseWarning @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level.inspect}>"
354
362
  end
355
363
  end
356
364
 
@@ -369,9 +377,9 @@ module Prism
369
377
  # The list of magic comments that were encountered during parsing.
370
378
  attr_reader :magic_comments
371
379
 
372
- # An optional location that represents the location of the content after the
373
- # __END__ marker. This content is loaded into the DATA constant when the
374
- # file being parsed is the main file being executed.
380
+ # An optional location that represents the location of the __END__ marker
381
+ # and the rest of the content of the file. This content is loaded into the
382
+ # DATA constant when the file being parsed is the main file being executed.
375
383
  attr_reader :data_loc
376
384
 
377
385
  # The list of errors that were generated during parsing.
@@ -27,7 +27,7 @@ module Prism
27
27
 
28
28
  # The minor version of prism that we are expecting to find in the serialized
29
29
  # strings.
30
- MINOR_VERSION = 19
30
+ MINOR_VERSION = 20
31
31
 
32
32
  # The patch version of prism that we are expecting to find in the serialized
33
33
  # strings.
@@ -103,8 +103,8 @@ module Prism
103
103
  comments = load_comments
104
104
  magic_comments = load_varuint.times.map { MagicComment.new(load_location, load_location) }
105
105
  data_loc = load_optional_location
106
- errors = load_varuint.times.map { ParseError.new(load_embedded_string, load_location) }
107
- warnings = load_varuint.times.map { ParseWarning.new(load_embedded_string, load_location) }
106
+ errors = load_varuint.times.map { ParseError.new(load_embedded_string, load_location, load_error_level) }
107
+ warnings = load_varuint.times.map { ParseWarning.new(load_embedded_string, load_location, load_warning_level) }
108
108
  [comments, magic_comments, data_loc, errors, warnings]
109
109
  end
110
110
 
@@ -218,9 +218,9 @@ module Prism
218
218
 
219
219
  constant =
220
220
  if start.nobits?(1 << 31)
221
- input.byteslice(start, length).to_sym
221
+ input.byteslice(start, length).force_encoding(@encoding).to_sym
222
222
  else
223
- serialized.byteslice(start & ((1 << 31) - 1), length).to_sym
223
+ serialized.byteslice(start & ((1 << 31) - 1), length).force_encoding(@encoding).to_sym
224
224
  end
225
225
 
226
226
  constant_pool[index] = constant
@@ -238,6 +238,30 @@ module Prism
238
238
  load_constant(index - 1) if index != 0
239
239
  end
240
240
 
241
+ def load_error_level
242
+ level = io.getbyte
243
+
244
+ case level
245
+ when 0
246
+ :fatal
247
+ else
248
+ raise "Unknown level: #{level}"
249
+ end
250
+ end
251
+
252
+ def load_warning_level
253
+ level = io.getbyte
254
+
255
+ case level
256
+ when 0
257
+ :default
258
+ when 1
259
+ :verbose
260
+ else
261
+ raise "Unknown level: #{level}"
262
+ end
263
+ end
264
+
241
265
  if RUBY_ENGINE == 'ruby'
242
266
  def load_node
243
267
  type = io.getbyte
@@ -259,7 +283,7 @@ module Prism
259
283
  when 7 then
260
284
  ArrayPatternNode.new(load_optional_node, Array.new(load_varuint) { load_node }, load_optional_node, Array.new(load_varuint) { load_node }, load_optional_location, load_optional_location, location)
261
285
  when 8 then
262
- AssocNode.new(load_node, load_optional_node, load_optional_location, location)
286
+ AssocNode.new(load_node, load_node, load_optional_location, location)
263
287
  when 9 then
264
288
  AssocSplatNode.new(load_optional_node, load_location, location)
265
289
  when 10 then
@@ -269,11 +293,11 @@ module Prism
269
293
  when 12 then
270
294
  BlockArgumentNode.new(load_optional_node, load_location, location)
271
295
  when 13 then
272
- BlockLocalVariableNode.new(load_required_constant, location)
296
+ BlockLocalVariableNode.new(load_varuint, load_required_constant, location)
273
297
  when 14 then
274
- BlockNode.new(Array.new(load_varuint) { load_required_constant }, load_varuint, load_optional_node, load_optional_node, load_location, load_location, location)
298
+ BlockNode.new(Array.new(load_varuint) { load_required_constant }, load_optional_node, load_optional_node, load_location, load_location, location)
275
299
  when 15 then
276
- BlockParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
300
+ BlockParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
277
301
  when 16 then
278
302
  BlockParametersNode.new(load_optional_node, Array.new(load_varuint) { load_node }, load_optional_location, load_optional_location, location)
279
303
  when 17 then
@@ -334,7 +358,7 @@ module Prism
334
358
  ConstantWriteNode.new(load_required_constant, load_location, load_node, load_location, location)
335
359
  when 45 then
336
360
  load_serialized_length
337
- DefNode.new(load_required_constant, load_location, load_optional_node, load_optional_node, load_optional_node, Array.new(load_varuint) { load_required_constant }, load_varuint, load_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, location)
361
+ DefNode.new(load_required_constant, load_location, load_optional_node, load_optional_node, load_optional_node, Array.new(load_varuint) { load_required_constant }, load_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, location)
338
362
  when 46 then
339
363
  DefinedNode.new(load_optional_location, load_node, load_optional_location, load_location, location)
340
364
  when 47 then
@@ -422,9 +446,9 @@ module Prism
422
446
  when 88 then
423
447
  KeywordHashNode.new(load_varuint, Array.new(load_varuint) { load_node }, location)
424
448
  when 89 then
425
- KeywordRestParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
449
+ KeywordRestParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
426
450
  when 90 then
427
- LambdaNode.new(Array.new(load_varuint) { load_required_constant }, load_varuint, load_location, load_location, load_location, load_optional_node, load_optional_node, location)
451
+ LambdaNode.new(Array.new(load_varuint) { load_required_constant }, load_location, load_location, load_location, load_optional_node, load_optional_node, location)
428
452
  when 91 then
429
453
  LocalVariableAndWriteNode.new(load_location, load_location, load_node, load_required_constant, load_varuint, location)
430
454
  when 92 then
@@ -464,9 +488,9 @@ module Prism
464
488
  when 109 then
465
489
  NumberedReferenceReadNode.new(load_varuint, location)
466
490
  when 110 then
467
- OptionalKeywordParameterNode.new(load_required_constant, load_location, load_node, location)
491
+ OptionalKeywordParameterNode.new(load_varuint, load_required_constant, load_location, load_node, location)
468
492
  when 111 then
469
- OptionalParameterNode.new(load_required_constant, load_location, load_location, load_node, location)
493
+ OptionalParameterNode.new(load_varuint, load_required_constant, load_location, load_location, load_node, location)
470
494
  when 112 then
471
495
  OrNode.new(load_node, load_node, load_location, location)
472
496
  when 113 then
@@ -492,15 +516,15 @@ module Prism
492
516
  when 123 then
493
517
  RegularExpressionNode.new(load_varuint, load_location, load_location, load_location, load_string, location)
494
518
  when 124 then
495
- RequiredKeywordParameterNode.new(load_required_constant, load_location, location)
519
+ RequiredKeywordParameterNode.new(load_varuint, load_required_constant, load_location, location)
496
520
  when 125 then
497
- RequiredParameterNode.new(load_required_constant, location)
521
+ RequiredParameterNode.new(load_varuint, load_required_constant, location)
498
522
  when 126 then
499
523
  RescueModifierNode.new(load_node, load_location, load_node, location)
500
524
  when 127 then
501
525
  RescueNode.new(load_location, Array.new(load_varuint) { load_node }, load_optional_location, load_optional_node, load_optional_node, load_optional_node, location)
502
526
  when 128 then
503
- RestParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
527
+ RestParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
504
528
  when 129 then
505
529
  RetryNode.new(location)
506
530
  when 130 then
@@ -582,7 +606,7 @@ module Prism
582
606
  },
583
607
  -> {
584
608
  location = load_location
585
- AssocNode.new(load_node, load_optional_node, load_optional_location, location)
609
+ AssocNode.new(load_node, load_node, load_optional_location, location)
586
610
  },
587
611
  -> {
588
612
  location = load_location
@@ -602,15 +626,15 @@ module Prism
602
626
  },
603
627
  -> {
604
628
  location = load_location
605
- BlockLocalVariableNode.new(load_required_constant, location)
629
+ BlockLocalVariableNode.new(load_varuint, load_required_constant, location)
606
630
  },
607
631
  -> {
608
632
  location = load_location
609
- BlockNode.new(Array.new(load_varuint) { load_required_constant }, load_varuint, load_optional_node, load_optional_node, load_location, load_location, location)
633
+ BlockNode.new(Array.new(load_varuint) { load_required_constant }, load_optional_node, load_optional_node, load_location, load_location, location)
610
634
  },
611
635
  -> {
612
636
  location = load_location
613
- BlockParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
637
+ BlockParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
614
638
  },
615
639
  -> {
616
640
  location = load_location
@@ -731,7 +755,7 @@ module Prism
731
755
  -> {
732
756
  location = load_location
733
757
  load_serialized_length
734
- DefNode.new(load_required_constant, load_location, load_optional_node, load_optional_node, load_optional_node, Array.new(load_varuint) { load_required_constant }, load_varuint, load_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, location)
758
+ DefNode.new(load_required_constant, load_location, load_optional_node, load_optional_node, load_optional_node, Array.new(load_varuint) { load_required_constant }, load_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, load_optional_location, location)
735
759
  },
736
760
  -> {
737
761
  location = load_location
@@ -907,11 +931,11 @@ module Prism
907
931
  },
908
932
  -> {
909
933
  location = load_location
910
- KeywordRestParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
934
+ KeywordRestParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
911
935
  },
912
936
  -> {
913
937
  location = load_location
914
- LambdaNode.new(Array.new(load_varuint) { load_required_constant }, load_varuint, load_location, load_location, load_location, load_optional_node, load_optional_node, location)
938
+ LambdaNode.new(Array.new(load_varuint) { load_required_constant }, load_location, load_location, load_location, load_optional_node, load_optional_node, location)
915
939
  },
916
940
  -> {
917
941
  location = load_location
@@ -991,11 +1015,11 @@ module Prism
991
1015
  },
992
1016
  -> {
993
1017
  location = load_location
994
- OptionalKeywordParameterNode.new(load_required_constant, load_location, load_node, location)
1018
+ OptionalKeywordParameterNode.new(load_varuint, load_required_constant, load_location, load_node, location)
995
1019
  },
996
1020
  -> {
997
1021
  location = load_location
998
- OptionalParameterNode.new(load_required_constant, load_location, load_location, load_node, location)
1022
+ OptionalParameterNode.new(load_varuint, load_required_constant, load_location, load_location, load_node, location)
999
1023
  },
1000
1024
  -> {
1001
1025
  location = load_location
@@ -1047,11 +1071,11 @@ module Prism
1047
1071
  },
1048
1072
  -> {
1049
1073
  location = load_location
1050
- RequiredKeywordParameterNode.new(load_required_constant, load_location, location)
1074
+ RequiredKeywordParameterNode.new(load_varuint, load_required_constant, load_location, location)
1051
1075
  },
1052
1076
  -> {
1053
1077
  location = load_location
1054
- RequiredParameterNode.new(load_required_constant, location)
1078
+ RequiredParameterNode.new(load_varuint, load_required_constant, location)
1055
1079
  },
1056
1080
  -> {
1057
1081
  location = load_location
@@ -1063,7 +1087,7 @@ module Prism
1063
1087
  },
1064
1088
  -> {
1065
1089
  location = load_location
1066
- RestParameterNode.new(load_optional_constant, load_optional_location, load_location, location)
1090
+ RestParameterNode.new(load_varuint, load_optional_constant, load_optional_location, load_location, location)
1067
1091
  },
1068
1092
  -> {
1069
1093
  location = load_location