Skip to content

Commit b6de2f9

Browse files
committed
Rename slice back to location
1 parent c3c268b commit b6de2f9

File tree

12 files changed

+373
-378
lines changed

12 files changed

+373
-378
lines changed

docs/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ The available values for `type` are:
6060
* `string` - A field that is a string. For example, this is used as the name of the method in a call node, since it cannot directly reference the source string (as in `@-` or `foo=`). This is a `pm_string_t` in C.
6161
* `constant` - A field that is an integer that represents an index in the constant pool. This is a `pm_constant_id_t` in C.
6262
* `constant[]` - A field that is an array of constants. This is a `pm_constant_id_list_t` in C.
63-
* `location` - A field that is a location. This is a `pm_slice_t` in C.
64-
* `location?` - A field that is a location that is optionally present. This is a `pm_slice_t` in C, but if the value is not present then the `length` field will be `0`.
63+
* `location` - A field that is a location. This is a `pm_location_t` in C.
64+
* `location?` - A field that is a location that is optionally present. This is a `pm_location_t` in C, but if the value is not present then the `length` field will be `0`.
6565
* `uint8` - A field that is an 8-bit unsigned integer. This is a `uint8_t` in C.
6666
* `uint32` - A field that is a 32-bit unsigned integer. This is a `uint32_t` in C.
6767

ext/prism/extension.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,17 @@ parser_location(VALUE source, bool freeze, uint32_t start, uint32_t length) {
461461
}
462462

463463
/**
464-
* Create a new Location instance from the given parser and slice.
464+
* Create a new Location instance from the given parser and location.
465465
*/
466-
#define PARSER_LOCATION_SLICE(source, freeze, slice) \
467-
parser_location(source, freeze, slice.start, slice.length)
466+
#define PARSER_LOCATION(source, freeze, location) \
467+
parser_location(source, freeze, location.start, location.length)
468468

469469
/**
470470
* Build a new Comment instance from the given parser and comment.
471471
*/
472472
static inline VALUE
473473
parser_comment(VALUE source, bool freeze, const pm_comment_t *comment) {
474-
VALUE argv[] = { PARSER_LOCATION_SLICE(source, freeze, comment->location) };
474+
VALUE argv[] = { PARSER_LOCATION(source, freeze, comment->location) };
475475
VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
476476
return rb_class_new_instance_freeze(1, argv, type, freeze);
477477
}
@@ -554,7 +554,7 @@ parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bo
554554
) {
555555
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(error->diag_id)));
556556
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(error->message, encoding));
557-
VALUE location = PARSER_LOCATION_SLICE(source, freeze, error->location);
557+
VALUE location = PARSER_LOCATION(source, freeze, error->location);
558558

559559
VALUE level = Qnil;
560560
switch (error->level) {
@@ -594,7 +594,7 @@ parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source,
594594
) {
595595
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(warning->diag_id)));
596596
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(warning->message, encoding));
597-
VALUE location = PARSER_LOCATION_SLICE(source, freeze, warning->location);
597+
VALUE location = PARSER_LOCATION(source, freeze, warning->location);
598598

599599
VALUE level = Qnil;
600600
switch (warning->level) {

include/prism/parser.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ typedef struct pm_comment {
463463
pm_list_node_t node;
464464

465465
/** The location of the comment in the source. */
466-
pm_slice_t location;
466+
pm_location_t location;
467467

468468
/** The type of comment that we've found. */
469469
pm_comment_type_t type;
@@ -480,10 +480,10 @@ typedef struct {
480480
pm_list_node_t node;
481481

482482
/** The key of the magic comment. */
483-
pm_slice_t key;
483+
pm_location_t key;
484484

485485
/** The value of the magic comment. */
486-
pm_slice_t value;
486+
pm_location_t value;
487487
} pm_magic_comment_t;
488488

489489
/**
@@ -531,7 +531,7 @@ typedef struct {
531531
pm_constant_id_t name;
532532

533533
/** The location of the local variable in the source. */
534-
pm_slice_t location;
534+
pm_location_t location;
535535

536536
/** The index of the local variable in the local table. */
537537
uint32_t index;
@@ -722,7 +722,7 @@ struct pm_parser {
722722
* and the rest of the content of the file. This content is loaded into the
723723
* DATA constant when the file being parsed is the main file being executed.
724724
*/
725-
pm_slice_t data_loc;
725+
pm_location_t data_loc;
726726

727727
/** The list of warnings that have been found while parsing. */
728728
pm_list_t warning_list;

rust/ruby-prism/build.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box<d
253253
writeln!(file)?;
254254
writeln!(file, " /// Returns the location of this node.")?;
255255
writeln!(file, " #[must_use]")?;
256-
writeln!(file, " pub fn location(&self) -> Slice<'pr> {{")?;
257-
writeln!(file, " let pointer: *mut pm_slice_t = unsafe {{ &raw mut (*self.pointer).base.location }};")?;
258-
writeln!(file, " Slice::new(self.parser, unsafe {{ &(*pointer) }})")?;
256+
writeln!(file, " pub fn location(&self) -> Location<'pr> {{")?;
257+
writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).base.location }};")?;
258+
writeln!(file, " Location::new(self.parser, unsafe {{ &(*pointer) }})")?;
259259
writeln!(file, " }}")?;
260260
writeln!(file)?;
261261
writeln!(file, " /// Returns the flags of this node.")?;
@@ -364,19 +364,19 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box<d
364364
writeln!(file, " }}")?;
365365
},
366366
NodeFieldType::Location => {
367-
writeln!(file, " pub fn {}(&self) -> Slice<'pr> {{", field.name)?;
368-
writeln!(file, " let pointer: *mut pm_slice_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?;
369-
writeln!(file, " Slice::new(self.parser, unsafe {{ &(*pointer) }})")?;
367+
writeln!(file, " pub fn {}(&self) -> Location<'pr> {{", field.name)?;
368+
writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?;
369+
writeln!(file, " Location::new(self.parser, unsafe {{ &(*pointer) }})")?;
370370
writeln!(file, " }}")?;
371371
},
372372
NodeFieldType::OptionalLocation => {
373-
writeln!(file, " pub fn {}(&self) -> Option<Slice<'pr>> {{", field.name)?;
374-
writeln!(file, " let pointer: *mut pm_slice_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?;
373+
writeln!(file, " pub fn {}(&self) -> Option<Location<'pr>> {{", field.name)?;
374+
writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?;
375375
writeln!(file, " let length = unsafe {{ (*pointer).length }};")?;
376376
writeln!(file, " if length == 0 {{")?;
377377
writeln!(file, " None")?;
378378
writeln!(file, " }} else {{")?;
379-
writeln!(file, " Some(Slice::new(self.parser, unsafe {{ &(*pointer) }}))")?;
379+
writeln!(file, " Some(Location::new(self.parser, unsafe {{ &(*pointer) }}))")?;
380380
writeln!(file, " }}")?;
381381
writeln!(file, " }}")?;
382382
},
@@ -558,7 +558,7 @@ use std::ptr::NonNull;
558558
559559
#[allow(clippy::wildcard_imports)]
560560
use ruby_prism_sys::*;
561-
use crate::{{ConstantId, ConstantList, Integer, Slice, NodeList}};
561+
use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}};
562562
"
563563
)?;
564564

@@ -621,10 +621,10 @@ impl<'pr> Node<'pr> {{
621621

622622
writeln!(file, " /// Returns the location of this node.")?;
623623
writeln!(file, " #[must_use]")?;
624-
writeln!(file, " pub fn location(&self) -> Slice<'pr> {{")?;
624+
writeln!(file, " pub fn location(&self) -> Location<'pr> {{")?;
625625
writeln!(file, " match *self {{")?;
626626
for node in &config.nodes {
627-
writeln!(file, " Self::{} {{ pointer, parser, .. }} => Slice::new(parser, unsafe {{ &((*pointer.cast::<pm_node_t>()).location) }}),", node.name)?;
627+
writeln!(file, " Self::{} {{ pointer, parser, .. }} => Location::new(parser, unsafe {{ &((*pointer.cast::<pm_node_t>()).location) }}),", node.name)?;
628628
}
629629
writeln!(file, " }}")?;
630630
writeln!(file, " }}")?;

rust/ruby-prism/src/lib.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ use std::mem::MaybeUninit;
1919
use std::ptr::NonNull;
2020

2121
pub use self::bindings::*;
22-
use ruby_prism_sys::{pm_comment_t, pm_comment_type_t, pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t, pm_slice_t};
22+
use ruby_prism_sys::{pm_comment_t, pm_comment_type_t, pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t, pm_location_t};
2323

2424
/// A range in the source file, represented as a start offset and length.
25-
pub struct Slice<'pr> {
25+
pub struct Location<'pr> {
2626
parser: NonNull<pm_parser_t>,
2727
pub(crate) start: u32,
2828
pub(crate) length: u32,
2929
marker: PhantomData<&'pr [u8]>,
3030
}
3131

32-
impl<'pr> Slice<'pr> {
32+
impl<'pr> Location<'pr> {
3333
/// Returns a byte slice for the range.
3434
#[must_use]
3535
pub fn as_slice(&self) -> &'pr [u8] {
@@ -39,13 +39,13 @@ impl<'pr> Slice<'pr> {
3939
}
4040
}
4141

42-
/// Return a Slice from the given `pm_slice_t`.
42+
/// Return a Location from the given `pm_location_t`.
4343
#[must_use]
44-
pub(crate) const fn new(parser: NonNull<pm_parser_t>, slice: &'pr pm_slice_t) -> Self {
45-
Slice {
44+
pub(crate) const fn new(parser: NonNull<pm_parser_t>, location: &'pr pm_location_t) -> Self {
45+
Location {
4646
parser,
47-
start: slice.start,
48-
length: slice.length,
47+
start: location.start,
48+
length: location.length,
4949
marker: PhantomData,
5050
}
5151
}
@@ -56,15 +56,15 @@ impl<'pr> Slice<'pr> {
5656
self.start + self.length
5757
}
5858

59-
/// Return a Slice starting at self and ending at the end of other.
59+
/// Return a Location starting at self and ending at the end of other.
6060
/// Returns None if both locations did not originate from the same parser,
6161
/// or if self starts after other.
6262
#[must_use]
6363
pub fn join(&self, other: &Self) -> Option<Self> {
6464
if self.parser != other.parser || self.start > other.start {
6565
None
6666
} else {
67-
Some(Slice {
67+
Some(Location {
6868
parser: self.parser,
6969
start: self.start,
7070
length: other.end() - self.start,
@@ -74,7 +74,7 @@ impl<'pr> Slice<'pr> {
7474
}
7575
}
7676

77-
impl std::fmt::Debug for Slice<'_> {
77+
impl std::fmt::Debug for Location<'_> {
7878
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7979
let slice: &[u8] = self.as_slice();
8080

@@ -332,8 +332,8 @@ impl<'pr> Diagnostic<'pr> {
332332

333333
/// The location of the diagnostic in the source.
334334
#[must_use]
335-
pub const fn location(&self) -> Slice<'pr> {
336-
Slice::new(self.parser, unsafe { &self.diag.as_ref().location })
335+
pub const fn location(&self) -> Location<'pr> {
336+
Location::new(self.parser, unsafe { &self.diag.as_ref().location })
337337
}
338338
}
339339

@@ -377,8 +377,8 @@ impl<'pr> Comment<'pr> {
377377

378378
/// The location of the comment in the source.
379379
#[must_use]
380-
pub const fn location(&self) -> Slice<'pr> {
381-
Slice::new(self.parser, unsafe { &self.content.as_ref().location })
380+
pub const fn location(&self) -> Location<'pr> {
381+
Location::new(self.parser, unsafe { &self.content.as_ref().location })
382382
}
383383
}
384384

@@ -510,9 +510,9 @@ impl<'pr> ParseResult<'pr> {
510510
/// Returns a slice of the source string that was parsed using the given
511511
/// slice range.
512512
#[must_use]
513-
pub fn as_slice(&self, slice: &Slice<'pr>) -> &'pr [u8] {
514-
let start = slice.start as usize;
515-
let end = start + slice.length as usize;
513+
pub fn as_slice(&self, location: &Location<'pr>) -> &'pr [u8] {
514+
let start = location.start as usize;
515+
let end = start + location.length as usize;
516516
&self.source[start..end]
517517
}
518518

@@ -574,12 +574,12 @@ impl<'pr> ParseResult<'pr> {
574574

575575
/// Returns an optional location of the __END__ marker and the rest of the content of the file.
576576
#[must_use]
577-
pub fn data_loc(&self) -> Option<Slice<'_>> {
577+
pub fn data_loc(&self) -> Option<Location<'_>> {
578578
let location = unsafe { &(*self.parser.as_ptr()).data_loc };
579579
if location.length == 0 {
580580
None
581581
} else {
582-
Some(Slice::new(self.parser, location))
582+
Some(Location::new(self.parser, location))
583583
}
584584
}
585585

0 commit comments

Comments
 (0)