2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) struct Token<'a> {
|
|
|
|
pub(crate) offset: usize,
|
|
|
|
pub(crate) length: usize,
|
|
|
|
pub(crate) line: usize,
|
|
|
|
pub(crate) column: usize,
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) src: &'a str,
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) kind: TokenKind,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Token<'a> {
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn lexeme(&self) -> &'a str {
|
2019-11-07 10:55:15 -08:00
|
|
|
&self.src[self.offset..self.offset + self.length]
|
2019-04-15 22:40:02 -07:00
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn error(&self, kind: CompilationErrorKind<'a>) -> CompilationError<'a> {
|
2017-11-16 23:30:08 -08:00
|
|
|
CompilationError {
|
2019-04-15 22:40:02 -07:00
|
|
|
column: self.column,
|
|
|
|
offset: self.offset,
|
2018-12-08 14:29:41 -08:00
|
|
|
line: self.line,
|
2019-11-07 10:55:15 -08:00
|
|
|
src: self.src,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: self.length,
|
2018-03-05 13:21:35 -08:00
|
|
|
kind,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|