Skip to content

Commit 6e87513

Browse files
authored
Merge pull request #616 from viperproject/meilers_pretty_printer_linebreaks
Breaking up long lines when pretty-printing
2 parents 5f9404e + 360cc44 commit 6e87513

2 files changed

Lines changed: 58 additions & 50 deletions

File tree

src/main/scala/viper/silver/ast/Expression.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ sealed trait Exp extends Hashable with Typed with Positioned with Infoed with Tr
4747
*/
4848
// lazy val proofObligations = Expressions.proofObligations(this)
4949

50+
override def toString() = {
51+
// Carbon relies on expression pretty-printing resulting in a string without line breaks,
52+
// so for the special case of directly converting an expression to a string,, we remove all line breaks
53+
// the pretty printer might have inserted.
54+
super.toString.replaceAll("\n", " ")
55+
}
56+
5057
}
5158

5259
// --- Simple integer and boolean expressions (binary and unary operations, literals)

src/main/scala/viper/silver/ast/pretty/PrettyPrinter.scala

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
484484
case v: LocalVarDecl => showVar(v)
485485
case dm: DomainMember => showDomainMember(dm)
486486
case Trigger(exps) =>
487-
text("{") <+> ssep(exps map show, char (',')) <+> "}"
487+
text("{") <+> ssep(exps map show, group(char (',') <> line)) <+> "}"
488488
case null => uninitialized
489489
}
490490

@@ -526,10 +526,10 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
526526
case Field(name, typ) =>
527527
text("field") <+> name <> ":" <+> show(typ)
528528
case Method(name, formalArgs, formalReturns, pres, posts, body) =>
529-
text("method") <+> name <> parens(showVars(formalArgs)) <> {
529+
group(text("method") <+> name <> nest(defaultIndent, parens(showVars(formalArgs))) <> {
530530
if (formalReturns.isEmpty) nil
531-
else nil <+> "returns" <+> parens(showVars(formalReturns))
532-
} <>
531+
else nest(defaultIndent, line <> "returns" <+> parens(showVars(formalReturns)))
532+
}) <>
533533
nest(defaultIndent,
534534
showContracts("requires", pres) <>
535535
showContracts("ensures", posts)
@@ -545,12 +545,12 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
545545
) <> line)
546546
})
547547
case Predicate(name, formalArgs, body) =>
548-
text("predicate") <+> name <> parens(showVars(formalArgs)) <+> (body match {
548+
text("predicate") <+> name <> nest(defaultIndent, parens(showVars(formalArgs))) <+> (body match {
549549
case None => nil
550550
case Some(exp) => braces(nest(defaultIndent, line <> show(exp)) <> line)
551551
})
552552
case Function(name, formalArgs, typ, pres, posts, optBody) =>
553-
text("function") <+> name <> parens(showVars(formalArgs)) <>
553+
text("function") <+> name <> nest(defaultIndent, parens(showVars(formalArgs))) <>
554554
":" <+> show(typ) <>
555555
nest(defaultIndent,
556556
showContracts("requires", pres) <>
@@ -574,7 +574,7 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
574574
if (contracts == null)
575575
line <> name <+> uninitialized
576576
else
577-
lineIfSomeNonEmpty(contracts) <> ssep(contracts map (text(name) <+> show(_)), line)
577+
lineIfSomeNonEmpty(contracts) <> ssep(contracts.map(c => text(name) <+> nest(defaultIndent, show(c))), line)
578578
}
579579

580580
/** Returns `n` lines if at least one element of `s` is non-empty, and an empty document otherwise. */
@@ -588,7 +588,7 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
588588
}
589589

590590
/** Show a list of formal arguments. */
591-
def showVars(vars: Seq[AnyLocalVarDecl]): Cont = ssep(vars map showVar, char (',') <> space)
591+
def showVars(vars: Seq[AnyLocalVarDecl]): Cont = ssep(vars map showVar, group(char (',') <> line))
592592
/** Show a variable name with the type of the variable (e.g. to be used in formal argument lists). */
593593
def showVar(v: AnyLocalVarDecl): Cont = v match {
594594
case l: LocalVarDecl => text(l.name) <> ":" <+> showType(l.typ)
@@ -644,18 +644,18 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
644644
val stmtDoc = stmt match {
645645
case NewStmt(target, fields) =>
646646
show(target) <+> ":=" <+> "new(" <> ssep(fields map (f => value(f.name)), char(',') <> space) <> ")"
647-
case LocalVarAssign(lhs, rhs) => show(lhs) <+> ":=" <+> show(rhs)
648-
case FieldAssign(lhs, rhs) => show(lhs) <+> ":=" <+> show(rhs)
649-
case Fold(e) => text("fold") <+> show(e)
650-
case Unfold(e) => text("unfold") <+> show(e)
647+
case LocalVarAssign(lhs, rhs) => show(lhs) <+> ":=" <+> nest(defaultIndent, show(rhs))
648+
case FieldAssign(lhs, rhs) => show(lhs) <+> ":=" <+> nest(defaultIndent, show(rhs))
649+
case Fold(e) => text("fold") <+> nest(defaultIndent, show(e))
650+
case Unfold(e) => text("unfold") <+> nest(defaultIndent, show(e))
651651
case Package(e, proofScript) => text("package") <+> show(e) <+> showBlock(proofScript)
652-
case Apply(e) => text("apply") <+> show(e)
653-
case Inhale(e) => text("inhale") <+> show(e)
654-
case Assume(e) => text("assume") <+> show(e)
655-
case Exhale(e) => text("exhale") <+> show(e)
656-
case Assert(e) => text("assert") <+> show(e)
652+
case Apply(e) => text("apply") <+> nest(defaultIndent, show(e))
653+
case Inhale(e) => text("inhale") <+> nest(defaultIndent, show(e))
654+
case Assume(e) => text("assume") <+> nest(defaultIndent, show(e))
655+
case Exhale(e) => text("exhale") <+> nest(defaultIndent, show(e))
656+
case Assert(e) => text("assert") <+> nest(defaultIndent, show(e))
657657
case MethodCall(mname, args, targets) =>
658-
val call = text(mname) <> parens(ssep(args map show, char(',') <> space))
658+
val call = text(mname) <> nest(defaultIndent, parens(ssep(args map show, group(char(',') <> line))))
659659
targets match {
660660
case Nil => call
661661
case _ => ssep(targets map show, char(',') <> space) <+> ":=" <+> call
@@ -725,34 +725,35 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
725725
case FieldAccess(rcv, field) =>
726726
show(rcv) <> "." <> field.name
727727
case PredicateAccess(params, predicateName) =>
728-
text(predicateName) <> parens(ssep(params map show, char (',') <> space))
728+
text(predicateName) <> parens(ssep(params map show, group(char (',') <> line)))
729729
case Unfolding(acc, exp) =>
730-
parens(text("unfolding") <+> show(acc) <+> "in" <+> show(exp))
730+
group(parens(text("unfolding") <+> nest(defaultIndent, show(acc)) <+> "in" <> nest(defaultIndent, line <> show(exp))))
731731
case Applying(wand, exp) =>
732-
parens(text("applying") <+> show(wand) <+> "in" <+> show(exp))
732+
parens(text("applying") <+> nest(defaultIndent, show(wand)) <+> "in" <> nest(defaultIndent, line <> show(exp)))
733733
case Old(exp) =>
734734
text("old") <> parens(show(exp))
735735
case LabelledOld(exp,label) =>
736736
text("old") <> brackets(label) <> parens(show(exp))
737737
case Let(v, exp, body) =>
738-
parens(text("let") <+> text(v.name) <+> "==" <+> parens(show(exp)) <+> "in" <+> show(body))
738+
parens(text("let") <+> text(v.name) <+> "==" <> nest(defaultIndent, line <> parens(show(exp))) <+>
739+
"in" <> nest(defaultIndent, line <> show(body)))
739740
case CondExp(cond, thn, els) =>
740-
parens(show(cond) <+> "?" <+> show(thn) <+> ":" <+> show(els))
741+
group(parens(show(cond) <+> "?" <> nest(defaultIndent, line <> show(thn) <+> ":" <@> show(els))))
741742
case Exists(v, triggers, exp) =>
742743
parens(text("exists") <+> showVars(v) <+> "::" <>
743-
(if (triggers.isEmpty) nil else space <> ssep(triggers map show, space)) <+>
744-
show(exp))
744+
nest(defaultIndent, (if (triggers.isEmpty) nil else space <> ssep(triggers map show, space)) <+>
745+
show(exp)))
745746
case Forall(v, triggers, exp) =>
746-
parens(text("forall") <+> showVars(v) <+> "::" <>
747-
(if (triggers.isEmpty) nil else space <> ssep(triggers map show, space)) <+>
748-
show(exp))
747+
group(parens(text("forall") <+> showVars(v) <+> "::" <>
748+
nest(defaultIndent, (if (triggers.isEmpty) nil else line <> ssep(triggers map show, line)) <@>
749+
show(exp))))
749750
case ForPerm(vars, resource, exp) =>
750-
parens(text("forperm")
751-
<+> showVars(vars)
752-
<+> brackets(show(resource)) <+> "::" <+> show(exp))
751+
group(parens(text("forperm")
752+
<> nest(defaultIndent, line <> showVars(vars)
753+
<+> brackets(show(resource)) <+> "::" <+> show(exp))))
753754

754755
case InhaleExhaleExp(in, ex) =>
755-
brackets(show(in) <> char (',') <+> show(ex))
756+
group(brackets(show(in) <> char (',') <@> show(ex)))
756757
case WildcardPerm() =>
757758
"wildcard"
758759
case FullPerm() =>
@@ -771,19 +772,19 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
771772
case AccessPredicate(loc, perm) =>
772773
text("acc") <> parens(show(loc) <> "," <+> show(perm))
773774
case FuncApp(funcname, args) =>
774-
text(funcname) <> parens(ssep(args map show, char (',') <> space))
775+
text(funcname) <> parens(ssep(args map show, group(char (',') <> line)))
775776
case dfa@DomainFuncApp(funcname, args, tvMap) =>
776777
if (tvMap.nonEmpty)
777778
// Type may be underconstrained, so to be safe we explicitly print out the type.
778-
parens(text(funcname) <> parens(ssep(args map show, char (',') <> space)) <> char(':') <+> show(dfa.typ))
779+
parens(text(funcname) <> parens(ssep(args map show, group(char (',') <> line))) <> char(':') <+> show(dfa.typ))
779780
else
780-
text(funcname) <> parens(ssep(args map show, char (',') <> space))
781+
text(funcname) <> parens(ssep(args map show, group(char (',') <> line)))
781782
case BackendFuncApp(func, args) =>
782-
text(func.name) <> parens(ssep(args map show, char(',') <> space))
783+
text(func.name) <> parens(ssep(args map show, group(char(',') <> line)))
783784
case EmptySeq(elemTyp) =>
784785
text("Seq[") <> showType(elemTyp) <> "]()"
785786
case ExplicitSeq(elems) =>
786-
text("Seq") <> parens(ssep(elems map show, char (',') <> space))
787+
text("Seq") <> parens(ssep(elems map show, group(char (',') <> line)))
787788
case RangeSeq(low, high) =>
788789
text("[") <> show(low) <> ".." <> show(high) <> ")"
789790
case si@SeqIndex(seq: PrettyOperatorExpression, idx) =>
@@ -801,47 +802,47 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
801802
case SeqDrop(seq, n) =>
802803
show(seq) <> brackets(show(n) <> "..")
803804
case SeqUpdate(seq, idx, elem) =>
804-
show(seq) <> brackets(show(idx) <+> ":=" <+> show(elem))
805+
show(seq) <> group(brackets(show(idx) <+> ":=" <@> show(elem)))
805806
case SeqLength(seq) =>
806807
surround(show(seq),char ('|'))
807808
case SeqContains(elem, seq) =>
808-
parens(show(elem) <+> "in" <+> show(seq))
809+
group(parens(show(elem) <+> "in" <@> show(seq)))
809810

810811
case EmptySet(elemTyp) =>
811812
text("Set[") <> showType(elemTyp) <> "]()"
812813
case ExplicitSet(elems) =>
813-
text("Set") <> parens(ssep(elems map show, char (',') <> space))
814+
text("Set") <> parens(ssep(elems map show, group(char (',') <> line)))
814815
case EmptyMultiset(elemTyp) =>
815816
text("Multiset[") <> showType(elemTyp) <> "]()"
816817
case ExplicitMultiset(elems) =>
817-
text("Multiset") <> parens(ssep(elems map show, char (',') <> space))
818+
text("Multiset") <> parens(ssep(elems map show, group(char (',') <> line)))
818819
case AnySetUnion(left, right) =>
819-
parens(show(left) <+> "union" <+> show(right))
820+
group(parens(show(left) <+> "union" <@> show(right)))
820821
case AnySetIntersection(left, right) =>
821-
parens(show(left) <+> "intersection" <+> show(right))
822+
group(parens(show(left) <+> "intersection" <@> show(right)))
822823
case AnySetSubset(left, right) =>
823-
parens(show(left) <+> "subset" <+> show(right))
824+
group(parens(show(left) <+> "subset" <@> show(right)))
824825
case AnySetMinus(left, right) =>
825-
parens(show(left) <+> "setminus" <+> show(right))
826+
group(parens(show(left) <+> "setminus" <@> show(right)))
826827
case AnySetContains(elem, s) =>
827-
parens(show(elem) <+> "in" <+> show(s))
828+
group(parens(show(elem) <+> "in" <@> show(s)))
828829
case AnySetCardinality(s) =>
829830
surround(show(s),char ('|'))
830831

831832
case EmptyMap(keyType, valueType) =>
832833
text("Map") <> brackets(showType(keyType) <> "," <> showType(valueType)) <> "()"
833834
case ExplicitMap(elems) =>
834-
text("Map") <> parens(ssep(elems map show, char(',') <> space))
835+
text("Map") <> parens(ssep(elems map show, group(char(',') <> line)))
835836
case Maplet(key, value) =>
836837
show(key) <+> ":=" <+> show(value)
837838
case MapLookup(base, key) =>
838839
show(base) <> brackets(show(key))
839840
case MapContains(key, base) =>
840-
parens(show(key) <+> "in" <+> show(base))
841+
group(parens(show(key) <+> "in" <@> show(base)))
841842
case MapCardinality(base) =>
842843
surround(show(base), char('|'))
843844
case MapUpdate(base, key, value) =>
844-
show(base) <> brackets(show(key) <+> ":=" <+> show(value))
845+
show(base) <> group(brackets(show(key) <+> ":=" <@> show(value)))
845846
case MapDomain(base) =>
846847
text("domain") <> parens(show(base))
847848
case MapRange(base) =>
@@ -886,6 +887,6 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
886887
toParenDoc(r)
887888
}
888889

889-
ld <+> text(b.op) <+> rd
890+
group(ld <+> text(b.op) <@> rd)
890891
}
891892
}

0 commit comments

Comments
 (0)