Skip to content

Commit f21b6a0

Browse files
committed
parse unsafe extern blocks and safe fns / static items
1 parent 18b0515 commit f21b6a0

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

grammar.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ module.exports = grammar({
280280

281281
foreign_mod_item: $ => seq(
282282
optional($.visibility_modifier),
283+
optional($._item_modifiers),
283284
$.extern_modifier,
284285
choice(
285286
';',
@@ -404,6 +405,7 @@ module.exports = grammar({
404405

405406
static_item: $ => seq(
406407
optional($.visibility_modifier),
408+
optional(choice('unsafe', 'safe')),
407409
'static',
408410

409411
// Not actual rust syntax, but made popular by the lazy_static crate.
@@ -456,12 +458,21 @@ module.exports = grammar({
456458
';',
457459
),
458460

459-
function_modifiers: $ => repeat1(choice(
461+
function_modifiers: $ => choice(
462+
$._item_modifiers,
463+
$.extern_modifier,
464+
seq(
465+
$._item_modifiers,
466+
$.extern_modifier,
467+
)
468+
),
469+
470+
_item_modifiers: _ => repeat1(choice(
460471
'async',
461472
'default',
462473
'const',
463474
'unsafe',
464-
$.extern_modifier,
475+
'safe',
465476
)),
466477

467478
where_clause: $ => prec.right(seq(

test/corpus/declarations.txt

+62
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)