Grammar Pocket Reference

Checksum: 4493876f55f9d93e4b5dd3dccc15a337693deb68955e9952648866ba8a8230f6

  • program ::= WS? { toplevel WS? } EOF ;
  • toplevel ::= inner_attribute
  • space_decl ::= "space" WS1 module_path ;
  • use_decl ::= "use" WS1 package_path [ use_group | use_glob ] [ WS1 "as" WS1 ident ] ;
  • use_glob ::= "." "*" ;
  • use_group ::= "." "{" WS? ( "*" | import_items [ WS? "," WS? "*" ]? ) WS? "}" ;
  • import_items ::= import_item { WS? "," WS? import_item } [ WS? "," ] ;
  • import_item ::= ident [ WS1 "as" WS1 ident ] ;
  • export_decl ::= "export" WS1 ( "*" | ident_list | "{" WS? ident_list WS? "}" ) ;
  • const_decl ::= { attr_prefix WS? } "const" WS1 ident [ WS? ":" WS? type_expr ] WS? "=" WS? expr ;
  • static_decl ::= { attr_prefix WS? } "static" WS1 ident [ WS? ":" WS? type_expr ] WS? "=" WS? expr ;
  • global_decl ::= { attr_prefix WS? } "global" WS1 ident [ WS? ":" WS? type_expr ] WS? "=" WS? expr ;
  • type_alias_decl ::= { attr_prefix WS? } "type" WS1 ident [ generic_params ] WS? "=" WS? type_expr ;
  • opaque_type_decl ::= { attr_prefix WS? } "opaque" WS1 "type" WS1 ident [ generic_params ] [ WS? "=" WS? type_expr ] ;
  • extern_type_decl ::= { attr_prefix WS? } "extern" WS1 "type" WS1 ident [ generic_params ] [ WS? ";" ] ;
  • form_decl ::= { attr_prefix WS? } "form" WS1 ident [ generic_params ] [ WS? where_clause ] WS? "{" WS? form_items? WS? "}" ;
  • class_decl ::= { attr_prefix WS? } "class" WS1 ident [ generic_params ] [ WS? where_clause ] WS? "{" WS? class_items? WS? "}" ;
  • union_decl ::= { attr_prefix WS? } "union" WS1 ident [ generic_params ] [ WS? where_clause ] WS? "{" WS? field_list? WS? "}" ;
  • form_items ::= form_item { WS? ","? WS? form_item } [ WS? "," ] ;
  • form_item ::= field_item | const_decl | type_alias_decl | proc_decl ;
  • class_items ::= class_item { WS? ","? WS? class_item } [ WS? "," ] ;
  • class_item ::= field_item | const_decl | type_alias_decl | proc_decl ;
  • field_list ::= field_item { WS? "," WS? field_item } [ WS? "," ] ;
  • field_item ::= { attr_prefix WS? } [ visibility WS1 ] ident WS? ":" WS? type_expr [ WS? "=" WS? expr ] ;
  • bits_decl ::= { attr_prefix WS? } "bits" WS1 ident [ WS? ":" WS? type_expr ] WS? "{" WS? bitfield_list? WS? "}" ;
  • bitfield_list ::= bitfield_item { WS? "," WS? bitfield_item } [ WS? "," ] ;
  • bitfield_item ::= ident WS? ":" WS? expr WS? ".." WS? expr ;
  • pick_decl ::= { attr_prefix WS? } "pick" WS1 ident [ generic_params ] [ WS? ":" WS? type_expr ] [ WS? where_clause ] WS? "{" WS? case_list? WS? "}" ;
  • case_list ::= case_item { WS? "," WS? case_item } [ WS? "," ] ;
  • case_item ::= [ "case" WS1 ] ident [ "(" WS? case_payload? WS? ")" ] [ WS? "=" WS? expr ] ;
  • case_payload ::= case_field { WS? "," WS? case_field } [ WS? "," ] ;
  • case_field ::= ident WS? ":" WS? type_expr | type_expr ;
  • flags_decl ::= { attr_prefix WS? } "flags" WS1 ident [ WS? ":" WS? type_expr ] WS? "{" WS? flag_list? WS? "}" ;
  • flag_list ::= flag_item { WS? "," WS? flag_item } [ WS? "," ] ;
  • flag_item ::= ident [ WS? "=" WS? expr ] ;
  • trait_decl ::= { attr_prefix WS? } [ "unsafe" WS1 ] "trait" WS1 ident [ generic_params ] [ WS? where_clause ] WS? "{" WS? trait_item* WS? "}" ;
  • trait_item ::= proc_signature | const_decl | associated_type_decl ;
  • associated_type_decl ::= "type" WS1 ident [ WS? ":" WS? trait_bound ] [ WS? "=" WS? type_expr ] ;
  • impl_decl ::= { attr_prefix WS? } [ "unsafe" WS1 ] "impl" WS1 impl_head [ WS? where_clause ] WS? "{" WS? impl_item* WS? "}" ;
  • impl_head ::= type_expr | type_expr WS1 "for" WS1 type_expr ;
  • impl_item ::= const_decl | type_alias_decl | associated_type_decl | proc_decl ;
  • proc_signature ::= { attr_prefix WS? } "proc" WS1 ident [ generic_params ]
  • proc_decl ::= { attr_prefix WS? } [ visibility WS1 ] proc_modifier* [ extern_clause WS1 ] "proc" WS1 proc_name [ generic_params ]
  • proc_name ::= ident | "operator" WS? operator_symbol ;
  • operator_symbol ::= "+" | "-" | "*" | "/" | "%"
  • proc_modifier ::= "async" WS1
  • proc_suffix ::= effects_clause
  • extern_block ::= { attr_prefix WS? } "extern" WS1 extern_abi WS? "{" WS? extern_item* WS? "}" ;
  • extern_item ::= extern_proc_decl | extern_type_decl | const_decl ;
  • extern_proc_decl ::= { attr_prefix WS? } "proc" WS1 ident [ generic_params ]
  • extern_clause ::= "extern" WS1 extern_abi ;
  • extern_abi ::= callconv | string_lit ;
  • callconv ::= "C" | "sysv64" | "win64" | "interrupt" | "naked" ;
  • intrinsic_decl ::= { attr_prefix WS? } "intrinsic" WS1 ident [ generic_params ]
  • macro_decl ::= { attr_prefix WS? } "macro" WS1 ident "(" WS? macro_param_list? WS? ")" WS? block ;
  • macro_param_list ::= macro_param { WS? "," WS? macro_param } [ WS? "," ] ;
  • macro_param ::= ident [ WS? ":" WS? macro_kind ] ;
  • macro_kind ::= "expr" | "stmt" | "type" | "ident" | "block" | "item" ;
  • comptime_decl ::= "comptime" WS? block ;
  • static_assert_decl ::= "static_assert" WS? "(" WS? expr [ WS? "," WS? expr ] WS? ")" ;
  • test_decl ::= { attr_prefix WS? } "test" WS1 string_lit WS? block ;
  • bench_decl ::= { attr_prefix WS? } "bench" WS1 string_lit WS? block ;
  • entry_decl ::= { attr_prefix WS? } "entry" WS1 ident WS1 "at" WS1 module_path WS? block ;
  • visibility ::= "pub" | "priv" | "pub" WS? "(" WS? visibility_scope WS? ")" ;
  • visibility_scope ::= "package" | "module" | "super" ;
  • generic_params ::= "[" WS? generic_param_list? WS? "]" ;
  • generic_param_list ::= generic_param { WS? "," WS? generic_param } [ WS? "," ] ;
  • generic_param ::= ident [ WS? ":" WS? trait_bound ] [ WS? "=" WS? type_expr ]
  • where_clause ::= "where" WS1 where_bounds ;
  • where_bounds ::= where_bound { WS? "," WS? where_bound } [ WS? "," ] ;
  • where_bound ::= type_expr WS? ":" WS? trait_bound
  • trait_bound ::= type_expr { WS? "+" WS? type_expr } ;
  • param_list ::= param { WS? "," WS? param } [ WS? "," ] ;
  • param ::= self_param | normal_param | variadic_param ;
  • self_param ::= [ "&" WS? ] [ "mut" WS1 ] "self" ;
  • normal_param ::= [ param_mode WS1 ] pattern [ WS? ":" WS? type_expr ] [ WS? "=" WS? expr ] ;
  • variadic_param ::= "..." | ident WS? ":" WS? "..." type_expr ;
  • param_mode ::= "mut" | "owned" | "borrow" | "move" ;
  • effects_clause ::= "effects" WS? "(" WS? ident_list? WS? ")" ;
  • requires_clause ::= "requires" WS? "(" WS? capability_list? WS? ")" ;
  • capability_list ::= capability { WS? "," WS? capability } [ WS? "," ] ;
  • capability ::= ident { "." ident } ;
  • module_path ::= relative? package_parts ;
  • package_path ::= relative? package_parts ;
  • relative ::= { "." } ;
  • package_parts ::= ident { ( "/" | "." | "::" ) ident } ;
  • ident_list ::= ident { WS? "," WS? ident } [ WS? "," ] ;
  • compiler_decl ::= { attr_prefix WS? } "compiler" WS1 ident WS? "{" WS? compiler_items? WS? "}" ;
  • compiler_items ::= compiler_item { WS? compiler_item } ;
  • compiler_item ::= compiler_phase_decl
  • compiler_phase_decl ::= "phase" WS1 ident WS? "{" WS?
  • compiler_stage_decl ::= "stage" WS1 ident WS? ":" WS? ident_list WS? ";" ;
  • compiler_artifact_decl ::= "artifact" WS1 ident WS? ":" WS? type_expr WS? ";" ;
  • compiler_pipeline_decl ::= "pipeline" WS1 ident WS? "{" WS? pipeline_step* WS? "}" ;
  • pipeline_step ::= "run" WS1 ident [ WS? "as" WS? ident ] WS? ";"
  • compiler_cache_decl ::= "cache" WS1 ident WS? "{" WS? cache_item* WS? "}" ;
  • cache_item ::= "key" WS? ":" WS? type_expr WS? ";"
  • compiler_target_decl ::= "target" WS1 ident WS? "{" WS? target_item* WS? "}" ;
  • target_item ::= ident WS? ":" WS? expr WS? ";" ;
  • query_decl ::= { attr_prefix WS? } "query" WS1 ident
  • query_body ::= block | ";" ;
  • pass_decl ::= { attr_prefix WS? } "pass" WS1 ident WS? "{" WS? pass_items? WS? "}" ;
  • pass_items ::= pass_item { WS? pass_item } ;
  • pass_item ::= "input" WS? ":" WS? type_expr WS? ";"
  • backend_decl ::= { attr_prefix WS? } "backend" WS1 ident WS? "{" WS? backend_items? WS? "}" ;
  • backend_items ::= backend_item { WS? backend_item } ;
  • backend_item ::= "target" WS? ":" WS? string_lit WS? ";"
  • backend_format ::= "c" | "llvm" | "asm" | "object" | "wasm" | "ir" ;
  • backend_output ::= "source" | "assembly" | "object" | "executable" | "library" ;
  • diagnostic_decl ::= { attr_prefix WS? } "diagnostic" WS1 diagnostic_code WS? "{" WS? diagnostic_items? WS? "}" ;
  • diagnostic_code ::= ident | string_lit ;
  • diagnostic_items ::= diagnostic_item { WS? diagnostic_item } ;
  • diagnostic_item ::= "level" WS? ":" WS? diagnostic_level WS? ";"
  • diagnostic_level ::= "error" | "warning" | "note" | "help" | "fatal" ;
  • attr_prefix ::= outer_attribute | doc_comment | docstring ;
  • inner_attribute ::= "#![" attr_path [ "(" WS? attr_arg_list? WS? ")" ] "]" ;
  • outer_attribute ::= "#[" attr_path [ "(" WS? attr_arg_list? WS? ")" ] "]" ;
  • doc_comment ::= "///" { ~NEWLINE ANY } ;
  • docstring ::= raw_string_lit ;
  • attr_path ::= ident { "." ident } ;