| 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 | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 21 | section_pending: bool, |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 22 | blocks_pending: Vec<Block<'a>>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 25 | impl<'a> OutFile<'a> { |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 26 | pub fn new(header: bool, opt: &'a Opt, types: &'a Types) -> Self { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 27 | OutFile { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 28 | header, |
| David Tolnay | e1476af | 2020-11-01 13:47:25 -0800 | [diff] [blame] | 29 | opt, |
| David Tolnay | b560a0f | 2020-10-30 21:28:45 -0700 | [diff] [blame] | 30 | types, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 31 | include: Includes::new(), |
| David Tolnay | 3be0e1f | 2020-10-31 20:53:00 -0700 | [diff] [blame] | 32 | builtin: Builtins::new(), |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 33 | content: RefCell::new(Content::new()), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | // Write a blank line if the preceding section had any contents. |
| 38 | pub fn next_section(&mut self) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 39 | self.content.get_mut().next_section(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 42 | pub fn begin_block(&mut self, block: Block<'a>) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 43 | self.content.get_mut().begin_block(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 46 | pub fn end_block(&mut self, block: Block<'a>) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 47 | self.content.get_mut().end_block(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 50 | pub fn write_fmt(&self, args: Arguments) { |
| 51 | let content = &mut *self.content.borrow_mut(); |
| 52 | Write::write_fmt(content, args).unwrap(); |
| 53 | } |
| 54 | |
| 55 | pub fn content(&self) -> Vec<u8> { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 56 | let include = &self.include.content.bytes; |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 57 | let builtin = &self.builtin.content.bytes; |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 58 | let content = &self.content.borrow().bytes; |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 59 | let len = include.len() + builtin.len() + content.len() + 2; |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 60 | let mut out = String::with_capacity(len); |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 61 | out.push_str(include); |
| David Tolnay | 8c14d9a | 2020-10-31 21:52:38 -0700 | [diff] [blame] | 62 | if !out.is_empty() && !builtin.is_empty() { |
| 63 | out.push('\n'); |
| 64 | } |
| 65 | out.push_str(builtin); |
| 66 | if !out.is_empty() && !content.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 67 | out.push('\n'); |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 68 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 69 | out.push_str(content); |
| David Tolnay | 91489ec | 2020-09-03 12:48:46 -0700 | [diff] [blame] | 70 | if out.is_empty() { |
| 71 | out.push_str("// empty\n"); |
| 72 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 73 | out.into_bytes() |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 77 | impl<'a> Write for Content<'a> { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 78 | fn write_str(&mut self, s: &str) -> fmt::Result { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 79 | self.write(s); |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 80 | Ok(()) |
| 81 | } |
| 82 | } |
| 83 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 84 | impl<'a> PartialEq for Content<'a> { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 85 | fn eq(&self, _other: &Content) -> bool { |
| 86 | true |
| 87 | } |
| 88 | } |
| 89 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 90 | impl<'a> Content<'a> { |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 91 | fn new() -> Self { |
| David Tolnay | 8810a54 | 2020-10-31 21:39:22 -0700 | [diff] [blame] | 92 | Content::default() |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 95 | pub fn next_section(&mut self) { |
| 96 | self.section_pending = true; |
| 97 | } |
| 98 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 99 | pub fn begin_block(&mut self, block: Block<'a>) { |
| David Tolnay | 0c033e3 | 2020-11-01 15:15:48 -0800 | [diff] [blame] | 100 | self.blocks_pending.push(block); |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| David Tolnay | 97c5b86 | 2020-11-01 14:59:01 -0800 | [diff] [blame] | 103 | pub fn end_block(&mut self, block: Block<'a>) { |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 104 | if self.blocks_pending.pop().is_none() { |
| David Tolnay | 2a160e4 | 2020-11-01 15:40:20 -0800 | [diff] [blame^] | 105 | Block::write_end(block, &mut self.bytes); |
| David Tolnay | cb2189f | 2020-10-31 21:23:08 -0700 | [diff] [blame] | 106 | self.section_pending = true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn write_fmt(&mut self, args: Arguments) { |
| 111 | Write::write_fmt(self, args).unwrap(); |
| 112 | } |
| 113 | |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 114 | fn write(&mut self, b: &str) { |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 115 | if !b.is_empty() { |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 116 | if !self.blocks_pending.is_empty() { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 117 | if !self.bytes.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 118 | self.bytes.push('\n'); |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 119 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 120 | for block in self.blocks_pending.drain(..) { |
| David Tolnay | 2a160e4 | 2020-11-01 15:40:20 -0800 | [diff] [blame^] | 121 | Block::write_begin(block, &mut self.bytes); |
| David Tolnay | b92e66f | 2020-03-01 13:36:55 -0800 | [diff] [blame] | 122 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 123 | self.section_pending = false; |
| 124 | } else if self.section_pending { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 125 | if !self.bytes.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 126 | self.bytes.push('\n'); |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 127 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 128 | self.section_pending = false; |
| 129 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 130 | self.bytes.push_str(b); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 132 | } |
| 133 | } |