2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct Token<'a> {
|
2018-12-08 14:29:41 -08:00
|
|
|
pub index: usize,
|
|
|
|
pub line: usize,
|
2017-11-16 23:30:08 -08:00
|
|
|
pub column: usize,
|
2018-12-08 14:29:41 -08:00
|
|
|
pub text: &'a str,
|
2017-11-16 23:30:08 -08:00
|
|
|
pub prefix: &'a str,
|
|
|
|
pub lexeme: &'a str,
|
2018-12-08 14:29:41 -08:00
|
|
|
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(),
|
2018-12-08 14:29:41 -08:00
|
|
|
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,
|
2017-12-02 14:59:07 -08:00
|
|
|
Comma,
|
2017-11-16 23:30:08 -08:00
|
|
|
Comment,
|
|
|
|
Dedent,
|
|
|
|
Eof,
|
|
|
|
Eol,
|
|
|
|
Equals,
|
|
|
|
Indent,
|
|
|
|
InterpolationEnd,
|
|
|
|
InterpolationStart,
|
|
|
|
Line,
|
|
|
|
Name,
|
2017-12-02 05:37:10 -08:00
|
|
|
ParenL,
|
|
|
|
ParenR,
|
2017-11-16 23:30:08 -08:00
|
|
|
Plus,
|
|
|
|
RawString,
|
|
|
|
StringToken,
|
|
|
|
Text,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TokenKind {
|
2019-04-11 15:23:14 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
2017-11-16 23:30:08 -08:00
|
|
|
use TokenKind::*;
|
2018-12-08 14:29:41 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|