/
compact_trivia.rs
89 lines (80 loc) · 2.98 KB
/
compact_trivia.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2019, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
use bitflags::bitflags;
use crate::lexable_trivia::LexableTrivia;
use crate::minimal_trivia::MinimalTrivium;
use crate::trivia_factory::SimpleTriviaFactory;
use crate::trivia_kind::TriviaKind;
bitflags! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct TriviaKinds : u8 {
const WHITE_SPACE = 1 << TriviaKind::WhiteSpace as u8;
const END_OF_LINE = 1 << TriviaKind::EndOfLine as u8;
const DELIMITED_COMMENT = 1 << TriviaKind::DelimitedComment as u8;
const SINGLELINE_COMMENT = 1 << TriviaKind::SingleLineComment as u8;
const FIX_ME = 1 << TriviaKind::FixMe as u8;
const IGNORE_ERROR = 1 << TriviaKind::IgnoreError as u8;
const FALL_THROUGH = 1 << TriviaKind::FallThrough as u8;
const EXTRA_TOKEN_ERROR = 1 << TriviaKind::ExtraTokenError as u8;
// NB: There are currently exactly 8 TriviaKinds, so we cannot add more
// without changing the representation of this bitflags type. Making it
// larger would require changing CompactToken too, since it currently
// depends upon TriviaKinds fitting into a u8.
}
}
impl TriviaKinds {
pub fn from_kind(kind: TriviaKind) -> Self {
match kind {
TriviaKind::WhiteSpace => Self::WHITE_SPACE,
TriviaKind::EndOfLine => Self::END_OF_LINE,
TriviaKind::DelimitedComment => Self::DELIMITED_COMMENT,
TriviaKind::SingleLineComment => Self::SINGLELINE_COMMENT,
TriviaKind::FixMe => Self::FIX_ME,
TriviaKind::IgnoreError => Self::IGNORE_ERROR,
TriviaKind::FallThrough => Self::FALL_THROUGH,
TriviaKind::ExtraTokenError => Self::EXTRA_TOKEN_ERROR,
// NB: See above note about adding more TriviaKind variants
}
}
pub fn has_kind(self, kind: TriviaKind) -> bool {
self.contains(Self::from_kind(kind))
}
}
#[derive(Copy, Clone, Debug)]
pub struct CompactTrivia {
pub kinds: TriviaKinds,
pub width: usize,
}
impl CompactTrivia {
pub fn make(kinds: TriviaKinds, width: usize) -> Self {
Self { kinds, width }
}
}
impl LexableTrivia for CompactTrivia {
type Trivium = MinimalTrivium;
fn is_empty(&self) -> bool {
self.kinds.is_empty()
}
fn has_kind(&self, kind: TriviaKind) -> bool {
self.kinds.has_kind(kind)
}
fn push(&mut self, trivium: MinimalTrivium) {
self.kinds |= TriviaKinds::from_kind(trivium.kind);
self.width += trivium.width;
}
fn extend(&mut self, other: Self) {
self.kinds |= other.kinds;
self.width += other.width;
}
}
impl SimpleTriviaFactory for CompactTrivia {
fn make() -> Self {
Self {
kinds: TriviaKinds::empty(),
width: 0,
}
}
}