Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions theme/reference.css
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,3 @@ html.navy svg.railroad,
html.ayu svg.railroad {
background-color: hsl(from var(--bg) h s calc(l + 5));
}

/* Styling specific to the Reference's negative-lookahead railroad node. */
svg.railroad g.exceptbox > rect {
fill:rgba(245, 160, 125, .1);
}
2 changes: 1 addition & 1 deletion tools/mdbook-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mdbook-markdown = "0.5.1"
mdbook-preprocessor = "0.5.1"
once_cell = "1.19.0"
pathdiff = "0.2.1"
railroad = { version = "0.3.9", default-features = false }
railroad = { version = "0.3.10", default-features = false }
regex = "1.12.2"
semver = "1.0.21"
serde_json = "1.0.113"
Expand Down
98 changes: 36 additions & 62 deletions tools/mdbook-spec/src/grammar/render_railroad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@ fn render_expression(expr: &Expression, cx: &RenderCtx, stack: bool) -> Option<B
}
}
ExpressionKind::NegativeLookahead(e) => {
let forward = render_expression(e, cx, stack)?;
let lbox =
LabeledBox::new(forward, Comment::new("not followed by".to_string()));
Box::new(lbox)
let forward = Sequence::new(vec![
render_expression(e, cx, stack)?,
Box::new(Continuation),
]);
Box::new(Annotation::new_ahead(Scale::new(
LabeledBox::new(
forward,
Comment::new("⚠️ Ahead must not match".to_owned()),
),
0.85,
)))
}
// Treat `e?` and `e{..=1}` / `e{0..=1}` equally.
ExpressionKind::Optional(e)
Expand Down Expand Up @@ -313,9 +320,8 @@ fn render_expression(expr: &Expression, cx: &RenderCtx, stack: bool) -> Option<B
} => unreachable!("closed range must have upper bound"),
ExpressionKind::RepeatRangeNamed(e, name) => {
let n = render_expression(e, cx, stack)?;
let cmt = format!("repeat exactly {name} times");
let lbox = LabeledBox::new(n, Comment::new(cmt));
Box::new(lbox)
let cmt = format!("repeat exactly `{name} - 1` times");
Box::new(Repeat::new(n, Comment::new(cmt)))
}
ExpressionKind::Nt(nt) => node_for_nt(cx, nt),
ExpressionKind::Terminal(t) => Box::new(Terminal::new(t.clone())),
Expand All @@ -342,9 +348,16 @@ fn render_expression(expr: &Expression, cx: &RenderCtx, stack: bool) -> Option<B
Box::new(Terminal::new(s))
}
ExpressionKind::NegExpression(e) => {
let n = render_expression(e, cx, stack)?;
let n = Sequence::new(vec![
Box::new(Continuation) as Box<dyn Node>,
render_expression(e, cx, stack)?,
]);
let ch = node_for_nt(cx, "CHAR");
Box::new(Except::new(Box::new(ch), n))
let assertion = Box::new(Annotation::new_behind(Scale::new(
LabeledBox::new(n, Comment::new("⚠️ Prior must not match".to_owned())),
0.85,
)));
Box::new(Sequence::new(vec![ch, assertion]))
}
ExpressionKind::Cut(e) => {
let rhs = render_expression(e, cx, stack)?;
Expand All @@ -361,7 +374,7 @@ fn render_expression(expr: &Expression, cx: &RenderCtx, stack: bool) -> Option<B
..
} = expr.kind
{
let cmt = format!("repeat count {name}");
let cmt = format!("repeat count `{name}`");
let lbox = LabeledBox::new(n, Comment::new(cmt));
Box::new(lbox) as Box<dyn Node>
} else {
Expand Down Expand Up @@ -410,45 +423,6 @@ fn strip_markdown(s: &str) -> String {
LINK_RE.replace_all(s, "$1").to_string()
}

struct Except {
inner: LabeledBox<Box<dyn Node>, Box<dyn Node>>,
}

impl Except {
fn new(inner: Box<dyn Node>, label: Box<dyn Node>) -> Self {
let grid = Box::new(VerticalGrid::new(vec![
Box::new(Comment::new("⚠️ with the exception of".to_owned())) as Box<dyn Node>,
label,
])) as Box<dyn Node>;
let mut this = Self {
inner: LabeledBox::new(inner, grid),
};
this.inner
.attr("class".to_owned())
.or_default()
.push_str(" exceptbox");
this
}
}

impl Node for Except {
fn entry_height(&self) -> i64 {
self.inner.entry_height()
}

fn height(&self) -> i64 {
self.inner.height()
}

fn width(&self) -> i64 {
self.inner.width()
}

fn draw(&self, x: i64, y: i64, h_dir: svg::HDir) -> svg::Element {
self.inner.draw(x, y, h_dir)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -574,7 +548,7 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("not followed by"),
svg.contains("Ahead must not match"),
"should contain the 'not followed by' label"
);
assert!(svg.contains("CHAR"), "should contain the nonterminal name");
Expand All @@ -590,7 +564,7 @@ mod tests {
0,
);
let svg = render_to_svg(&expr).unwrap();
assert!(svg.contains("not followed by"));
assert!(svg.contains("Ahead must not match"));
assert!(svg.contains("CR"));
}

Expand All @@ -607,7 +581,7 @@ mod tests {
0,
);
let svg = render_to_svg(&expr).unwrap();
assert!(svg.contains("not followed by"));
assert!(svg.contains("Ahead must not match"));
assert!(svg.contains("e"));
assert!(svg.contains("E"));
}
Expand Down Expand Up @@ -699,7 +673,7 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("with the exception of"),
svg.contains("Prior must not match"),
"neg expression should have exception label"
);
}
Expand All @@ -708,7 +682,7 @@ mod tests {

#[test]
fn repeat_range_named_reference() {
// RepeatRangeNamed renders with a "repeat exactly n times"
// RepeatRangeNamed renders with a "repeat exactly `n` times"
// label.
let expr = Expression::new_kind(
ExpressionKind::RepeatRangeNamed(
Expand All @@ -719,8 +693,8 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("repeat exactly n times"),
"expected 'repeat exactly n times' label, got: {svg}"
svg.contains("repeat exactly `n - 1` times"),
"expected 'repeat exactly `n - 1` times' label, got: {svg}"
);
}

Expand All @@ -739,8 +713,8 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("repeat count n"),
"expected 'repeat count n' label, got: {svg}"
svg.contains("repeat count `n`"),
"expected 'repeat count `n`' label, got: {svg}"
);
}

Expand All @@ -760,8 +734,8 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("repeat count k"),
"expected 'repeat count k' label, got: {svg}"
svg.contains("repeat count `k`"),
"expected 'repeat count `k`' label, got: {svg}"
);
}

Expand Down Expand Up @@ -802,8 +776,8 @@ mod tests {
);
let svg = render_to_svg(&expr).unwrap();
assert!(
svg.contains("repeat count n"),
"expected 'repeat count n' label on identity range"
svg.contains("repeat count `n`"),
"expected 'repeat count `n`' label on identity range"
);
}
}
Loading