Skip to content

Commit b3c2aff

Browse files
author
_
committed
cleaner line break selection
1 parent 7acc93f commit b3c2aff

File tree

3 files changed

+74
-35
lines changed

3 files changed

+74
-35
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ MANIFEST
55
build
66
dist
77
pfa/pfa
8+
pfai
9+
pfa/pfai
810
*.o
11+
*.files
12+
*.includes
13+
*.creator
14+
*.config
15+
*.user
16+
*.directory
17+
TODO

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
all: pfa/pfai pfa/pfa
3+
4+
pfa/pfa: pfa/pfa.c
5+
gcc -Wall -fno-omit-frame-pointer -Os pfa/pfa.c -o pfa/pfa
6+
7+
8+
pfa/pfai: pfa/pfa
9+
cp pfa/pfa pfa/pfai
10+
11+
clean:
12+
rm -f pfa/pfai pfa/pfa

pfa/pfa.c

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ enum {
4545
LINE_IS_NORMAL,
4646
};
4747

48+
enum { SSCORE_COMMENT = 10000, SSCORE_NESTING = -100 };
49+
4850
struct vlbuf {
4951
union {
5052
void *vd;
@@ -396,7 +398,11 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
396398
case TOK_SPECIAL:
397399
case TOK_LABEL: {
398400
if (isalpha_lead(nxt) || ('0' <= nxt && nxt <= '9')) {
399-
401+
} else if (nxt == '\'' || nxt == '\"') {
402+
/* String with prefix */
403+
proctok = TOK_STRING;
404+
nstrleads = 1;
405+
string_starter = nxt;
400406
} else {
401407
tokfin = 1;
402408
proctok = TOK_INBETWEEN;
@@ -642,11 +648,17 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
642648
/* Line wrapping & printing, oh joy */
643649
char *tokpos = tokbuf.d.ch;
644650
int nests = 0;
651+
int pptok = TOK_INBETWEEN;
652+
int pretok = TOK_INBETWEEN;
653+
int postok = toks.d.in[0];
645654
for (int i = 0; i < ntoks; i++) {
646-
int pptok = i > 0 ? toks.d.in[i - 1] : TOK_INBETWEEN;
647-
int pretok = toks.d.in[i];
648-
int postok = toks.d.in[i + 1];
655+
pptok = pretok;
656+
pretok = postok;
657+
postok = toks.d.in[i + 1];
649658
int toklen = strlen(tokpos);
659+
if (pretok == TOK_OBRACE) {
660+
nests++;
661+
}
650662

651663
if (pretok == TOK_LCONT) {
652664
/* ignore line breaks */
@@ -667,13 +679,24 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
667679
}
668680
buildpt += strapp(buildpt, sos);
669681
splitpoints.d.in[nsplits] = buildpt - laccum.d.ch;
670-
split_ratings.d.in[nsplits] = -1;
682+
split_ratings.d.in[nsplits] = SSCORE_COMMENT;
671683
split_nestings.d.in[nsplits] = nests;
672684
nsplits++;
673685
} else {
674686
buildpt += strapp(buildpt, tokpos);
675687
splitpoints.d.in[nsplits] = buildpt - laccum.d.ch;
676-
split_ratings.d.in[nsplits] = 0;
688+
if (pretok == TOK_COMMA && postok != TOK_CBRACE && nests > 0) {
689+
split_ratings.d.in[nsplits] = 1;
690+
} else if (pretok == TOK_COLON && postok != TOK_CBRACE) {
691+
split_ratings.d.in[nsplits] = 1;
692+
} else if (pretok == TOK_LABEL && postok == TOK_OBRACE) {
693+
split_ratings.d.in[nsplits] = SSCORE_NESTING;
694+
} else if (pretok == TOK_DOT || postok == TOK_DOT) {
695+
split_ratings.d.in[nsplits] = -2;
696+
} else {
697+
split_ratings.d.in[nsplits] = 0;
698+
}
699+
677700
split_nestings.d.in[nsplits] = nests;
678701
nsplits++;
679702
}
@@ -689,7 +712,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
689712
} else if (pretok == TOK_LCONT) {
690713
space = 0;
691714
} else if (pretok == TOK_EQUAL || postok == TOK_EQUAL) {
692-
space = nests == 0;
715+
space = (nests == 0);
693716
} else if (pretok == TOK_SPECIAL) {
694717
if (postok == TOK_COLON) {
695718
space = 0;
@@ -706,8 +729,6 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
706729
space = 0;
707730
} else if (pretok == TOK_OPERATOR && postok == TOK_UNARYOP) {
708731
space = 1;
709-
} else if (pretok == TOK_LABEL && postok == TOK_STRING) {
710-
space = 0;
711732
} else if (pretok == TOK_LABEL && postok == TOK_UNARYOP) {
712733
space = 1;
713734
} else if (pretok == TOK_CBRACE && postok == TOK_UNARYOP) {
@@ -749,9 +770,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
749770
buildpt += strapp(buildpt, " ");
750771
}
751772

752-
if (pretok == TOK_OBRACE) {
753-
nests++;
754-
} else if (postok == TOK_CBRACE) {
773+
if (postok == TOK_CBRACE) {
755774
nests--;
756775
}
757776
}
@@ -768,44 +787,43 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
768787
memcpy(lineout.d.ch, &laccum.d.ch[fr], to - fr);
769788
lineout.d.ch[to - fr] = '\0';
770789
int nlen = to - fr;
771-
int force_split = i > 0 ? split_ratings.d.in[i - 1] < 0 : 0;
790+
int comment_split =
791+
i > 0 ? split_ratings.d.in[i - 1] == SSCORE_COMMENT : 0;
772792

773793
/* The previous location provides the break-off score */
774-
int bscore = 0, bk = -1;
794+
int best_score = -1000000, bk = -1;
775795
for (int rleft = length_left, k = i; k < nsplits && rleft >= 0; k++) {
776-
/* Estimate segment length */
796+
/* Estimate segment length, walk further */
777797
int fr = k > 0 ? splitpoints.d.in[k - 1] : 0;
778-
int ofr = k > 1 ? splitpoints.d.in[k - 2] : 0;
798+
// int ofr = k > 1 ? splitpoints.d.in[k - 2] : 0;
779799
int to = k >= nsplits - 1 ? eoff : splitpoints.d.in[k];
780-
int len = to - fr;
781-
int isc = 100;
782-
/* Just by previous. Q: split tok? or 'saved length' field, w/
783-
* comments counting as -inf ls. Issue: what if there's less than N
784-
* fields left ?*/
785-
if (laccum.d.ch[ofr] == ',') {
786-
isc = rleft - len - 15;
787-
} else if (laccum.d.ch[ofr] == ':') {
788-
isc = rleft - len - 15;
789-
} else {
790-
isc = rleft - len;
791-
}
792-
rleft -= len;
793-
if (isc < bscore) {
794-
bscore = isc;
800+
int seglen = to - fr;
801+
rleft -= seglen;
802+
803+
/* We split at the zone with the highest score */
804+
int reduced_nestings = split_nestings.d.in[k - 1];
805+
if (reduced_nestings > 0)
806+
reduced_nestings--;
807+
int split_score = k > 0 ? (split_ratings.d.in[k - 1] +
808+
SSCORE_NESTING * reduced_nestings)
809+
: 0;
810+
if (split_score >= best_score) {
811+
best_score = split_score;
795812
bk = k;
796813
}
814+
797815
/* Never hold up a terminator */
798816
if (rleft >= 0 && k == nsplits - 1) {
799817
bk = -1;
800818
}
801819
}
802-
int want_split = bk == i;
803-
int length_split = nlen >= length_left;
820+
int want_split = (bk == i);
821+
int length_split = (nlen >= length_left);
804822

805823
int continuing = 1;
806824
if (i == 0) {
807825
continuing = 1;
808-
} else if (force_split || length_split || want_split) {
826+
} else if (comment_split || length_split || want_split) {
809827
continuing = 0;
810828
} else {
811829
continuing = 1;
@@ -821,7 +839,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
821839
prn = &lineout.d.ch[1];
822840
nlen -= 1;
823841
}
824-
if (force_split || split_nestings.d.in[i - 1] > 0) {
842+
if (comment_split || split_nestings.d.in[i - 1] > 0) {
825843
formfilelen = vlbuf_append(formfile, "\n ", formfilelen, out);
826844
} else {
827845
formfilelen =

0 commit comments

Comments
 (0)