Skip to content

Commit 1214d68

Browse files
committed
Use a new technique for rendering negative expressions
This is the suggestions from lukaslueg
1 parent 904b010 commit 1214d68

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Diff for: mdbook-spec/src/grammar.rs

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ fn check_unexpected_roots(grammar: &Grammar, diag: &mut Diagnostics) {
187187
let expected: HashSet<_> = [
188188
"CfgAttrAttribute",
189189
"CfgAttribute",
190+
"CHAR",
190191
"Crate",
191192
"INNER_LINE_DOC",
192193
"LINE_COMMENT",

Diff for: mdbook-spec/src/grammar/render_railroad.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ impl Expression {
199199
}
200200
ExpressionKind::NegExpression(e) => {
201201
let n = e.render_railroad(stack, link_map, reverse)?;
202-
let lbox = LabeledBox::new(n, Comment::new("any character except".to_string()));
203-
Box::new(lbox)
202+
let ch = node_for_nt(link_map, "CHAR");
203+
Box::new(Except::new(Box::new(ch), n))
204204
}
205205
ExpressionKind::Unicode(s) => Box::new(Terminal::new(format!("U+{}", s))),
206206
};
@@ -242,3 +242,42 @@ fn strip_markdown(s: &str) -> String {
242242
LazyLock::new(|| Regex::new(r"(?s)\[([^\]]+)\](?:\[[^\]]*\]|\([^)]*\))?").unwrap());
243243
LINK_RE.replace_all(s, "$1").to_string()
244244
}
245+
246+
struct Except {
247+
inner: LabeledBox<Box<dyn Node>, Box<dyn Node>>,
248+
}
249+
250+
impl Except {
251+
fn new(inner: Box<dyn Node>, label: Box<dyn Node>) -> Self {
252+
let grid = Box::new(VerticalGrid::new(vec![
253+
Box::new(Comment::new("⚠️ with the exception of".to_owned())) as Box<dyn Node>,
254+
label,
255+
])) as Box<dyn Node>;
256+
let mut this = Self {
257+
inner: LabeledBox::new(inner, grid),
258+
};
259+
this.inner
260+
.attr("class".to_owned())
261+
.or_default()
262+
.push_str(" exceptbox");
263+
this
264+
}
265+
}
266+
267+
impl Node for Except {
268+
fn entry_height(&self) -> i64 {
269+
self.inner.entry_height()
270+
}
271+
272+
fn height(&self) -> i64 {
273+
self.inner.height()
274+
}
275+
276+
fn width(&self) -> i64 {
277+
self.inner.width()
278+
}
279+
280+
fn draw(&self, x: i64, y: i64, h_dir: svg::HDir) -> svg::Element {
281+
self.inner.draw(x, y, h_dir)
282+
}
283+
}

Diff for: src/notation.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ The following are common definitions used in the grammar.
5252

5353
r[input.syntax]
5454
```grammar,lexer
55+
CHAR -> <a Unicode scalar value>
56+
5557
NUL -> U+0000
5658
5759
TAB -> U+0009

Diff for: theme/reference.css

+4
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,7 @@ svg.railroad g.labeledbox>rect {
705705
stroke-dasharray: 5px;
706706
fill: rgba(90, 90, 150, .1);
707707
}
708+
709+
svg.railroad g.exceptbox > rect {
710+
fill:rgba(245, 160, 125, .1);
711+
}

0 commit comments

Comments
 (0)