tokit 0.0.0

Blazing fast parser combinators: parse-while-lexing (zero-copy), deterministic LALR-style parsing, no backtracking. Flexible emitters for fail-fast runtime or greedy compiler diagnostics
Documentation
use super::{Trailing, UnexpectedToken};
use crate::{Lexer, Token, punct::*};

macro_rules! alias {
  (
    $(
      $(#[$attr:meta])*
      $name:ident
    ), +$(,)?
  ) => {
    paste::paste! {
      $(
        $(#[$attr])*
        pub type [< UnexpectedTrailing $name >] <'inp, L, Lang = ()> = UnexpectedTrailingOf<'inp, $name, L, Lang>;

        impl<T, Kind, S> UnexpectedToken<'_, T, Kind, S, Trailing<$name>> {
          #[doc = "Create a new `UnexpectedToken` error indicating a trailing `" $name "` was found."]
          #[cfg_attr(not(tarpaulin), inline(always))]
          pub const fn [< trailing_ $name:snake>](
            span: S,
            token: T,
          ) -> Self {
            Self::[< trailing_ $name:snake _of>](span, token)
          }
        }

        impl<T, Kind, S, Lang> UnexpectedToken<'_, T, Kind, S, Trailing<$name, Lang>> {
          #[doc = "Create a new `UnexpectedToken` error indicating a trailing `" $name "` was found for the given langauge."]
          #[cfg_attr(not(tarpaulin), inline(always))]
          pub const fn [< trailing_ $name:snake _of>](
            span: S,
            token: T,
          ) -> Self {
            Self::trailing_of(span, token)
          }
        }

        impl<T, Kind, S, Lang> ::core::fmt::Debug for UnexpectedToken<'_, T, Kind, S, Trailing<$name, Lang>>
        where
          S: ::core::fmt::Debug,
          T: ::core::fmt::Debug,
          Lang: ?Sized,
        {
          #[cfg_attr(not(tarpaulin), inline(always))]
          fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            f.debug_struct(stringify!([< UnexpectedTrailing $name >]))
              .field("span", &self.span)
              .field("found", &self.found)
              .finish()
          }
        }

        impl<T, Kind, S, Lang> ::core::fmt::Display for UnexpectedToken<'_, T, Kind, S, Trailing<$name, Lang>>
        where
          S: ::core::fmt::Display,
          Lang: ?Sized,
        {
          #[cfg_attr(not(tarpaulin), inline(always))]
          fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            write!(
              f,
              "unexpected trailing {} token at {}",
              stringify!([< $name: snake >]),
              self.span
            )
          }
        }

        impl<T, Kind, S, Lang> ::core::error::Error for UnexpectedToken<'_, T, Kind, S, Trailing<$name, Lang>>
        where
          S: ::core::fmt::Display + ::core::fmt::Debug,
          T: ::core::fmt::Debug,
          Lang: ?Sized,
        {
        }
      )*
    }
  };
}

alias! {
  /// A type alias for an `UnexpectedToken` error indicating a trailing comma was found.
  Comma,
  /// A type alias for an `UnexpectedToken` error indicating a trailing dot was found.
  Dot,
  /// A type alias for an `UnexpectedToken` error indicating a trailing underscore was found.
  Underscore,
  /// A type alias for an `UnexpectedToken` error indicating a trailing pipe was found.
  Pipe,
  /// A type alias for an `UnexpectedToken` error indicating a trailing ampersand was found.
  Ampersand,
  /// A type alias for an `UnexpectedToken` error indicating a trailing hyphen was found.
  Hyphen,
  /// A type alias for an `UnexpectedToken` error indicating a trailing double colon was found.
  DoubleColon,
}

/// A type alias for an `UnexpectedPrefix` error indicating a trailing punctuator was found for a given lexer and separator.
pub type UnexpectedTrailingOf<'inp, Sep, L, Lang = ()> = UnexpectedToken<
  'inp,
  <L as Lexer<'inp>>::Token,
  <<L as Lexer<'inp>>::Token as Token<'inp>>::Kind,
  <L as Lexer<'inp>>::Span,
  Trailing<Sep, Lang>,
>;