Lines Matching +full:path +full:- +full:parse
9 use crate::parse::{Parse, ParseStream, Parser, Result};
20 /// - Outer attributes like `#[repr(transparent)]`. These appear outside or
23 /// - Inner attributes like `#![feature(proc_macro)]`. These appear inside
26 /// - Outer one-line doc comments like `/// Example`.
28 /// - Inner one-line doc comments like `//! Please file an issue`.
30 /// - Outer documentation blocks `/** Example */`.
32 /// - Inner documentation blocks `/*! Please file an issue */`.
37 /// Every attribute has a `path` that indicates the intended interpretation
38 /// of the rest of the attribute's contents. The path and the optional
42 /// - Meta::Path — attributes whose information content conveys just a
43 /// path, for example the `#[test]` attribute.
45 /// - Meta::List — attributes that carry arbitrary tokens after the
46 /// path, surrounded by a delimiter (parenthesis, bracket, or brace). For
49 /// - Meta::NameValue — attributes with an `=` sign after the path,
50 /// followed by a Rust expression. For example `#[path =
53 /// All doc comments are represented in the NameValue style with a path of
59 /// ~~~~~~Path
62 /// #[path = "sys/windows.rs"]
63 /// ~~~~Path
67 /// ^^^^Meta::Path
74 /// This type does not implement the [`Parse`] trait and thus cannot be
75 /// parsed directly by [`ParseStream::parse`]. Instead use
78 /// which you intend to parse.
80 /// [`Parse`]: parse::Parse
81 /// [`ParseStream::parse`]: parse::ParseBuffer::parse
82 /// [`ParseStream::call`]: parse::ParseBuffer::call
86 /// use syn::parse::{Parse, ParseStream};
90 /// // #[path = "s.tmpl"]
99 /// impl Parse for UnitStruct {
100 /// fn parse(input: ParseStream) -> Result<Self> {
103 /// struct_token: input.parse()?,
104 /// name: input.parse()?,
105 /// semi_token: input.parse()?,
118 /// TokenStream`. Macros are expected to check the `path` of the attribute,
119 /// decide whether they recognize it, and then parse the remaining tokens
121 /// attribute. Use [`parse_args()`] to parse those tokens into the expected
142 /// * Multi-line comments...
154 /// * Multi-line comments...
174 /// Returns the path that identifies the interpretation of this attribute.
177 /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`.
178 pub fn path(&self) -> &Path { in path() argument
179 self.meta.path() in path()
182 /// Parse the arguments to the attribute as a syntax tree.
206 /// if attr.path().is_ident("precondition") {
214 pub fn parse_args<T: Parse>(&self) -> Result<T> { in parse_args()
215 self.parse_args_with(T::parse) in parse_args()
218 /// Parse the arguments to the attribute using the given parser.
231 /// // Attribute does not have a Parse impl, so we couldn't directly do:
237 pub fn parse_args_with<F: Parser>(&self, parser: F) -> Result<F::Output> { in parse_args_with()
239 Meta::Path(path) => Err(crate::error::new2( in parse_args_with()
240 path.segments.first().unwrap().ident.span(), in parse_args_with()
241 path.segments.last().unwrap().ident.span(), in parse_args_with()
245 parsing::DisplayPath(path), in parse_args_with()
253 parsing::DisplayPath(&meta.path), in parse_args_with()
260 /// Parse the arguments to the attribute, expecting it to follow the
261 /// conventional structure used by most of Rust's built-in attributes.
266 /// need to parse arbitrarily goofy attribute syntax.
268 /// [syntax]: https://doc.rust-lang.org/reference/attributes.html#meta-item-attribute-syntax
272 /// We'll parse a struct, and then parse some of Rust's `#[repr]` attribute
288 /// if attr.path().is_ident("repr") {
291 /// if meta.path.is_ident("C") {
297 /// if meta.path.is_ident("transparent") {
303 /// if meta.path.is_ident("align") {
306 /// let lit: LitInt = content.parse()?;
313 /// if meta.path.is_ident("packed") {
317 /// let lit: LitInt = content.parse()?;
354 /// if attr.path().is_ident("repr") {
359 /// Meta::Path(path) if path.is_ident("C") => {
364 /// Meta::List(meta) if meta.path.is_ident("align") => {
385 logic: impl FnMut(ParseNestedMeta) -> Result<()>, in parse_nested_meta()
386 ) -> Result<()> { in parse_nested_meta()
395 /// [*Parsing from tokens to Attribute*](#parsing-from-tokens-to-attribute).
398 pub fn parse_outer(input: ParseStream) -> Result<Vec<Self>> { in parse_outer()
411 /// [*Parsing from tokens to Attribute*](#parsing-from-tokens-to-attribute).
414 pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> { in parse_inner()
427 /// - `#[repr(transparent)]`
428 /// - `/// # Example`
429 /// - `/** Please file an issue */`
433 /// - `#![feature(proc_macro)]`
434 /// - `//! # Example`
435 /// - `/*! Please file an issue */`
444 /// Content of a compile-time structured attribute.
446 /// ## Path
448 /// A meta path is like the `test` in `#[test]`.
456 /// A name-value meta is like the `path = "..."` in `#[path =
463 /// [syntax tree enum]: Expr#syntax-tree-enums
466 Path(Path),
471 /// A name-value pair within an attribute, like `feature = "nightly"`.
480 pub path: Path,
487 /// A name-value pair within an attribute, like `feature = "nightly"`.
490 pub path: Path,
497 /// Returns the path that begins this structured meta item.
500 /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`.
501 pub fn path(&self) -> &Path { in path() argument
503 Meta::Path(path) => path, in path()
504 Meta::List(meta) => &meta.path, in path()
505 Meta::NameValue(meta) => &meta.path, in path()
512 pub fn require_path_only(&self) -> Result<&Path> { in require_path_only() argument
514 Meta::Path(path) => return Ok(path), in require_path_only()
521 /// Error if this is a `Meta::Path` or `Meta::NameValue`.
524 pub fn require_list(&self) -> Result<&MetaList> { in require_list()
527 Meta::Path(path) => Err(crate::error::new2( in require_list()
528 path.segments.first().unwrap().ident.span(), in require_list()
529 path.segments.last().unwrap().ident.span(), in require_list()
532 parsing::DisplayPath(path), in require_list()
539 /// Error if this is a `Meta::Path` or `Meta::List`.
542 pub fn require_name_value(&self) -> Result<&MetaNameValue> { in require_name_value()
545 Meta::Path(path) => Err(crate::error::new2( in require_name_value()
546 path.segments.first().unwrap().ident.span(), in require_name_value()
547 path.segments.last().unwrap().ident.span(), in require_name_value()
550 parsing::DisplayPath(path), in require_name_value()
562 pub fn parse_args<T: Parse>(&self) -> Result<T> { in parse_args()
563 self.parse_args_with(T::parse) in parse_args()
569 pub fn parse_args_with<F: Parser>(&self, parser: F) -> Result<F::Output> { in parse_args_with()
571 crate::parse::parse_scoped(parser, scope, self.tokens.clone()) in parse_args_with()
579 logic: impl FnMut(ParseNestedMeta) -> Result<()>, in parse_nested_meta()
580 ) -> Result<()> { in parse_nested_meta()
588 fn outer(self) -> Self::Ret; in outer()
589 fn inner(self) -> Self::Ret; in inner()
593 type Ret = iter::Filter<slice::Iter<'a, Attribute>, fn(&&Attribute) -> bool>;
595 fn outer(self) -> Self::Ret { in outer()
596 fn is_outer(attr: &&Attribute) -> bool { in outer()
605 fn inner(self) -> Self::Ret { in inner()
606 fn is_inner(attr: &&Attribute) -> bool { in inner()
619 use crate::parse::discouraged::Speculative;
620 use crate::parse::{Parse, ParseStream, Result};
623 pub(crate) fn parse_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> { in parse_inner()
630 pub(crate) fn single_parse_inner(input: ParseStream) -> Result<Attribute> { in single_parse_inner()
633 pound_token: input.parse()?, in single_parse_inner()
634 style: AttrStyle::Inner(input.parse()?), in single_parse_inner()
636 meta: content.parse()?, in single_parse_inner()
640 pub(crate) fn single_parse_outer(input: ParseStream) -> Result<Attribute> { in single_parse_outer()
643 pound_token: input.parse()?, in single_parse_outer()
646 meta: content.parse()?, in single_parse_outer()
651 impl Parse for Meta {
652 fn parse(input: ParseStream) -> Result<Self> { in parse() method
653 let path = input.call(Path::parse_mod_style)?; in parse() localVariable
654 parse_meta_after_path(path, input) in parse()
659 impl Parse for MetaList {
660 fn parse(input: ParseStream) -> Result<Self> { in parse() method
661 let path = input.call(Path::parse_mod_style)?; in parse() localVariable
662 parse_meta_list_after_path(path, input) in parse()
667 impl Parse for MetaNameValue {
668 fn parse(input: ParseStream) -> Result<Self> { in parse() method
669 let path = input.call(Path::parse_mod_style)?; in parse() localVariable
670 parse_meta_name_value_after_path(path, input) in parse()
674 pub(crate) fn parse_meta_after_path(path: Path, input: ParseStream) -> Result<Meta> { in parse_meta_after_path() argument
676 parse_meta_list_after_path(path, input).map(Meta::List) in parse_meta_after_path()
678 parse_meta_name_value_after_path(path, input).map(Meta::NameValue) in parse_meta_after_path()
680 Ok(Meta::Path(path)) in parse_meta_after_path()
684 fn parse_meta_list_after_path(path: Path, input: ParseStream) -> Result<MetaList> { in parse_meta_list_after_path() argument
687 path, in parse_meta_list_after_path()
693 fn parse_meta_name_value_after_path(path: Path, input: ParseStream) -> Result<MetaNameValue> { in parse_meta_name_value_after_path() argument
694 let eq_token: Token![=] = input.parse()?; in parse_meta_name_value_after_path()
696 let lit: Option<Lit> = ahead.parse()?; in parse_meta_name_value_after_path()
706 input.parse()? in parse_meta_name_value_after_path()
709 path, in parse_meta_name_value_after_path()
718 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { in fmt()
726 pub(super) struct DisplayPath<'a>(pub &'a Path);
729 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { in fmt()
763 self.path.to_tokens(tokens); in to_tokens()
771 self.path.to_tokens(tokens); in to_tokens()