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.

151 lines
3.5 KiB
Rust

use std::fmt;
/// A single syntactic element.
#[derive(Debug, Clone)]
pub struct Token {
pub kind: Kind,
pub pos: Position,
/// raw text
pub raw: String,
}
#[derive(Debug, Clone)]
pub struct Position {
pub file: String,
pub line: usize,
pub col: usize,
}
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,
OParen,
CParen,
Comma,
Semi,
Eq,
EqEq,
Bang,
BangEq,
Gt,
GtGt,
GtEq,
GtGtEq,
Lt,
LtLt,
LtEq,
LtLtEq,
Amp,
AmpAmp,
AmpEq,
AmpAmpEq,
Pipe,
PipePipe,
PipeEq,
PipePipeEq,
Caret,
CaretEq,
Plus,
PlusEq,
Minus,
MinusEq,
Asterisk,
AsteriskAsterisk,
AsteriskEq,
AsteriskAsteriskEq,
Slash,
SlashEq,
Percent,
PercentEq,
DependKeyword,
IncludeKeyword,
TargetKeyword,
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::OParen => "oparen",
Kind::CParen => "cparen",
Kind::Comma => "comma",
Kind::Semi => "semi",
Kind::Eq => "eq",
Kind::EqEq => "eqeq",
Kind::Bang => "bang",
Kind::BangEq => "bangeq",
Kind::Gt => "gt",
Kind::GtGt => "gtgt",
Kind::GtEq => "gteq",
Kind::GtGtEq => "gtgteq",
Kind::Lt => "lt",
Kind::LtLt => "ltlt",
Kind::LtEq => "lteq",
Kind::LtLtEq => "ltlteq",
Kind::Amp => "amp",
Kind::AmpAmp => "ampamp",
Kind::AmpEq => "ampeq",
Kind::AmpAmpEq => "ampampeq",
Kind::Pipe => "pipe",
Kind::PipePipe => "pipepipe",
Kind::PipeEq => "pipeeq",
Kind::PipePipeEq => "pipepipeeq",
Kind::Caret => "caret",
Kind::CaretEq => "careteq",
Kind::Plus => "plus",
Kind::PlusEq => "pluseq",
Kind::Minus => "minus",
Kind::MinusEq => "minuseq",
Kind::Asterisk => "asterisk",
Kind::AsteriskAsterisk => "asteriskasterisk",
Kind::AsteriskEq => "asteriskeq",
Kind::AsteriskAsteriskEq => "asteriskasteriskeq",
Kind::Slash => "slash",
Kind::SlashEq => "slasheq",
Kind::Percent => "percent",
Kind::PercentEq => "percenteq",
Kind::DependKeyword => "keyword",
Kind::IncludeKeyword => "keyword",
Kind::SetKeyword => "keyword",
Kind::SourceKeyword => "keyword",
Kind::TargetKeyword => "keyword",
Kind::TypeKeyword => "keyword",
Kind::StringLiteral => "string",
Kind::IntLiteral => "int",
Kind::Comment => "comment",
}
)
}
}