prism 1.9.0 → 2.0.0.rc2
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/CHANGELOG.md +88 -0
- data/CODE_OF_CONDUCT.md +1 -1
- data/LICENSE.md +1 -1
- data/Makefile +41 -12
- data/README.md +35 -97
- data/config.yml +174 -161
- data/docs/build_system.md +2 -2
- data/docs/configuration.md +5 -4
- data/docs/encoding.md +2 -2
- data/docs/fuzzing.md +4 -11
- data/docs/javascript.md +19 -4
- data/docs/mapping.md +1 -1
- data/docs/releasing.md +77 -5
- data/docs/relocation.md +2 -4
- data/docs/serialization.md +22 -24
- data/docs/testing.md +46 -29
- data/ext/prism/api_node.c +1461 -1401
- data/ext/prism/extconf.rb +3 -4
- data/ext/prism/extension.c +796 -422
- data/ext/prism/extension.h +3 -3
- data/include/prism/arena.h +37 -0
- data/include/prism/ast.h +2727 -610
- data/include/prism/buffer.h +52 -0
- data/include/prism/comments.h +55 -0
- data/include/prism/compiler/accel.h +26 -0
- data/include/prism/compiler/align.h +36 -0
- data/include/prism/compiler/assume.h +25 -0
- data/include/prism/compiler/bswap.h +46 -0
- data/include/prism/compiler/endianness.h +34 -0
- data/include/prism/compiler/exported.h +24 -0
- data/include/prism/compiler/fallthrough.h +22 -0
- data/include/prism/compiler/filesystem.h +32 -0
- data/include/prism/compiler/flex_array.h +19 -0
- data/include/prism/compiler/force_inline.h +21 -0
- data/include/prism/compiler/format.h +25 -0
- data/include/prism/compiler/inline.h +17 -0
- data/include/prism/compiler/nodiscard.h +22 -0
- data/include/prism/compiler/nonnull.h +18 -0
- data/include/prism/compiler/unused.h +18 -0
- data/include/prism/constant_pool.h +81 -0
- data/include/prism/diagnostic.h +38 -403
- data/include/prism/errors_format.h +53 -0
- data/include/prism/excludes.h +29 -0
- data/include/prism/integer.h +41 -0
- data/include/prism/internal/allocator.h +68 -0
- data/include/prism/internal/allocator_debug.h +88 -0
- data/include/prism/internal/arena.h +108 -0
- data/include/prism/internal/bit.h +42 -0
- data/include/prism/internal/buffer.h +94 -0
- data/include/prism/internal/char.h +145 -0
- data/include/prism/internal/comments.h +53 -0
- data/include/prism/internal/constant_pool.h +152 -0
- data/include/prism/internal/diagnostic.h +389 -0
- data/include/prism/{encoding.h → internal/encoding.h} +27 -68
- data/include/prism/internal/integer.h +68 -0
- data/include/prism/internal/isinf.h +16 -0
- data/include/prism/internal/line_offset_list.h +34 -0
- data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
- data/include/prism/internal/magic_comments.h +23 -0
- data/include/prism/internal/memchr.h +15 -0
- data/include/prism/internal/node.h +38 -0
- data/include/prism/internal/options.h +232 -0
- data/include/prism/internal/parser.h +1004 -0
- data/include/prism/internal/regexp.h +26 -0
- data/include/prism/internal/serialize.h +39 -0
- data/include/prism/internal/source.h +72 -0
- data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
- data/include/prism/internal/stringy.h +35 -0
- data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
- data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
- data/include/prism/internal/tokens.h +11 -0
- data/include/prism/internal/xxh3.h +16 -0
- data/include/prism/json.h +32 -0
- data/include/prism/line_offset_list.h +61 -0
- data/include/prism/magic_comments.h +35 -0
- data/include/prism/node.h +18 -53
- data/include/prism/options.h +146 -280
- data/include/prism/parser.h +356 -867
- data/include/prism/prettyprint.h +6 -9
- data/include/prism/serialize.h +115 -0
- data/include/prism/source.h +155 -0
- data/include/prism/stream.h +28 -0
- data/include/prism/string_query.h +74 -0
- data/include/prism/stringy.h +72 -0
- data/include/prism/version.h +12 -3
- data/include/prism.h +43 -320
- data/lib/prism/compiler.rb +319 -305
- data/lib/prism/desugar_compiler.rb +105 -34
- data/lib/prism/dispatcher.rb +646 -473
- data/lib/prism/dot_visitor.rb +592 -511
- data/lib/prism/dsl.rb +391 -25
- data/lib/prism/ffi.rb +258 -90
- data/lib/prism/inspect_visitor.rb +357 -325
- data/lib/prism/lex_compat.rb +254 -212
- data/lib/prism/mutation_compiler.rb +313 -305
- data/lib/prism/node.rb +20966 -6992
- data/lib/prism/node_ext.rb +180 -187
- data/lib/prism/node_find.rb +215 -0
- data/lib/prism/parse_result/errors.rb +7 -1
- data/lib/prism/parse_result/newlines.rb +276 -8
- data/lib/prism/parse_result.rb +468 -139
- data/lib/prism/pattern.rb +65 -20
- data/lib/prism/reflection.rb +23 -12
- data/lib/prism/relocation.rb +149 -87
- data/lib/prism/serialize.rb +3460 -539
- data/lib/prism/string_query.rb +24 -1
- data/lib/prism/translation/parser/builder.rb +10 -2
- data/lib/prism/translation/parser/compiler.rb +44 -52
- data/lib/prism/translation/parser/lexer.rb +41 -92
- data/lib/prism/translation/parser.rb +1 -1
- data/lib/prism/translation/parser_current.rb +1 -1
- data/lib/prism/translation/ripper/lexer.rb +8 -10
- data/lib/prism/translation/ripper/sexp.rb +2 -10
- data/lib/prism/translation/ripper/shim.rb +2 -0
- data/lib/prism/translation/ripper.rb +967 -209
- data/lib/prism/translation/ruby_parser.rb +73 -316
- data/lib/prism/translation.rb +2 -0
- data/lib/prism/visitor.rb +632 -5
- data/lib/prism.rb +79 -9
- data/prism.gemspec +128 -62
- data/rbi/generated/prism/compiler.rbi +489 -0
- data/rbi/generated/prism/desugar_compiler.rbi +306 -0
- data/rbi/generated/prism/dispatcher.rbi +1138 -0
- data/rbi/generated/prism/dot_visitor.rbi +618 -0
- data/rbi/generated/prism/dsl.rbi +751 -0
- data/rbi/generated/prism/inspect_visitor.rbi +503 -0
- data/rbi/generated/prism/lex_compat.rbi +162 -0
- data/rbi/generated/prism/mutation_compiler.rbi +464 -0
- data/rbi/generated/prism/node.rbi +14407 -0
- data/rbi/generated/prism/node_ext.rbi +259 -0
- data/rbi/generated/prism/node_find.rbi +79 -0
- data/rbi/generated/prism/parse_result/errors.rbi +21 -0
- data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
- data/rbi/generated/prism/parse_result.rbi +776 -0
- data/rbi/generated/prism/pattern.rbi +134 -0
- data/rbi/generated/prism/reflection.rbi +89 -0
- data/rbi/generated/prism/relocation.rbi +329 -0
- data/rbi/generated/prism/serialize.rbi +184 -0
- data/rbi/generated/prism/string_query.rbi +43 -0
- data/rbi/generated/prism/translation.rbi +7 -0
- data/rbi/generated/prism/visitor.rbi +650 -0
- data/rbi/generated/prism.rbi +86 -0
- data/rbi/prism/translation/parser_versions.rbi +5 -0
- data/rbi/rubyvm/node_find.rbi +14 -0
- data/sig/generated/prism/compiler.rbs +492 -0
- data/sig/generated/prism/desugar_compiler.rbs +305 -0
- data/sig/generated/prism/dispatcher.rbs +1292 -0
- data/sig/generated/prism/dot_visitor.rbs +635 -0
- data/sig/generated/prism/dsl.rbs +928 -0
- data/sig/generated/prism/inspect_visitor.rbs +509 -0
- data/sig/generated/prism/lex_compat.rbs +166 -0
- data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
- data/sig/generated/prism/node.rbs +20896 -0
- data/sig/generated/prism/node_ext.rbs +290 -0
- data/sig/generated/prism/node_find.rbs +90 -0
- data/sig/generated/prism/parse_result/errors.rbs +22 -0
- data/sig/generated/prism/parse_result/newlines.rbs +223 -0
- data/sig/generated/prism/parse_result.rbs +871 -0
- data/sig/generated/prism/pattern.rbs +156 -0
- data/sig/generated/prism/reflection.rbs +101 -0
- data/sig/generated/prism/relocation.rbs +428 -0
- data/sig/generated/prism/serialize.rbs +198 -0
- data/sig/generated/prism/string_query.rbs +43 -0
- data/sig/generated/prism/translation.rbs +8 -0
- data/sig/generated/prism/visitor.rbs +1111 -0
- data/sig/generated/prism.rbs +84 -0
- data/sig/manifest.yaml +2 -0
- data/src/arena.c +117 -0
- data/src/{util/pm_buffer.c → buffer.c} +53 -21
- data/src/{util/pm_char.c → char.c} +28 -57
- data/src/comments.c +454 -0
- data/src/constant_pool.c +406 -0
- data/src/diagnostic.c +97 -65
- data/src/encoding.c +8 -3
- data/src/errors_format.c +519 -0
- data/src/{util/pm_integer.c → integer.c} +33 -22
- data/src/json.c +5733 -0
- data/src/line_offset_list.c +100 -0
- data/src/list.c +24 -0
- data/src/{util/pm_memchr.c → memchr.c} +8 -6
- data/src/node.c +3504 -7674
- data/src/options.c +162 -49
- data/src/parser.c +367 -0
- data/src/prettyprint.c +421 -354
- data/src/prism.c +8402 -7161
- data/src/regexp.c +961 -45
- data/src/serialize.c +460 -424
- data/src/source.c +518 -0
- data/src/static_literals.c +111 -43
- data/src/string_query.c +180 -0
- data/src/stringy.c +91 -0
- data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
- data/src/strpbrk.c +447 -0
- data/src/{token_type.c → tokens.c} +39 -18
- data/src/xxh3.c +292 -0
- metadata +128 -63
- data/ext/prism/api_pack.c +0 -276
- data/include/prism/defines.h +0 -260
- data/include/prism/pack.h +0 -163
- data/include/prism/regexp.h +0 -43
- data/include/prism/util/pm_buffer.h +0 -236
- data/include/prism/util/pm_char.h +0 -204
- data/include/prism/util/pm_constant_pool.h +0 -218
- data/include/prism/util/pm_integer.h +0 -130
- data/include/prism/util/pm_memchr.h +0 -29
- data/include/prism/util/pm_newline_list.h +0 -113
- data/include/prism/util/pm_string.h +0 -200
- data/lib/prism/pack.rb +0 -230
- data/lib/prism/parse_result/comments.rb +0 -188
- data/rbi/prism/compiler.rbi +0 -12
- data/rbi/prism/dsl.rbi +0 -524
- data/rbi/prism/inspect_visitor.rbi +0 -12
- data/rbi/prism/node.rbi +0 -8750
- data/rbi/prism/node_ext.rbi +0 -107
- data/rbi/prism/parse_result.rbi +0 -404
- data/rbi/prism/reflection.rbi +0 -58
- data/rbi/prism/string_query.rbi +0 -12
- data/rbi/prism/visitor.rbi +0 -473
- data/rbi/prism.rbi +0 -63
- data/sig/prism/compiler.rbs +0 -9
- data/sig/prism/dispatcher.rbs +0 -19
- data/sig/prism/dot_visitor.rbs +0 -6
- data/sig/prism/dsl.rbs +0 -351
- data/sig/prism/inspect_visitor.rbs +0 -22
- data/sig/prism/lex_compat.rbs +0 -10
- data/sig/prism/node.rbs +0 -4042
- data/sig/prism/node_ext.rbs +0 -149
- data/sig/prism/pack.rbs +0 -43
- data/sig/prism/parse_result/comments.rbs +0 -38
- data/sig/prism/parse_result.rbs +0 -197
- data/sig/prism/pattern.rbs +0 -13
- data/sig/prism/reflection.rbs +0 -50
- data/sig/prism/relocation.rbs +0 -185
- data/sig/prism/serialize.rbs +0 -8
- data/sig/prism/string_query.rbs +0 -11
- data/sig/prism/visitor.rbs +0 -169
- data/sig/prism.rbs +0 -272
- data/src/pack.c +0 -509
- data/src/util/pm_constant_pool.c +0 -342
- data/src/util/pm_list.c +0 -49
- data/src/util/pm_newline_list.c +0 -125
- data/src/util/pm_string.c +0 -381
- data/src/util/pm_strpbrk.c +0 -206
data/lib/prism/relocation.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
# :markup: markdown
|
|
3
|
+
#--
|
|
4
|
+
# rbs_inline: enabled
|
|
3
5
|
|
|
4
6
|
module Prism
|
|
5
7
|
# Prism parses deterministically for the same input. This provides a nice
|
|
@@ -12,6 +14,31 @@ module Prism
|
|
|
12
14
|
# "save" nodes and locations using a minimal amount of memory (just the
|
|
13
15
|
# node_id and a field identifier) and then reify them later.
|
|
14
16
|
module Relocation
|
|
17
|
+
# @rbs!
|
|
18
|
+
# type entry_value = untyped
|
|
19
|
+
# type entry_values = Hash[Symbol, entry_value]
|
|
20
|
+
#
|
|
21
|
+
# interface _Value
|
|
22
|
+
# def start_line: () -> Integer
|
|
23
|
+
# def end_line: () -> Integer
|
|
24
|
+
# def start_offset: () -> Integer
|
|
25
|
+
# def end_offset: () -> Integer
|
|
26
|
+
# def start_character_offset: () -> Integer
|
|
27
|
+
# def end_character_offset: () -> Integer
|
|
28
|
+
# def cached_start_code_units_offset: (_CodeUnitsCache cache) -> Integer
|
|
29
|
+
# def cached_end_code_units_offset: (_CodeUnitsCache cache) -> Integer
|
|
30
|
+
# def start_column: () -> Integer
|
|
31
|
+
# def end_column: () -> Integer
|
|
32
|
+
# def start_character_column: () -> Integer
|
|
33
|
+
# def end_character_column: () -> Integer
|
|
34
|
+
# def cached_start_code_units_column: (_CodeUnitsCache cache) -> Integer
|
|
35
|
+
# def cached_end_code_units_column: (_CodeUnitsCache cache) -> Integer
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
# interface _Field
|
|
39
|
+
# def fields: (_Value value) -> entry_values
|
|
40
|
+
# end
|
|
41
|
+
|
|
15
42
|
# An entry in a repository that will lazily reify its values when they are
|
|
16
43
|
# first accessed.
|
|
17
44
|
class Entry
|
|
@@ -21,109 +48,131 @@ module Prism
|
|
|
21
48
|
class MissingValueError < StandardError
|
|
22
49
|
end
|
|
23
50
|
|
|
51
|
+
# @rbs @repository: Repository?
|
|
52
|
+
# @rbs @values: Hash[Symbol, untyped]?
|
|
53
|
+
|
|
24
54
|
# Initialize a new entry with the given repository.
|
|
55
|
+
#
|
|
56
|
+
#: (Repository repository) -> void
|
|
25
57
|
def initialize(repository)
|
|
26
58
|
@repository = repository
|
|
27
59
|
@values = nil
|
|
28
60
|
end
|
|
29
61
|
|
|
30
62
|
# Fetch the filepath of the value.
|
|
63
|
+
#
|
|
64
|
+
#: () -> String
|
|
31
65
|
def filepath
|
|
32
66
|
fetch_value(:filepath)
|
|
33
67
|
end
|
|
34
68
|
|
|
35
69
|
# Fetch the start line of the value.
|
|
70
|
+
#
|
|
71
|
+
#: () -> Integer
|
|
36
72
|
def start_line
|
|
37
73
|
fetch_value(:start_line)
|
|
38
74
|
end
|
|
39
75
|
|
|
40
76
|
# Fetch the end line of the value.
|
|
77
|
+
#
|
|
78
|
+
#: () -> Integer
|
|
41
79
|
def end_line
|
|
42
80
|
fetch_value(:end_line)
|
|
43
81
|
end
|
|
44
82
|
|
|
45
83
|
# Fetch the start byte offset of the value.
|
|
84
|
+
#
|
|
85
|
+
#: () -> Integer
|
|
46
86
|
def start_offset
|
|
47
87
|
fetch_value(:start_offset)
|
|
48
88
|
end
|
|
49
89
|
|
|
50
90
|
# Fetch the end byte offset of the value.
|
|
91
|
+
#
|
|
92
|
+
#: () -> Integer
|
|
51
93
|
def end_offset
|
|
52
94
|
fetch_value(:end_offset)
|
|
53
95
|
end
|
|
54
96
|
|
|
55
97
|
# Fetch the start character offset of the value.
|
|
98
|
+
#
|
|
99
|
+
#: () -> Integer
|
|
56
100
|
def start_character_offset
|
|
57
101
|
fetch_value(:start_character_offset)
|
|
58
102
|
end
|
|
59
103
|
|
|
60
104
|
# Fetch the end character offset of the value.
|
|
105
|
+
#
|
|
106
|
+
#: () -> Integer
|
|
61
107
|
def end_character_offset
|
|
62
108
|
fetch_value(:end_character_offset)
|
|
63
109
|
end
|
|
64
110
|
|
|
65
111
|
# Fetch the start code units offset of the value, for the encoding that
|
|
66
112
|
# was configured on the repository.
|
|
113
|
+
#
|
|
114
|
+
#: () -> Integer
|
|
67
115
|
def start_code_units_offset
|
|
68
116
|
fetch_value(:start_code_units_offset)
|
|
69
117
|
end
|
|
70
118
|
|
|
71
119
|
# Fetch the end code units offset of the value, for the encoding that was
|
|
72
120
|
# configured on the repository.
|
|
121
|
+
#
|
|
122
|
+
#: () -> Integer
|
|
73
123
|
def end_code_units_offset
|
|
74
124
|
fetch_value(:end_code_units_offset)
|
|
75
125
|
end
|
|
76
126
|
|
|
77
127
|
# Fetch the start byte column of the value.
|
|
128
|
+
#
|
|
129
|
+
#: () -> Integer
|
|
78
130
|
def start_column
|
|
79
131
|
fetch_value(:start_column)
|
|
80
132
|
end
|
|
81
133
|
|
|
82
134
|
# Fetch the end byte column of the value.
|
|
135
|
+
#
|
|
136
|
+
#: () -> Integer
|
|
83
137
|
def end_column
|
|
84
138
|
fetch_value(:end_column)
|
|
85
139
|
end
|
|
86
140
|
|
|
87
141
|
# Fetch the start character column of the value.
|
|
142
|
+
#
|
|
143
|
+
#: () -> Integer
|
|
88
144
|
def start_character_column
|
|
89
145
|
fetch_value(:start_character_column)
|
|
90
146
|
end
|
|
91
147
|
|
|
92
148
|
# Fetch the end character column of the value.
|
|
149
|
+
#
|
|
150
|
+
#: () -> Integer
|
|
93
151
|
def end_character_column
|
|
94
152
|
fetch_value(:end_character_column)
|
|
95
153
|
end
|
|
96
154
|
|
|
97
155
|
# Fetch the start code units column of the value, for the encoding that
|
|
98
156
|
# was configured on the repository.
|
|
157
|
+
#
|
|
158
|
+
#: () -> Integer
|
|
99
159
|
def start_code_units_column
|
|
100
160
|
fetch_value(:start_code_units_column)
|
|
101
161
|
end
|
|
102
162
|
|
|
103
163
|
# Fetch the end code units column of the value, for the encoding that was
|
|
104
164
|
# configured on the repository.
|
|
165
|
+
#
|
|
166
|
+
#: () -> Integer
|
|
105
167
|
def end_code_units_column
|
|
106
168
|
fetch_value(:end_code_units_column)
|
|
107
169
|
end
|
|
108
170
|
|
|
109
|
-
# Fetch the leading comments of the value.
|
|
110
|
-
def leading_comments
|
|
111
|
-
fetch_value(:leading_comments)
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
# Fetch the trailing comments of the value.
|
|
115
|
-
def trailing_comments
|
|
116
|
-
fetch_value(:trailing_comments)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
# Fetch the leading and trailing comments of the value.
|
|
120
|
-
def comments
|
|
121
|
-
leading_comments.concat(trailing_comments)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
171
|
# Reify the values on this entry with the given values. This is an
|
|
125
172
|
# internal-only API that is called from the repository when it is time to
|
|
126
173
|
# reify the values.
|
|
174
|
+
#
|
|
175
|
+
#: (entry_values values) -> void
|
|
127
176
|
def reify!(values) # :nodoc:
|
|
128
177
|
@repository = nil
|
|
129
178
|
@values = values
|
|
@@ -132,6 +181,8 @@ module Prism
|
|
|
132
181
|
private
|
|
133
182
|
|
|
134
183
|
# Fetch a value from the entry, raising an error if it is missing.
|
|
184
|
+
#
|
|
185
|
+
#: (Symbol name) -> entry_value
|
|
135
186
|
def fetch_value(name)
|
|
136
187
|
values.fetch(name) do
|
|
137
188
|
raise MissingValueError, "No value for #{name}, make sure the " \
|
|
@@ -140,27 +191,35 @@ module Prism
|
|
|
140
191
|
end
|
|
141
192
|
|
|
142
193
|
# Return the values from the repository, reifying them if necessary.
|
|
194
|
+
#
|
|
195
|
+
#: () -> entry_values
|
|
143
196
|
def values
|
|
144
|
-
@values || (@repository
|
|
197
|
+
@values || (@repository&.reify!; @values) #: entry_values
|
|
145
198
|
end
|
|
146
199
|
end
|
|
147
200
|
|
|
148
201
|
# Represents the source of a repository that will be reparsed.
|
|
149
202
|
class Source
|
|
150
203
|
# The value that will need to be reparsed.
|
|
151
|
-
attr_reader :value
|
|
204
|
+
attr_reader :value #: untyped
|
|
152
205
|
|
|
153
206
|
# Initialize the source with the given value.
|
|
207
|
+
#
|
|
208
|
+
#: (untyped value) -> void
|
|
154
209
|
def initialize(value)
|
|
155
210
|
@value = value
|
|
156
211
|
end
|
|
157
212
|
|
|
158
213
|
# Reparse the value and return the parse result.
|
|
214
|
+
#
|
|
215
|
+
#: () -> ParseResult
|
|
159
216
|
def result
|
|
160
217
|
raise NotImplementedError, "Subclasses must implement #result"
|
|
161
218
|
end
|
|
162
219
|
|
|
163
220
|
# Create a code units cache for the given encoding.
|
|
221
|
+
#
|
|
222
|
+
#: (Encoding encoding) -> _CodeUnitsCache
|
|
164
223
|
def code_units_cache(encoding)
|
|
165
224
|
result.code_units_cache(encoding)
|
|
166
225
|
end
|
|
@@ -169,6 +228,8 @@ module Prism
|
|
|
169
228
|
# A source that is represented by a file path.
|
|
170
229
|
class SourceFilepath < Source
|
|
171
230
|
# Reparse the file and return the parse result.
|
|
231
|
+
#
|
|
232
|
+
#: () -> ParseResult
|
|
172
233
|
def result
|
|
173
234
|
Prism.parse_file(value)
|
|
174
235
|
end
|
|
@@ -177,6 +238,8 @@ module Prism
|
|
|
177
238
|
# A source that is represented by a string.
|
|
178
239
|
class SourceString < Source
|
|
179
240
|
# Reparse the string and return the parse result.
|
|
241
|
+
#
|
|
242
|
+
#: () -> ParseResult
|
|
180
243
|
def result
|
|
181
244
|
Prism.parse(value)
|
|
182
245
|
end
|
|
@@ -185,14 +248,18 @@ module Prism
|
|
|
185
248
|
# A field that represents the file path.
|
|
186
249
|
class FilepathField
|
|
187
250
|
# The file path that this field represents.
|
|
188
|
-
attr_reader :value
|
|
251
|
+
attr_reader :value #: String
|
|
189
252
|
|
|
190
253
|
# Initialize a new field with the given file path.
|
|
254
|
+
#
|
|
255
|
+
#: (String value) -> void
|
|
191
256
|
def initialize(value)
|
|
192
257
|
@value = value
|
|
193
258
|
end
|
|
194
259
|
|
|
195
260
|
# Fetch the file path.
|
|
261
|
+
#
|
|
262
|
+
#: (_Value _value) -> entry_values
|
|
196
263
|
def fields(_value)
|
|
197
264
|
{ filepath: value }
|
|
198
265
|
end
|
|
@@ -201,6 +268,8 @@ module Prism
|
|
|
201
268
|
# A field representing the start and end lines.
|
|
202
269
|
class LinesField
|
|
203
270
|
# Fetches the start and end line of a value.
|
|
271
|
+
#
|
|
272
|
+
#: (_Value value) -> entry_values
|
|
204
273
|
def fields(value)
|
|
205
274
|
{ start_line: value.start_line, end_line: value.end_line }
|
|
206
275
|
end
|
|
@@ -209,6 +278,8 @@ module Prism
|
|
|
209
278
|
# A field representing the start and end byte offsets.
|
|
210
279
|
class OffsetsField
|
|
211
280
|
# Fetches the start and end byte offset of a value.
|
|
281
|
+
#
|
|
282
|
+
#: (_Value value) -> entry_values
|
|
212
283
|
def fields(value)
|
|
213
284
|
{ start_offset: value.start_offset, end_offset: value.end_offset }
|
|
214
285
|
end
|
|
@@ -217,6 +288,8 @@ module Prism
|
|
|
217
288
|
# A field representing the start and end character offsets.
|
|
218
289
|
class CharacterOffsetsField
|
|
219
290
|
# Fetches the start and end character offset of a value.
|
|
291
|
+
#
|
|
292
|
+
#: (_Value value) -> entry_values
|
|
220
293
|
def fields(value)
|
|
221
294
|
{
|
|
222
295
|
start_character_offset: value.start_character_offset,
|
|
@@ -229,12 +302,16 @@ module Prism
|
|
|
229
302
|
class CodeUnitOffsetsField
|
|
230
303
|
# A pointer to the repository object that is used for lazily creating a
|
|
231
304
|
# code units cache.
|
|
232
|
-
attr_reader :repository
|
|
305
|
+
attr_reader :repository #: Repository
|
|
233
306
|
|
|
234
307
|
# The associated encoding for the code units.
|
|
235
|
-
attr_reader :encoding
|
|
308
|
+
attr_reader :encoding #: Encoding
|
|
309
|
+
|
|
310
|
+
# @rbs @cache: _CodeUnitsCache?
|
|
236
311
|
|
|
237
312
|
# Initialize a new field with the associated repository and encoding.
|
|
313
|
+
#
|
|
314
|
+
#: (Repository repository, Encoding encoding) -> void
|
|
238
315
|
def initialize(repository, encoding)
|
|
239
316
|
@repository = repository
|
|
240
317
|
@encoding = encoding
|
|
@@ -243,6 +320,8 @@ module Prism
|
|
|
243
320
|
|
|
244
321
|
# Fetches the start and end code units offset of a value for a particular
|
|
245
322
|
# encoding.
|
|
323
|
+
#
|
|
324
|
+
#: (_Value value) -> entry_values
|
|
246
325
|
def fields(value)
|
|
247
326
|
{
|
|
248
327
|
start_code_units_offset: value.cached_start_code_units_offset(cache),
|
|
@@ -253,6 +332,8 @@ module Prism
|
|
|
253
332
|
private
|
|
254
333
|
|
|
255
334
|
# Lazily create a code units cache for the associated encoding.
|
|
335
|
+
#
|
|
336
|
+
#: () -> _CodeUnitsCache
|
|
256
337
|
def cache
|
|
257
338
|
@cache ||= repository.code_units_cache(encoding)
|
|
258
339
|
end
|
|
@@ -261,6 +342,8 @@ module Prism
|
|
|
261
342
|
# A field representing the start and end byte columns.
|
|
262
343
|
class ColumnsField
|
|
263
344
|
# Fetches the start and end byte column of a value.
|
|
345
|
+
#
|
|
346
|
+
#: (_Value value) -> entry_values
|
|
264
347
|
def fields(value)
|
|
265
348
|
{ start_column: value.start_column, end_column: value.end_column }
|
|
266
349
|
end
|
|
@@ -269,6 +352,8 @@ module Prism
|
|
|
269
352
|
# A field representing the start and end character columns.
|
|
270
353
|
class CharacterColumnsField
|
|
271
354
|
# Fetches the start and end character column of a value.
|
|
355
|
+
#
|
|
356
|
+
#: (_Value value) -> entry_values
|
|
272
357
|
def fields(value)
|
|
273
358
|
{
|
|
274
359
|
start_character_column: value.start_character_column,
|
|
@@ -282,12 +367,16 @@ module Prism
|
|
|
282
367
|
class CodeUnitColumnsField
|
|
283
368
|
# The repository object that is used for lazily creating a code units
|
|
284
369
|
# cache.
|
|
285
|
-
attr_reader :repository
|
|
370
|
+
attr_reader :repository #: Repository
|
|
286
371
|
|
|
287
372
|
# The associated encoding for the code units.
|
|
288
|
-
attr_reader :encoding
|
|
373
|
+
attr_reader :encoding #: Encoding
|
|
374
|
+
|
|
375
|
+
# @rbs @cache: _CodeUnitsCache?
|
|
289
376
|
|
|
290
377
|
# Initialize a new field with the associated repository and encoding.
|
|
378
|
+
#
|
|
379
|
+
#: (Repository repository, Encoding encoding) -> void
|
|
291
380
|
def initialize(repository, encoding)
|
|
292
381
|
@repository = repository
|
|
293
382
|
@encoding = encoding
|
|
@@ -296,6 +385,8 @@ module Prism
|
|
|
296
385
|
|
|
297
386
|
# Fetches the start and end code units column of a value for a particular
|
|
298
387
|
# encoding.
|
|
388
|
+
#
|
|
389
|
+
#: (_Value value) -> entry_values
|
|
299
390
|
def fields(value)
|
|
300
391
|
{
|
|
301
392
|
start_code_units_column: value.cached_start_code_units_column(cache),
|
|
@@ -306,48 +397,13 @@ module Prism
|
|
|
306
397
|
private
|
|
307
398
|
|
|
308
399
|
# Lazily create a code units cache for the associated encoding.
|
|
400
|
+
#
|
|
401
|
+
#: () -> _CodeUnitsCache
|
|
309
402
|
def cache
|
|
310
403
|
@cache ||= repository.code_units_cache(encoding)
|
|
311
404
|
end
|
|
312
405
|
end
|
|
313
406
|
|
|
314
|
-
# An abstract field used as the parent class of the two comments fields.
|
|
315
|
-
class CommentsField
|
|
316
|
-
# An object that represents a slice of a comment.
|
|
317
|
-
class Comment
|
|
318
|
-
# The slice of the comment.
|
|
319
|
-
attr_reader :slice
|
|
320
|
-
|
|
321
|
-
# Initialize a new comment with the given slice.
|
|
322
|
-
def initialize(slice)
|
|
323
|
-
@slice = slice
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
|
|
327
|
-
private
|
|
328
|
-
|
|
329
|
-
# Create comment objects from the given values.
|
|
330
|
-
def comments(values)
|
|
331
|
-
values.map { |value| Comment.new(value.slice) }
|
|
332
|
-
end
|
|
333
|
-
end
|
|
334
|
-
|
|
335
|
-
# A field representing the leading comments.
|
|
336
|
-
class LeadingCommentsField < CommentsField
|
|
337
|
-
# Fetches the leading comments of a value.
|
|
338
|
-
def fields(value)
|
|
339
|
-
{ leading_comments: comments(value.leading_comments) }
|
|
340
|
-
end
|
|
341
|
-
end
|
|
342
|
-
|
|
343
|
-
# A field representing the trailing comments.
|
|
344
|
-
class TrailingCommentsField < CommentsField
|
|
345
|
-
# Fetches the trailing comments of a value.
|
|
346
|
-
def fields(value)
|
|
347
|
-
{ trailing_comments: comments(value.trailing_comments) }
|
|
348
|
-
end
|
|
349
|
-
end
|
|
350
|
-
|
|
351
407
|
# A repository is a configured collection of fields and a set of entries
|
|
352
408
|
# that knows how to reparse a source and reify the values.
|
|
353
409
|
class Repository
|
|
@@ -358,15 +414,17 @@ module Prism
|
|
|
358
414
|
|
|
359
415
|
# The source associated with this repository. This will be either a
|
|
360
416
|
# SourceFilepath (the most common use case) or a SourceString.
|
|
361
|
-
attr_reader :source
|
|
417
|
+
attr_reader :source #: Source
|
|
362
418
|
|
|
363
419
|
# The fields that have been configured on this repository.
|
|
364
|
-
attr_reader :fields
|
|
420
|
+
attr_reader :fields #: Hash[Symbol, _Field]
|
|
365
421
|
|
|
366
422
|
# The entries that have been saved on this repository.
|
|
367
|
-
attr_reader :entries
|
|
423
|
+
attr_reader :entries #: Hash[Integer, Hash[Symbol, Entry]]
|
|
368
424
|
|
|
369
425
|
# Initialize a new repository with the given source.
|
|
426
|
+
#
|
|
427
|
+
#: (Source source) -> void
|
|
370
428
|
def initialize(source)
|
|
371
429
|
@source = source
|
|
372
430
|
@fields = {}
|
|
@@ -374,76 +432,78 @@ module Prism
|
|
|
374
432
|
end
|
|
375
433
|
|
|
376
434
|
# Create a code units cache for the given encoding from the source.
|
|
435
|
+
#
|
|
436
|
+
#: (Encoding encoding) -> _CodeUnitsCache
|
|
377
437
|
def code_units_cache(encoding)
|
|
378
438
|
source.code_units_cache(encoding)
|
|
379
439
|
end
|
|
380
440
|
|
|
381
441
|
# Configure the filepath field for this repository and return self.
|
|
442
|
+
#
|
|
443
|
+
#: () -> self
|
|
382
444
|
def filepath
|
|
383
445
|
raise ConfigurationError, "Can only specify filepath for a filepath source" unless source.is_a?(SourceFilepath)
|
|
384
446
|
field(:filepath, FilepathField.new(source.value))
|
|
385
447
|
end
|
|
386
448
|
|
|
387
449
|
# Configure the lines field for this repository and return self.
|
|
450
|
+
#
|
|
451
|
+
#: () -> self
|
|
388
452
|
def lines
|
|
389
453
|
field(:lines, LinesField.new)
|
|
390
454
|
end
|
|
391
455
|
|
|
392
456
|
# Configure the offsets field for this repository and return self.
|
|
457
|
+
#
|
|
458
|
+
#: () -> self
|
|
393
459
|
def offsets
|
|
394
460
|
field(:offsets, OffsetsField.new)
|
|
395
461
|
end
|
|
396
462
|
|
|
397
463
|
# Configure the character offsets field for this repository and return
|
|
398
464
|
# self.
|
|
465
|
+
#
|
|
466
|
+
#: () -> self
|
|
399
467
|
def character_offsets
|
|
400
468
|
field(:character_offsets, CharacterOffsetsField.new)
|
|
401
469
|
end
|
|
402
470
|
|
|
403
471
|
# Configure the code unit offsets field for this repository for a specific
|
|
404
472
|
# encoding and return self.
|
|
473
|
+
#
|
|
474
|
+
#: (Encoding encoding) -> self
|
|
405
475
|
def code_unit_offsets(encoding)
|
|
406
476
|
field(:code_unit_offsets, CodeUnitOffsetsField.new(self, encoding))
|
|
407
477
|
end
|
|
408
478
|
|
|
409
479
|
# Configure the columns field for this repository and return self.
|
|
480
|
+
#
|
|
481
|
+
#: () -> self
|
|
410
482
|
def columns
|
|
411
483
|
field(:columns, ColumnsField.new)
|
|
412
484
|
end
|
|
413
485
|
|
|
414
486
|
# Configure the character columns field for this repository and return
|
|
415
487
|
# self.
|
|
488
|
+
#
|
|
489
|
+
#: () -> self
|
|
416
490
|
def character_columns
|
|
417
491
|
field(:character_columns, CharacterColumnsField.new)
|
|
418
492
|
end
|
|
419
493
|
|
|
420
494
|
# Configure the code unit columns field for this repository for a specific
|
|
421
495
|
# encoding and return self.
|
|
496
|
+
#
|
|
497
|
+
#: (Encoding encoding) -> self
|
|
422
498
|
def code_unit_columns(encoding)
|
|
423
499
|
field(:code_unit_columns, CodeUnitColumnsField.new(self, encoding))
|
|
424
500
|
end
|
|
425
501
|
|
|
426
|
-
# Configure the leading comments field for this repository and return
|
|
427
|
-
# self.
|
|
428
|
-
def leading_comments
|
|
429
|
-
field(:leading_comments, LeadingCommentsField.new)
|
|
430
|
-
end
|
|
431
|
-
|
|
432
|
-
# Configure the trailing comments field for this repository and return
|
|
433
|
-
# self.
|
|
434
|
-
def trailing_comments
|
|
435
|
-
field(:trailing_comments, TrailingCommentsField.new)
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
# Configure both the leading and trailing comment fields for this
|
|
439
|
-
# repository and return self.
|
|
440
|
-
def comments
|
|
441
|
-
leading_comments.trailing_comments
|
|
442
|
-
end
|
|
443
|
-
|
|
444
502
|
# This method is called from nodes and locations when they want to enter
|
|
445
503
|
# themselves into the repository. It it internal-only and meant to be
|
|
446
504
|
# called from the #save* APIs.
|
|
505
|
+
#
|
|
506
|
+
#: (Integer node_id, Symbol field_name) -> Entry
|
|
447
507
|
def enter(node_id, field_name) # :nodoc:
|
|
448
508
|
entry = Entry.new(self)
|
|
449
509
|
@entries[node_id][field_name] = entry
|
|
@@ -453,20 +513,16 @@ module Prism
|
|
|
453
513
|
# This method is called from the entries in the repository when they need
|
|
454
514
|
# to reify their values. It is internal-only and meant to be called from
|
|
455
515
|
# the various value APIs.
|
|
516
|
+
#
|
|
517
|
+
#: () -> void
|
|
456
518
|
def reify! # :nodoc:
|
|
457
519
|
result = source.result
|
|
458
520
|
|
|
459
|
-
# Attach the comments if they have been requested as part of the
|
|
460
|
-
# configuration of this repository.
|
|
461
|
-
if fields.key?(:leading_comments) || fields.key?(:trailing_comments)
|
|
462
|
-
result.attach_comments!
|
|
463
|
-
end
|
|
464
|
-
|
|
465
521
|
queue = [result.value] #: Array[Prism::node]
|
|
466
522
|
while (node = queue.shift)
|
|
467
523
|
@entries[node.node_id].each do |field_name, entry|
|
|
468
524
|
value = node.public_send(field_name)
|
|
469
|
-
values = {} #:
|
|
525
|
+
values = {} #: entry_values
|
|
470
526
|
|
|
471
527
|
fields.each_value do |field|
|
|
472
528
|
values.merge!(field.fields(value))
|
|
@@ -485,6 +541,8 @@ module Prism
|
|
|
485
541
|
|
|
486
542
|
# Append the given field to the repository and return the repository so
|
|
487
543
|
# that these calls can be chained.
|
|
544
|
+
#
|
|
545
|
+
#: (Symbol name, _Field) -> self
|
|
488
546
|
def field(name, value)
|
|
489
547
|
raise ConfigurationError, "Cannot specify multiple #{name} fields" if @fields.key?(name)
|
|
490
548
|
@fields[name] = value
|
|
@@ -493,11 +551,15 @@ module Prism
|
|
|
493
551
|
end
|
|
494
552
|
|
|
495
553
|
# Create a new repository for the given filepath.
|
|
554
|
+
#
|
|
555
|
+
#: (String value) -> Repository
|
|
496
556
|
def self.filepath(value)
|
|
497
557
|
Repository.new(SourceFilepath.new(value))
|
|
498
558
|
end
|
|
499
559
|
|
|
500
560
|
# Create a new repository for the given string.
|
|
561
|
+
#
|
|
562
|
+
#: (String value) -> Repository
|
|
501
563
|
def self.string(value)
|
|
502
564
|
Repository.new(SourceString.new(value))
|
|
503
565
|
end
|