|
| 1 | +pub use crate::codegen::group_kind::GroupKind; |
| 2 | +pub use crate::codegen::token_kind::TokenKind; |
| 3 | + |
| 4 | +#[derive(Debug, Clone, PartialEq)] |
| 5 | +pub enum LineType { |
| 6 | + /// Must break (semicolon, etc.) |
| 7 | + Hard, |
| 8 | + /// Break if group doesn't fit |
| 9 | + Soft, |
| 10 | + /// Break if group doesn't fit, but collapse to space if it does |
| 11 | + SoftOrSpace, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, Clone, PartialEq)] |
| 15 | +pub enum LayoutEvent { |
| 16 | + Token(TokenKind), |
| 17 | + Space, |
| 18 | + Line(LineType), |
| 19 | + GroupStart { kind: GroupKind }, |
| 20 | + GroupEnd, |
| 21 | + IndentStart, |
| 22 | + IndentEnd, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Default)] |
| 26 | +pub struct EventEmitter { |
| 27 | + pub events: Vec<LayoutEvent>, |
| 28 | +} |
| 29 | + |
| 30 | +impl EventEmitter { |
| 31 | + pub fn new() -> Self { |
| 32 | + Self::default() |
| 33 | + } |
| 34 | + |
| 35 | + pub fn token(&mut self, token: TokenKind) { |
| 36 | + self.events.push(LayoutEvent::Token(token)); |
| 37 | + } |
| 38 | + |
| 39 | + pub fn space(&mut self) { |
| 40 | + self.events.push(LayoutEvent::Space); |
| 41 | + } |
| 42 | + |
| 43 | + pub fn line(&mut self, line_type: LineType) { |
| 44 | + self.events.push(LayoutEvent::Line(line_type)); |
| 45 | + } |
| 46 | + |
| 47 | + pub fn group_start(&mut self, kind: GroupKind) { |
| 48 | + self.events.push(LayoutEvent::GroupStart { kind }); |
| 49 | + } |
| 50 | + |
| 51 | + pub fn group_end(&mut self) { |
| 52 | + self.events.push(LayoutEvent::GroupEnd); |
| 53 | + } |
| 54 | + |
| 55 | + pub fn indent_start(&mut self) { |
| 56 | + self.events.push(LayoutEvent::IndentStart); |
| 57 | + } |
| 58 | + |
| 59 | + pub fn indent_end(&mut self) { |
| 60 | + self.events.push(LayoutEvent::IndentEnd); |
| 61 | + } |
| 62 | +} |
0 commit comments