@@ -21,6 +21,12 @@ def initialize(source, start_line = 1, offsets = [])
21
21
@offsets = offsets # set after parsing is done
22
22
end
23
23
24
+ # Returns the encoding of the source code, which is set by parameters to the
25
+ # parser or by the encoding magic comment.
26
+ def encoding
27
+ source . encoding
28
+ end
29
+
24
30
# Perform a byteslice on the source code using the given byte offset and
25
31
# byte length.
26
32
def slice ( byte_offset , length )
@@ -108,16 +114,46 @@ class Location
108
114
# The length of this location in bytes.
109
115
attr_reader :length
110
116
111
- # The list of comments attached to this location
112
- attr_reader :comments
113
-
114
117
# Create a new location object with the given source, start byte offset, and
115
118
# byte length.
116
119
def initialize ( source , start_offset , length )
117
120
@source = source
118
121
@start_offset = start_offset
119
122
@length = length
120
- @comments = [ ]
123
+
124
+ # These are used to store comments that are associated with this location.
125
+ # They are initialized to `nil` to save on memory when there are no
126
+ # comments to be attached and/or the comment-related APIs are not used.
127
+ @leading_comments = nil
128
+ @trailing_comments = nil
129
+ end
130
+
131
+ # These are the comments that are associated with this location that exist
132
+ # before the start of this location.
133
+ def leading_comments
134
+ @leading_comments ||= [ ]
135
+ end
136
+
137
+ # Attach a comment to the leading comments of this location.
138
+ def leading_comment ( comment )
139
+ leading_comments << comment
140
+ end
141
+
142
+ # These are the comments that are associated with this location that exist
143
+ # after the end of this location.
144
+ def trailing_comments
145
+ @trailing_comments ||= [ ]
146
+ end
147
+
148
+ # Attach a comment to the trailing comments of this location.
149
+ def trailing_comment ( comment )
150
+ trailing_comments << comment
151
+ end
152
+
153
+ # Returns all comments that are associated with this location (both leading
154
+ # and trailing comments).
155
+ def comments
156
+ ( @leading_comments || [ ] ) . concat ( @trailing_comments || [ ] )
121
157
end
122
158
123
159
# Create a new location object with the given options.
@@ -268,6 +304,11 @@ def initialize(location)
268
304
def deconstruct_keys ( keys )
269
305
{ location : location }
270
306
end
307
+
308
+ # Returns the content of the comment by slicing it from the source code.
309
+ def slice
310
+ location . slice
311
+ end
271
312
end
272
313
273
314
# InlineComment objects are the most common. They correspond to comments in
@@ -437,6 +478,11 @@ def deconstruct_keys(keys)
437
478
{ value : value , comments : comments , magic_comments : magic_comments , data_loc : data_loc , errors : errors , warnings : warnings }
438
479
end
439
480
481
+ # Returns the encoding of the source code that was parsed.
482
+ def encoding
483
+ source . encoding
484
+ end
485
+
440
486
# Returns true if there were no errors during parsing and false if there
441
487
# were.
442
488
def success?
0 commit comments