Skip to content

Commit 28c8cb1

Browse files
committed
parse unsafe extern blocks and safe fns / static items
1 parent 261b202 commit 28c8cb1

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

grammar.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ module.exports = grammar({
110110
[$.array_expression],
111111
[$.visibility_modifier],
112112
[$.visibility_modifier, $.scoped_identifier, $.scoped_type_identifier],
113-
[$.foreign_mod_item, $.function_modifiers],
114113
],
115114

116115
word: $ => $.identifier,
@@ -280,7 +279,8 @@ module.exports = grammar({
280279
),
281280

282281
foreign_mod_item: $ => seq(
283-
optional('unsafe'),
282+
optional($.visibility_modifier),
283+
optional($._item_modifiers),
284284
$.extern_modifier,
285285
choice(
286286
';',
@@ -405,6 +405,7 @@ module.exports = grammar({
405405

406406
static_item: $ => seq(
407407
optional($.visibility_modifier),
408+
optional(choice('unsafe', 'safe')),
408409
'static',
409410

410411
// Not actual rust syntax, but made popular by the lazy_static crate.
@@ -457,12 +458,21 @@ module.exports = grammar({
457458
';',
458459
),
459460

460-
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(
461471
'async',
462472
'default',
463473
'const',
464474
'unsafe',
465-
$.extern_modifier,
475+
'safe',
466476
)),
467477

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

test/corpus/declarations.txt

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

0 commit comments

Comments
 (0)