Skip to content

Commit 380ea4f

Browse files
committed
parse unsafe extern blocks and safe fns / static items
1 parent 02da1b2 commit 380ea4f

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.
@@ -460,6 +463,7 @@ module.exports = grammar({
460463
'default',
461464
'const',
462465
'unsafe',
466+
'safe',
463467
$.extern_modifier,
464468
)),
465469

test/corpus/declarations.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,3 +2526,65 @@ pub struct Loaf<T: Sized, const N: usize = 1>([T; N]);
25262526
(array_type
25272527
(type_identifier)
25282528
(identifier)))))
2529+
2530+
================================================================================
2531+
Unsafe extern blocks
2532+
================================================================================
2533+
2534+
unsafe extern "C" {
2535+
pub safe fn sqrt(x: f64) -> f64;
2536+
pub unsafe fn strlen(p: *const std::ffi::c_char) -> usize;
2537+
pub fn free(p: *mut core::ffi::c_void);
2538+
pub safe static IMPORTANT_BYTES: [u8; 256];
2539+
}
2540+
2541+
--------------------------------------------------------------------------------
2542+
2543+
(source_file
2544+
(foreign_mod_item
2545+
(extern_modifier
2546+
(string_literal
2547+
(string_content)))
2548+
(declaration_list
2549+
(function_signature_item
2550+
(visibility_modifier)
2551+
(function_modifiers)
2552+
(identifier)
2553+
(parameters
2554+
(parameter
2555+
(identifier)
2556+
(primitive_type)))
2557+
(primitive_type))
2558+
(function_signature_item
2559+
(visibility_modifier)
2560+
(function_modifiers)
2561+
(identifier)
2562+
(parameters
2563+
(parameter
2564+
(identifier)
2565+
(pointer_type
2566+
(scoped_type_identifier
2567+
(scoped_identifier
2568+
(identifier)
2569+
(identifier))
2570+
(type_identifier)))))
2571+
(primitive_type))
2572+
(function_signature_item
2573+
(visibility_modifier)
2574+
(identifier)
2575+
(parameters
2576+
(parameter
2577+
(identifier)
2578+
(pointer_type
2579+
(mutable_specifier)
2580+
(scoped_type_identifier
2581+
(scoped_identifier
2582+
(identifier)
2583+
(identifier))
2584+
(type_identifier))))))
2585+
(static_item
2586+
(visibility_modifier)
2587+
(identifier)
2588+
(array_type
2589+
(primitive_type)
2590+
(integer_literal))))))

0 commit comments

Comments
 (0)