Skip to content

Commit e323716

Browse files
committed
Render * repeats in forward direction
There are two ways to render a "zero or more" (i.e. `*`) repeat. One is to put nothing on the main forward line and to put the pattern on the recurrent edge, and the other is to put the pattern on the main forward line and to have an empty recurrent edge and an empty bypass edge. That is, for the latter, we can think of `thing*` as `(thing+)?`. Doing it that latter way means an additional edge, but it buys us something big in return, which is that it keeps all the patterns going in the forward direction. Doing it the other way means the patterns have to be reversed so as to put them underneath on that recurrent edge, and it means that readers then have to read them right to left. Reversing the elements also causes a bug in some diagrams where the lines end up running in opposing directions and so the trains crash into each other. See: - #1787 (comment) Keeping things in the forward direction avoids this problem. In this commit, we'll leave in place all the infrastructure for reversing the elements though it is no longer used. We can of course pull this out later.
1 parent 4d69e5a commit e323716

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,12 @@ impl Expression {
163163
Box::new(Optional::new(n))
164164
}
165165
ExpressionKind::Repeat(e) => {
166-
// Railroad renders everything in the opposite order. However,
167-
// our grammar is not written that way, so we need to undo the
168-
// reversal.
169-
let n = e.render_railroad(stack, link_map, !reverse)?;
170-
Box::new(Repeat::new(railroad::Empty, n))
166+
let n = e.render_railroad(stack, link_map, reverse)?;
167+
Box::new(Optional::new(Repeat::new(n, railroad::Empty)))
171168
}
172169
ExpressionKind::RepeatNonGreedy(e) => {
173-
let n = e.render_railroad(stack, link_map, !reverse)?;
174-
let r = Box::new(Repeat::new(railroad::Empty, n));
170+
let n = e.render_railroad(stack, link_map, reverse)?;
171+
let r = Box::new(Optional::new(Repeat::new(n, railroad::Empty)));
175172
let lbox = LabeledBox::new(r, Comment::new("non-greedy".to_string()));
176173
Box::new(lbox)
177174
}

0 commit comments

Comments
 (0)