Skip to content

Commit 3257bfb

Browse files
committed
Refactor: distinguish type- and term- level priority
Encapsulate the precedence rules in `parsing.precedence` while allowing for different precedence between term- and type-level operators, as mandated by the spec. This is just a refactoring, the actual priority changes are separate. * Add an `isType` flag to `parsing.precedence` and add constants for `AndTypePrec` and `OrTypePrec` (distinct from `OrPrec` and `AndPrec`, which is now unused and dropped). * In the parser, consider `isType` to decide priority. * In the pretty-printer, use `AndTypePrec` and `OrTypePrec` where appropriate.
1 parent 8d8dfff commit 3257bfb

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,13 @@ object Parsers {
471471
syntaxError(MixedLeftAndRightAssociativeOps(op1, op2, op2LeftAssoc), offset)
472472

473473
def reduceStack(base: List[OpInfo], top: Tree, prec: Int, leftAssoc: Boolean, op2: Name, isType: Boolean): Tree = {
474-
if (opStack != base && precedence(opStack.head.operator.name) == prec)
474+
if (opStack != base && precedence(opStack.head.operator.name, isType) == prec)
475475
checkAssoc(opStack.head.offset, opStack.head.operator.name, op2, leftAssoc)
476476
def recur(top: Tree): Tree = {
477477
if (opStack == base) top
478478
else {
479479
val opInfo = opStack.head
480-
val opPrec = precedence(opInfo.operator.name)
480+
val opPrec = precedence(opInfo.operator.name, isType)
481481
if (prec < opPrec || leftAssoc && prec == opPrec) {
482482
opStack = opStack.tail
483483
recur {
@@ -514,7 +514,7 @@ object Parsers {
514514
var top = first
515515
while (isIdent && isOperator) {
516516
val op = if (isType) typeIdent() else termIdent()
517-
top = reduceStack(base, top, precedence(op.name), isLeftAssoc(op.name), op.name, isType)
517+
top = reduceStack(base, top, precedence(op.name, isType), isLeftAssoc(op.name), op.name, isType)
518518
opStack = OpInfo(top, op, in.offset) :: opStack
519519
newLineOptWhenFollowing(canStartOperand)
520520
if (maybePostfix && !canStartOperand(in.token)) {

compiler/src/dotty/tools/dotc/parsing/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import core.NameOps._
77

88
package object parsing {
99

10-
def precedence(operator: Name): Int =
10+
def precedence(operator: Name, isType: Boolean = false): Int =
1111
if (operator eq nme.ERROR) -1
1212
else {
1313
val firstCh = operator.firstPart.head

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
161161
}
162162
finally openRecs = openRecs.tail
163163
case AndType(tp1, tp2) =>
164-
changePrec(AndPrec) { toText(tp1) ~ " & " ~ toText(tp2) }
164+
changePrec(AndTypePrec) { toText(tp1) ~ " & " ~ toText(tp2) }
165165
case OrType(tp1, tp2) =>
166-
changePrec(OrPrec) { toText(tp1) ~ " | " ~ toText(tp2) }
166+
changePrec(OrTypePrec) { toText(tp1) ~ " | " ~ toText(tp2) }
167167
case tp: ErrorType =>
168168
s"<error ${tp.msg.msg}>"
169169
case tp: WildcardType =>

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
375375
case SingletonTypeTree(ref) =>
376376
toTextLocal(ref) ~ "." ~ keywordStr("type")
377377
case AndTypeTree(l, r) =>
378-
changePrec(AndPrec) { toText(l) ~ " & " ~ toText(r) }
378+
changePrec(AndTypePrec) { toText(l) ~ " & " ~ toText(r) }
379379
case OrTypeTree(l, r) =>
380-
changePrec(OrPrec) { toText(l) ~ " | " ~ toText(r) }
380+
changePrec(OrTypePrec) { toText(l) ~ " | " ~ toText(r) }
381381
case RefinedTypeTree(tpt, refines) =>
382382
toTextLocal(tpt) ~ " " ~ blockText(refines)
383383
case AppliedTypeTree(tpt, args) =>

compiler/src/dotty/tools/dotc/printing/package.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.dotc
22

3-
import core.StdNames.nme
3+
import core.StdNames.{nme,tpnme}
44
import parsing.{precedence, minPrec, maxPrec, minInfixPrec}
55
import util.Property.Key
66

@@ -9,7 +9,8 @@ package object printing {
99
type Precedence = Int
1010

1111
val DotPrec = parsing.maxPrec
12-
val AndPrec = parsing.precedence(nme.raw.AMP)
12+
val AndTypePrec = parsing.precedence(tpnme.raw.AMP, true)
13+
val OrTypePrec = parsing.precedence(tpnme.raw.BAR, true)
1314
val OrPrec = parsing.precedence(nme.raw.BAR)
1415
val InfixPrec = parsing.minInfixPrec
1516
val GlobalPrec = parsing.minPrec

0 commit comments

Comments
 (0)