just/src/token.rs

82 lines
1.6 KiB
Rust
Raw Normal View History

2017-11-16 23:30:08 -08:00
use common::*;
#[derive(Debug, PartialEq, Clone)]
pub struct Token<'a> {
pub index: usize,
pub line: usize,
2017-11-16 23:30:08 -08:00
pub column: usize,
pub text: &'a str,
2017-11-16 23:30:08 -08:00
pub prefix: &'a str,
pub lexeme: &'a str,
pub kind: TokenKind,
2017-11-16 23:30:08 -08:00
}
impl<'a> Token<'a> {
pub fn error(&self, kind: CompilationErrorKind<'a>) -> CompilationError<'a> {
CompilationError {
2018-03-05 13:21:35 -08:00
column: self.column + self.prefix.len(),
index: self.index + self.prefix.len(),
line: self.line,
text: self.text,
width: Some(self.lexeme.len()),
2018-03-05 13:21:35 -08:00
kind,
2017-11-16 23:30:08 -08:00
}
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TokenKind {
At,
Backtick,
Colon,
Comma,
2017-11-16 23:30:08 -08:00
Comment,
Dedent,
Eof,
Eol,
Equals,
Indent,
InterpolationEnd,
InterpolationStart,
Line,
Name,
ParenL,
ParenR,
2017-11-16 23:30:08 -08:00
Plus,
RawString,
StringToken,
Text,
}
impl Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use TokenKind::*;
write!(
f,
"{}",
match *self {
Backtick => "backtick",
Colon => "':'",
Comma => "','",
Comment => "comment",
Dedent => "dedent",
Eof => "end of file",
Eol => "end of line",
Equals => "'='",
Indent => "indent",
InterpolationEnd => "'}}'",
InterpolationStart => "'{{'",
Line => "command",
Name => "name",
Plus => "'+'",
At => "'@'",
ParenL => "'('",
ParenR => "')'",
StringToken => "string",
RawString => "raw string",
Text => "command text",
}
)
2017-11-16 23:30:08 -08:00
}
}