-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pretty-printing: handle type-operator precedence correctly #4072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
586527d
Seal Texts.Text
Blaisorblade dac4265
Bugfix: missing return
Blaisorblade df2189b
Refactor argText
Blaisorblade 8d8dfff
Fix #4068: Fix pretty-printing precedence for function types
Blaisorblade 3257bfb
Refactor: distinguish type- and term- level priority
Blaisorblade a87418b
Treat & and | as *left*-associative
Blaisorblade 500a9e5
Use precedence levels properly when printing infix types
Blaisorblade 6e8dfb4
Handle associativity mismatches for type operator in pretty-printer
Blaisorblade 5e01496
Use typeSymbol not classSymbol
Blaisorblade 0333dd6
Fix typo
Blaisorblade dfdf9a1
Type-printer tests: simplify per review comment
Blaisorblade de8eb4d
Hardcode SIP-33: term and type operators have the same priority
Blaisorblade f9990c7
Give normative references for operator precedence
Blaisorblade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,24 @@ abstract class Printer { | |
|
||
private[this] var prec: Precedence = GlobalPrec | ||
|
||
/** The current precedence level */ | ||
/** The current precedence level. | ||
* WHen pretty-printing arguments of operator `op`, `currentPrecedence` must equal `op`'s precedence level, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/WHen/When/ |
||
* so that pretty-printing expressions using lower-precedence operators can insert parentheses automatically | ||
* by calling `changePrec`. | ||
*/ | ||
def currentPrecedence = prec | ||
|
||
/** Generate text using `op`, assuming a given precedence level `prec`. */ | ||
/** Generate text using `op`, assuming a given precedence level `prec`. | ||
* | ||
* ### `atPrec` vs `changePrec` | ||
* | ||
* This is to be used when changing precedence inside some sort of parentheses: | ||
* for instance, to print T[A]` use | ||
* `toText(T) ~ '[' ~ atPrec(GlobalPrec) { toText(A) } ~ ']'`. | ||
* | ||
* If the presence of the parentheses depends on precedence, inserting them manually is most certainly a bug. | ||
* Use `changePrec` instead to generate them exactly when needed. | ||
*/ | ||
def atPrec(prec: Precedence)(op: => Text): Text = { | ||
val outerPrec = this.prec | ||
this.prec = prec | ||
|
@@ -30,6 +44,27 @@ abstract class Printer { | |
|
||
/** Generate text using `op`, assuming a given precedence level `prec`. | ||
* If new level `prec` is lower than previous level, put text in parentheses. | ||
* | ||
* ### `atPrec` vs `changePrec` | ||
* | ||
* To pretty-print `A op B`, you need something like | ||
* `changePrec(parsing.precedence(op, isType)) { toText(a) ~ op ~ toText(b) }` // BUGGY | ||
* that will insert parentheses around `A op B` if, for instance, the | ||
* preceding operator has higher precedence. | ||
* | ||
* But that does not handle infix operators with left- or right- associativity. | ||
* | ||
* If op and op' have the same precedence and associativity, | ||
* A op B op' C parses as (A op B) op' C if op and op' are left-associative, and as | ||
* A op (B op' C) if they're right-associative, so we need respectively | ||
* ```scala | ||
* val isType = ??? // is this a term or type operator? | ||
* val prec = parsing.precedence(op, isType) | ||
* // either: | ||
* changePrec(prec) { toText(a) ~ op ~ atPrec(prec + 1) { toText(b) } } // for left-associative op and op' | ||
* // or: | ||
* changePrec(prec) { atPrec(prec + 1) { toText(a) } ~ op ~ toText(b) } // for right-associative op and op' | ||
* ``` | ||
*/ | ||
def changePrec(prec: Precedence)(op: => Text): Text = | ||
if (prec < this.prec) atPrec(prec) ("(" ~ op ~ ")") else atPrec(prec)(op) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" } | ||
val toInt: Any => Int = <func1> | ||
scala> val hoFun: (Int => Int) => Int = new { def apply(a: Int => Int) = 1; override def toString() = "<func2>" } | ||
val hoFun: (Int => Int) => Int = <func2> | ||
scala> val curriedFun: Int => (Int => Int) = new { def apply(a: Int) = _ => 1; override def toString() = "<func3>" } | ||
val curriedFun: Int => Int => Int = <func3> | ||
scala> val tupFun: ((Int, Int)) => Int = new { def apply(a: (Int, Int)) = 1; override def toString() = "<func4>" } | ||
val tupFun: ((Int, Int)) => Int = <func4> | ||
scala> val binFun: (Int, Int) => Int = new { def apply(a: Int, b: Int) = 1; override def toString() = "<func5>" } | ||
val binFun: (Int, Int) => Int = <func5> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rewrite these tests as: - scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" }
- val toInt: Any => Int = <func1>
+ scala> def toInt: Any => Int = ???
+ def toInt: Any => Int There is too much noise with vals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isType
unused?