mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-14 00:47:18 +00:00
De-magic number parser::Output encoding
This commit is contained in:
parent
bfe82cda48
commit
dab685dd87
1 changed files with 36 additions and 13 deletions
|
@ -31,47 +31,70 @@ pub enum Step<'a> {
|
|||
}
|
||||
|
||||
impl Output {
|
||||
const EVENT_MASK: u32 = 0b1;
|
||||
const TAG_MASK: u32 = 0x0000_00F0;
|
||||
const N_INPUT_TOKEN_MASK: u32 = 0x0000_FF00;
|
||||
const KIND_MASK: u32 = 0xFFFF_0000;
|
||||
|
||||
const ERROR_SHIFT: u32 = Self::EVENT_MASK.trailing_ones();
|
||||
const TAG_SHIFT: u32 = Self::TAG_MASK.trailing_zeros();
|
||||
const N_INPUT_TOKEN_SHIFT: u32 = Self::N_INPUT_TOKEN_MASK.trailing_zeros();
|
||||
const KIND_SHIFT: u32 = Self::KIND_MASK.trailing_zeros();
|
||||
|
||||
const TOKEN_EVENT: u8 = 0;
|
||||
const ENTER_EVENT: u8 = 1;
|
||||
const EXIT_EVENT: u8 = 2;
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = Step<'_>> {
|
||||
self.event.iter().map(|&event| {
|
||||
if event & 0b1 == 0 {
|
||||
return Step::Error { msg: self.error[(event as usize) >> 1].as_str() };
|
||||
if event & Self::EVENT_MASK == 0 {
|
||||
return Step::Error {
|
||||
msg: self.error[(event as usize) >> Self::ERROR_SHIFT].as_str(),
|
||||
};
|
||||
}
|
||||
let tag = ((event & 0x0000_00F0) >> 4) as u8;
|
||||
let tag = ((event & Self::TAG_MASK) >> Self::TAG_SHIFT) as u8;
|
||||
match tag {
|
||||
0 => {
|
||||
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||
let n_input_tokens = ((event & 0x0000_FF00) >> 8) as u8;
|
||||
Self::TOKEN_EVENT => {
|
||||
let kind: SyntaxKind =
|
||||
(((event & Self::KIND_MASK) >> Self::KIND_SHIFT) as u16).into();
|
||||
let n_input_tokens =
|
||||
((event & Self::N_INPUT_TOKEN_MASK) >> Self::N_INPUT_TOKEN_SHIFT) as u8;
|
||||
Step::Token { kind, n_input_tokens }
|
||||
}
|
||||
1 => {
|
||||
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||
Self::ENTER_EVENT => {
|
||||
let kind: SyntaxKind =
|
||||
(((event & Self::KIND_MASK) >> Self::KIND_SHIFT) as u16).into();
|
||||
Step::Enter { kind }
|
||||
}
|
||||
2 => Step::Exit,
|
||||
Self::EXIT_EVENT => Step::Exit,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
||||
let e = ((kind as u16 as u32) << 16) | ((n_tokens as u32) << 8) | 1;
|
||||
let e = ((kind as u16 as u32) << Self::KIND_SHIFT)
|
||||
| ((n_tokens as u32) << Self::N_INPUT_TOKEN_SHIFT)
|
||||
| Self::EVENT_MASK;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn enter_node(&mut self, kind: SyntaxKind) {
|
||||
let e = ((kind as u16 as u32) << 16) | (1 << 4) | 1;
|
||||
let e = ((kind as u16 as u32) << Self::KIND_SHIFT)
|
||||
| ((Self::ENTER_EVENT as u32) << Self::TAG_SHIFT)
|
||||
| Self::EVENT_MASK;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn leave_node(&mut self) {
|
||||
let e = 2 << 4 | 1;
|
||||
let e = (Self::EXIT_EVENT as u32) << Self::TAG_SHIFT | Self::EVENT_MASK;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn error(&mut self, error: String) {
|
||||
let idx = self.error.len();
|
||||
self.error.push(error);
|
||||
let e = (idx as u32) << 1;
|
||||
let e = (idx as u32) << Self::ERROR_SHIFT;
|
||||
self.event.push(e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue