Skip to content

Commit f3157b9

Browse files
committed
Refactor quote parsing
1 parent f767c6e commit f3157b9

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,9 +1737,6 @@ object Parsers {
17371737
})
17381738
else t
17391739

1740-
/** The block in a quote or splice */
1741-
def stagedBlock() = inBraces(block(simplify = true))
1742-
17431740
/** TypeBlock ::= {TypeBlockStat semi} Type
17441741
*/
17451742
def typeBlock(): Tree =
@@ -1752,7 +1749,7 @@ object Parsers {
17521749
while in.token == TYPE do tdefs += typeBlockStat()
17531750
tdefs.toList
17541751

1755-
/** TypeBlockStat ::= ‘type’ {nl} TypeDcl
1752+
/** TypeBlockStat ::= ‘type’ {nl} TypeDcl
17561753
*/
17571754
def typeBlockStat(): Tree =
17581755
val mods = defAnnotsMods(BitSet())
@@ -1761,6 +1758,19 @@ object Parsers {
17611758
if in.isNewLine then in.nextToken()
17621759
tdef
17631760

1761+
/** Quoted ::= ‘'’ ‘{’ Block ‘}’
1762+
* | ‘'’ ‘[’ TypeBlock ‘]’
1763+
*/
1764+
def quote(inPattern: Boolean): Tree =
1765+
atSpan(in.skipToken()) {
1766+
withinStaged(StageKind.Quoted | (if (inPattern) StageKind.QuotedPattern else 0)) {
1767+
val body =
1768+
if (in.token == LBRACKET) inBrackets(typeBlock())
1769+
else inBraces(block(simplify = true))
1770+
Quote(body, Nil)
1771+
}
1772+
}
1773+
17641774
/** ExprSplice ::= ‘$’ spliceId -- if inside quoted block
17651775
* | ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
17661776
* | ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
@@ -1775,7 +1785,8 @@ object Parsers {
17751785
val expr =
17761786
if (in.name.length == 1) {
17771787
in.nextToken()
1778-
withinStaged(StageKind.Spliced)(if (inPattern) inBraces(pattern()) else stagedBlock())
1788+
val inPattern = (staged & StageKind.QuotedPattern) != 0
1789+
withinStaged(StageKind.Spliced)(inBraces(if inPattern then pattern() else block(simplify = true)))
17791790
}
17801791
else atSpan(in.offset + 1) {
17811792
val id = Ident(in.name.drop(1))
@@ -2498,14 +2509,7 @@ object Parsers {
24982509
canApply = false
24992510
blockExpr()
25002511
case QUOTE =>
2501-
atSpan(in.skipToken()) {
2502-
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
2503-
val body =
2504-
if (in.token == LBRACKET) inBrackets(typeBlock())
2505-
else stagedBlock()
2506-
Quote(body, Nil)
2507-
}
2508-
}
2512+
quote(location.inPattern)
25092513
case NEW =>
25102514
canApply = false
25112515
newExpr()

0 commit comments

Comments
 (0)