321 lines
12 KiB
Rust
321 lines
12 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! `randsequence` — the production grammar of IEEE 1800 §18.17.
|
|
//!
|
|
//! One file for one sub-grammar, the way the SVA and covergroup grammars each
|
|
//! have their own: a `randsequence` body is a production list, not a
|
|
//! statement list, and nothing outside it reads these productions.
|
|
//!
|
|
//! The nesting between an alternative's items and the control shapes that
|
|
//! wrap them is real recursion, so `parse_prod_item` brackets itself with
|
|
//! [`Parser::enter`] / [`Parser::leave`] the way every other recursive
|
|
//! production does.
|
|
|
|
use super::*;
|
|
|
|
impl<'a> Parser<'a> {
|
|
/// `randsequence ( [name] ) production { production } endsequence`
|
|
/// (IEEE 1800 §18.17), the cursor on the keyword.
|
|
pub(super) fn parse_randsequence(&mut self) -> PResult<RandSequence> {
|
|
self.expect_kw("randsequence")?;
|
|
let after_kw = self.take_before_open("(");
|
|
self.expect_sym("(")?;
|
|
let after_open = self.take_gap();
|
|
let start =
|
|
if self.cur().kind == TokenKind::Ident { Some(self.expect_name()?) } else { None };
|
|
let before_close = self.gap_before(")");
|
|
self.expect_sym(")")?;
|
|
let mut productions = Vec::new();
|
|
while !self.at_body_end("endsequence") {
|
|
productions.push(self.parse_production()?);
|
|
}
|
|
let dangling = self.take_dangling();
|
|
self.expect_closer("endsequence")?;
|
|
Ok(RandSequence { after_kw, after_open, start, before_close, productions, dangling })
|
|
}
|
|
|
|
/// One production: `[data_type] name [( ports )] : alternatives ;`.
|
|
///
|
|
/// The optional return type is told from the name by what follows the
|
|
/// identifier: a production's name is the last identifier before the `(`,
|
|
/// the `:` or another identifier, so a second identifier means the first
|
|
/// one was the type.
|
|
fn parse_production(&mut self) -> PResult<Production> {
|
|
let start = self.here();
|
|
let mut meta = self.take_meta();
|
|
let ret_type = self.parse_opt_production_type()?;
|
|
let Name { text: name, span: name_span } = self.expect_name()?;
|
|
let has_parens = self.cur().is_sym("(");
|
|
let args = self.parse_tf_ports(false)?.args;
|
|
let before_colon = self.gap_before(":");
|
|
self.expect_sym(":")?;
|
|
let mut alternatives = vec![self.parse_alternative(Vec::new())?];
|
|
while self.cur().is_sym("|") {
|
|
// The gap in front of the `|` belongs to the alternative it opens,
|
|
// so it prints on the bar's own side of the previous one.
|
|
let before_bar = self.take_gap();
|
|
self.skip();
|
|
alternatives.push(self.parse_alternative(before_bar)?);
|
|
}
|
|
let span = self.span_from(start);
|
|
let before_semi = self.gap_before(";");
|
|
self.expect_sym(";")?;
|
|
if meta.trailing.is_none() {
|
|
meta.trailing = self.take_trailing();
|
|
}
|
|
Ok(Production {
|
|
meta,
|
|
ret_type,
|
|
name,
|
|
name_span,
|
|
has_parens,
|
|
args,
|
|
before_colon,
|
|
alternatives,
|
|
before_semi,
|
|
span,
|
|
})
|
|
}
|
|
|
|
/// The production's return type, when it wrote one. `void` is a keyword
|
|
/// rather than an identifier, so it is read here too and kept as the type
|
|
/// name it is written as.
|
|
fn parse_opt_production_type(&mut self) -> PResult<Option<DataType>> {
|
|
let t = self.cur();
|
|
let typed = if t.is_kw("void") {
|
|
true
|
|
} else if t.kind == TokenKind::Keyword {
|
|
// `int name : …`, `bit [3:0] name : …` — a type keyword can only
|
|
// be the type, never the production's own name.
|
|
TYPE_KEYWORDS.contains(&t.text.as_str())
|
|
} else {
|
|
// Two identifiers in a row: the first is a user-defined type.
|
|
t.kind == TokenKind::Ident && self.nth(1).kind == TokenKind::Ident
|
|
};
|
|
if !typed {
|
|
return Ok(None);
|
|
}
|
|
Ok(Some(self.parse_opt_data_type()?))
|
|
}
|
|
|
|
/// One `|`-separated right-hand side, with its optional `:= weight`.
|
|
fn parse_alternative(&mut self, before_bar: Vec<Comment>) -> PResult<Alternative> {
|
|
let after_bar = self.take_gap();
|
|
let mut items = vec![self.parse_prod_item()?];
|
|
while self.at_prod_item() {
|
|
items.push(self.parse_prod_item()?);
|
|
}
|
|
let mut before_weight = Vec::new();
|
|
let mut weight = None;
|
|
if self.at_weight_op() {
|
|
before_weight = self.take_gap();
|
|
self.skip(); // :
|
|
self.skip(); // =
|
|
weight = Some(self.parse_weight()?);
|
|
}
|
|
Ok(Alternative { before_bar, after_bar, items, before_weight, weight })
|
|
}
|
|
|
|
/// True when the cursor is on the `:=` of a weight. The lexer has no `:=`
|
|
/// token — it is a `:` and an `=` written adjacently, the same two tokens
|
|
/// a `dist` weight is read from — so adjacency is checked by span and
|
|
/// `a : = 5` written with a space is not one.
|
|
fn at_weight_op(&self) -> bool {
|
|
self.cur().is_sym(":")
|
|
&& self.nth(1).is_sym("=")
|
|
&& self.nth(1).span.start == self.cur().span.end
|
|
}
|
|
|
|
/// The `:= expr [ { code } ]` of a weighted alternative (§18.17.2).
|
|
///
|
|
/// §18.17.2's `weight_specification` is a number, a scoped name or a
|
|
/// *parenthesized* expression — never a bare operator expression — and
|
|
/// that is what keeps `a := 3 | b := 2` readable: an unparenthesized `|`
|
|
/// separates the alternatives, it does not or two weights together. So
|
|
/// the weight is read as a postfix expression and stops there.
|
|
fn parse_weight(&mut self) -> PResult<Weight> {
|
|
let after_op = self.take_gap();
|
|
let expr = self.parse_postfix()?;
|
|
let block =
|
|
if self.cur().is_sym("{") { Some(Box::new(self.parse_rs_code_block()?)) } else { None };
|
|
Ok(Weight { after_op, expr, block })
|
|
}
|
|
|
|
/// True when the cursor opens another item of the alternative rather than
|
|
/// ending it. An alternative ends at `|`, `:=`, `;`, or the `}` / `endcase`
|
|
/// of whatever encloses it.
|
|
fn at_prod_item(&self) -> bool {
|
|
let t = self.cur();
|
|
t.kind == TokenKind::Ident
|
|
|| t.is_sym("{")
|
|
|| t.is_kw("if")
|
|
|| t.is_kw("repeat")
|
|
|| t.is_kw("case")
|
|
|| t.is_kw("rand")
|
|
}
|
|
|
|
/// One `rs_prod`: a production reference, a code block, or one of the four
|
|
/// control shapes.
|
|
fn parse_prod_item(&mut self) -> PResult<ProdItem> {
|
|
self.enter()?;
|
|
let item = self.parse_prod_item_inner();
|
|
self.leave();
|
|
item
|
|
}
|
|
|
|
fn parse_prod_item_inner(&mut self) -> PResult<ProdItem> {
|
|
let t = self.cur();
|
|
if t.is_sym("{") {
|
|
return Ok(ProdItem::Block(Box::new(self.parse_rs_code_block()?)));
|
|
}
|
|
if t.is_kw("if") {
|
|
return self.parse_rs_if();
|
|
}
|
|
if t.is_kw("repeat") {
|
|
self.skip();
|
|
let after_kw = self.take_before_open("(");
|
|
self.expect_sym("(")?;
|
|
let count = self.parse_expr()?;
|
|
self.expect_sym(")")?;
|
|
let body = Box::new(self.parse_prod_item()?);
|
|
return Ok(ProdItem::Repeat { after_kw, count, body });
|
|
}
|
|
if t.is_kw("case") {
|
|
return self.parse_rs_case();
|
|
}
|
|
if t.is_kw("rand") {
|
|
return self.parse_rand_join();
|
|
}
|
|
Ok(ProdItem::Ref(self.parse_prod_ref()?))
|
|
}
|
|
|
|
/// `if ( expr ) item [ else item ]` — the arms are production items, not
|
|
/// statements, so this is not `parse_if_stmt`.
|
|
fn parse_rs_if(&mut self) -> PResult<ProdItem> {
|
|
self.expect_kw("if")?;
|
|
let after_kw = self.take_before_open("(");
|
|
self.expect_sym("(")?;
|
|
let cond = self.parse_expr()?;
|
|
self.expect_sym(")")?;
|
|
let then = Box::new(self.parse_prod_item()?);
|
|
// Whatever was written in the gap before `else` stays on the
|
|
// keyword's left, as an `if` statement's does.
|
|
let mut before_else = Vec::new();
|
|
if self.cur().is_kw("else") {
|
|
before_else = self.take_dangling();
|
|
}
|
|
let (after_else, els) = if self.eat_kw("else") {
|
|
(self.take_gap(), Some(Box::new(self.parse_prod_item()?)))
|
|
} else {
|
|
(Vec::new(), None)
|
|
};
|
|
Ok(ProdItem::If { after_kw, cond, then, before_else, after_else, els })
|
|
}
|
|
|
|
/// `case ( expr ) arm { arm } endcase`, whose arms take production items.
|
|
fn parse_rs_case(&mut self) -> PResult<ProdItem> {
|
|
self.expect_kw("case")?;
|
|
let after_kw = self.take_before_open("(");
|
|
self.expect_sym("(")?;
|
|
let expr = self.parse_expr()?;
|
|
self.expect_sym(")")?;
|
|
let mut items = Vec::new();
|
|
while !self.at_body_end("endcase") {
|
|
items.push(self.parse_rs_case_item()?);
|
|
}
|
|
let dangling = self.take_dangling();
|
|
self.expect_closer("endcase")?;
|
|
Ok(ProdItem::Case { after_kw, expr, items, dangling })
|
|
}
|
|
|
|
/// One arm: `expr {, expr} : item ;` or `default [:] item ;`.
|
|
fn parse_rs_case_item(&mut self) -> PResult<ProdCaseItem> {
|
|
let start = self.here();
|
|
let mut meta = self.take_meta();
|
|
let mut labels = Vec::new();
|
|
let is_default = self.eat_kw("default");
|
|
if !is_default {
|
|
let first = self.parse_expr_item()?;
|
|
labels = self.parse_expr_list(first, ":")?;
|
|
}
|
|
let before_colon = self.gap_before(":");
|
|
// A `default` arm may write no `:` at all (§18.17.4).
|
|
let has_colon = self.cur().is_sym(":");
|
|
if has_colon {
|
|
self.skip();
|
|
} else if !is_default {
|
|
return self.error("expected `:` in a randsequence case arm");
|
|
}
|
|
let after_colon = self.take_gap();
|
|
let item = self.parse_prod_item()?;
|
|
let span = self.span_from(start);
|
|
let before_semi = self.gap_before(";");
|
|
self.expect_sym(";")?;
|
|
if meta.trailing.is_none() {
|
|
meta.trailing = self.take_trailing();
|
|
}
|
|
Ok(ProdCaseItem {
|
|
meta,
|
|
labels,
|
|
is_default,
|
|
before_colon,
|
|
after_colon,
|
|
item,
|
|
before_semi,
|
|
span,
|
|
})
|
|
}
|
|
|
|
/// `rand join [ ( expr ) ] item item { item }` (§18.17.6): the items are
|
|
/// interleaved rather than run in order, and there are at least two.
|
|
fn parse_rand_join(&mut self) -> PResult<ProdItem> {
|
|
self.expect_kw("rand")?;
|
|
let after_rand = self.take_gap();
|
|
self.expect_kw("join")?;
|
|
let weight = if self.cur().is_sym("(") {
|
|
self.skip();
|
|
let e = self.parse_expr()?;
|
|
self.expect_sym(")")?;
|
|
Some(e)
|
|
} else {
|
|
None
|
|
};
|
|
let mut items = vec![self.parse_prod_item()?];
|
|
while self.at_prod_item() {
|
|
items.push(self.parse_prod_item()?);
|
|
}
|
|
Ok(ProdItem::RandJoin { after_rand, weight, items })
|
|
}
|
|
|
|
/// A production reference: `name` or `name(a, b)`.
|
|
fn parse_prod_ref(&mut self) -> PResult<ProdRef> {
|
|
let Name { text: name, span: name_span } = self.expect_name()?;
|
|
let before_args = self.take_before_open("(");
|
|
let has_parens = self.cur().is_sym("(");
|
|
let mut args = Vec::new();
|
|
if has_parens {
|
|
self.skip();
|
|
if !self.cur().is_sym(")") {
|
|
let first = self.parse_expr_item_with(true)?;
|
|
args = self.parse_expr_list(first, ")")?;
|
|
}
|
|
self.expect_sym(")")?;
|
|
}
|
|
Ok(ProdRef { name, name_span, has_parens, before_args, args })
|
|
}
|
|
|
|
/// An `rs_code_block`: `{ … }` around ordinary statements.
|
|
fn parse_rs_code_block(&mut self) -> PResult<CodeBlock> {
|
|
self.expect_sym("{")?;
|
|
let mut stmts = Vec::new();
|
|
while !self.cur().is_sym("}") && !self.at_eof() {
|
|
stmts.push(self.stmt_or_recover()?);
|
|
}
|
|
let dangling = self.take_dangling();
|
|
self.expect_sym("}")?;
|
|
Ok(CodeBlock { stmts, dangling })
|
|
}
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|