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