pub enum Lit {
    Bool(LitBool),
    Byte(LitByte),
    Int(LitInt),
    Uint(LitUint),
    Float(LitFloat),
    Char(LitChar),
    Str(LitStr),
    ByteStr(LitByteStr),
}
Expand description

Represents any literal value.

The following is a lazy doc-test for ensuring that the Span of literals is preserved when converted between parsel::ast::Lit and syn::Lit.

// This deliberately uses `parse_str()` instead of `parse_quote!{}`
// because the former results in meaningful `Span` values, while
// the latter would cause every token to be spanned to the call site.
let source = r#"
true
   false  6.283
  100000023456         b'\xa3'
      "foo bar baz" - 74819253
   -99887766554433221100
    b"This is a byte string! A literal one."
"#;
let parsel: Many<Lit> = parsel::parse_str(source).unwrap();
let syn: Many<syn::Lit> = parsel::parse_str(source).unwrap();

let parsel_strings: Vec<_> = parsel
    .iter()
    .map(Lit::to_string)
    .collect();
let syn_strings: Vec<_> = syn
    .iter()
    .map(|lit| -> String {
        // if we simply used `TokenStream::to_string()` here, then multi-token
        // negative literals would fail the test since their representation
        // sometimes includes a space between the sign and the digits.
        lit.to_token_stream().into_iter().map(|tt| tt.to_string()).collect()
    })
    .collect();

assert_eq!(parsel_strings, syn_strings);

let parsel_spans: Vec<_> = parsel
    .iter()
    .map(Lit::span)
    .map(|span| (span.start(), span.end()))
    .collect();
let syn_spans: Vec<_> = syn
    .iter()
    .map(syn::Lit::span)
    .map(|span| (span.start(), span.end()))
    .collect();

assert_eq!(parsel_spans, syn_spans);

let parsel_converted_spans: Vec<_> = parsel
    .into_iter()
    .map(|lit| syn::Lit::from(lit).span())
    .map(|span| (span.start(), span.end()))
    .collect();
let syn_converted_spans: Vec<_> = syn
    .into_iter()
    .map(|lit| Lit::try_from(lit).unwrap().span())
    .map(|span| (span.start(), span.end()))
    .collect();

assert_eq!(parsel_converted_spans, syn_converted_spans);

Variants

Bool(LitBool)

Byte(LitByte)

Int(LitInt)

Uint(LitUint)

Float(LitFloat)

Char(LitChar)

Str(LitStr)

ByteStr(LitByteStr)

Implementations

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Write self to the given TokenStream. Read more
Convert self directly into a TokenStream object. Read more
Convert self directly into a TokenStream object. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty. Read more

TODO(H2CO3): a faster, less naive implementation would be great. We should use the byte offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].byte_range(source),  4..10);
assert_eq!(tokens[1].byte_range(source), 13..17);
assert_eq!(tokens[2].byte_range(source), 19..38);
assert_eq!(tokens[3].byte_range(source), 45..54);

TODO(H2CO3): a faster, less naive implementation would be great. We should use the char offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].char_range(source),  4..10);
assert_eq!(tokens[1].char_range(source), 13..17);
assert_eq!(tokens[2].char_range(source), 19..37);
assert_eq!(tokens[3].char_range(source), 44..51);
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.