herb 0.11.1 → 0.11.2
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.
- checksums.yaml +4 -4
- data/Rakefile +4 -2
- data/config.yml +6 -0
- data/ext/herb/extconf.rb +8 -31
- data/ext/herb/nodes.c +75 -75
- data/ext/herb/parser_options_helpers.c +26 -0
- data/lib/herb/analysis/partial_index.rb +1 -1
- data/lib/herb/analysis/partial_resolution.rb +27 -0
- data/lib/herb/ast/helpers.rb +1 -1
- data/lib/herb/configuration.rb +11 -1
- data/lib/herb/dev/classifier.rb +1 -1
- data/lib/herb/engine/compiler.rb +5 -8
- data/lib/herb/engine/slots/dynamics_compiler.rb +53 -14
- data/lib/herb/engine/slots/subtree_compiler.rb +1 -1
- data/lib/herb/engine/slots/visitor.rb +32 -4
- data/lib/herb/engine/visitors/instrumentation_visitor.rb +18 -6
- data/lib/herb/parser_options.rb +6 -3
- data/lib/herb/project.rb +19 -8
- data/lib/herb/version.rb +1 -1
- data/lib/herb.rb +1 -1
- data/sig/herb/analysis/partial_resolution.rbs +8 -0
- data/sig/herb/configuration.rbs +3 -0
- data/sig/herb/engine/slots/dynamics_compiler.rbs +24 -7
- data/sig/herb/engine/slots/visitor.rbs +14 -0
- data/sig/herb/engine/visitors/instrumentation_visitor.rbs +5 -1
- data/sig/herb/parser_options.rbs +6 -4
- data/sig/herb.rbs +2 -2
- data/src/analyze/action_view/attribute_extraction_helpers.c +40 -21
- data/src/analyze/action_view/generated_handlers.c +36 -36
- data/src/analyze/action_view/image_tag.c +7 -3
- data/src/analyze/action_view/javascript_include_tag.c +6 -3
- data/src/analyze/action_view/link_to.c +23 -23
- data/src/analyze/action_view/registry.c +7 -3
- data/src/analyze/action_view/tag_helper_node_builders.c +6 -13
- data/src/analyze/action_view/tag_helpers.c +139 -90
- data/src/analyze/action_view/turbo_frame_tag.c +5 -4
- data/src/analyze/analyze.c +35 -19
- data/src/analyze/analyze_helpers.c +161 -188
- data/src/analyze/analyzed_ruby.c +8 -7
- data/src/analyze/control_type.c +29 -22
- data/src/analyze/herb_directives.c +16 -20
- data/src/analyze/iteration_nodes.c +69 -24
- data/src/analyze/parse_errors.c +32 -22
- data/src/analyze/postfix_conditionals.c +23 -13
- data/src/analyze/prism_annotate.c +29 -28
- data/src/analyze/render_nodes.c +51 -41
- data/src/analyze/strict_locals.c +14 -15
- data/src/analyze/ternary_conditionals.c +14 -10
- data/src/herb.c +10 -8
- data/src/include/analyze/action_view/tag_helper_handler.h +1 -1
- data/src/include/analyze/action_view/tag_helper_node_builders.h +1 -0
- data/src/include/analyze/action_view/tag_helpers.h +1 -1
- data/src/include/analyze/analyze.h +3 -2
- data/src/include/analyze/analyzed_ruby.h +4 -2
- data/src/include/analyze/helpers.h +6 -6
- data/src/include/herb.h +3 -2
- data/src/include/parser/parser_options.h +5 -0
- data/src/include/prism/prism_context.h +12 -12
- data/src/include/prism/prism_helpers.h +30 -0
- data/src/include/prism_version.h +11 -0
- data/src/include/version.h +1 -1
- data/src/parser/parser_helpers.c +1 -2
- data/src/parser.c +6 -7
- data/src/parser_options_defaults.c +1 -0
- data/src/prism/prism_helpers.c +84 -50
- data/src/prism/ruby_parser.c +20 -20
- data/templates/ext/herb/nodes.c.erb +5 -5
- data/templates/ext/herb/parser_options_helpers.c.erb +33 -0
- data/templates/java/nodes.c.erb +6 -6
- data/templates/java/parser_options_helpers.c.erb +41 -2
- data/templates/java/parser_options_helpers.h.erb +2 -1
- data/templates/javascript/packages/node/extension/nodes.cpp.erb +5 -5
- data/templates/javascript/packages/node/extension/parser_options_helpers.cpp.erb +34 -2
- data/templates/javascript/packages/node/extension/parser_options_helpers.h.erb +1 -1
- data/templates/lib/herb/parser_options.rb.erb +1 -1
- data/templates/rust/src/prism/generated_deserialize.rs.erb +90 -44
- data/templates/rust/src/prism/generated_prettyprint.rs.erb +18 -27
- data/templates/src/analyze/action_view/generated_handlers.c.erb +23 -22
- data/templates/src/include/parser/parser_options.h.erb +10 -0
- data/templates/src/include/prism_version.h.erb +17 -0
- data/templates/src/parser_options_defaults.c.erb +1 -1
- data/templates/template.rb +17 -2
- data/templates/wasm/nodes.cpp.erb +5 -5
- data/templates/wasm/parser_options_helpers.cpp.erb +24 -0
- data/vendor/prism/Rakefile +76 -7
- data/vendor/prism/config.yml +174 -161
- data/vendor/prism/include/prism/arena.h +45 -0
- data/vendor/prism/include/prism/arena.hpp +42 -0
- data/vendor/prism/include/prism/ast.h +2738 -611
- data/vendor/prism/include/prism/ast.hpp +641 -0
- data/vendor/prism/include/prism/buffer.h +60 -0
- data/vendor/prism/include/prism/buffer.hpp +55 -0
- data/vendor/prism/include/prism/comments.h +72 -0
- data/vendor/prism/include/prism/compiler/accel.h +26 -0
- data/vendor/prism/include/prism/compiler/align.h +36 -0
- data/vendor/prism/include/prism/compiler/assume.h +25 -0
- data/vendor/prism/include/prism/compiler/branch.h +26 -0
- data/vendor/prism/include/prism/compiler/bswap.h +46 -0
- data/vendor/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/vendor/prism/include/prism/compiler/endianness.h +34 -0
- data/vendor/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/vendor/prism/include/prism/compiler/exported.h +24 -0
- data/vendor/prism/include/prism/compiler/fallthrough.h +22 -0
- data/vendor/prism/include/prism/compiler/filesystem.h +32 -0
- data/vendor/prism/include/prism/compiler/flex_array.h +19 -0
- data/vendor/prism/include/prism/compiler/force_inline.h +21 -0
- data/vendor/prism/include/prism/compiler/format.h +27 -0
- data/vendor/prism/include/prism/compiler/inline.h +17 -0
- data/vendor/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/vendor/prism/include/prism/compiler/nodiscard.h +22 -0
- data/vendor/prism/include/prism/compiler/nonnull.h +18 -0
- data/vendor/prism/include/prism/compiler/unused.h +18 -0
- data/vendor/prism/include/prism/constant_pool.h +106 -0
- data/vendor/prism/include/prism/diagnostic.h +55 -402
- data/vendor/prism/include/prism/errors_format.h +61 -0
- data/vendor/prism/include/prism/excludes.h +29 -0
- data/vendor/prism/include/prism/integer.h +41 -0
- data/vendor/prism/include/prism/internal/allocator.h +68 -0
- data/vendor/prism/include/prism/internal/allocator_debug.h +88 -0
- data/vendor/prism/include/prism/internal/arena.h +108 -0
- data/vendor/prism/include/prism/internal/bit.h +42 -0
- data/vendor/prism/include/prism/internal/buffer.h +94 -0
- data/vendor/prism/include/prism/internal/char.h +145 -0
- data/vendor/prism/include/prism/internal/comments.h +54 -0
- data/vendor/prism/include/prism/internal/constant_pool.h +152 -0
- data/vendor/prism/include/prism/internal/diagnostic.h +389 -0
- data/vendor/prism/include/prism/{encoding.h → internal/encoding.h} +27 -68
- data/vendor/prism/include/prism/internal/integer.h +68 -0
- data/vendor/prism/include/prism/internal/isinf.h +16 -0
- data/vendor/prism/include/prism/internal/line_offset_list.h +34 -0
- data/vendor/prism/include/prism/{util/pm_list.h → internal/list.h} +11 -52
- data/vendor/prism/include/prism/internal/magic_comments.h +23 -0
- data/vendor/prism/include/prism/internal/memchr.h +15 -0
- data/vendor/prism/include/prism/internal/node.h +42 -0
- data/vendor/prism/include/prism/internal/options.h +232 -0
- data/vendor/prism/include/prism/internal/parser.h +1015 -0
- data/vendor/prism/include/prism/internal/regexp.h +26 -0
- data/vendor/prism/include/prism/internal/serialize.h +39 -0
- data/vendor/prism/include/prism/internal/source.h +72 -0
- data/vendor/prism/include/prism/internal/static_literals.h +60 -0
- data/vendor/prism/include/prism/internal/stringy.h +30 -0
- data/vendor/prism/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
- data/vendor/prism/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
- data/vendor/prism/include/prism/internal/tokens.h +11 -0
- data/vendor/prism/include/prism/internal/xxh3.h +16 -0
- data/vendor/prism/include/prism/json.h +40 -0
- data/vendor/prism/include/prism/line_offset_list.h +69 -0
- data/vendor/prism/include/prism/magic_comments.h +53 -0
- data/vendor/prism/include/prism/node.h +59 -49
- data/vendor/prism/include/prism/node.hpp +272 -0
- data/vendor/prism/include/prism/node_type.hpp +38 -0
- data/vendor/prism/include/prism/options.h +154 -279
- data/vendor/prism/include/prism/options.hpp +43 -0
- data/vendor/prism/include/prism/parser.h +367 -870
- data/vendor/prism/include/prism/parser.hpp +274 -0
- data/vendor/prism/include/prism/prettyprint.h +14 -9
- data/vendor/prism/include/prism/serialize.h +123 -0
- data/vendor/prism/include/prism/source.h +163 -0
- data/vendor/prism/include/prism/stream.h +36 -0
- data/vendor/prism/include/prism/string_query.h +82 -0
- data/vendor/prism/include/prism/stringy.h +61 -0
- data/vendor/prism/include/prism/stringy.hpp +31 -0
- data/vendor/prism/include/prism/version.h +20 -3
- data/vendor/prism/include/prism.h +35 -320
- data/vendor/prism/include/prism.hpp +24 -0
- data/vendor/prism/src/arena.c +122 -0
- data/vendor/prism/src/{util/pm_buffer.c → buffer.c} +53 -21
- data/vendor/prism/src/{util/pm_char.c → char.c} +28 -57
- data/vendor/prism/src/comments.c +455 -0
- data/vendor/prism/src/constant_pool.c +406 -0
- data/vendor/prism/src/diagnostic.c +120 -68
- data/vendor/prism/src/encoding.c +8 -3
- data/vendor/prism/src/errors_format.c +519 -0
- data/vendor/prism/src/{util/pm_integer.c → integer.c} +38 -22
- data/vendor/prism/src/json.c +5733 -0
- data/vendor/prism/src/line_offset_list.c +100 -0
- data/vendor/prism/src/list.c +24 -0
- data/vendor/prism/src/{util/pm_memchr.c → memchr.c} +8 -6
- data/vendor/prism/src/node.c +3465 -7787
- data/vendor/prism/src/options.c +161 -54
- data/vendor/prism/src/parser.c +371 -0
- data/vendor/prism/src/prettyprint.c +421 -354
- data/vendor/prism/src/prism.c +8522 -7280
- data/vendor/prism/src/regexp.c +960 -45
- data/vendor/prism/src/serialize.c +2434 -2048
- data/vendor/prism/src/source.c +524 -0
- data/vendor/prism/src/static_literals.c +303 -360
- data/vendor/prism/src/string_query.c +180 -0
- data/vendor/prism/src/stringy.c +61 -0
- data/vendor/prism/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
- data/vendor/prism/src/strpbrk.c +447 -0
- data/vendor/prism/src/{token_type.c → tokens.c} +39 -18
- data/vendor/prism/src/xxh3.c +292 -0
- data/vendor/prism/templates/ext/prism/api_node.c.erb +58 -44
- data/vendor/prism/templates/include/prism/ast.h.erb +67 -17
- data/vendor/prism/templates/include/prism/ast.hpp.erb +31 -0
- data/vendor/prism/templates/include/prism/internal/diagnostic.h.erb +60 -0
- data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/AbstractNodeVisitor.java.erb +1 -1
- data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Loader.java.erb +232 -133
- data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Nodes.java.erb +62 -23
- data/vendor/prism/templates/javascript/src/deserialize.js.erb +153 -146
- data/vendor/prism/templates/javascript/src/nodes.js.erb +4 -9
- data/vendor/prism/templates/lib/prism/compiler.rb.erb +14 -5
- data/vendor/prism/templates/lib/prism/dispatcher.rb.erb +20 -12
- data/vendor/prism/templates/lib/prism/dot_visitor.rb.erb +37 -11
- data/vendor/prism/templates/lib/prism/dsl.rb.erb +43 -4
- data/vendor/prism/templates/lib/prism/inspect_visitor.rb.erb +26 -10
- data/vendor/prism/templates/lib/prism/mutation_compiler.rb.erb +5 -2
- data/vendor/prism/templates/lib/prism/node.rb.erb +283 -105
- data/vendor/prism/templates/lib/prism/reflection.rb.erb +11 -2
- data/vendor/prism/templates/lib/prism/serialize.rb.erb +291 -134
- data/vendor/prism/templates/lib/prism/visitor.rb.erb +18 -0
- data/vendor/prism/templates/src/diagnostic.c.erb +114 -63
- data/vendor/prism/templates/src/json.c.erb +130 -0
- data/vendor/prism/templates/src/node.c.erb +79 -236
- data/vendor/prism/templates/src/prettyprint.c.erb +24 -13
- data/vendor/prism/templates/src/serialize.c.erb +369 -183
- data/vendor/prism/templates/src/{token_type.c.erb → tokens.c.erb} +22 -11
- data/vendor/prism/templates/template.rb +193 -91
- metadata +100 -42
- data/vendor/prism/include/prism/defines.h +0 -260
- data/vendor/prism/include/prism/pack.h +0 -163
- data/vendor/prism/include/prism/regexp.h +0 -43
- data/vendor/prism/include/prism/static_literals.h +0 -121
- data/vendor/prism/include/prism/util/pm_buffer.h +0 -236
- data/vendor/prism/include/prism/util/pm_char.h +0 -204
- data/vendor/prism/include/prism/util/pm_constant_pool.h +0 -218
- data/vendor/prism/include/prism/util/pm_integer.h +0 -130
- data/vendor/prism/include/prism/util/pm_memchr.h +0 -29
- data/vendor/prism/include/prism/util/pm_newline_list.h +0 -113
- data/vendor/prism/include/prism/util/pm_string.h +0 -200
- data/vendor/prism/src/pack.c +0 -509
- data/vendor/prism/src/util/pm_constant_pool.c +0 -342
- data/vendor/prism/src/util/pm_list.c +0 -49
- data/vendor/prism/src/util/pm_newline_list.c +0 -125
- data/vendor/prism/src/util/pm_string.c +0 -381
- data/vendor/prism/src/util/pm_strpbrk.c +0 -206
- data/vendor/prism/templates/include/prism/diagnostic.h.erb +0 -130
- data/vendor/prism/templates/rbi/prism/dsl.rbi.erb +0 -68
- data/vendor/prism/templates/rbi/prism/node.rbi.erb +0 -167
- data/vendor/prism/templates/rbi/prism/visitor.rbi.erb +0 -18
- data/vendor/prism/templates/sig/prism/_private/dot_visitor.rbs.erb +0 -45
- data/vendor/prism/templates/sig/prism/dsl.rbs.erb +0 -31
- data/vendor/prism/templates/sig/prism/mutation_compiler.rbs.erb +0 -7
- data/vendor/prism/templates/sig/prism/node.rbs.erb +0 -136
- data/vendor/prism/templates/sig/prism/visitor.rbs.erb +0 -17
- data/vendor/prism/templates/sig/prism.rbs.erb +0 -92
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
1
4
|
require "stringio"
|
|
2
5
|
require_relative "polyfill/unpack1"
|
|
3
6
|
|
|
4
7
|
module Prism
|
|
5
8
|
# A module responsible for deserializing parse results.
|
|
6
|
-
module Serialize
|
|
9
|
+
module Serialize # :nodoc:
|
|
7
10
|
# The major version of prism that we are expecting to find in the serialized
|
|
8
11
|
# strings.
|
|
9
|
-
MAJOR_VERSION =
|
|
12
|
+
MAJOR_VERSION = 2
|
|
10
13
|
|
|
11
14
|
# The minor version of prism that we are expecting to find in the serialized
|
|
12
15
|
# strings.
|
|
13
|
-
MINOR_VERSION =
|
|
16
|
+
MINOR_VERSION = 1
|
|
14
17
|
|
|
15
18
|
# The patch version of prism that we are expecting to find in the serialized
|
|
16
19
|
# strings.
|
|
@@ -20,34 +23,40 @@ module Prism
|
|
|
20
23
|
#
|
|
21
24
|
# The formatting of the source of this method is purposeful to illustrate
|
|
22
25
|
# the structure of the serialized data.
|
|
26
|
+
#
|
|
27
|
+
#: (String input, String serialized, bool freeze) -> ParseResult
|
|
23
28
|
def self.load_parse(input, serialized, freeze)
|
|
24
29
|
input = input.dup
|
|
25
|
-
source = Source.for(input)
|
|
30
|
+
source = Source.for(input, 1, [])
|
|
26
31
|
loader = Loader.new(source, serialized)
|
|
27
32
|
|
|
28
33
|
loader.load_header
|
|
29
34
|
encoding = loader.load_encoding
|
|
30
35
|
start_line = loader.load_varsint
|
|
36
|
+
checksum = loader.load_uint64
|
|
31
37
|
offsets = loader.load_line_offsets(freeze)
|
|
32
38
|
|
|
33
39
|
source.replace_start_line(start_line)
|
|
40
|
+
source.replace_checksum(checksum)
|
|
34
41
|
source.replace_offsets(offsets)
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
loader.load_comments(source.comments, freeze)
|
|
44
|
+
loader.load_comment_attachments(source, freeze)
|
|
37
45
|
magic_comments = loader.load_magic_comments(freeze)
|
|
38
46
|
data_loc = loader.load_optional_location_object(freeze)
|
|
39
47
|
errors = loader.load_errors(encoding, freeze)
|
|
40
48
|
warnings = loader.load_warnings(encoding, freeze)
|
|
49
|
+
continuable = loader.load_bool
|
|
41
50
|
cpool_base = loader.load_uint32
|
|
42
51
|
cpool_size = loader.load_varuint
|
|
43
52
|
|
|
44
|
-
constant_pool = ConstantPool.new(
|
|
53
|
+
constant_pool = ConstantPool.new(serialized, cpool_base, cpool_size)
|
|
45
54
|
|
|
46
|
-
node = loader.
|
|
55
|
+
node = loader.load_nodes(constant_pool, encoding, freeze, cpool_base) #: ProgramNode
|
|
47
56
|
loader.load_constant_pool(constant_pool)
|
|
48
57
|
raise unless loader.eof?
|
|
49
58
|
|
|
50
|
-
result = ParseResult.new(node,
|
|
59
|
+
result = ParseResult.new(node, magic_comments, data_loc, errors, warnings, continuable, source)
|
|
51
60
|
result.freeze if freeze
|
|
52
61
|
|
|
53
62
|
input.force_encoding(encoding)
|
|
@@ -73,26 +82,32 @@ module Prism
|
|
|
73
82
|
#
|
|
74
83
|
# The formatting of the source of this method is purposeful to illustrate
|
|
75
84
|
# the structure of the serialized data.
|
|
85
|
+
#
|
|
86
|
+
#: (String input, String serialized, bool freeze) -> LexResult
|
|
76
87
|
def self.load_lex(input, serialized, freeze)
|
|
77
|
-
source = Source.for(input)
|
|
88
|
+
source = Source.for(input, 1, [])
|
|
78
89
|
loader = Loader.new(source, serialized)
|
|
79
90
|
|
|
80
91
|
tokens = loader.load_tokens
|
|
81
92
|
encoding = loader.load_encoding
|
|
82
93
|
start_line = loader.load_varsint
|
|
94
|
+
checksum = loader.load_uint64
|
|
83
95
|
offsets = loader.load_line_offsets(freeze)
|
|
84
96
|
|
|
85
97
|
source.replace_start_line(start_line)
|
|
98
|
+
source.replace_checksum(checksum)
|
|
86
99
|
source.replace_offsets(offsets)
|
|
87
100
|
|
|
88
|
-
|
|
101
|
+
loader.load_comments(source.comments, freeze)
|
|
102
|
+
loader.load_comment_attachments(source, freeze)
|
|
89
103
|
magic_comments = loader.load_magic_comments(freeze)
|
|
90
104
|
data_loc = loader.load_optional_location_object(freeze)
|
|
91
105
|
errors = loader.load_errors(encoding, freeze)
|
|
92
106
|
warnings = loader.load_warnings(encoding, freeze)
|
|
107
|
+
continuable = loader.load_bool
|
|
93
108
|
raise unless loader.eof?
|
|
94
109
|
|
|
95
|
-
result = LexResult.new(tokens,
|
|
110
|
+
result = LexResult.new(tokens, magic_comments, data_loc, errors, warnings, continuable, source)
|
|
96
111
|
|
|
97
112
|
tokens.each do |token|
|
|
98
113
|
token[0].value.force_encoding(encoding)
|
|
@@ -117,18 +132,24 @@ module Prism
|
|
|
117
132
|
#
|
|
118
133
|
# The formatting of the source of this method is purposeful to illustrate
|
|
119
134
|
# the structure of the serialized data.
|
|
135
|
+
#
|
|
136
|
+
#: (String input, String serialized, bool freeze) -> Array[Comment]
|
|
120
137
|
def self.load_parse_comments(input, serialized, freeze)
|
|
121
|
-
source = Source.for(input)
|
|
138
|
+
source = Source.for(input, 1, [])
|
|
122
139
|
loader = Loader.new(source, serialized)
|
|
123
140
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
start_line =
|
|
141
|
+
loader.load_header
|
|
142
|
+
loader.load_encoding
|
|
143
|
+
start_line = loader.load_varsint
|
|
144
|
+
checksum = loader.load_uint64
|
|
145
|
+
offsets = loader.load_line_offsets(freeze)
|
|
127
146
|
|
|
128
147
|
source.replace_start_line(start_line)
|
|
148
|
+
source.replace_checksum(checksum)
|
|
149
|
+
source.replace_offsets(offsets)
|
|
129
150
|
|
|
130
|
-
result =
|
|
131
|
-
raise unless
|
|
151
|
+
result = loader.load_comments(source.comments, freeze)
|
|
152
|
+
raise unless loader.eof?
|
|
132
153
|
|
|
133
154
|
source.deep_freeze if freeze
|
|
134
155
|
result
|
|
@@ -139,35 +160,41 @@ module Prism
|
|
|
139
160
|
#
|
|
140
161
|
# The formatting of the source of this method is purposeful to illustrate
|
|
141
162
|
# the structure of the serialized data.
|
|
163
|
+
#
|
|
164
|
+
#: (String input, String serialized, bool freeze) -> ParseLexResult
|
|
142
165
|
def self.load_parse_lex(input, serialized, freeze)
|
|
143
|
-
source = Source.for(input)
|
|
166
|
+
source = Source.for(input, 1, [])
|
|
144
167
|
loader = Loader.new(source, serialized)
|
|
145
168
|
|
|
146
169
|
tokens = loader.load_tokens
|
|
147
170
|
loader.load_header
|
|
148
171
|
encoding = loader.load_encoding
|
|
149
172
|
start_line = loader.load_varsint
|
|
173
|
+
checksum = loader.load_uint64
|
|
150
174
|
offsets = loader.load_line_offsets(freeze)
|
|
151
175
|
|
|
152
176
|
source.replace_start_line(start_line)
|
|
177
|
+
source.replace_checksum(checksum)
|
|
153
178
|
source.replace_offsets(offsets)
|
|
154
179
|
|
|
155
|
-
|
|
180
|
+
loader.load_comments(source.comments, freeze)
|
|
181
|
+
loader.load_comment_attachments(source, freeze)
|
|
156
182
|
magic_comments = loader.load_magic_comments(freeze)
|
|
157
183
|
data_loc = loader.load_optional_location_object(freeze)
|
|
158
184
|
errors = loader.load_errors(encoding, freeze)
|
|
159
185
|
warnings = loader.load_warnings(encoding, freeze)
|
|
186
|
+
continuable = loader.load_bool
|
|
160
187
|
cpool_base = loader.load_uint32
|
|
161
188
|
cpool_size = loader.load_varuint
|
|
162
189
|
|
|
163
|
-
constant_pool = ConstantPool.new(
|
|
190
|
+
constant_pool = ConstantPool.new(serialized, cpool_base, cpool_size)
|
|
164
191
|
|
|
165
|
-
node = loader.
|
|
192
|
+
node = loader.load_nodes(constant_pool, encoding, freeze, cpool_base) #: ProgramNode
|
|
166
193
|
loader.load_constant_pool(constant_pool)
|
|
167
194
|
raise unless loader.eof?
|
|
168
195
|
|
|
169
|
-
value = [node, tokens]
|
|
170
|
-
result = ParseLexResult.new(value,
|
|
196
|
+
value = [node, tokens] #: [ProgramNode, Array[Token]]
|
|
197
|
+
result = ParseLexResult.new(value, magic_comments, data_loc, errors, warnings, continuable, source)
|
|
171
198
|
|
|
172
199
|
tokens.each do |token|
|
|
173
200
|
token[0].value.force_encoding(encoding)
|
|
@@ -189,34 +216,36 @@ module Prism
|
|
|
189
216
|
end
|
|
190
217
|
|
|
191
218
|
class ConstantPool # :nodoc:
|
|
192
|
-
attr_reader :size
|
|
219
|
+
attr_reader :size #: Integer
|
|
220
|
+
|
|
221
|
+
# @rbs @serialized: String
|
|
222
|
+
# @rbs @base: Integer
|
|
223
|
+
# @rbs @pool: Array[Symbol?]
|
|
193
224
|
|
|
194
|
-
|
|
195
|
-
|
|
225
|
+
#: (String serialized, Integer base, Integer size) -> void
|
|
226
|
+
def initialize(serialized, base, size)
|
|
196
227
|
@serialized = serialized
|
|
197
228
|
@base = base
|
|
198
229
|
@size = size
|
|
199
230
|
@pool = Array.new(size, nil)
|
|
200
231
|
end
|
|
201
232
|
|
|
233
|
+
#: (Integer index, Encoding encoding) -> Symbol
|
|
202
234
|
def get(index, encoding)
|
|
203
235
|
@pool[index] ||=
|
|
204
236
|
begin
|
|
205
237
|
offset = @base + index * 8
|
|
206
|
-
start = @serialized.unpack1("L", offset: offset)
|
|
207
|
-
length = @serialized.unpack1("L", offset: offset + 4)
|
|
238
|
+
start = @serialized.unpack1("L", offset: offset) #: Integer
|
|
239
|
+
length = @serialized.unpack1("L", offset: offset + 4) #: Integer
|
|
208
240
|
|
|
209
|
-
|
|
210
|
-
@input.byteslice(start, length).force_encoding(encoding).to_sym
|
|
211
|
-
else
|
|
212
|
-
@serialized.byteslice(start & ((1 << 31) - 1), length).force_encoding(encoding).to_sym
|
|
213
|
-
end
|
|
241
|
+
(@serialized.byteslice(start, length) or raise).force_encoding(encoding).to_sym
|
|
214
242
|
end
|
|
215
243
|
end
|
|
216
244
|
end
|
|
217
245
|
|
|
218
246
|
if RUBY_ENGINE == "truffleruby"
|
|
219
247
|
# StringIO is synchronized and that adds a high overhead on TruffleRuby.
|
|
248
|
+
# @rbs skip
|
|
220
249
|
class FastStringIO # :nodoc:
|
|
221
250
|
attr_accessor :pos
|
|
222
251
|
|
|
@@ -246,8 +275,11 @@ module Prism
|
|
|
246
275
|
end
|
|
247
276
|
|
|
248
277
|
class Loader # :nodoc:
|
|
249
|
-
attr_reader :input
|
|
278
|
+
attr_reader :input #: String
|
|
279
|
+
attr_reader :io #: StringIO
|
|
280
|
+
attr_reader :source #: Source
|
|
250
281
|
|
|
282
|
+
#: (Source source, String serialized) -> void
|
|
251
283
|
def initialize(source, serialized)
|
|
252
284
|
@input = source.source.dup
|
|
253
285
|
raise unless serialized.encoding == Encoding::BINARY
|
|
@@ -256,57 +288,91 @@ module Prism
|
|
|
256
288
|
define_load_node_lambdas if RUBY_ENGINE != "ruby"
|
|
257
289
|
end
|
|
258
290
|
|
|
291
|
+
#: () -> bool
|
|
259
292
|
def eof?
|
|
260
293
|
io.getbyte
|
|
261
294
|
io.eof?
|
|
262
295
|
end
|
|
263
296
|
|
|
297
|
+
#: (ConstantPool constant_pool) -> void
|
|
264
298
|
def load_constant_pool(constant_pool)
|
|
265
299
|
trailer = 0
|
|
266
300
|
|
|
267
301
|
constant_pool.size.times do |index|
|
|
268
|
-
|
|
269
|
-
trailer += length
|
|
302
|
+
length = (io.read(8) or raise).unpack1("L", offset: 4) #: Integer
|
|
303
|
+
trailer += length
|
|
270
304
|
end
|
|
271
305
|
|
|
272
306
|
io.read(trailer)
|
|
273
307
|
end
|
|
274
308
|
|
|
309
|
+
#: () -> void
|
|
275
310
|
def load_header
|
|
276
311
|
raise "Invalid serialization" if io.read(5) != "PRISM"
|
|
277
|
-
raise "Invalid serialization" if io.read(3).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION]
|
|
312
|
+
raise "Invalid serialization" if (io.read(3) or raise).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION]
|
|
278
313
|
raise "Invalid serialization (location fields must be included but are not)" if io.getbyte != 0
|
|
279
314
|
end
|
|
280
315
|
|
|
316
|
+
#: () -> Encoding
|
|
281
317
|
def load_encoding
|
|
282
|
-
encoding = Encoding.find(io.read(load_varuint))
|
|
318
|
+
encoding = Encoding.find((io.read(load_varuint) or raise)) or raise
|
|
283
319
|
@input = input.force_encoding(encoding).freeze
|
|
284
320
|
encoding
|
|
285
321
|
end
|
|
286
322
|
|
|
323
|
+
#: (bool freeze) -> Array[Integer]
|
|
287
324
|
def load_line_offsets(freeze)
|
|
288
325
|
offsets = Array.new(load_varuint) { load_varuint }
|
|
289
326
|
offsets.freeze if freeze
|
|
290
327
|
offsets
|
|
291
328
|
end
|
|
292
329
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
330
|
+
# Load the comments attached to each node into the comment attachments and
|
|
331
|
+
# comment ranges of the given source. For each node that owns comments, the
|
|
332
|
+
# index at which its comments begin is stored at its node id and the index
|
|
333
|
+
# at which they end at the following node id.
|
|
334
|
+
#
|
|
335
|
+
#: (Source source, bool freeze) -> void
|
|
336
|
+
def load_comment_attachments(source, freeze)
|
|
337
|
+
comments = source.comments
|
|
338
|
+
attachments = source.comment_attachments
|
|
339
|
+
ranges = source.comment_ranges
|
|
340
|
+
|
|
341
|
+
load_varuint.times do
|
|
342
|
+
node_id = load_varuint
|
|
343
|
+
ranges[node_id] = attachments.length
|
|
344
|
+
load_varuint.times { attachments << comments[load_varuint] }
|
|
345
|
+
ranges[node_id + 1] = attachments.length
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
if freeze
|
|
349
|
+
attachments.freeze
|
|
350
|
+
ranges.freeze
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# Load the comments into the given list, which belongs to the source that
|
|
355
|
+
# the comments refer to.
|
|
356
|
+
#
|
|
357
|
+
#: (Array[Comment] comments, bool freeze) -> Array[Comment]
|
|
358
|
+
def load_comments(comments, freeze)
|
|
359
|
+
load_varuint.times do
|
|
360
|
+
comment =
|
|
361
|
+
case load_varuint
|
|
362
|
+
when 0 then InlineComment.new(load_location_object(freeze))
|
|
363
|
+
when 1 then EmbDocComment.new(load_location_object(freeze))
|
|
364
|
+
else raise
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
comment.freeze if freeze
|
|
368
|
+
comments << comment
|
|
369
|
+
end
|
|
305
370
|
|
|
306
371
|
comments.freeze if freeze
|
|
307
372
|
comments
|
|
308
373
|
end
|
|
309
374
|
|
|
375
|
+
#: (bool freeze) -> Array[MagicComment]
|
|
310
376
|
def load_magic_comments(freeze)
|
|
311
377
|
magic_comments =
|
|
312
378
|
Array.new(load_varuint) do
|
|
@@ -331,10 +397,11 @@ module Prism
|
|
|
331
397
|
<%- warnings.each do |warning| -%>
|
|
332
398
|
<%= warning.name.downcase.to_sym.inspect %>,
|
|
333
399
|
<%- end -%>
|
|
334
|
-
].freeze
|
|
400
|
+
].freeze #: Array[Symbol]
|
|
335
401
|
|
|
336
402
|
private_constant :DIAGNOSTIC_TYPES
|
|
337
403
|
|
|
404
|
+
#: () -> Symbol
|
|
338
405
|
def load_error_level
|
|
339
406
|
level = io.getbyte
|
|
340
407
|
|
|
@@ -350,13 +417,14 @@ module Prism
|
|
|
350
417
|
end
|
|
351
418
|
end
|
|
352
419
|
|
|
420
|
+
#: (Encoding encoding, bool freeze) -> Array[ParseError]
|
|
353
421
|
def load_errors(encoding, freeze)
|
|
354
422
|
errors =
|
|
355
423
|
Array.new(load_varuint) do
|
|
356
424
|
error =
|
|
357
425
|
ParseError.new(
|
|
358
426
|
DIAGNOSTIC_TYPES.fetch(load_varuint),
|
|
359
|
-
|
|
427
|
+
load_string(encoding),
|
|
360
428
|
load_location_object(freeze),
|
|
361
429
|
load_error_level
|
|
362
430
|
)
|
|
@@ -369,6 +437,7 @@ module Prism
|
|
|
369
437
|
errors
|
|
370
438
|
end
|
|
371
439
|
|
|
440
|
+
#: () -> Symbol
|
|
372
441
|
def load_warning_level
|
|
373
442
|
level = io.getbyte
|
|
374
443
|
|
|
@@ -382,13 +451,14 @@ module Prism
|
|
|
382
451
|
end
|
|
383
452
|
end
|
|
384
453
|
|
|
454
|
+
#: (Encoding encoding, bool freeze) -> Array[ParseWarning]
|
|
385
455
|
def load_warnings(encoding, freeze)
|
|
386
456
|
warnings =
|
|
387
457
|
Array.new(load_varuint) do
|
|
388
458
|
warning =
|
|
389
459
|
ParseWarning.new(
|
|
390
460
|
DIAGNOSTIC_TYPES.fetch(load_varuint),
|
|
391
|
-
|
|
461
|
+
load_string(encoding),
|
|
392
462
|
load_location_object(freeze),
|
|
393
463
|
load_warning_level
|
|
394
464
|
)
|
|
@@ -401,18 +471,18 @@ module Prism
|
|
|
401
471
|
warnings
|
|
402
472
|
end
|
|
403
473
|
|
|
474
|
+
#: () -> Array[Token]
|
|
404
475
|
def load_tokens
|
|
405
|
-
tokens = []
|
|
476
|
+
tokens = [] #: Array[Token]
|
|
406
477
|
|
|
407
478
|
while (type = TOKEN_TYPES.fetch(load_varuint))
|
|
408
|
-
|
|
409
|
-
|
|
479
|
+
location = load_location_object(false)
|
|
480
|
+
|
|
410
481
|
lex_state = load_varuint
|
|
411
482
|
|
|
412
|
-
|
|
413
|
-
token = Token.new(@source, type, location.slice, location)
|
|
483
|
+
token = Token.new(@source, type, location.slice, location, lex_state)
|
|
414
484
|
|
|
415
|
-
tokens <<
|
|
485
|
+
tokens << token
|
|
416
486
|
end
|
|
417
487
|
|
|
418
488
|
tokens
|
|
@@ -420,25 +490,29 @@ module Prism
|
|
|
420
490
|
|
|
421
491
|
# variable-length integer using https://en.wikipedia.org/wiki/LEB128
|
|
422
492
|
# This is also what protobuf uses: https://protobuf.dev/programming-guides/encoding/#varints
|
|
493
|
+
#
|
|
494
|
+
#: () -> Integer
|
|
423
495
|
def load_varuint
|
|
424
|
-
n = io.getbyte
|
|
496
|
+
n = (io.getbyte or raise)
|
|
425
497
|
if n < 128
|
|
426
498
|
n
|
|
427
499
|
else
|
|
428
500
|
n -= 128
|
|
429
501
|
shift = 0
|
|
430
|
-
while (b = io.getbyte) >= 128
|
|
502
|
+
while (b = (io.getbyte or raise)) >= 128
|
|
431
503
|
n += (b - 128) << (shift += 7)
|
|
432
504
|
end
|
|
433
505
|
n + (b << (shift + 7))
|
|
434
506
|
end
|
|
435
507
|
end
|
|
436
508
|
|
|
509
|
+
#: () -> Integer
|
|
437
510
|
def load_varsint
|
|
438
511
|
n = load_varuint
|
|
439
512
|
(n >> 1) ^ (-(n & 1))
|
|
440
513
|
end
|
|
441
514
|
|
|
515
|
+
#: () -> Integer
|
|
442
516
|
def load_integer
|
|
443
517
|
negative = io.getbyte != 0
|
|
444
518
|
length = load_varuint
|
|
@@ -450,133 +524,212 @@ module Prism
|
|
|
450
524
|
value
|
|
451
525
|
end
|
|
452
526
|
|
|
527
|
+
#: () -> Float
|
|
453
528
|
def load_double
|
|
454
|
-
io.read(8).unpack1("D")
|
|
529
|
+
(io.read(8) or raise).unpack1("D") #: Float
|
|
455
530
|
end
|
|
456
531
|
|
|
457
|
-
|
|
458
|
-
|
|
532
|
+
#: () -> bool
|
|
533
|
+
def load_bool
|
|
534
|
+
(io.getbyte or raise) != 0
|
|
459
535
|
end
|
|
460
536
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
load_node(constant_pool, encoding, freeze)
|
|
465
|
-
end
|
|
537
|
+
#: () -> Integer
|
|
538
|
+
def load_uint32
|
|
539
|
+
(io.read(4) or raise).unpack1("L") #: Integer
|
|
466
540
|
end
|
|
467
541
|
|
|
468
|
-
|
|
469
|
-
|
|
542
|
+
#: () -> Integer
|
|
543
|
+
def load_uint64
|
|
544
|
+
(io.read(8) or raise).unpack1("Q") #: Integer
|
|
470
545
|
end
|
|
471
546
|
|
|
547
|
+
#: (Encoding encoding) -> String
|
|
472
548
|
def load_string(encoding)
|
|
473
|
-
|
|
474
|
-
when 1
|
|
475
|
-
input.byteslice(load_varuint, load_varuint).force_encoding(encoding).freeze
|
|
476
|
-
when 2
|
|
477
|
-
load_embedded_string(encoding)
|
|
478
|
-
else
|
|
479
|
-
raise "Unknown serialized string type: #{type}"
|
|
480
|
-
end
|
|
549
|
+
(io.read(load_varuint) or raise).force_encoding(encoding).freeze
|
|
481
550
|
end
|
|
482
551
|
|
|
552
|
+
#: (bool freeze) -> Location
|
|
483
553
|
def load_location_object(freeze)
|
|
484
554
|
location = Location.new(source, load_varuint, load_varuint)
|
|
485
555
|
location.freeze if freeze
|
|
486
556
|
location
|
|
487
557
|
end
|
|
488
558
|
|
|
559
|
+
# Load a location object from the serialized data. Note that we are lying
|
|
560
|
+
# about the signature a bit here, because we sometimes load it as a packed
|
|
561
|
+
# integer instead of an object.
|
|
562
|
+
#
|
|
563
|
+
#: (bool freeze) -> Location
|
|
489
564
|
def load_location(freeze)
|
|
490
565
|
return load_location_object(freeze) if freeze
|
|
491
|
-
(load_varuint << 32) | load_varuint
|
|
566
|
+
(load_varuint << 32) | load_varuint #: Location
|
|
492
567
|
end
|
|
493
568
|
|
|
569
|
+
# Load an optional location object from the serialized data if it is
|
|
570
|
+
# present. Note that we are lying about the signature a bit here, because
|
|
571
|
+
# we sometimes load it as a packed integer instead of an object.
|
|
572
|
+
#
|
|
573
|
+
#: (bool freeze) -> Location?
|
|
494
574
|
def load_optional_location(freeze)
|
|
495
575
|
load_location(freeze) if io.getbyte != 0
|
|
496
576
|
end
|
|
497
577
|
|
|
578
|
+
#: (bool freeze) -> Location?
|
|
498
579
|
def load_optional_location_object(freeze)
|
|
499
580
|
load_location_object(freeze) if io.getbyte != 0
|
|
500
581
|
end
|
|
501
582
|
|
|
583
|
+
#: (ConstantPool constant_pool, Encoding encoding) -> Symbol
|
|
502
584
|
def load_constant(constant_pool, encoding)
|
|
503
585
|
index = load_varuint
|
|
504
586
|
constant_pool.get(index - 1, encoding)
|
|
505
587
|
end
|
|
506
588
|
|
|
589
|
+
#: (ConstantPool constant_pool, Encoding encoding) -> Symbol?
|
|
507
590
|
def load_optional_constant(constant_pool, encoding)
|
|
508
591
|
index = load_varuint
|
|
509
592
|
constant_pool.get(index - 1, encoding) if index != 0
|
|
510
593
|
end
|
|
511
594
|
|
|
512
595
|
if RUBY_ENGINE == "ruby"
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
when Prism::Template::
|
|
536
|
-
when Prism::Template::
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
596
|
+
# Load the nodes that are serialized before the given offset and return
|
|
597
|
+
# the root. Nodes are serialized in post-order, so the children of each
|
|
598
|
+
# node are the topmost values on the stack, in field order, by the time
|
|
599
|
+
# its record is read.
|
|
600
|
+
#
|
|
601
|
+
#: (ConstantPool constant_pool, Encoding encoding, bool freeze, Integer finish) -> node
|
|
602
|
+
def load_nodes(constant_pool, encoding, freeze, finish)
|
|
603
|
+
stack = [] #: Array[node?]
|
|
604
|
+
|
|
605
|
+
while io.pos < finish
|
|
606
|
+
case (type = io.getbyte)
|
|
607
|
+
when 0
|
|
608
|
+
stack << nil
|
|
609
|
+
when <%= Prism::Template::LAZY_MARKER %>
|
|
610
|
+
io.read(8)
|
|
611
|
+
<%- nodes.each_with_index do |node, index| -%>
|
|
612
|
+
when <%= index + 1 %>
|
|
613
|
+
node_id = load_varuint
|
|
614
|
+
location = load_location(freeze)
|
|
615
|
+
flags = load_varuint
|
|
616
|
+
<%- node.fields.each do |field| -%>
|
|
617
|
+
<%- case field -%>
|
|
618
|
+
<%- when Prism::Template::NodeField, Prism::Template::OptionalNodeField -%>
|
|
619
|
+
<%- when Prism::Template::NodeListField -%>
|
|
620
|
+
<%= field.name %>_size = load_varuint
|
|
621
|
+
<%- when Prism::Template::StringField -%>
|
|
622
|
+
field_<%= field.name %> = load_string(encoding)
|
|
623
|
+
<%- when Prism::Template::ConstantField -%>
|
|
624
|
+
field_<%= field.name %> = load_constant(constant_pool, encoding)
|
|
625
|
+
<%- when Prism::Template::OptionalConstantField -%>
|
|
626
|
+
field_<%= field.name %> = load_optional_constant(constant_pool, encoding)
|
|
627
|
+
<%- when Prism::Template::ConstantListField -%>
|
|
628
|
+
field_<%= field.name %> = Array.new(load_varuint) { load_constant(constant_pool, encoding) }
|
|
629
|
+
field_<%= field.name %>.freeze if freeze
|
|
630
|
+
<%- when Prism::Template::LocationField -%>
|
|
631
|
+
field_<%= field.name %> = load_location(freeze)
|
|
632
|
+
<%- when Prism::Template::OptionalLocationField -%>
|
|
633
|
+
field_<%= field.name %> = load_optional_location(freeze)
|
|
634
|
+
<%- when Prism::Template::UInt8Field -%>
|
|
635
|
+
field_<%= field.name %> = (io.getbyte or raise)
|
|
636
|
+
<%- when Prism::Template::UInt32Field -%>
|
|
637
|
+
field_<%= field.name %> = load_varuint
|
|
638
|
+
<%- when Prism::Template::IntegerField -%>
|
|
639
|
+
field_<%= field.name %> = load_integer
|
|
640
|
+
<%- when Prism::Template::DoubleField -%>
|
|
641
|
+
field_<%= field.name %> = load_double
|
|
642
|
+
<%- else raise -%>
|
|
643
|
+
<%- end -%>
|
|
644
|
+
<%- end -%>
|
|
645
|
+
<%- node.child_fields.reverse_each do |field| -%>
|
|
646
|
+
<%- if field.is_a?(Prism::Template::NodeListField) -%>
|
|
647
|
+
field_<%= field.name %> = stack.slice!(-<%= field.name %>_size, <%= field.name %>_size) #: <%= field.rbs_class %>
|
|
648
|
+
field_<%= field.name %>.freeze if freeze
|
|
649
|
+
<%- else -%>
|
|
650
|
+
field_<%= field.name %> = stack.pop #: <%= field.rbs_class %>
|
|
651
|
+
<%- end -%>
|
|
652
|
+
<%- end -%>
|
|
653
|
+
value = <%= node.name %>.new(source, node_id, location, flags<%= node.fields.map { |field| ", field_#{field.name}" }.join %>)
|
|
654
|
+
value.freeze if freeze
|
|
655
|
+
stack << value
|
|
541
656
|
<%- end -%>
|
|
657
|
+
else
|
|
658
|
+
raise "Unknown node type: #{type}"
|
|
659
|
+
end
|
|
542
660
|
end
|
|
543
661
|
|
|
544
|
-
|
|
545
|
-
|
|
662
|
+
raise unless io.pos == finish && stack.length == 1
|
|
663
|
+
stack.first or raise
|
|
546
664
|
end
|
|
547
665
|
else
|
|
548
|
-
|
|
549
|
-
|
|
666
|
+
# @rbs skip
|
|
667
|
+
def load_nodes(constant_pool, encoding, freeze, finish)
|
|
668
|
+
stack = [] #: Array[node?]
|
|
669
|
+
|
|
670
|
+
while io.pos < finish
|
|
671
|
+
type = (io.getbyte or raise)
|
|
672
|
+
|
|
673
|
+
if type == 0
|
|
674
|
+
stack << nil
|
|
675
|
+
elsif type == <%= Prism::Template::LAZY_MARKER %>
|
|
676
|
+
io.read(8)
|
|
677
|
+
else
|
|
678
|
+
stack << @load_node_lambdas[type].call(constant_pool, encoding, freeze, stack)
|
|
679
|
+
end
|
|
680
|
+
end
|
|
681
|
+
|
|
682
|
+
raise unless io.pos == finish && stack.length == 1
|
|
683
|
+
stack.first or raise
|
|
550
684
|
end
|
|
551
685
|
|
|
686
|
+
# @rbs skip
|
|
552
687
|
def define_load_node_lambdas
|
|
553
688
|
@load_node_lambdas = [
|
|
554
689
|
nil,
|
|
555
690
|
<%- nodes.each do |node| -%>
|
|
556
|
-
-> (constant_pool, encoding, freeze) {
|
|
691
|
+
-> (constant_pool, encoding, freeze, stack) {
|
|
557
692
|
node_id = load_varuint
|
|
558
693
|
location = load_location(freeze)
|
|
559
|
-
|
|
560
|
-
|
|
694
|
+
flags = load_varuint
|
|
695
|
+
<%- node.fields.each do |field| -%>
|
|
696
|
+
<%- case field -%>
|
|
697
|
+
<%- when Prism::Template::NodeField, Prism::Template::OptionalNodeField -%>
|
|
698
|
+
<%- when Prism::Template::NodeListField -%>
|
|
699
|
+
<%= field.name %>_size = load_varuint
|
|
700
|
+
<%- when Prism::Template::StringField -%>
|
|
701
|
+
field_<%= field.name %> = load_string(encoding)
|
|
702
|
+
<%- when Prism::Template::ConstantField -%>
|
|
703
|
+
field_<%= field.name %> = load_constant(constant_pool, encoding)
|
|
704
|
+
<%- when Prism::Template::OptionalConstantField -%>
|
|
705
|
+
field_<%= field.name %> = load_optional_constant(constant_pool, encoding)
|
|
706
|
+
<%- when Prism::Template::ConstantListField -%>
|
|
707
|
+
field_<%= field.name %> = Array.new(load_varuint) { load_constant(constant_pool, encoding) }
|
|
708
|
+
field_<%= field.name %>.freeze if freeze
|
|
709
|
+
<%- when Prism::Template::LocationField -%>
|
|
710
|
+
field_<%= field.name %> = load_location(freeze)
|
|
711
|
+
<%- when Prism::Template::OptionalLocationField -%>
|
|
712
|
+
field_<%= field.name %> = load_optional_location(freeze)
|
|
713
|
+
<%- when Prism::Template::UInt8Field -%>
|
|
714
|
+
field_<%= field.name %> = (io.getbyte or raise)
|
|
715
|
+
<%- when Prism::Template::UInt32Field -%>
|
|
716
|
+
field_<%= field.name %> = load_varuint
|
|
717
|
+
<%- when Prism::Template::IntegerField -%>
|
|
718
|
+
field_<%= field.name %> = load_integer
|
|
719
|
+
<%- when Prism::Template::DoubleField -%>
|
|
720
|
+
field_<%= field.name %> = load_double
|
|
721
|
+
<%- else raise -%>
|
|
722
|
+
<%- end -%>
|
|
561
723
|
<%- end -%>
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
when Prism::Template::LocationField then "load_location(freeze)"
|
|
572
|
-
when Prism::Template::OptionalLocationField then "load_optional_location(freeze)"
|
|
573
|
-
when Prism::Template::UInt8Field then "io.getbyte"
|
|
574
|
-
when Prism::Template::UInt32Field then "load_varuint"
|
|
575
|
-
when Prism::Template::IntegerField then "load_integer"
|
|
576
|
-
when Prism::Template::DoubleField then "load_double"
|
|
577
|
-
else raise
|
|
578
|
-
end
|
|
579
|
-
}].join(", ") -%>)
|
|
724
|
+
<%- node.child_fields.reverse_each do |field| -%>
|
|
725
|
+
<%- if field.is_a?(Prism::Template::NodeListField) -%>
|
|
726
|
+
field_<%= field.name %> = stack.slice!(-<%= field.name %>_size, <%= field.name %>_size) #: <%= field.rbs_class %>
|
|
727
|
+
field_<%= field.name %>.freeze if freeze
|
|
728
|
+
<%- else -%>
|
|
729
|
+
field_<%= field.name %> = stack.pop #: <%= field.rbs_class %>
|
|
730
|
+
<%- end -%>
|
|
731
|
+
<%- end -%>
|
|
732
|
+
value = <%= node.name %>.new(source, node_id, location, flags<%= node.fields.map { |field| ", field_#{field.name}" }.join %>)
|
|
580
733
|
value.freeze if freeze
|
|
581
734
|
value
|
|
582
735
|
},
|
|
@@ -584,6 +737,10 @@ module Prism
|
|
|
584
737
|
]
|
|
585
738
|
end
|
|
586
739
|
end
|
|
740
|
+
|
|
741
|
+
# @rbs!
|
|
742
|
+
# @load_node_lambdas: Array[Proc]
|
|
743
|
+
# def define_load_node_lambdas: () -> void
|
|
587
744
|
end
|
|
588
745
|
|
|
589
746
|
# The token types that can be indexed by their enum values.
|
|
@@ -592,7 +749,7 @@ module Prism
|
|
|
592
749
|
<%- tokens.each do |token| -%>
|
|
593
750
|
<%= token.name.to_sym.inspect %>,
|
|
594
751
|
<%- end -%>
|
|
595
|
-
].freeze
|
|
752
|
+
].freeze #: Array[Symbol?]
|
|
596
753
|
|
|
597
754
|
private_constant :MAJOR_VERSION, :MINOR_VERSION, :PATCH_VERSION
|
|
598
755
|
private_constant :ConstantPool, :FastStringIO, :Loader, :TOKEN_TYPES
|