prism 0.24.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +69 -1
  4. data/Makefile +22 -16
  5. data/README.md +45 -6
  6. data/config.yml +510 -4
  7. data/docs/build_system.md +31 -0
  8. data/docs/configuration.md +3 -0
  9. data/docs/cruby_compilation.md +1 -1
  10. data/docs/parser_translation.md +14 -9
  11. data/docs/releasing.md +7 -9
  12. data/docs/ripper_translation.md +50 -0
  13. data/docs/ruby_api.md +1 -0
  14. data/docs/serialization.md +26 -5
  15. data/ext/prism/api_node.c +911 -815
  16. data/ext/prism/api_pack.c +9 -0
  17. data/ext/prism/extconf.rb +34 -13
  18. data/ext/prism/extension.c +341 -68
  19. data/ext/prism/extension.h +5 -4
  20. data/include/prism/ast.h +213 -64
  21. data/include/prism/defines.h +106 -2
  22. data/include/prism/diagnostic.h +146 -72
  23. data/include/prism/encoding.h +22 -4
  24. data/include/prism/node.h +93 -0
  25. data/include/prism/options.h +82 -7
  26. data/include/prism/pack.h +11 -0
  27. data/include/prism/parser.h +203 -54
  28. data/include/prism/prettyprint.h +8 -0
  29. data/include/prism/static_literals.h +118 -0
  30. data/include/prism/util/pm_buffer.h +65 -2
  31. data/include/prism/util/pm_constant_pool.h +18 -1
  32. data/include/prism/util/pm_integer.h +119 -0
  33. data/include/prism/util/pm_list.h +1 -1
  34. data/include/prism/util/pm_newline_list.h +8 -0
  35. data/include/prism/util/pm_string.h +26 -2
  36. data/include/prism/version.h +2 -2
  37. data/include/prism.h +59 -1
  38. data/lib/prism/compiler.rb +8 -1
  39. data/lib/prism/debug.rb +46 -3
  40. data/lib/prism/desugar_compiler.rb +4 -2
  41. data/lib/prism/dispatcher.rb +29 -0
  42. data/lib/prism/dot_visitor.rb +87 -16
  43. data/lib/prism/dsl.rb +24 -12
  44. data/lib/prism/ffi.rb +77 -12
  45. data/lib/prism/lex_compat.rb +17 -15
  46. data/lib/prism/mutation_compiler.rb +11 -0
  47. data/lib/prism/node.rb +2112 -2499
  48. data/lib/prism/node_ext.rb +77 -29
  49. data/lib/prism/pack.rb +4 -0
  50. data/lib/prism/parse_result/comments.rb +34 -17
  51. data/lib/prism/parse_result/newlines.rb +3 -1
  52. data/lib/prism/parse_result.rb +83 -32
  53. data/lib/prism/pattern.rb +16 -4
  54. data/lib/prism/polyfill/string.rb +12 -0
  55. data/lib/prism/reflection.rb +421 -0
  56. data/lib/prism/serialize.rb +450 -102
  57. data/lib/prism/translation/parser/compiler.rb +189 -50
  58. data/lib/prism/translation/parser/lexer.rb +103 -22
  59. data/lib/prism/translation/parser/rubocop.rb +41 -13
  60. data/lib/prism/translation/parser.rb +119 -7
  61. data/lib/prism/translation/parser33.rb +1 -1
  62. data/lib/prism/translation/parser34.rb +1 -1
  63. data/lib/prism/translation/ripper/sexp.rb +125 -0
  64. data/lib/prism/translation/ripper/shim.rb +5 -0
  65. data/lib/prism/translation/ripper.rb +3212 -462
  66. data/lib/prism/translation/ruby_parser.rb +35 -18
  67. data/lib/prism/translation.rb +3 -1
  68. data/lib/prism/visitor.rb +10 -0
  69. data/lib/prism.rb +9 -18
  70. data/prism.gemspec +39 -6
  71. data/rbi/prism/compiler.rbi +14 -0
  72. data/rbi/prism/desugar_compiler.rbi +5 -0
  73. data/rbi/prism/mutation_compiler.rbi +5 -0
  74. data/rbi/prism/node.rbi +8674 -0
  75. data/rbi/prism/node_ext.rbi +102 -0
  76. data/rbi/prism/parse_result.rbi +307 -0
  77. data/rbi/prism/reflection.rbi +64 -0
  78. data/rbi/prism/translation/parser/compiler.rbi +13 -0
  79. data/rbi/prism/translation/parser.rbi +11 -0
  80. data/rbi/prism/translation/parser33.rbi +6 -0
  81. data/rbi/prism/translation/parser34.rbi +6 -0
  82. data/rbi/prism/translation/ripper/ripper_compiler.rbi +5 -0
  83. data/rbi/prism/translation/ripper.rbi +25 -0
  84. data/rbi/prism/translation/ruby_parser.rbi +11 -0
  85. data/rbi/prism/visitor.rbi +470 -0
  86. data/rbi/prism.rbi +38 -7748
  87. data/sig/prism/compiler.rbs +9 -0
  88. data/sig/prism/dispatcher.rbs +16 -0
  89. data/sig/prism/dot_visitor.rbs +6 -0
  90. data/sig/prism/dsl.rbs +462 -0
  91. data/sig/prism/mutation_compiler.rbs +158 -0
  92. data/sig/prism/node.rbs +3538 -0
  93. data/sig/prism/node_ext.rbs +78 -0
  94. data/sig/prism/pack.rbs +43 -0
  95. data/sig/prism/parse_result.rbs +128 -0
  96. data/sig/prism/pattern.rbs +13 -0
  97. data/sig/prism/reflection.rbs +56 -0
  98. data/sig/prism/serialize.rbs +7 -0
  99. data/sig/prism/visitor.rbs +168 -0
  100. data/sig/prism.rbs +188 -4767
  101. data/src/diagnostic.c +597 -230
  102. data/src/encoding.c +211 -108
  103. data/src/node.c +7526 -447
  104. data/src/options.c +66 -31
  105. data/src/pack.c +33 -17
  106. data/src/prettyprint.c +1294 -1385
  107. data/src/prism.c +4015 -1149
  108. data/src/regexp.c +17 -2
  109. data/src/serialize.c +47 -28
  110. data/src/static_literals.c +552 -0
  111. data/src/token_type.c +4 -3
  112. data/src/util/pm_buffer.c +147 -20
  113. data/src/util/pm_char.c +4 -4
  114. data/src/util/pm_constant_pool.c +35 -11
  115. data/src/util/pm_integer.c +635 -0
  116. data/src/util/pm_list.c +1 -1
  117. data/src/util/pm_newline_list.c +14 -5
  118. data/src/util/pm_string.c +134 -5
  119. data/src/util/pm_string_list.c +2 -2
  120. metadata +41 -8
  121. data/docs/ripper.md +0 -36
  122. data/include/prism/util/pm_state_stack.h +0 -42
  123. data/rbi/prism_static.rbi +0 -207
  124. data/sig/prism_static.rbs +0 -201
  125. data/src/util/pm_state_stack.c +0 -25
@@ -6,7 +6,7 @@ module Prism
6
6
  module Translation
7
7
  # This module is the entry-point for converting a prism syntax tree into the
8
8
  # seattlerb/ruby_parser gem's syntax tree.
9
- module RubyParser
9
+ class RubyParser
10
10
  # A prism visitor that builds Sexp objects.
11
11
  class Compiler < ::Prism::Compiler
12
12
  # This is the name of the file that we are compiling. We set it on every
@@ -192,19 +192,19 @@ module Prism
192
192
 
193
193
  if node.opening == "("
194
194
  result.line = node.opening_loc.start_line
195
- result.max_line = node.closing_loc.end_line
195
+ result.line_max = node.closing_loc.end_line
196
196
  shadow_loc = false
197
197
  end
198
198
 
199
199
  if node.locals.any?
200
200
  shadow = s(node, :shadow).concat(visit_all(node.locals))
201
201
  shadow.line = node.locals.first.location.start_line
202
- shadow.max_line = node.locals.last.location.end_line
202
+ shadow.line_max = node.locals.last.location.end_line
203
203
  result << shadow
204
204
 
205
205
  if shadow_loc
206
206
  result.line = shadow.line
207
- result.max_line = shadow.max_line
207
+ result.line_max = shadow.line_max
208
208
  end
209
209
  end
210
210
 
@@ -1274,6 +1274,11 @@ module Prism
1274
1274
  s(node, :self)
1275
1275
  end
1276
1276
 
1277
+ # A shareable constant.
1278
+ def visit_shareable_constant_node(node)
1279
+ visit(node.write)
1280
+ end
1281
+
1277
1282
  # class << self; end
1278
1283
  # ^^^^^^^^^^^^^^^^^^
1279
1284
  def visit_singleton_class_node(node)
@@ -1407,7 +1412,7 @@ module Prism
1407
1412
 
1408
1413
  if node.heredoc?
1409
1414
  result.line = node.content_loc.start_line
1410
- result.max_line = node.content_loc.end_line
1415
+ result.line_max = node.content_loc.end_line
1411
1416
  end
1412
1417
 
1413
1418
  result
@@ -1434,7 +1439,7 @@ module Prism
1434
1439
  result = Sexp.new(*arguments)
1435
1440
  result.file = file
1436
1441
  result.line = node.location.start_line
1437
- result.max_line = node.location.end_line
1442
+ result.line_max = node.location.end_line
1438
1443
  result
1439
1444
  end
1440
1445
 
@@ -1490,31 +1495,43 @@ module Prism
1490
1495
 
1491
1496
  private_constant :Compiler
1492
1497
 
1498
+ # Parse the given source and translate it into the seattlerb/ruby_parser
1499
+ # gem's Sexp format.
1500
+ def parse(source, filepath = "(string)")
1501
+ translate(Prism.parse(source), filepath)
1502
+ end
1503
+
1504
+ # Parse the given file and translate it into the seattlerb/ruby_parser
1505
+ # gem's Sexp format.
1506
+ def parse_file(filepath)
1507
+ translate(Prism.parse_file(filepath), filepath)
1508
+ end
1509
+
1493
1510
  class << self
1494
1511
  # Parse the given source and translate it into the seattlerb/ruby_parser
1495
1512
  # gem's Sexp format.
1496
1513
  def parse(source, filepath = "(string)")
1497
- translate(Prism.parse(source), filepath)
1514
+ new.parse(source, filepath)
1498
1515
  end
1499
1516
 
1500
1517
  # Parse the given file and translate it into the seattlerb/ruby_parser
1501
1518
  # gem's Sexp format.
1502
1519
  def parse_file(filepath)
1503
- translate(Prism.parse_file(filepath), filepath)
1520
+ new.parse_file(filepath)
1504
1521
  end
1522
+ end
1505
1523
 
1506
- private
1524
+ private
1507
1525
 
1508
- # Translate the given parse result and filepath into the
1509
- # seattlerb/ruby_parser gem's Sexp format.
1510
- def translate(result, filepath)
1511
- if result.failure?
1512
- error = result.errors.first
1513
- raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
1514
- end
1515
-
1516
- result.value.accept(Compiler.new(filepath))
1526
+ # Translate the given parse result and filepath into the
1527
+ # seattlerb/ruby_parser gem's Sexp format.
1528
+ def translate(result, filepath)
1529
+ if result.failure?
1530
+ error = result.errors.first
1531
+ raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
1517
1532
  end
1533
+
1534
+ result.value.accept(Compiler.new(filepath))
1518
1535
  end
1519
1536
  end
1520
1537
  end
@@ -3,8 +3,10 @@
3
3
  module Prism
4
4
  # This module is responsible for converting the prism syntax tree into other
5
5
  # syntax trees.
6
- module Translation
6
+ module Translation # steep:ignore
7
7
  autoload :Parser, "prism/translation/parser"
8
+ autoload :Parser33, "prism/translation/parser33"
9
+ autoload :Parser34, "prism/translation/parser34"
8
10
  autoload :Ripper, "prism/translation/ripper"
9
11
  autoload :RubyParser, "prism/translation/ruby_parser"
10
12
  end
data/lib/prism/visitor.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  =begin
3
4
  This file is generated by the templates/template.rb script and should not be
4
5
  modified manually. See templates/lib/prism/visitor.rb.erb
@@ -14,16 +15,19 @@ module Prism
14
15
  # Calls `accept` on the given node if it is not `nil`, which in turn should
15
16
  # call back into this visitor by calling the appropriate `visit_*` method.
16
17
  def visit(node)
18
+ # @type self: _Visitor
17
19
  node&.accept(self)
18
20
  end
19
21
 
20
22
  # Visits each node in `nodes` by calling `accept` on each one.
21
23
  def visit_all(nodes)
24
+ # @type self: _Visitor
22
25
  nodes.each { |node| node&.accept(self) }
23
26
  end
24
27
 
25
28
  # Visits the child nodes of `node` by calling `accept` on each one.
26
29
  def visit_child_nodes(node)
30
+ # @type self: _Visitor
27
31
  node.compact_child_nodes.each { |node| node.accept(self) }
28
32
  end
29
33
  end
@@ -309,6 +313,9 @@ module Prism
309
313
  # Visit a InterpolatedXStringNode node
310
314
  alias visit_interpolated_x_string_node visit_child_nodes
311
315
 
316
+ # Visit a ItParametersNode node
317
+ alias visit_it_parameters_node visit_child_nodes
318
+
312
319
  # Visit a KeywordHashNode node
313
320
  alias visit_keyword_hash_node visit_child_nodes
314
321
 
@@ -441,6 +448,9 @@ module Prism
441
448
  # Visit a SelfNode node
442
449
  alias visit_self_node visit_child_nodes
443
450
 
451
+ # Visit a ShareableConstantNode node
452
+ alias visit_shareable_constant_node visit_child_nodes
453
+
444
454
  # Visit a SingletonClassNode node
445
455
  alias visit_singleton_class_node visit_child_nodes
446
456
 
data/lib/prism.rb CHANGED
@@ -24,6 +24,7 @@ module Prism
24
24
  autoload :NodeInspector, "prism/node_inspector"
25
25
  autoload :Pack, "prism/pack"
26
26
  autoload :Pattern, "prism/pattern"
27
+ autoload :Reflection, "prism/reflection"
27
28
  autoload :Serialize, "prism/serialize"
28
29
  autoload :Translation, "prism/translation"
29
30
  autoload :Visitor, "prism/visitor"
@@ -44,7 +45,7 @@ module Prism
44
45
  #
45
46
  # For supported options, see Prism::parse.
46
47
  def self.lex_compat(source, **options)
47
- LexCompat.new(source, **options).result
48
+ LexCompat.new(source, **options).result # steep:ignore
48
49
  end
49
50
 
50
51
  # :call-seq:
@@ -54,7 +55,7 @@ module Prism
54
55
  # returns the same tokens. Raises SyntaxError if the syntax in source is
55
56
  # invalid.
56
57
  def self.lex_ripper(source)
57
- LexRipper.new(source).result
58
+ LexRipper.new(source).result # steep:ignore
58
59
  end
59
60
 
60
61
  # :call-seq:
@@ -64,22 +65,6 @@ module Prism
64
65
  def self.load(source, serialized)
65
66
  Serialize.load(source, serialized)
66
67
  end
67
-
68
- # :call-seq:
69
- # Prism::parse_failure?(source, **options) -> bool
70
- #
71
- # Returns true if the source parses with errors.
72
- def self.parse_failure?(source, **options)
73
- !parse_success?(source, **options)
74
- end
75
-
76
- # :call-seq:
77
- # Prism::parse_file_failure?(filepath, **options) -> bool
78
- #
79
- # Returns true if the file at filepath parses with errors.
80
- def self.parse_file_failure?(filepath, **options)
81
- !parse_file_success?(filepath, **options)
82
- end
83
68
  end
84
69
 
85
70
  require_relative "prism/node"
@@ -94,6 +79,12 @@ require_relative "prism/parse_result/newlines"
94
79
  # module that uses FFI to call into the library.
95
80
  if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
96
81
  require "prism/prism"
82
+
83
+ # The C extension is the default backend on CRuby.
84
+ Prism::BACKEND = :CEXT
97
85
  else
98
86
  require_relative "prism/ffi"
87
+
88
+ # The FFI backend is used on other Ruby implementations.
89
+ Prism::BACKEND = :FFI
99
90
  end
data/prism.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "prism"
5
- spec.version = "0.24.0"
5
+ spec.version = "0.26.0"
6
6
  spec.authors = ["Shopify"]
7
7
  spec.email = ["ruby@shopify.com"]
8
8
 
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  spec.require_paths = ["lib"]
16
16
  spec.files = [
17
+ "BSDmakefile",
17
18
  "CHANGELOG.md",
18
19
  "CODE_OF_CONDUCT.md",
19
20
  "CONTRIBUTING.md",
@@ -34,7 +35,7 @@ Gem::Specification.new do |spec|
34
35
  "docs/parser_translation.md",
35
36
  "docs/parsing_rules.md",
36
37
  "docs/releasing.md",
37
- "docs/ripper.md",
38
+ "docs/ripper_translation.md",
38
39
  "docs/ruby_api.md",
39
40
  "docs/ruby_parser_translation.md",
40
41
  "docs/serialization.md",
@@ -54,13 +55,14 @@ Gem::Specification.new do |spec|
54
55
  "include/prism/parser.h",
55
56
  "include/prism/prettyprint.h",
56
57
  "include/prism/regexp.h",
58
+ "include/prism/static_literals.h",
57
59
  "include/prism/util/pm_buffer.h",
58
60
  "include/prism/util/pm_char.h",
59
61
  "include/prism/util/pm_constant_pool.h",
62
+ "include/prism/util/pm_integer.h",
60
63
  "include/prism/util/pm_list.h",
61
64
  "include/prism/util/pm_memchr.h",
62
65
  "include/prism/util/pm_newline_list.h",
63
- "include/prism/util/pm_state_stack.h",
64
66
  "include/prism/util/pm_strncasecmp.h",
65
67
  "include/prism/util/pm_string.h",
66
68
  "include/prism/util/pm_string_list.h",
@@ -84,6 +86,8 @@ Gem::Specification.new do |spec|
84
86
  "lib/prism/parse_result/comments.rb",
85
87
  "lib/prism/parse_result/newlines.rb",
86
88
  "lib/prism/pattern.rb",
89
+ "lib/prism/polyfill/string.rb",
90
+ "lib/prism/reflection.rb",
87
91
  "lib/prism/serialize.rb",
88
92
  "lib/prism/translation.rb",
89
93
  "lib/prism/translation/parser.rb",
@@ -93,6 +97,8 @@ Gem::Specification.new do |spec|
93
97
  "lib/prism/translation/parser/lexer.rb",
94
98
  "lib/prism/translation/parser/rubocop.rb",
95
99
  "lib/prism/translation/ripper.rb",
100
+ "lib/prism/translation/ripper/sexp.rb",
101
+ "lib/prism/translation/ripper/shim.rb",
96
102
  "lib/prism/translation/ruby_parser.rb",
97
103
  "lib/prism/visitor.rb",
98
104
  "src/diagnostic.c",
@@ -102,14 +108,15 @@ Gem::Specification.new do |spec|
102
108
  "src/prettyprint.c",
103
109
  "src/regexp.c",
104
110
  "src/serialize.c",
111
+ "src/static_literals.c",
105
112
  "src/token_type.c",
106
113
  "src/util/pm_buffer.c",
107
114
  "src/util/pm_char.c",
108
115
  "src/util/pm_constant_pool.c",
116
+ "src/util/pm_integer.c",
109
117
  "src/util/pm_list.c",
110
118
  "src/util/pm_memchr.c",
111
119
  "src/util/pm_newline_list.c",
112
- "src/util/pm_state_stack.c",
113
120
  "src/util/pm_string.c",
114
121
  "src/util/pm_string_list.c",
115
122
  "src/util/pm_strncasecmp.c",
@@ -118,9 +125,35 @@ Gem::Specification.new do |spec|
118
125
  "src/prism.c",
119
126
  "prism.gemspec",
120
127
  "sig/prism.rbs",
121
- "sig/prism_static.rbs",
128
+ "sig/prism/compiler.rbs",
129
+ "sig/prism/dispatcher.rbs",
130
+ "sig/prism/dot_visitor.rbs",
131
+ "sig/prism/dsl.rbs",
132
+ "sig/prism/mutation_compiler.rbs",
133
+ "sig/prism/node.rbs",
134
+ "sig/prism/node_ext.rbs",
135
+ "sig/prism/pack.rbs",
136
+ "sig/prism/parse_result.rbs",
137
+ "sig/prism/pattern.rbs",
138
+ "sig/prism/reflection.rbs",
139
+ "sig/prism/serialize.rbs",
140
+ "sig/prism/visitor.rbs",
122
141
  "rbi/prism.rbi",
123
- "rbi/prism_static.rbi"
142
+ "rbi/prism/compiler.rbi",
143
+ "rbi/prism/desugar_compiler.rbi",
144
+ "rbi/prism/mutation_compiler.rbi",
145
+ "rbi/prism/node_ext.rbi",
146
+ "rbi/prism/node.rbi",
147
+ "rbi/prism/parse_result.rbi",
148
+ "rbi/prism/reflection.rbi",
149
+ "rbi/prism/translation/parser.rbi",
150
+ "rbi/prism/translation/parser/compiler.rbi",
151
+ "rbi/prism/translation/parser33.rbi",
152
+ "rbi/prism/translation/parser34.rbi",
153
+ "rbi/prism/translation/ripper.rbi",
154
+ "rbi/prism/translation/ripper/ripper_compiler.rbi",
155
+ "rbi/prism/translation/ruby_parser.rbi",
156
+ "rbi/prism/visitor.rbi"
124
157
  ]
125
158
 
126
159
  spec.extensions = ["ext/prism/extconf.rb"]
@@ -0,0 +1,14 @@
1
+ # typed: strict
2
+
3
+ class Prism::Compiler
4
+ Result = type_member
5
+
6
+ sig { params(node: T.nilable(Prism::Node)).returns(T.nilable(Result)) }
7
+ def visit(node); end
8
+
9
+ sig { params(nodes: T::Array[T.nilable(Prism::Node)]).returns(T::Array[T.nilable(Result)]) }
10
+ def visit_all(nodes); end
11
+
12
+ sig { params(node: Prism::Node).returns(T::Array[T.nilable(Result)]) }
13
+ def visit_child_nodes(node); end
14
+ end
@@ -0,0 +1,5 @@
1
+ # typed: strict
2
+
3
+ class Prism::DesugarCompiler < Prism::MutationCompiler
4
+ Result = type_member { { fixed: Prism::Node } }
5
+ end
@@ -0,0 +1,5 @@
1
+ # typed: strict
2
+
3
+ class Prism::MutationCompiler < Prism::Compiler
4
+ Result = type_member
5
+ end