//! `randsequence` — laying out the production grammar of IEEE 1800 §18.17. //! //! One production per line, indented inside the `randsequence`/`endsequence` //! pair; a production's alternatives print after the `:` and, when the line //! does not fit, one per line with each `|` under that `:` — the shape the //! LRM's own examples are written in. use crate::doc::*; use vlog_parser::ast::*; use vlog_parser::token::Comment; use super::{name_doc, Fmt}; impl<'a> Fmt<'a> { /// `randsequence (main) … endsequence`. pub(super) fn rand_sequence(&self, r: &RandSequence) -> Doc { let mut docs = Vec::new(); self.word_then("randsequence", &r.after_kw, &mut docs); docs.push(text("(")); self.slot_comments(&r.after_open, &mut docs); if let Some(n) = &r.start { docs.push(name_doc(&n.text)); } self.slot_comments(&r.before_close, &mut docs); docs.push(text(")")); let mut body = Vec::new(); for p in &r.productions { body.push(if p.meta.blank_before { Doc::BlankLine } else { Doc::HardLine }); body.extend(self.leading_comments(&p.meta)); body.push(self.production(p)); body.extend(self.trailing(&p.meta)); } body.extend(self.dangling(&r.dangling)); docs.push(indent(body)); docs.push(Doc::HardLine); docs.push(text("endsequence")); concat(docs) } /// One production: its head, then the alternatives after the `:`. /// /// The alternatives are one group: they stay on the head's line while they /// fit, and otherwise break one per line with the `|` starting each new /// one, aligned under the head's indent. fn production(&self, p: &Production) -> Doc { let mut head = Vec::new(); if let Some(t) = self.data_type(p.ret_type.as_ref().unwrap_or(&DataType::default())) { head.push(t); head.push(text(" ")); } head.push(name_doc(&p.name)); if p.has_parens { head.push(self.tf_args(&p.args, &[])); } self.slot_comments(&p.before_colon, &mut head); head.push(text(if p.before_colon.is_empty() { " :" } else { ":" })); // One alternative rides on the head's line: it is the common shape, // and a code block inside it cuddles onto the `:` the way a `begin` // cuddles onto an `if`. Several break one per line, each `|` starting // its own, when they do not all fit. if let [only] = p.alternatives.as_slice() { self.after_bar(&only.after_bar, &mut head); head.push(self.alternative(only)); return concat(vec![concat(head), self.semi(&p.before_semi)]); } let mut alts = Vec::new(); for (i, alt) in p.alternatives.iter().enumerate() { // The comments written in front of the `|` print at the end of // the alternative before it, which is where the source put them; // a line comment among them forces the break the `|` needs. let broke = i > 0 && self.inline_comments(&alt.before_bar, &mut alts); if i > 0 { alts.push(if broke { Doc::HardLine } else { Doc::Line }); alts.push(text("|")); self.after_bar(&alt.after_bar, &mut alts); } else if alt.after_bar.is_empty() { alts.push(Doc::Line); } else { // A comment here is the separator; the break opportunity it // replaces is not wanted on the first alternative, which has // no `|` of its own to start a line with. self.inline_comments_spaced(&alt.after_bar, &mut alts); } alts.push(self.alternative(alt)); } head.push(indent(alts)); concat(vec![group(head), self.semi(&p.before_semi)]) } /// The separator between the `:`/`|` an alternative follows and its first /// item: the comments written there, or a plain space. fn after_bar(&self, comments: &[Comment], docs: &mut Vec) { match comments.is_empty() { true => docs.push(text(" ")), false => self.inline_comments_spaced(comments, docs), } } /// One `|`-separated right-hand side, with its `:= weight` when it has /// one. The separator in front of it is the caller's, since only the /// caller knows whether a `|` or a line break stands there. fn alternative(&self, a: &Alternative) -> Doc { let mut docs = Vec::new(); for (i, item) in a.items.iter().enumerate() { if i > 0 { docs.push(text(" ")); } docs.push(self.prod_item(item)); } if let Some(w) = &a.weight { self.slot_comments(&a.before_weight, &mut docs); docs.push(text(if a.before_weight.is_empty() { " :=" } else { ":=" })); self.word_then("", &w.after_op, &mut docs); docs.push(self.expr(&w.expr)); if let Some(block) = &w.block { docs.push(text(" ")); docs.push(self.code_block(block)); } } concat(docs) } /// One item of an alternative — a reference, a code block, or one of the /// four control shapes. fn prod_item(&self, item: &ProdItem) -> Doc { match item { ProdItem::Ref(r) => self.prod_ref(r), ProdItem::Block(b) => self.code_block(b), ProdItem::If { after_kw, cond, then, before_else, after_else, els } => { let mut docs = vec![text("if")]; self.word_then("", after_kw, &mut docs); docs.push(self.parens(cond)); docs.push(text(" ")); docs.push(self.prod_item(then)); if let Some(e) = els { self.word_then("", before_else, &mut docs); docs.push(text("else")); self.word_then("", after_else, &mut docs); docs.push(self.prod_item(e)); } concat(docs) } ProdItem::Repeat { after_kw, count, body } => { let mut docs = vec![text("repeat")]; self.word_then("", after_kw, &mut docs); docs.push(self.parens(count)); docs.push(text(" ")); docs.push(self.prod_item(body)); concat(docs) } ProdItem::Case { after_kw, expr, items, dangling } => { let mut docs = vec![text("case")]; self.word_then("", after_kw, &mut docs); docs.push(self.parens(expr)); let mut body = Vec::new(); for arm in items { body.push(if arm.meta.blank_before { Doc::BlankLine } else { Doc::HardLine }); body.extend(self.leading_comments(&arm.meta)); body.push(self.prod_case_item(arm)); body.extend(self.trailing(&arm.meta)); } body.extend(self.dangling(dangling)); docs.push(indent(body)); docs.push(Doc::HardLine); docs.push(text("endcase")); concat(docs) } ProdItem::RandJoin { after_rand, weight, items } => { let mut docs = vec![text("rand")]; self.word_then("", after_rand, &mut docs); docs.push(text("join")); if let Some(w) = weight { docs.push(text(" ")); docs.push(self.parens(w)); } for item in items { docs.push(text(" ")); docs.push(self.prod_item(item)); } concat(docs) } } } /// One arm of an `rs_case`: `labels : item;` or `default : item;`. /// /// The labels and their `:` are printed by the same helper a `case` /// statement's arm uses — including the `default` whose `:` IEEE 1800 /// leaves optional — because they are the same tokens with the same /// comment slots; only the body is a production item rather than a /// statement. fn prod_case_item(&self, arm: &ProdCaseItem) -> Doc { let mut docs = vec![self.case_labels(&arm.labels, &arm.before_colon, arm.span)]; self.word_then("", &arm.after_colon, &mut docs); docs.push(self.prod_item(&arm.item)); docs.push(self.semi(&arm.before_semi)); concat(docs) } /// `name` or `name(a, b)`. fn prod_ref(&self, r: &ProdRef) -> Doc { let mut docs = vec![name_doc(&r.name)]; if r.has_parens { self.slot_comments(&r.before_args, &mut docs); docs.push(self.expr_list("(", &r.args, ")")); } concat(docs) } /// An `rs_code_block`: `{ statements }`, laid out as a `begin`/`end` body /// is — one statement per line, indented, the braces on their own lines /// when the block holds anything at all. fn code_block(&self, b: &CodeBlock) -> Doc { if b.stmts.is_empty() && b.dangling.is_empty() { return text("{}"); } let mut body = Vec::new(); if !b.stmts.is_empty() { body.push(Doc::HardLine); body.extend(self.stmt_seq(&b.stmts)); } body.extend(self.dangling(&b.dangling)); concat(vec![text("{"), indent(body), Doc::HardLine, text("}")]) } }