cl-token: Rename Type to TokenKind, Data to TokenData to match the project's general style
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! # Token
|
||||
//!
|
||||
//! Stores a component of a file as a [Type], some [Data], and a line and column number
|
||||
//! Stores a component of a file as a [TokenKind], some [TokenData], and a line and column number
|
||||
#![warn(clippy::all)]
|
||||
#![feature(decl_macro)]
|
||||
|
||||
@@ -9,5 +9,5 @@ pub mod token_data;
|
||||
pub mod token_type;
|
||||
|
||||
pub use token::Token;
|
||||
pub use token_data::Data;
|
||||
pub use token_type::Type;
|
||||
pub use token_data::TokenData;
|
||||
pub use token_type::TokenKind;
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
//! A [Token] contains a single unit of lexical information, and an optional bit of [Data]
|
||||
use super::{Data, Type};
|
||||
//! A [Token] contains a single unit of lexical information, and an optional bit of [TokenData]
|
||||
use super::{TokenData, TokenKind};
|
||||
|
||||
/// Contains a single unit of lexical information,
|
||||
/// and an optional bit of [Data]
|
||||
/// and an optional bit of [TokenData]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Token {
|
||||
ty: Type,
|
||||
data: Data,
|
||||
ty: TokenKind,
|
||||
data: TokenData,
|
||||
line: u32,
|
||||
col: u32,
|
||||
}
|
||||
impl Token {
|
||||
/// Creates a new [Token] out of a [Type], [Data], line, and column.
|
||||
pub fn new(ty: Type, data: impl Into<Data>, line: u32, col: u32) -> Self {
|
||||
/// Creates a new [Token] out of a [TokenKind], [TokenData], line, and column.
|
||||
pub fn new(ty: TokenKind, data: impl Into<TokenData>, line: u32, col: u32) -> Self {
|
||||
Self { ty, data: data.into(), line, col }
|
||||
}
|
||||
/// Casts this token to a new [Type]
|
||||
pub fn cast(self, ty: Type) -> Self {
|
||||
/// Casts this token to a new [TokenKind]
|
||||
pub fn cast(self, ty: TokenKind) -> Self {
|
||||
Self { ty, ..self }
|
||||
}
|
||||
/// Returns the [Type] of this token
|
||||
pub fn ty(&self) -> Type {
|
||||
/// Returns the [TokenKind] of this token
|
||||
pub fn ty(&self) -> TokenKind {
|
||||
self.ty
|
||||
}
|
||||
/// Returns a reference to this token's [Data]
|
||||
pub fn data(&self) -> &Data {
|
||||
/// Returns a reference to this token's [TokenData]
|
||||
pub fn data(&self) -> &TokenData {
|
||||
&self.data
|
||||
}
|
||||
/// Converts this token into its inner [Data]
|
||||
pub fn into_data(self) -> Data {
|
||||
/// Converts this token into its inner [TokenData]
|
||||
pub fn into_data(self) -> TokenData {
|
||||
self.data
|
||||
}
|
||||
/// Returns the line where this token originated
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Additional data stored within a [Token](super::Token),
|
||||
//! external to its [Type](super::token_type::Type)
|
||||
//! external to its [TokenKind](super::token_type::TokenKind)
|
||||
/// Additional data stored within a [Token](super::Token),
|
||||
/// external to its [Type](super::token_type::Type)
|
||||
/// external to its [TokenKind](super::token_type::TokenKind)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Data {
|
||||
pub enum TokenData {
|
||||
/// [Token](super::Token) contains an [identifier](str)
|
||||
Identifier(Box<str>),
|
||||
/// [Token](super::Token) contains a [String]
|
||||
@@ -27,19 +27,19 @@ from! {
|
||||
}
|
||||
/// Implements [From] for an enum
|
||||
macro from($($value:ident: $src:ty => $dst:expr),*$(,)?) {
|
||||
$(impl From<$src> for Data {
|
||||
$(impl From<$src> for TokenData {
|
||||
fn from($value: $src) -> Self { $dst }
|
||||
})*
|
||||
}
|
||||
impl std::fmt::Display for Data {
|
||||
impl std::fmt::Display for TokenData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Data::Identifier(v) => v.fmt(f),
|
||||
Data::String(v) => write!(f, "\"{v}\""),
|
||||
Data::Character(v) => write!(f, "'{v}'"),
|
||||
Data::Integer(v) => v.fmt(f),
|
||||
Data::Float(v) => v.fmt(f),
|
||||
Data::None => "None".fmt(f),
|
||||
TokenData::Identifier(v) => v.fmt(f),
|
||||
TokenData::String(v) => write!(f, "\"{v}\""),
|
||||
TokenData::Character(v) => write!(f, "'{v}'"),
|
||||
TokenData::Integer(v) => v.fmt(f),
|
||||
TokenData::Float(v) => v.fmt(f),
|
||||
TokenData::None => "None".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::{fmt::Display, str::FromStr};
|
||||
|
||||
/// Stores a [Token's](super::Token) lexical information
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Type {
|
||||
pub enum TokenKind {
|
||||
// Invalid syntax
|
||||
Invalid,
|
||||
// Any kind of comment
|
||||
@@ -98,102 +98,102 @@ pub enum Type {
|
||||
XorXor, // ^^
|
||||
}
|
||||
|
||||
impl Display for Type {
|
||||
impl Display for TokenKind {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Type::Invalid => "invalid".fmt(f),
|
||||
Type::Comment => "comment".fmt(f),
|
||||
Type::Identifier => "identifier".fmt(f),
|
||||
TokenKind::Invalid => "invalid".fmt(f),
|
||||
TokenKind::Comment => "comment".fmt(f),
|
||||
TokenKind::Identifier => "identifier".fmt(f),
|
||||
|
||||
Type::Break => "break".fmt(f),
|
||||
Type::Cl => "cl".fmt(f),
|
||||
Type::Const => "const".fmt(f),
|
||||
Type::Continue => "continue".fmt(f),
|
||||
Type::Else => "else".fmt(f),
|
||||
Type::Enum => "enum".fmt(f),
|
||||
Type::False => "false".fmt(f),
|
||||
Type::For => "for".fmt(f),
|
||||
Type::Fn => "fn".fmt(f),
|
||||
Type::If => "if".fmt(f),
|
||||
Type::Impl => "impl".fmt(f),
|
||||
Type::In => "in".fmt(f),
|
||||
Type::Let => "let".fmt(f),
|
||||
Type::Mod => "mod".fmt(f),
|
||||
Type::Mut => "mut".fmt(f),
|
||||
Type::Pub => "pub".fmt(f),
|
||||
Type::Return => "return".fmt(f),
|
||||
Type::SelfKw => "self".fmt(f),
|
||||
Type::SelfTy => "Self".fmt(f),
|
||||
Type::Static => "static".fmt(f),
|
||||
Type::Struct => "struct".fmt(f),
|
||||
Type::Super => "super".fmt(f),
|
||||
Type::True => "true".fmt(f),
|
||||
Type::Type => "type".fmt(f),
|
||||
Type::While => "while".fmt(f),
|
||||
TokenKind::Break => "break".fmt(f),
|
||||
TokenKind::Cl => "cl".fmt(f),
|
||||
TokenKind::Const => "const".fmt(f),
|
||||
TokenKind::Continue => "continue".fmt(f),
|
||||
TokenKind::Else => "else".fmt(f),
|
||||
TokenKind::Enum => "enum".fmt(f),
|
||||
TokenKind::False => "false".fmt(f),
|
||||
TokenKind::For => "for".fmt(f),
|
||||
TokenKind::Fn => "fn".fmt(f),
|
||||
TokenKind::If => "if".fmt(f),
|
||||
TokenKind::Impl => "impl".fmt(f),
|
||||
TokenKind::In => "in".fmt(f),
|
||||
TokenKind::Let => "let".fmt(f),
|
||||
TokenKind::Mod => "mod".fmt(f),
|
||||
TokenKind::Mut => "mut".fmt(f),
|
||||
TokenKind::Pub => "pub".fmt(f),
|
||||
TokenKind::Return => "return".fmt(f),
|
||||
TokenKind::SelfKw => "self".fmt(f),
|
||||
TokenKind::SelfTy => "Self".fmt(f),
|
||||
TokenKind::Static => "static".fmt(f),
|
||||
TokenKind::Struct => "struct".fmt(f),
|
||||
TokenKind::Super => "super".fmt(f),
|
||||
TokenKind::True => "true".fmt(f),
|
||||
TokenKind::Type => "type".fmt(f),
|
||||
TokenKind::While => "while".fmt(f),
|
||||
|
||||
Type::Integer => "integer literal".fmt(f),
|
||||
Type::Float => "float literal".fmt(f),
|
||||
Type::String => "string literal".fmt(f),
|
||||
Type::Character => "char literal".fmt(f),
|
||||
TokenKind::Integer => "integer literal".fmt(f),
|
||||
TokenKind::Float => "float literal".fmt(f),
|
||||
TokenKind::String => "string literal".fmt(f),
|
||||
TokenKind::Character => "char literal".fmt(f),
|
||||
|
||||
Type::LCurly => "left curly".fmt(f),
|
||||
Type::RCurly => "right curly".fmt(f),
|
||||
Type::LBrack => "left brack".fmt(f),
|
||||
Type::RBrack => "right brack".fmt(f),
|
||||
Type::LParen => "left paren".fmt(f),
|
||||
Type::RParen => "right paren".fmt(f),
|
||||
Type::Amp => "and".fmt(f),
|
||||
Type::AmpAmp => "and-and".fmt(f),
|
||||
Type::AmpEq => "and-assign".fmt(f),
|
||||
Type::Arrow => "arrow".fmt(f),
|
||||
Type::At => "at".fmt(f),
|
||||
Type::Backslash => "backslash".fmt(f),
|
||||
Type::Bang => "bang".fmt(f),
|
||||
Type::BangBang => "not-not".fmt(f),
|
||||
Type::BangEq => "not equal to".fmt(f),
|
||||
Type::Bar => "or".fmt(f),
|
||||
Type::BarBar => "or-or".fmt(f),
|
||||
Type::BarEq => "or-assign".fmt(f),
|
||||
Type::Colon => "colon".fmt(f),
|
||||
Type::ColonColon => "path separator".fmt(f),
|
||||
Type::Comma => "comma".fmt(f),
|
||||
Type::Dot => "dot".fmt(f),
|
||||
Type::DotDot => "exclusive range".fmt(f),
|
||||
Type::DotDotEq => "inclusive range".fmt(f),
|
||||
Type::Eq => "assign".fmt(f),
|
||||
Type::EqEq => "equal to".fmt(f),
|
||||
Type::FatArrow => "fat arrow".fmt(f),
|
||||
Type::Grave => "grave".fmt(f),
|
||||
Type::Gt => "greater than".fmt(f),
|
||||
Type::GtEq => "greater than or equal to".fmt(f),
|
||||
Type::GtGt => "shift right".fmt(f),
|
||||
Type::GtGtEq => "shift right-assign".fmt(f),
|
||||
Type::Hash => "hash".fmt(f),
|
||||
Type::HashBang => "shebang".fmt(f),
|
||||
Type::Lt => "less than".fmt(f),
|
||||
Type::LtEq => "less than or equal to".fmt(f),
|
||||
Type::LtLt => "shift left".fmt(f),
|
||||
Type::LtLtEq => "shift left-assign".fmt(f),
|
||||
Type::Minus => "sub".fmt(f),
|
||||
Type::MinusEq => "sub-assign".fmt(f),
|
||||
Type::Plus => "add".fmt(f),
|
||||
Type::PlusEq => "add-assign".fmt(f),
|
||||
Type::Question => "huh?".fmt(f),
|
||||
Type::Rem => "rem".fmt(f),
|
||||
Type::RemEq => "rem-assign".fmt(f),
|
||||
Type::Semi => "ignore".fmt(f),
|
||||
Type::Slash => "div".fmt(f),
|
||||
Type::SlashEq => "div-assign".fmt(f),
|
||||
Type::Star => "star".fmt(f),
|
||||
Type::StarEq => "star-assign".fmt(f),
|
||||
Type::Tilde => "tilde".fmt(f),
|
||||
Type::Xor => "xor".fmt(f),
|
||||
Type::XorEq => "xor-assign".fmt(f),
|
||||
Type::XorXor => "cat-ears".fmt(f),
|
||||
TokenKind::LCurly => "left curly".fmt(f),
|
||||
TokenKind::RCurly => "right curly".fmt(f),
|
||||
TokenKind::LBrack => "left brack".fmt(f),
|
||||
TokenKind::RBrack => "right brack".fmt(f),
|
||||
TokenKind::LParen => "left paren".fmt(f),
|
||||
TokenKind::RParen => "right paren".fmt(f),
|
||||
TokenKind::Amp => "and".fmt(f),
|
||||
TokenKind::AmpAmp => "and-and".fmt(f),
|
||||
TokenKind::AmpEq => "and-assign".fmt(f),
|
||||
TokenKind::Arrow => "arrow".fmt(f),
|
||||
TokenKind::At => "at".fmt(f),
|
||||
TokenKind::Backslash => "backslash".fmt(f),
|
||||
TokenKind::Bang => "bang".fmt(f),
|
||||
TokenKind::BangBang => "not-not".fmt(f),
|
||||
TokenKind::BangEq => "not equal to".fmt(f),
|
||||
TokenKind::Bar => "or".fmt(f),
|
||||
TokenKind::BarBar => "or-or".fmt(f),
|
||||
TokenKind::BarEq => "or-assign".fmt(f),
|
||||
TokenKind::Colon => "colon".fmt(f),
|
||||
TokenKind::ColonColon => "path separator".fmt(f),
|
||||
TokenKind::Comma => "comma".fmt(f),
|
||||
TokenKind::Dot => "dot".fmt(f),
|
||||
TokenKind::DotDot => "exclusive range".fmt(f),
|
||||
TokenKind::DotDotEq => "inclusive range".fmt(f),
|
||||
TokenKind::Eq => "assign".fmt(f),
|
||||
TokenKind::EqEq => "equal to".fmt(f),
|
||||
TokenKind::FatArrow => "fat arrow".fmt(f),
|
||||
TokenKind::Grave => "grave".fmt(f),
|
||||
TokenKind::Gt => "greater than".fmt(f),
|
||||
TokenKind::GtEq => "greater than or equal to".fmt(f),
|
||||
TokenKind::GtGt => "shift right".fmt(f),
|
||||
TokenKind::GtGtEq => "shift right-assign".fmt(f),
|
||||
TokenKind::Hash => "hash".fmt(f),
|
||||
TokenKind::HashBang => "shebang".fmt(f),
|
||||
TokenKind::Lt => "less than".fmt(f),
|
||||
TokenKind::LtEq => "less than or equal to".fmt(f),
|
||||
TokenKind::LtLt => "shift left".fmt(f),
|
||||
TokenKind::LtLtEq => "shift left-assign".fmt(f),
|
||||
TokenKind::Minus => "sub".fmt(f),
|
||||
TokenKind::MinusEq => "sub-assign".fmt(f),
|
||||
TokenKind::Plus => "add".fmt(f),
|
||||
TokenKind::PlusEq => "add-assign".fmt(f),
|
||||
TokenKind::Question => "huh?".fmt(f),
|
||||
TokenKind::Rem => "rem".fmt(f),
|
||||
TokenKind::RemEq => "rem-assign".fmt(f),
|
||||
TokenKind::Semi => "ignore".fmt(f),
|
||||
TokenKind::Slash => "div".fmt(f),
|
||||
TokenKind::SlashEq => "div-assign".fmt(f),
|
||||
TokenKind::Star => "star".fmt(f),
|
||||
TokenKind::StarEq => "star-assign".fmt(f),
|
||||
TokenKind::Tilde => "tilde".fmt(f),
|
||||
TokenKind::Xor => "xor".fmt(f),
|
||||
TokenKind::XorEq => "xor-assign".fmt(f),
|
||||
TokenKind::XorXor => "cat-ears".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl FromStr for Type {
|
||||
impl FromStr for TokenKind {
|
||||
/// [FromStr] can only fail when an identifier isn't a keyword
|
||||
type Err = ();
|
||||
/// Parses a string s to return a Keyword
|
||||
|
||||
Reference in New Issue
Block a user