GuntherRademacher / rr

RR - Railroad Diagram Generator
Apache License 2.0
483 stars 49 forks source link

Slight variation in production leads to "backwards" display #21

Closed vsajip closed 1 year ago

vsajip commented 1 year ago

The following production

key_value ::= ( key ( ':' | '=' ) value ( ',' | EOL )? EOL* )*

leads to diagram

ss27

However, adding EOL* to the beginning of the production

key_value ::= EOL* ( key ( ':' | '=' ) value ( ',' | EOL )? EOL* )*

leads to

ss28

which, while not wrong, can confuse the reader because of the right-to-left ordering of key and value in the diagram. Is there a workaround for this? I tried changing some of the options but nothing caused left-to-right display of key and value.

GuntherRademacher commented 1 year ago

RR tries to detect repetitions with separators, and it comes up here with an empty repeated part, with everything else as the separator. This depends on the order of left- and right-factoring steps, and there is no way to influence this, but rephrasing the production. In this case you will get the desired result, if you put EOL* first in the original repetition, rather than last:

key_value ::= ( EOL* key ( ':' | '=' ) value ','? )* EOL* 

But what I would probably do is separate the key value pair from the repetion into separate productions:

key_value_pairs ::= EOL* ( key_value_pair EOL* )*
key_value_pair ::= key ( ':' | '=' ) value ','?