just/src/token.rs

78 lines
1.7 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,
pub column: usize,
pub text: &'a str,
pub prefix: &'a str,
pub lexeme: &'a str,
pub kind: TokenKind,
}
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(),
2017-11-16 23:30:08 -08:00
index: self.index + self.prefix.len(),
line: self.line,
2018-03-05 13:21:35 -08:00
text: self.text,
2017-11-16 23:30:08 -08:00
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 => "','",
2017-11-16 23:30:08 -08:00
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 => "')'",
2017-11-16 23:30:08 -08:00
StringToken => "string",
RawString => "raw string",
Text => "command text",
})
}
}