Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/ruby-prism-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings {
.allowlist_type("pm_diagnostic_t")
.allowlist_type("pm_list_t")
.allowlist_type("pm_magic_comment_t")
.allowlist_type("pm_line_offset_list_t")
.allowlist_type("pm_node_t")
.allowlist_type("pm_node_type")
.allowlist_type("pm_parser_t")
Expand Down
22 changes: 22 additions & 0 deletions rust/ruby-prism-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ mod bindings {
}

pub use self::bindings::*;

/// Line and column information for a given byte offset relative to the
/// beginning of the source.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct pm_line_column_t {
/// The 1-indexed line number relative to the start line configured on the
/// parser.
pub line: i32,
/// The 0-indexed column number in bytes.
pub column: u32,
Comment on lines +39 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message for this explains that these are troublesome for the WASM target...but why are they troublesome, and can that be fixed instead? They really shouldn't be treated any differently than other structures, and it would be better to not maintain these declarations by hand. I trust bindgen more than manual declarations.

}

extern "C" {
/// Return the line and column number for the given byte offset relative to
/// the beginning of the source.
pub fn pm_line_offset_list_line_column(
list: *const pm_line_offset_list_t,
cursor: u32,
start_line: i32,
) -> pm_line_column_t;
}
44 changes: 44 additions & 0 deletions rust/ruby-prism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ mod bindings {
}

mod node;
mod node_ext;
mod parse_result;

use std::mem::MaybeUninit;
use std::ptr::NonNull;

pub use self::bindings::*;
pub use self::node::{ConstantId, ConstantList, ConstantListIter, Integer, NodeList, NodeListIter};
pub use self::node_ext::ConstantPathError;
pub use self::parse_result::{Comment, CommentType, Comments, Diagnostic, Diagnostics, Location, MagicComment, MagicComments, ParseResult};

use ruby_prism_sys::{pm_parse, pm_parser_init, pm_parser_t};
Expand Down Expand Up @@ -160,6 +162,48 @@ mod tests {
assert_eq!(slice, "222");
}

#[test]
#[allow(clippy::similar_names)]
fn location_line_column_test() {
let source = "foo\nbar\nbaz";
let result = parse(source.as_ref());

let node = result.node();
let program = node.as_program_node().unwrap();
let statements = program.statements().body();
let mut iter = statements.iter();
let _foo = iter.next().unwrap();
let bar = iter.next().unwrap();
let baz = iter.next().unwrap();

let bar_loc = bar.location();
assert_eq!(bar_loc.start_line(), 2);
assert_eq!(bar_loc.end_line(), 2);
assert_eq!(bar_loc.start_column(), 0);
assert_eq!(bar_loc.end_column(), 3);

let baz_loc = baz.location();
assert_eq!(baz_loc.start_line(), 3);
assert_eq!(baz_loc.end_line(), 3);
assert_eq!(baz_loc.start_column(), 0);
assert_eq!(baz_loc.end_column(), 3);
}

#[test]
fn test_chop() {
let result = parse(b"foo");
let mut location = result.node().as_program_node().unwrap().location();

assert_eq!(location.chop().as_slice(), b"fo");
assert_eq!(location.chop().chop().chop().as_slice(), b"");

// Check that we don't go negative.
for _ in 0..10 {
location = location.chop();
}
assert_eq!(location.as_slice(), b"");
}

#[test]
fn visitor_test() {
use super::{visit_interpolated_regular_expression_node, visit_regular_expression_node, InterpolatedRegularExpressionNode, RegularExpressionNode, Visit};
Expand Down
Loading