-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathmod.rs
More file actions
742 lines (627 loc) · 23.1 KB
/
mod.rs
File metadata and controls
742 lines (627 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
use rbs_encoding_type_t::RBS_ENCODING_UTF_8;
use ruby_rbs_sys::bindings::*;
use std::marker::PhantomData;
use std::ptr::NonNull;
/// Parse RBS code into an AST.
///
/// ```rust
/// use ruby_rbs::node::parse;
/// let rbs_code = r#"type foo = "hello""#;
/// let signature = parse(rbs_code.as_bytes());
/// assert!(signature.is_ok(), "Failed to parse RBS signature");
/// ```
pub fn parse(rbs_code: &[u8]) -> Result<SignatureNode<'_>, String> {
unsafe {
let start_ptr = rbs_code.as_ptr() as *const std::os::raw::c_char;
let end_ptr = start_ptr.add(rbs_code.len());
let bytes = rbs_code.len() as i32;
let raw_rbs_string_value = rbs_string_new(start_ptr, end_ptr);
let encoding_ptr = &rbs_encodings[RBS_ENCODING_UTF_8 as usize] as *const rbs_encoding_t;
let parser = rbs_parser_new(raw_rbs_string_value, encoding_ptr, 0, bytes);
let mut signature: *mut rbs_signature_t = std::ptr::null_mut();
let result = rbs_parse_signature(parser, &mut signature);
let signature_node = SignatureNode {
parser: NonNull::new_unchecked(parser),
pointer: signature,
marker: PhantomData,
};
if result {
Ok(signature_node)
} else {
let error_message = (*parser)
.error
.as_ref()
.filter(|error| !error.message.is_null())
.map(|error| {
std::ffi::CStr::from_ptr(error.message)
.to_string_lossy()
.into_owned()
})
.unwrap_or_else(|| String::from("Failed to parse RBS signature"));
Err(error_message)
}
}
}
impl Drop for SignatureNode<'_> {
fn drop(&mut self) {
unsafe {
rbs_parser_free(self.parser.as_ptr());
}
}
}
/// Instance variable name specification for attributes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AttrIvarName {
/// The attribute has inferred instance variable (nil)
Unspecified,
/// The attribute has no instance variable (false)
Empty,
/// The attribute has instance variable with the given name
Name(rbs_constant_id_t),
}
impl AttrIvarName {
/// Converts the raw C struct to the Rust enum.
#[must_use]
pub fn from_raw(raw: rbs_attr_ivar_name_t) -> Self {
match raw.tag {
rbs_attr_ivar_name_tag::RBS_ATTR_IVAR_NAME_TAG_UNSPECIFIED => Self::Unspecified,
rbs_attr_ivar_name_tag::RBS_ATTR_IVAR_NAME_TAG_EMPTY => Self::Empty,
rbs_attr_ivar_name_tag::RBS_ATTR_IVAR_NAME_TAG_NAME => Self::Name(raw.name),
_ => panic!("Unknown ivar_name_tag: {}", raw.tag),
}
}
}
pub struct NodeList<'a> {
parser: NonNull<rbs_parser_t>,
pointer: *mut rbs_node_list_t,
marker: PhantomData<&'a mut rbs_node_list_t>,
}
impl<'a> NodeList<'a> {
#[must_use]
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_node_list_t) -> Self {
Self {
parser,
pointer,
marker: PhantomData,
}
}
/// Returns an iterator over the nodes.
#[must_use]
pub fn iter(&self) -> NodeListIter<'a> {
NodeListIter {
parser: self.parser,
current: unsafe { (*self.pointer).head },
marker: PhantomData,
}
}
}
pub struct NodeListIter<'a> {
parser: NonNull<rbs_parser_t>,
current: *mut rbs_node_list_node_t,
marker: PhantomData<&'a mut rbs_node_list_node_t>,
}
impl<'a> Iterator for NodeListIter<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.current.is_null() {
None
} else {
let pointer_data = unsafe { *self.current };
let node = Node::new(self.parser, pointer_data.node);
self.current = pointer_data.next;
Some(node)
}
}
}
pub struct RBSHash<'a> {
parser: NonNull<rbs_parser_t>,
pointer: *mut rbs_hash,
marker: PhantomData<&'a mut rbs_hash>,
}
impl<'a> RBSHash<'a> {
#[must_use]
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_hash) -> Self {
Self {
parser,
pointer,
marker: PhantomData,
}
}
/// Returns an iterator over the key-value pairs.
#[must_use]
pub fn iter(&self) -> RBSHashIter<'a> {
RBSHashIter {
parser: self.parser,
current: unsafe { (*self.pointer).head },
marker: PhantomData,
}
}
}
pub struct RBSHashIter<'a> {
parser: NonNull<rbs_parser_t>,
current: *mut rbs_hash_node_t,
marker: PhantomData<&'a mut rbs_hash_node_t>,
}
impl<'a> Iterator for RBSHashIter<'a> {
type Item = (Node<'a>, Node<'a>);
fn next(&mut self) -> Option<Self::Item> {
if self.current.is_null() {
None
} else {
let pointer_data = unsafe { *self.current };
let key = Node::new(self.parser, pointer_data.key);
let value = Node::new(self.parser, pointer_data.value);
self.current = pointer_data.next;
Some((key, value))
}
}
}
pub struct RBSLocationRange {
range: rbs_location_range,
}
impl RBSLocationRange {
#[must_use]
pub fn new(range: rbs_location_range) -> Self {
Self { range }
}
#[must_use]
pub fn start(&self) -> i32 {
self.range.start_byte
}
#[must_use]
pub fn end(&self) -> i32 {
self.range.end_byte
}
}
pub struct RBSLocationRangeList<'a> {
#[allow(dead_code)]
parser: NonNull<rbs_parser_t>,
pointer: *mut rbs_location_range_list_t,
marker: PhantomData<&'a mut rbs_location_range_list_t>,
}
impl<'a> RBSLocationRangeList<'a> {
/// Returns an iterator over the location ranges.
#[must_use]
pub fn iter(&self) -> RBSLocationRangeListIter {
RBSLocationRangeListIter {
current: unsafe { (*self.pointer).head },
}
}
}
pub struct RBSLocationRangeListIter {
current: *mut rbs_location_range_list_node_t,
}
impl Iterator for RBSLocationRangeListIter {
type Item = RBSLocationRange;
fn next(&mut self) -> Option<Self::Item> {
if self.current.is_null() {
None
} else {
let pointer_data = unsafe { *self.current };
let range = RBSLocationRange::new(pointer_data.range);
self.current = pointer_data.next;
Some(range)
}
}
}
#[derive(Debug)]
pub struct RBSString {
pointer: *const rbs_string_t,
}
impl RBSString {
#[must_use]
pub fn new(pointer: *const rbs_string_t) -> Self {
Self { pointer }
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
unsafe {
let s = *self.pointer;
std::slice::from_raw_parts(s.start as *const u8, s.end.offset_from(s.start) as usize)
}
}
}
impl SymbolNode<'_> {
#[must_use]
pub fn name(&self) -> &[u8] {
unsafe {
let constant_ptr = rbs_constant_pool_id_to_constant(
&(*self.parser.as_ptr()).constant_pool,
(*self.pointer).constant_id,
);
if constant_ptr.is_null() {
panic!("Constant ID for symbol is not present in the pool");
}
let constant = &*constant_ptr;
std::slice::from_raw_parts(constant.start, constant.length)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_error_contains_actual_message() {
let rbs_code = "class { end";
let result = parse(rbs_code.as_bytes());
let error_message = result.unwrap_err();
assert_eq!(error_message, "expected one of class/module/constant name");
}
#[test]
fn test_parse() {
let rbs_code = r#"type foo = "hello""#;
let signature = parse(rbs_code.as_bytes());
assert!(signature.is_ok(), "Failed to parse RBS signature");
let rbs_code2 = r#"class Foo end"#;
let signature2 = parse(rbs_code2.as_bytes());
assert!(signature2.is_ok(), "Failed to parse RBS signature");
}
#[test]
fn test_parse_integer() {
let rbs_code = r#"type foo = 1"#;
let signature = parse(rbs_code.as_bytes());
assert!(signature.is_ok(), "Failed to parse RBS signature");
let signature_node = signature.unwrap();
if let Node::TypeAlias(node) = signature_node.declarations().iter().next().unwrap()
&& let Node::LiteralType(literal) = node.type_()
&& let Node::Integer(integer) = literal.literal()
{
assert_eq!(
"1".to_string(),
String::from_utf8(integer.string_representation().as_bytes().to_vec()).unwrap()
);
} else {
panic!("No literal type node found");
}
}
#[test]
fn test_rbs_hash_via_record_type() {
// RecordType stores its fields in an RBSHash via all_fields()
let rbs_code = r#"type foo = { name: String, age: Integer }"#;
let signature = parse(rbs_code.as_bytes());
assert!(signature.is_ok(), "Failed to parse RBS signature");
let signature_node = signature.unwrap();
if let Node::TypeAlias(type_alias) = signature_node.declarations().iter().next().unwrap()
&& let Node::RecordType(record) = type_alias.type_()
{
let hash = record.all_fields();
let fields: Vec<_> = hash.iter().collect();
assert_eq!(fields.len(), 2, "Expected 2 fields in record");
// Build a map of field names to type names
let mut field_types: Vec<(String, String)> = Vec::new();
for (key, value) in &fields {
let Node::Symbol(sym) = key else {
panic!("Expected Symbol key");
};
let Node::RecordFieldType(field_type) = value else {
panic!("Expected RecordFieldType value");
};
let Node::ClassInstanceType(class_type) = field_type.type_() else {
panic!("Expected ClassInstanceType");
};
let key_name = String::from_utf8(sym.name().to_vec()).unwrap();
let type_name_node = class_type.name();
let type_name_sym = type_name_node.name();
let type_name = String::from_utf8(type_name_sym.name().to_vec()).unwrap();
field_types.push((key_name, type_name));
}
assert!(
field_types.contains(&("name".to_string(), "String".to_string())),
"Expected 'name: String'"
);
assert!(
field_types.contains(&("age".to_string(), "Integer".to_string())),
"Expected 'age: Integer'"
);
} else {
panic!("Expected TypeAlias with RecordType");
}
}
#[test]
fn visitor_test() {
struct Visitor {
visited: Vec<String>,
}
impl Visit for Visitor {
fn visit_bool_type_node(&mut self, node: &BoolTypeNode) {
self.visited.push("type:bool".to_string());
crate::node::visit_bool_type_node(self, node);
}
fn visit_class_node(&mut self, node: &ClassNode) {
self.visited.push(format!(
"class:{}",
String::from_utf8(node.name().name().name().to_vec()).unwrap()
));
crate::node::visit_class_node(self, node);
}
fn visit_class_instance_type_node(&mut self, node: &ClassInstanceTypeNode) {
self.visited.push(format!(
"type:{}",
String::from_utf8(node.name().name().name().to_vec()).unwrap()
));
crate::node::visit_class_instance_type_node(self, node);
}
fn visit_class_super_node(&mut self, node: &ClassSuperNode) {
self.visited.push(format!(
"super:{}",
String::from_utf8(node.name().name().name().to_vec()).unwrap()
));
crate::node::visit_class_super_node(self, node);
}
fn visit_function_type_node(&mut self, node: &FunctionTypeNode) {
let count = node.required_positionals().iter().count();
self.visited
.push(format!("function:required_positionals:{count}"));
crate::node::visit_function_type_node(self, node);
}
fn visit_method_definition_node(&mut self, node: &MethodDefinitionNode) {
self.visited.push(format!(
"method:{}",
String::from_utf8(node.name().name().to_vec()).unwrap()
));
crate::node::visit_method_definition_node(self, node);
}
fn visit_record_type_node(&mut self, node: &RecordTypeNode) {
self.visited.push("record".to_string());
crate::node::visit_record_type_node(self, node);
}
fn visit_symbol_node(&mut self, node: &SymbolNode) {
self.visited.push(format!(
"symbol:{}",
String::from_utf8(node.name().to_vec()).unwrap()
));
crate::node::visit_symbol_node(self, node);
}
}
let rbs_code = r#"
class Foo < Bar
def process: ({ name: String, age: Integer }, bool) -> void
end
"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let mut visitor = Visitor {
visited: Vec::new(),
};
visitor.visit(&signature.as_node());
assert_eq!(
vec![
"class:Foo",
"symbol:Foo",
"super:Bar",
"symbol:Bar",
"method:process",
"symbol:process",
"function:required_positionals:2",
"record",
"symbol:name",
"type:String",
"symbol:String",
"symbol:age",
"type:Integer",
"symbol:Integer",
"type:bool",
],
visitor.visited
);
}
#[test]
fn test_node_location_ranges() {
let rbs_code = r#"type foo = 1"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let declaration = signature.declarations().iter().next().unwrap();
let Node::TypeAlias(type_alias) = declaration else {
panic!("Expected TypeAlias");
};
// TypeAlias spans the entire declaration
let loc = type_alias.location();
assert_eq!(0, loc.start());
assert_eq!(12, loc.end());
// The literal "1" is at position 11-12
let Node::LiteralType(literal) = type_alias.type_() else {
panic!("Expected LiteralType");
};
let Node::Integer(integer) = literal.literal() else {
panic!("Expected Integer");
};
let int_loc = integer.location();
assert_eq!(11, int_loc.start());
assert_eq!(12, int_loc.end());
}
#[test]
fn test_sub_locations() {
let rbs_code = r#"class Foo < Bar end"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let declaration = signature.declarations().iter().next().unwrap();
let Node::Class(class) = declaration else {
panic!("Expected Class");
};
// Test required sub-locations
let keyword_loc = class.keyword_location();
assert_eq!(0, keyword_loc.start());
assert_eq!(5, keyword_loc.end());
let name_loc = class.name_location();
assert_eq!(6, name_loc.start());
assert_eq!(9, name_loc.end());
let end_loc = class.end_location();
assert_eq!(16, end_loc.start());
assert_eq!(19, end_loc.end());
// Test optional sub-location that's present
let lt_loc = class.lt_location();
assert!(lt_loc.is_some());
let lt = lt_loc.unwrap();
assert_eq!(10, lt.start());
assert_eq!(11, lt.end());
// Test optional sub-location that's not present (no type params in this class)
let type_params_loc = class.type_params_location();
assert!(type_params_loc.is_none());
}
#[test]
fn test_type_alias_sub_locations() {
let rbs_code = r#"type foo = String"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let declaration = signature.declarations().iter().next().unwrap();
let Node::TypeAlias(type_alias) = declaration else {
panic!("Expected TypeAlias");
};
// Test required sub-locations
let keyword_loc = type_alias.keyword_location();
assert_eq!(0, keyword_loc.start());
assert_eq!(4, keyword_loc.end());
let name_loc = type_alias.name_location();
assert_eq!(5, name_loc.start());
assert_eq!(8, name_loc.end());
let eq_loc = type_alias.eq_location();
assert_eq!(9, eq_loc.start());
assert_eq!(10, eq_loc.end());
// Test optional sub-location that's not present (no type params)
let type_params_loc = type_alias.type_params_location();
assert!(type_params_loc.is_none());
}
#[test]
fn test_module_sub_locations() {
let rbs_code = r#"module Foo[T] : Bar end"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let declaration = signature.declarations().iter().next().unwrap();
let Node::Module(module) = declaration else {
panic!("Expected Module");
};
// Test required sub-locations
let keyword_loc = module.keyword_location();
assert_eq!(0, keyword_loc.start());
assert_eq!(6, keyword_loc.end());
let name_loc = module.name_location();
assert_eq!(7, name_loc.start());
assert_eq!(10, name_loc.end());
let end_loc = module.end_location();
assert_eq!(20, end_loc.start());
assert_eq!(23, end_loc.end());
// Test optional sub-locations that are present
let type_params_loc = module.type_params_location();
assert!(type_params_loc.is_some());
let tp = type_params_loc.unwrap();
assert_eq!(10, tp.start());
assert_eq!(13, tp.end());
let colon_loc = module.colon_location();
assert!(colon_loc.is_some());
let colon = colon_loc.unwrap();
assert_eq!(14, colon.start());
assert_eq!(15, colon.end());
let self_types_loc = module.self_types_location();
assert!(self_types_loc.is_some());
let st = self_types_loc.unwrap();
assert_eq!(16, st.start());
assert_eq!(19, st.end());
}
#[test]
fn test_enum_types() {
let rbs_code = r#"
class Foo
attr_reader name: String
def self.process: () -> void
alias instance_method target_method
alias self.singleton_method self.target_method
end
class Bar[out T, in U, V]
end
"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let declarations: Vec<_> = signature.declarations().iter().collect();
// Test class Foo
let Node::Class(class_foo) = &declarations[0] else {
panic!("Expected Class");
};
let members: Vec<_> = class_foo.members().iter().collect();
// attr_reader - should be instance with unspecified visibility (default)
if let Node::AttrReader(attr) = &members[0] {
assert_eq!(attr.kind(), AttributeKind::Instance);
assert_eq!(attr.visibility(), AttributeVisibility::Unspecified);
} else {
panic!("Expected AttrReader");
}
// def self.process - should be singleton method with unspecified visibility (default)
if let Node::MethodDefinition(method) = &members[1] {
assert_eq!(method.kind(), MethodDefinitionKind::Singleton);
assert_eq!(method.visibility(), MethodDefinitionVisibility::Unspecified);
} else {
panic!("Expected MethodDefinition");
}
// alias instance_method
if let Node::Alias(alias) = &members[2] {
assert_eq!(alias.kind(), AliasKind::Instance);
} else {
panic!("Expected Alias");
}
// alias self.singleton_method
if let Node::Alias(alias) = &members[3] {
assert_eq!(alias.kind(), AliasKind::Singleton);
} else {
panic!("Expected Alias");
}
// Test class Bar with type params
let Node::Class(class_bar) = &declarations[1] else {
panic!("Expected Class");
};
let type_params: Vec<_> = class_bar.type_params().iter().collect();
assert_eq!(type_params.len(), 3);
// out T - covariant
if let Node::TypeParam(param) = &type_params[0] {
assert_eq!(param.variance(), TypeParamVariance::Covariant);
} else {
panic!("Expected TypeParam");
}
// in U - contravariant
if let Node::TypeParam(param) = &type_params[1] {
assert_eq!(param.variance(), TypeParamVariance::Contravariant);
} else {
panic!("Expected TypeParam");
}
// V - invariant (default)
if let Node::TypeParam(param) = &type_params[2] {
assert_eq!(param.variance(), TypeParamVariance::Invariant);
} else {
panic!("Expected TypeParam");
}
}
#[test]
fn test_ivar_name_enum() {
let rbs_code = r#"
class Foo
attr_reader name: String
attr_accessor age(): Integer
attr_writer email(@email): String
end
"#;
let signature = parse(rbs_code.as_bytes()).unwrap();
let Node::Class(class) = signature.declarations().iter().next().unwrap() else {
panic!("Expected Class");
};
let members: Vec<_> = class.members().iter().collect();
// attr_reader name: String - should be Unspecified (inferred as @name)
if let Node::AttrReader(attr) = &members[0] {
let ivar = attr.ivar_name();
assert_eq!(ivar, AttrIvarName::Unspecified);
} else {
panic!("Expected AttrReader");
}
// attr_accessor age(): Integer - should be Empty (no ivar)
if let Node::AttrAccessor(attr) = &members[1] {
let ivar = attr.ivar_name();
assert_eq!(ivar, AttrIvarName::Empty);
} else {
panic!("Expected AttrAccessor");
}
// attr_writer email(@email): String - should be Name with constant ID
if let Node::AttrWriter(attr) = &members[2] {
let ivar = attr.ivar_name();
match ivar {
AttrIvarName::Name(id) => {
assert!(id > 0, "Expected valid constant ID");
}
_ => panic!("Expected AttrIvarName::Name, got {:?}", ivar),
}
} else {
panic!("Expected AttrWriter");
}
}
}