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