Skip to content

Commit

Permalink
Refactor quote parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed May 31, 2023
1 parent 6cb6ae3 commit 6c10549
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1737,9 +1737,6 @@ object Parsers {
})
else t

/** The block in a quote or splice */
def stagedBlock() = inBraces(block(simplify = true))

/** TypeBlock ::= {TypeBlockStat semi} Type
*/
def typeBlock(): Tree =
Expand All @@ -1752,7 +1749,7 @@ object Parsers {
while in.token == TYPE do tdefs += typeBlockStat()
tdefs.toList

/** TypeBlockStat ::= ‘type’ {nl} TypeDcl
/** TypeBlockStat ::= ‘type’ {nl} TypeDcl
*/
def typeBlockStat(): Tree =
val mods = defAnnotsMods(BitSet())
Expand All @@ -1761,6 +1758,19 @@ object Parsers {
if in.isNewLine then in.nextToken()
tdef

/** Quoted ::= ‘'’ ‘{’ Block ‘}’
* | ‘'’ ‘[’ TypeBlock ‘]’
*/
def quote(inPattern: Boolean): Tree =
atSpan(in.skipToken()) {
withinStaged(StageKind.Quoted | (if (inPattern) StageKind.QuotedPattern else 0)) {
val body =
if (in.token == LBRACKET) inBrackets(typeBlock())
else inBraces(block(simplify = true))
Quote(body, Nil)
}
}

/** ExprSplice ::= ‘$’ spliceId -- if inside quoted block
* | ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
* | ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
Expand All @@ -1775,7 +1785,8 @@ object Parsers {
val expr =
if (in.name.length == 1) {
in.nextToken()
withinStaged(StageKind.Spliced)(if (inPattern) inBraces(pattern()) else stagedBlock())
val inPattern = (staged & StageKind.QuotedPattern) != 0
withinStaged(StageKind.Spliced)(inBraces(if inPattern then pattern() else block(simplify = true)))
}
else atSpan(in.offset + 1) {
val id = Ident(in.name.drop(1))
Expand Down Expand Up @@ -2498,14 +2509,7 @@ object Parsers {
canApply = false
blockExpr()
case QUOTE =>
atSpan(in.skipToken()) {
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
val body =
if (in.token == LBRACKET) inBrackets(typeBlock())
else stagedBlock()
Quote(body, Nil)
}
}
quote(location.inPattern)
case NEW =>
canApply = false
newExpr()
Expand Down

0 comments on commit 6c10549

Please sign in to comment.