use oak_core::{Token, TokenType, UniversalTokenRole};
pub type PythonToken = Token<PythonTokenType>;
impl PythonTokenType {
pub fn is_keyword(&self) -> bool {
matches!(
self,
Self::AndKeyword
| Self::AsKeyword
| Self::AssertKeyword
| Self::AsyncKeyword
| Self::AwaitKeyword
| Self::BreakKeyword
| Self::ClassKeyword
| Self::ContinueKeyword
| Self::DefKeyword
| Self::DelKeyword
| Self::ElifKeyword
| Self::ElseKeyword
| Self::ExceptKeyword
| Self::FalseKeyword
| Self::FinallyKeyword
| Self::ForKeyword
| Self::FromKeyword
| Self::GlobalKeyword
| Self::IfKeyword
| Self::ImportKeyword
| Self::InKeyword
| Self::IsKeyword
| Self::LambdaKeyword
| Self::NoneKeyword
| Self::NonlocalKeyword
| Self::NotKeyword
| Self::OrKeyword
| Self::PassKeyword
| Self::RaiseKeyword
| Self::ReturnKeyword
| Self::TrueKeyword
| Self::TryKeyword
| Self::WhileKeyword
| Self::WithKeyword
| Self::YieldKeyword
)
}
}
impl PythonTokenType {
pub fn is_trivia(&self) -> bool {
matches!(self, Self::Whitespace | Self::Comment)
}
}
impl TokenType for PythonTokenType {
type Role = UniversalTokenRole;
const END_OF_STREAM: Self = Self::Error;
fn is_ignored(&self) -> bool {
self.is_trivia()
}
fn role(&self) -> Self::Role {
match self {
_ => UniversalTokenRole::None,
}
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u16)]
pub enum PythonTokenType {
Whitespace,
Comment,
Identifier,
Number,
String,
Bytes,
FString,
AndKeyword,
AsKeyword,
AssertKeyword,
AsyncKeyword,
AwaitKeyword,
BreakKeyword,
ClassKeyword,
ContinueKeyword,
DefKeyword,
DelKeyword,
ElifKeyword,
ElseKeyword,
ExceptKeyword,
FalseKeyword,
FinallyKeyword,
ForKeyword,
FromKeyword,
GlobalKeyword,
IfKeyword,
ImportKeyword,
InKeyword,
IsKeyword,
LambdaKeyword,
NoneKeyword,
NonlocalKeyword,
NotKeyword,
OrKeyword,
PassKeyword,
RaiseKeyword,
ReturnKeyword,
TrueKeyword,
TryKeyword,
WhileKeyword,
WithKeyword,
YieldKeyword,
Plus,
Minus,
Star,
DoubleStar,
Slash,
DoubleSlash,
Percent,
At,
LeftShift,
RightShift,
Ampersand,
Pipe,
Caret,
Tilde,
Less,
Greater,
LessEqual,
GreaterEqual,
Equal,
NotEqual,
Assign,
PlusAssign,
MinusAssign,
StarAssign,
DoubleStarAssign,
SlashAssign,
DoubleSlashAssign,
PercentAssign,
AtAssign,
AmpersandAssign,
PipeAssign,
CaretAssign,
LeftShiftAssign,
RightShiftAssign,
LeftParen,
RightParen,
LeftBracket,
RightBracket,
LeftBrace,
RightBrace,
Comma,
Colon,
Semicolon,
Dot,
Arrow,
Ellipsis,
Newline,
Indent,
Dedent,
Eof,
Error,
}
impl From<PythonTokenType> for u16 {
fn from(k: PythonTokenType) -> u16 {
k as u16
}
}
impl From<u16> for PythonTokenType {
fn from(d: u16) -> PythonTokenType {
unsafe { core::mem::transmute::<u16, PythonTokenType>(d) }
}
}