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