//! The constructs lucisv keeps as source bytes rather than as a tree, and //! the capture helpers every speculative parse falls back to. //! //! Vendor and formal collateral — five blocks and two lines that lucisv //! deliberately does not model. Each one is copied out of the source by //! span for the reason a `property` declaration is: the body is a second //! grammar — a specify path's `=>`/`*>` connections, a UDP truth table, a //! checker's assertion vocabulary, a config's `design`/`instance`/`liblist` //! clauses — with no layout convention worth inventing. See //! `docs/features/vendor-formal-grammar/README.md`. //! //! [`Parser::skip_balanced`] and [`Parser::verbatim_since`] are the //! primitives: skip to the matching closer, then cut the bytes since a mark. use super::*; impl<'a> Parser<'a> { /// A block kept byte-for-byte: the keyword at the cursor through the /// matching `close`, its `: label` included. /// /// The scan counts its own opener, so a `checker` written inside a /// `checker` — the only one of the five that may nest (IEEE 1800 §17.1) — /// closes on the right keyword. A `;` after the closer is left where it /// is: none of the five takes one, so it is the null item it looks like /// and the `empty-item` lint gets to say so. pub(super) fn parse_opaque_block(&mut self, close: &str) -> PResult { let start = self.here(); let col = self.cur().col; let open = self.bump().text; let mut depth = 1usize; while !self.at_eof() { let t = self.cur(); if t.is_kw(close) { depth -= 1; if depth == 0 { break; } } else if t.is_kw(&open) { depth += 1; } self.skip(); } self.expect_closer(close)?; self.parse_label()?; Ok(self.verbatim_since(start, col)) } /// An item kept byte-for-byte that ends at its own `;`: the `library /// lib_name file_path, ...;` line a library map file is made of (IEEE /// 1800 §33.3), and the `specparam` spelling /// [`Parser::parse_specparam`] could not read. pub(super) fn parse_opaque_line(&mut self) -> PResult { let start = self.here(); let col = self.cur().col; self.skip(); self.skip_to_semi()?; Ok(self.verbatim_since(start, col)) } /// `specparam [range] name = constant_expression {, ...};` (IEEE 1800 /// §3.12). /// /// A specify parameter is a declaration with a `localparam`'s shape, so it /// becomes the same [`ParamDecl`] node — which is what makes the `tRISE` /// of a vendored cell model hover, resolve and complete. The one spelling /// that does not fit is the `PATHPULSE$a$b = (reject, error);` form, whose /// value is a pair rather than an expression; that one rewinds and is kept /// verbatim, the way a constraint body the grammar does not know is. pub(super) fn parse_specparam(&mut self) -> PResult { let mark = self.rewind_mark(); match self.parse_specparam_decl() { Ok(decl) => Ok(ItemKind::ParamDecl(decl)), Err(_) => { self.rewind_to(mark); self.parse_opaque_line().map(ItemKind::Verbatim) } } } fn parse_specparam_decl(&mut self) -> PResult { let kw = self.bump().text; let after_kw = self.take_gap(); let ty = self.parse_opt_data_type()?; let declarators = self.parse_declarators()?; self.expect_sym(";")?; Ok(ParamDecl { kw, after_kw, is_type: false, ty, declarators }) } /// `alias a = b = c;` (IEEE 1800 §10.11) — one bidirectional connection /// between two or more net lvalues. /// /// The `=` between the terms is a *separator*, not an operator: no term is /// the value of any other, which is why they are kept as a list rather /// than as a nested assignment. How many there are, and whether they are /// legal lvalues, is a checker's business. pub(super) fn parse_alias(&mut self) -> PResult { self.skip(); // alias let mut nets = vec![self.parse_expr()?]; while self.eat_sym("=") { nets.push(self.parse_expr()?); } self.expect_sym(";")?; Ok(ItemKind::Alias(nets)) } /// True when `expect` at the cursor opens a concurrent `expect` statement /// (IEEE 1800 §16.17) rather than calling a task named `expect`. /// /// The two spellings are `expect (property_spec) action_block` and /// `expect(args);`, and only what stands *between* the parentheses tells /// them apart. The corpus has 41 of the second — `expect(word1[7:0]);` in /// picorv32's spiflash testbench — and none of the first, so the word is /// never reserved and the assertion reading is taken only when the /// parenthesised run holds something no argument list can: a `##` delay, a /// `|->` or `|=>` implication, `throughout`, `disable iff`, or a clocking /// event `@(`. Anything else stays the call it has always been. /// /// The list is deliberately narrow and its misses are safe: an /// `expect (done) else $error;` written with a purely boolean property is /// read as a call, which is the parse error it already was. pub(super) fn at_expect_statement(&self) -> bool { if !word_at(self.cur(), "expect") || !self.nth(1).is_sym("(") { return false; } // `scan_matching` answers one past the `)`, and the closer itself can // hold no operator, so the scan stops before it either way. let end = self.scan_matching(self.pos + 1, "(", ")"); for i in (self.pos + 2)..end { let Some(t) = self.tokens.get(i) else { return false }; let next = self.tokens.get(i + 1); let sequence = match t.kind { // `|->` and `|=>` are two tokens each: the lexer's operator // table has no entry for either, deliberately, and adding one // would buy nothing outside a property. TokenKind::Symbol => { t.text == "##" || (t.text == "|" && next.is_some_and(|n| n.is_sym("->"))) || (t.text == "|=" && next.is_some_and(|n| n.is_sym(">"))) || (t.text == "@" && next.is_some_and(|n| n.is_sym("("))) } TokenKind::Ident => t.text == "throughout", TokenKind::Keyword => t.text == "disable" && next.is_some_and(|n| n.is_kw("iff")), _ => false, }; if sequence { return true; } } false } /// `expect (property_spec) action_block`, kept as verbatim source for the /// reason [`Parser::parse_concurrent_assertion`] gives. pub(super) fn parse_expect_statement(&mut self) -> PResult { let start = self.here(); let col = self.cur().col; self.skip(); // expect self.expect_sym("(")?; self.skip_balanced("(", ")")?; self.skip_action_block()?; Ok(self.verbatim_since(start, col)) } /// Consume tokens through the `close` that matches the `open` already /// consumed. Brackets inside strings and comments cannot appear here — /// the lexer has already turned those into single tokens. pub(super) fn skip_balanced(&mut self, open: &str, close: &str) -> PResult<()> { let mut depth = 1usize; while depth > 0 { if self.at_eof() { return self.error(format!("expected `{close}`")); } let t = self.cur(); if t.is_sym(open) { depth += 1; } else if t.is_sym(close) { depth -= 1; } self.skip(); } Ok(()) } /// The source bytes consumed since `start`, as verbatim text. /// /// A comment written *inside* the block is part of those bytes, so the /// copy [`Parser::bump`] moved into a pending bucket is dropped — leaving /// it there would reattach it to the next node and print it a second time. /// The filter is by span rather than by a saved length precisely so the /// comment written *after* the block's last token on the same line /// (`} // end of constraint`) survives: it is outside the span, so it /// stays pending and is harvested by whoever comes next. pub(super) fn verbatim_since(&mut self, start: u32, col: u32) -> RawText { let span = self.span_from(start); let outside = |c: &Comment| c.span.end <= span.start || c.span.start >= span.end; self.pending_leading.retain(outside); self.pending_trailing.retain(outside); RawText { text: self.slice(span), col, span } } /// Consume everything through the next `;` at bracket depth zero. /// /// Bracketed runs are skipped whole, so a `;` written inside a /// `dist { ... }` set — or inside a library map's `-incdir` list — does /// not end the scan early. pub(super) fn skip_to_semi(&mut self) -> PResult<()> { loop { if self.at_eof() { return self.error("expected `;`"); } let t = self.cur(); if t.is_sym("(") || t.is_sym("[") || t.is_sym("{") { let (open, close) = match t.text.as_str() { "(" => ("(", ")"), "[" => ("[", "]"), _ => ("{", "}"), }; self.skip(); self.skip_balanced(open, close)?; } else if self.eat_sym(";") { return Ok(()); } else { self.skip(); } } } }