2019-11-07 10:55:15 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
2020-02-14 04:49:25 -08:00
|
|
|
/// A name. This is effectively just a `Token` of kind `Identifier`, but we give
|
|
|
|
/// it its own type for clarity.
|
2019-11-07 10:55:15 -08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
|
|
|
pub(crate) struct Name<'src> {
|
|
|
|
pub(crate) offset: usize,
|
|
|
|
pub(crate) length: usize,
|
2020-02-10 20:07:06 -08:00
|
|
|
pub(crate) line: usize,
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) column: usize,
|
2020-02-10 20:07:06 -08:00
|
|
|
pub(crate) src: &'src str,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Name<'src> {
|
|
|
|
/// The name's text contents
|
|
|
|
pub(crate) fn lexeme(&self) -> &'src str {
|
|
|
|
&self.src[self.offset..self.offset + self.length]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn this name back into a token
|
|
|
|
pub(crate) fn token(&self) -> Token<'src> {
|
|
|
|
Token {
|
2020-02-10 20:07:06 -08:00
|
|
|
kind: TokenKind::Identifier,
|
2019-11-07 10:55:15 -08:00
|
|
|
offset: self.offset,
|
|
|
|
length: self.length,
|
2020-02-10 20:07:06 -08:00
|
|
|
line: self.line,
|
2019-11-07 10:55:15 -08:00
|
|
|
column: self.column,
|
2020-02-10 20:07:06 -08:00
|
|
|
src: self.src,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_identifier(token: Token<'src>) -> Name {
|
|
|
|
assert_eq!(token.kind, TokenKind::Identifier);
|
|
|
|
Name {
|
|
|
|
offset: token.offset,
|
|
|
|
length: token.length,
|
2020-02-10 20:07:06 -08:00
|
|
|
line: token.line,
|
2019-11-07 10:55:15 -08:00
|
|
|
column: token.column,
|
2020-02-10 20:07:06 -08:00
|
|
|
src: token.src,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn error(&self, kind: CompilationErrorKind<'src>) -> CompilationError<'src> {
|
|
|
|
self.token().error(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Name<'_> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.lexeme())
|
|
|
|
}
|
|
|
|
}
|