| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 1 | use crate::gen::block::Block; |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 2 | use crate::gen::builtin::Builtins; |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 3 | use crate::gen::include::Includes; |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 4 | use crate::gen::Opt; |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 5 | use crate::syntax::Types; |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 6 | use std::cell::RefCell; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | use std::fmt::{self, Arguments, Write}; |
| 8 | |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 9 | pub(crate) struct OutFile<'a> { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | pub header: bool, |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 11 | pub opt: &'a Opt, |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 12 | pub types: &'a Types<'a>, |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 13 | pub include: Includes<'a>, |
| 14 | pub builtin: Builtins<'a>, |
| 15 | content: RefCell<Content<'a>>, |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 18 | #[derive(Default)] |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 19 | pub struct Content<'a> { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 20 | bytes: String, |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 21 | blocks: Vec<BlockBoundary<'a>>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | section_pending: bool, |
| David Tolnay | f02146e | 2020-11-01 15:35:42 -0800 | [diff] [blame] | 23 | blocks_pending: usize, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 26 | #[derive(Copy, Clone, PartialEq, Debug)] |
| 27 | enum BlockBoundary<'a> { |
| 28 | Begin(Block<'a>), |
| 29 | End(Block<'a>), |
| 30 | } |
| 31 | |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 32 | impl<'a> OutFile<'a> { |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 33 | pub fn new(header: bool, opt: &'a Opt, types: &'a Types) -> Self { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 34 | OutFile { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 35 | header, |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 36 | opt, |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 37 | types, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 38 | include: Includes::new(), |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 39 | builtin: Builtins::new(), |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 40 | content: RefCell::new(Content::new()), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | // Write a blank line if the preceding section had any contents. |
| 45 | pub fn next_section(&mut self) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 46 | self.content.get_mut().next_section(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 49 | pub fn begin_block(&mut self, block: Block<'a>) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 50 | self.content.get_mut().begin_block(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 53 | pub fn end_block(&mut self, block: Block<'a>) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 54 | self.content.get_mut().end_block(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 57 | pub fn write_fmt(&self, args: Arguments) { |
| 58 | let content = &mut *self.content.borrow_mut(); |
| 59 | Write::write_fmt(content, args).unwrap(); |
| 60 | } |
| 61 | |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 62 | pub fn content(&mut self) -> Vec<u8> { |
| 63 | self.flush(); |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 64 | let include = &self.include.content.bytes; |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 65 | let builtin = &self.builtin.content.bytes; |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 66 | let content = &self.content.get_mut().bytes; |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 67 | let len = include.len() + builtin.len() + content.len() + 2; |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 68 | let mut out = String::with_capacity(len); |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 69 | out.push_str(include); |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 70 | if !out.is_empty() && !builtin.is_empty() { |
| 71 | out.push('\n'); |
| 72 | } |
| 73 | out.push_str(builtin); |
| 74 | if !out.is_empty() && !content.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 75 | out.push('\n'); |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 76 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 77 | out.push_str(content); |
| David Tolnay | 91489ec | 2020-09-03 12:48:46 -0700 | [diff] [blame] | 78 | if out.is_empty() { |
| 79 | out.push_str("// empty\n"); |
| 80 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 81 | out.into_bytes() |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 82 | } |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 83 | |
| 84 | fn flush(&mut self) { |
| 85 | self.include.content.flush(); |
| 86 | self.builtin.content.flush(); |
| 87 | self.content.get_mut().flush(); |
| 88 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 91 | impl<'a> Write for Content<'a> { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 92 | fn write_str(&mut self, s: &str) -> fmt::Result { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 93 | self.write(s); |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 94 | Ok(()) |
| 95 | } |
| 96 | } |
| 97 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 98 | impl<'a> PartialEq for Content<'a> { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 99 | fn eq(&self, _other: &Content) -> bool { |
| 100 | true |
| 101 | } |
| 102 | } |
| 103 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 104 | impl<'a> Content<'a> { |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 105 | fn new() -> Self { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 106 | Content::default() |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 109 | pub fn next_section(&mut self) { |
| 110 | self.section_pending = true; |
| 111 | } |
| 112 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 113 | pub fn begin_block(&mut self, block: Block<'a>) { |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 114 | self.push_block_boundary(BlockBoundary::Begin(block)); |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 117 | pub fn end_block(&mut self, block: Block<'a>) { |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 118 | self.push_block_boundary(BlockBoundary::End(block)); |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | pub fn write_fmt(&mut self, args: Arguments) { |
| 122 | Write::write_fmt(self, args).unwrap(); |
| 123 | } |
| 124 | |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 125 | fn write(&mut self, b: &str) { |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 126 | if !b.is_empty() { |
| David Tolnay | f02146e | 2020-11-01 15:35:42 -0800 | [diff] [blame] | 127 | if self.blocks_pending > 0 { |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 128 | self.flush_blocks(); |
| 129 | } |
| 130 | if self.section_pending && !self.bytes.is_empty() { |
| David Tolnay | f02146e | 2020-11-01 15:35:42 -0800 | [diff] [blame] | 131 | self.bytes.push('\n'); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 132 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 133 | self.bytes.push_str(b); |
| David Tolnay | f02146e | 2020-11-01 15:35:42 -0800 | [diff] [blame] | 134 | self.section_pending = false; |
| 135 | self.blocks_pending = 0; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 136 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 137 | } |
| David Tolnay | e3b3982 | 2020-11-01 15:55:21 -0800 | [diff] [blame^] | 138 | |
| 139 | fn push_block_boundary(&mut self, boundary: BlockBoundary<'a>) { |
| 140 | if self.blocks_pending > 0 && boundary == self.blocks.last().unwrap().rev() { |
| 141 | self.blocks.pop(); |
| 142 | self.blocks_pending -= 1; |
| 143 | } else { |
| 144 | self.blocks.push(boundary); |
| 145 | self.blocks_pending += 1; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn flush(&mut self) { |
| 150 | if self.blocks_pending > 0 { |
| 151 | self.flush_blocks(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn flush_blocks(&mut self) { |
| 156 | self.section_pending = !self.bytes.is_empty(); |
| 157 | let mut read = self.blocks.len() - self.blocks_pending; |
| 158 | let mut write = read; |
| 159 | |
| 160 | while read < self.blocks.len() { |
| 161 | match self.blocks[read] { |
| 162 | BlockBoundary::Begin(begin_block) => { |
| 163 | if self.section_pending { |
| 164 | self.bytes.push('\n'); |
| 165 | self.section_pending = false; |
| 166 | } |
| 167 | Block::write_begin(begin_block, &mut self.bytes); |
| 168 | self.blocks[write] = BlockBoundary::Begin(begin_block); |
| 169 | write += 1; |
| 170 | } |
| 171 | BlockBoundary::End(end_block) => { |
| 172 | write = write.checked_sub(1).unwrap(); |
| 173 | let begin_block = self.blocks[write]; |
| 174 | assert_eq!(begin_block, BlockBoundary::Begin(end_block)); |
| 175 | Block::write_end(end_block, &mut self.bytes); |
| 176 | self.section_pending = true; |
| 177 | } |
| 178 | } |
| 179 | read += 1; |
| 180 | } |
| 181 | |
| 182 | self.blocks.truncate(write); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | impl<'a> BlockBoundary<'a> { |
| 187 | fn rev(self) -> BlockBoundary<'a> { |
| 188 | match self { |
| 189 | BlockBoundary::Begin(block) => BlockBoundary::End(block), |
| 190 | BlockBoundary::End(block) => BlockBoundary::Begin(block), |
| 191 | } |
| 192 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 193 | } |