From 8b65095eda63c20f9c618725b21233792206bab1 Mon Sep 17 00:00:00 2001 From: schwarze Date: Fri, 7 Jul 2017 19:06:15 +0000 Subject: [PATCH] add parentheses to the output where required for disambiguation --- .../usr.bin/mandoc/eqn/fromto/basic.out_ascii | 2 +- .../usr.bin/mandoc/eqn/fromto/noarg.out_ascii | 2 +- .../mandoc/eqn/fromto/precedence.out_ascii | 2 +- .../usr.bin/mandoc/eqn/over/noarg.out_ascii | 2 +- .../mandoc/eqn/over/precedence.out_ascii | 4 +-- .../mandoc/eqn/subsup/combine.out_ascii | 2 +- .../usr.bin/mandoc/eqn/subsup/noarg.out_ascii | 2 +- .../mandoc/eqn/subsup/precedence.out_ascii | 2 +- .../mandoc/eqn/subsup/sub_group.out_ascii | 2 +- usr.bin/mandoc/eqn_term.c | 34 +++++++++++++++---- 10 files changed, 38 insertions(+), 16 deletions(-) diff --git a/regress/usr.bin/mandoc/eqn/fromto/basic.out_ascii b/regress/usr.bin/mandoc/eqn/fromto/basic.out_ascii index e4530a15f34..b67ec47b391 100644 --- a/regress/usr.bin/mandoc/eqn/fromto/basic.out_ascii +++ b/regress/usr.bin/mandoc/eqn/fromto/basic.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ffrroommttoo--bbaassiicc - vertical stacking DDEESSCCRRIIPPTTIIOONN - initial text E_(_i = 1)^oo 1/_i^2 final text + initial text E_(_i = 1)^oo 1/(_i^2) final text OpenBSD July 4, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/fromto/noarg.out_ascii b/regress/usr.bin/mandoc/eqn/fromto/noarg.out_ascii index c09dcde2910..f5f1e8fa86f 100644 --- a/regress/usr.bin/mandoc/eqn/fromto/noarg.out_ascii +++ b/regress/usr.bin/mandoc/eqn/fromto/noarg.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ffrroommttoo--nnooaarrgg - vertical stacking lacks final argument DDEESSCCRRIIPPTTIIOONN - initial text _x__a^^ final text + initial text _x_(_a^)^ final text OpenBSD July 4, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/fromto/precedence.out_ascii b/regress/usr.bin/mandoc/eqn/fromto/precedence.out_ascii index 1eba1befd65..b7da7180a7b 100644 --- a/regress/usr.bin/mandoc/eqn/fromto/precedence.out_ascii +++ b/regress/usr.bin/mandoc/eqn/fromto/precedence.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ssuubbssuupp--pprreecceeddeennccee - precedence of subscripts and superscripts DDEESSCCRRIIPPTTIIOONN - initial text _X__a_^_c^ ; X_aa^_c ; _X_1^2__a__c^_e^_o__r^_s final text + initial text _X_(_a_)^_c^ ; X_aa^_c ; (_X_1^2)_(_a__c^_e)^(_o__r^_s) final text OpenBSD July 6, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/over/noarg.out_ascii b/regress/usr.bin/mandoc/eqn/over/noarg.out_ascii index 22ceb49e94c..c30afa55f92 100644 --- a/regress/usr.bin/mandoc/eqn/over/noarg.out_ascii +++ b/regress/usr.bin/mandoc/eqn/over/noarg.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE oovveerr--nnooaarrgg - fraction operator without arguments DDEESSCCRRIIPPTTIIOONN - initial text // final text + initial text (/)/ final text OpenBSD July 4, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/over/precedence.out_ascii b/regress/usr.bin/mandoc/eqn/over/precedence.out_ascii index a77aa47fb60..1059b7425a5 100644 --- a/regress/usr.bin/mandoc/eqn/over/precedence.out_ascii +++ b/regress/usr.bin/mandoc/eqn/over/precedence.out_ascii @@ -4,7 +4,7 @@ NNAAMMEE oovveerr--pprreecceeddeennccee - precedence of the fraction operator DDEESSCCRRIIPPTTIIOONN - initial text 1 + _x + _x^2/2 + _x^3/(2 * 3) ; _a^/_c~ ; aa/cc ; sqrt(_a)/sqrt(_c) - final text + initial text 1 + _x + (_x^2)/2 + (_x^3)/(2 * 3) ; _a^/_c~ ; aa/cc ; + sqrt(_a)/sqrt(_c) final text OpenBSD July 6, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/subsup/combine.out_ascii b/regress/usr.bin/mandoc/eqn/subsup/combine.out_ascii index 8812f6f6681..468a814c52b 100644 --- a/regress/usr.bin/mandoc/eqn/subsup/combine.out_ascii +++ b/regress/usr.bin/mandoc/eqn/subsup/combine.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ssuubbssuupp--ccoommbbiinnee - combination of subscripts and superscripts DDEESSCCRRIIPPTTIIOONN - initial text _x_1^2 + _e^_x_2 final text + initial text _x_1^2 + _e^(_x_2) final text OpenBSD July 4, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/subsup/noarg.out_ascii b/regress/usr.bin/mandoc/eqn/subsup/noarg.out_ascii index d5b87b244e4..eec09f18199 100644 --- a/regress/usr.bin/mandoc/eqn/subsup/noarg.out_ascii +++ b/regress/usr.bin/mandoc/eqn/subsup/noarg.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ssuubbssuupp--nnooaarrgg - empty subscripts and superscripts DDEESSCCRRIIPPTTIIOONN - initial text _x_1^^ final text + initial text _x_(1^)^ final text OpenBSD July 4, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/subsup/precedence.out_ascii b/regress/usr.bin/mandoc/eqn/subsup/precedence.out_ascii index 60619b547e9..21207dd6586 100644 --- a/regress/usr.bin/mandoc/eqn/subsup/precedence.out_ascii +++ b/regress/usr.bin/mandoc/eqn/subsup/precedence.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ssuubbssuupp--pprreecceeddeennccee - precedence of subscripts and superscripts DDEESSCCRRIIPPTTIIOONN - initial text _x^_1_^2 + _e~^_x^__s<-> ; I_II^_I + I^II__I final text + initial text (_x^)_(1_)^2 + (_e~)^((_x^)__s<->) ; I_II^_I + I^(II__I) final text OpenBSD July 6, 2017 OpenBSD diff --git a/regress/usr.bin/mandoc/eqn/subsup/sub_group.out_ascii b/regress/usr.bin/mandoc/eqn/subsup/sub_group.out_ascii index 85e3568a586..5628e6d966a 100644 --- a/regress/usr.bin/mandoc/eqn/subsup/sub_group.out_ascii +++ b/regress/usr.bin/mandoc/eqn/subsup/sub_group.out_ascii @@ -4,6 +4,6 @@ NNAAMMEE ssuubbssuupp--ssuubb__ggrroouupp - grouping of subscripts DDEESSCCRRIIPPTTIIOONN - initial text _x__i + _x__j_1 + (_M__i)__j final text + initial text _x__i + _x_(_j_1) + (_M__i)__j final text OpenBSD July 4, 2017 OpenBSD diff --git a/usr.bin/mandoc/eqn_term.c b/usr.bin/mandoc/eqn_term.c index 503919fdbf8..4a48e793318 100644 --- a/usr.bin/mandoc/eqn_term.c +++ b/usr.bin/mandoc/eqn_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: eqn_term.c,v 1.7 2017/07/06 00:08:52 schwarze Exp $ */ +/* $OpenBSD: eqn_term.c,v 1.8 2017/07/07 19:06:15 schwarze Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015, 2017 Ingo Schwarze @@ -49,21 +49,40 @@ static void eqn_box(struct termp *p, const struct eqn_box *bp) { const struct eqn_box *child; + int delim; + + /* Delimiters around this box? */ if ((bp->type == EQN_LIST && bp->expectargs > 1) || (bp->type == EQN_PILE && (bp->prev || bp->next)) || - (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) { + (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT || + /* Diacritic followed by ^ or _. */ + ((bp->top != NULL || bp->bottom != NULL) && + bp->parent->type == EQN_SUBEXPR && + bp->parent->pos != EQNPOS_OVER && bp->next != NULL) || + /* Nested over, sub, sup, from, to. */ + (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT && + ((bp->parent->type == EQN_LIST && bp->expectargs == 1) || + (bp->parent->type == EQN_SUBEXPR && + bp->pos != EQNPOS_SQRT)))))) { if (bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) p->flags |= TERMP_NOSPACE; term_word(p, bp->left != NULL ? bp->left : "("); p->flags |= TERMP_NOSPACE; - } + delim = 1; + } else + delim = 0; + + /* Handle Fonts and text. */ + if (bp->font != EQNFONT_NONE) term_fontpush(p, fontmap[(int)bp->font]); if (bp->text != NULL) term_word(p, bp->text); + /* Special box types. */ + if (bp->pos == EQNPOS_SQRT) { term_word(p, "sqrt"); if (bp->first != NULL) { @@ -109,6 +128,8 @@ eqn_box(struct termp *p, const struct eqn_box *bp) } } + /* Handle Fonts and diacritics. */ + if (bp->font != EQNFONT_NONE) term_fontpop(p); if (bp->top != NULL) { @@ -119,9 +140,10 @@ eqn_box(struct termp *p, const struct eqn_box *bp) p->flags |= TERMP_NOSPACE; term_word(p, "_"); } - if ((bp->type == EQN_LIST && bp->expectargs > 1) || - (bp->type == EQN_PILE && (bp->prev || bp->next)) || - (bp->parent != NULL && bp->parent->pos == EQNPOS_SQRT)) { + + /* Right delimiter after this box? */ + + if (delim) { p->flags |= TERMP_NOSPACE; term_word(p, bp->right != NULL ? bp->right : ")"); if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL) -- 2.20.1