Skip to content

Commit 97bc8d9

Browse files
committed
Refactor quote parsing
1 parent 646f2f9 commit 97bc8d9

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,20 @@ object Parsers {
17611758
if in.isNewLine then in.nextToken()
17621759
tdef
17631760

1761+
/** Quoted ::= ‘'’ ‘{’ Block ‘}’
1762+
* | ‘'’ ‘[’ Type ‘]’
1763+
* | ‘'’ ‘[’ TypeBlock ‘]’
1764+
*/
1765+
def quote(inPattern: Boolean): Tree =
1766+
atSpan(in.skipToken()) {
1767+
withinStaged(StageKind.Quoted | (if (inPattern) StageKind.QuotedPattern else 0)) {
1768+
val body =
1769+
if (in.token == LBRACKET) inBrackets(typeBlock())
1770+
else inBraces(block(simplify = true))
1771+
Quote(body, Nil)
1772+
}
1773+
}
1774+
17641775
/** ExprSplice ::= ‘$’ spliceId -- if inside quoted block
17651776
* | ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
17661777
* | ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
@@ -1775,7 +1786,7 @@ object Parsers {
17751786
if (in.name.length == 1) {
17761787
in.nextToken()
17771788
val inPattern = (staged & StageKind.QuotedPattern) != 0
1778-
withinStaged(StageKind.Spliced)(if (inPattern) inBraces(pattern()) else stagedBlock())
1789+
withinStaged(StageKind.Spliced)(inBraces(if (inPattern) pattern() else block(simplify = true)))
17791790
}
17801791
else atSpan(in.offset + 1) {
17811792
val id = Ident(in.name.drop(1))
@@ -2496,14 +2507,7 @@ object Parsers {
24962507
canApply = false
24972508
blockExpr()
24982509
case QUOTE =>
2499-
atSpan(in.skipToken()) {
2500-
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
2501-
val body =
2502-
if (in.token == LBRACKET) inBrackets(typeBlock())
2503-
else stagedBlock()
2504-
Quote(body, Nil)
2505-
}
2506-
}
2510+
quote(location.inPattern)
25072511
case NEW =>
25082512
canApply = false
25092513
newExpr()

0 commit comments

Comments
 (0)