You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.9 KiB
Rust

use std::fmt;
/// A single syntactic element.
#[derive(Debug, Clone)]
pub struct Token {
pub kind: Kind,
/// line of the first character (starting from 1)
pub line: usize,
/// column of the first character (starting from 1)
pub col: usize,
/// raw text
pub raw: String,
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: \"{}\"", self.kind, self.raw)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Kind {
Ident,
OBrace,
CBrace,
OBracket,
CBracket,
Comma,
Semi,
Eq,
Plus,
Minus,
Asterisk,
Slash,
Percent,
DependKeyword,
IncludeKeyword,
ModuleKeyword,
SetKeyword,
SourceKeyword,
TypeKeyword,
StringLiteral,
IntLiteral,
Comment,
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Kind::Ident => "ident",
Kind::OBrace => "obrace",
Kind::CBrace => "cbrace",
Kind::OBracket => "obracket",
Kind::CBracket => "cbracket",
Kind::Comma => "comma",
Kind::Semi => "semi",
Kind::Eq => "eq",
Kind::Plus => "plus",
Kind::Minus => "minus",
Kind::Asterisk => "asterisk",
Kind::Slash => "slash",
Kind::Percent => "percent",
Kind::DependKeyword => "keyword",
Kind::IncludeKeyword => "keyword",
Kind::ModuleKeyword => "keyword",
Kind::SetKeyword => "keyword",
Kind::SourceKeyword => "keyword",
Kind::TypeKeyword => "keyword",
Kind::StringLiteral => "string",
Kind::IntLiteral => "int",
Kind::Comment => "comment",
}
)
}
}