Skip to content

Commit 3fd3b66

Browse files
committed
parse unsafe extern blocks and safe fns / static items
1 parent 4bbd594 commit 3fd3b66

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

grammar.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module.exports = grammar({
109109
[$.parameters, $.tuple_struct_pattern],
110110
[$.array_expression],
111111
[$.visibility_modifier],
112+
[$.foreign_mod_item, $.function_modifiers],
112113
[$.visibility_modifier, $.scoped_identifier, $.scoped_type_identifier],
113114
],
114115

@@ -279,6 +280,7 @@ module.exports = grammar({
279280
),
280281

281282
foreign_mod_item: $ => seq(
283+
optional('unsafe'),
282284
optional($.visibility_modifier),
283285
$.extern_modifier,
284286
choice(
@@ -404,6 +406,7 @@ module.exports = grammar({
404406

405407
static_item: $ => seq(
406408
optional($.visibility_modifier),
409+
optional(choice('unsafe', 'safe')),
407410
'static',
408411

409412
// Not actual rust syntax, but made popular by the lazy_static crate.
@@ -461,6 +464,7 @@ module.exports = grammar({
461464
'default',
462465
'const',
463466
'unsafe',
467+
'safe',
464468
$.extern_modifier,
465469
)),
466470

test/corpus/declarations.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,3 +2613,65 @@ pub struct Loaf<T: Sized, const N: usize = 1>([T; N]);
26132613
(array_type
26142614
(type_identifier)
26152615
(identifier)))))
2616+
2617+
================================================================================
2618+
Unsafe extern blocks
2619+
================================================================================
2620+
2621+
unsafe extern "C" {
2622+
pub safe fn sqrt(x: f64) -> f64;
2623+
pub unsafe fn strlen(p: *const std::ffi::c_char) -> usize;
2624+
pub fn free(p: *mut core::ffi::c_void);
2625+
pub safe static IMPORTANT_BYTES: [u8; 256];
2626+
}
2627+
2628+
--------------------------------------------------------------------------------
2629+
2630+
(source_file
2631+
(foreign_mod_item
2632+
(extern_modifier
2633+
(string_literal
2634+
(string_content)))
2635+
(declaration_list
2636+
(function_signature_item
2637+
(visibility_modifier)
2638+
(function_modifiers)
2639+
(identifier)
2640+
(parameters
2641+
(parameter
2642+
(identifier)
2643+
(primitive_type)))
2644+
(primitive_type))
2645+
(function_signature_item
2646+
(visibility_modifier)
2647+
(function_modifiers)
2648+
(identifier)
2649+
(parameters
2650+
(parameter
2651+
(identifier)
2652+
(pointer_type
2653+
(scoped_type_identifier
2654+
(scoped_identifier
2655+
(identifier)
2656+
(identifier))
2657+
(type_identifier)))))
2658+
(primitive_type))
2659+
(function_signature_item
2660+
(visibility_modifier)
2661+
(identifier)
2662+
(parameters
2663+
(parameter
2664+
(identifier)
2665+
(pointer_type
2666+
(mutable_specifier)
2667+
(scoped_type_identifier
2668+
(scoped_identifier
2669+
(identifier)
2670+
(identifier))
2671+
(type_identifier))))))
2672+
(static_item
2673+
(visibility_modifier)
2674+
(identifier)
2675+
(array_type
2676+
(primitive_type)
2677+
(integer_literal))))))

0 commit comments

Comments
 (0)