@@ -951,7 +951,7 @@ object Parsers {
951951 recur(top)
952952 }
953953
954- /** operand { infixop operand | ‘given’ (operand | ParArgumentExprs) } [postfixop],
954+ /** operand { infixop operand | MatchClause } [postfixop],
955955 *
956956 * respecting rules of associativity and precedence.
957957 * @param isOperator the current token counts as an operator.
@@ -981,18 +981,14 @@ object Parsers {
981981 }
982982 else recur(operand())
983983 }
984- else reduceStack(base, top, minPrec, leftAssoc = true , in.name, isType)
984+ else
985+ val t = reduceStack(base, top, minPrec, leftAssoc = true , in.name, isType)
986+ if ! isType && in.token == MATCH then recur(matchClause(t))
987+ else t
985988
986989 recur(first)
987990 }
988991
989- def applyGiven (t : Tree , operand : () => Tree ): Tree =
990- atSpan(startOffset(t), in.offset) {
991- in.nextToken()
992- val args = if (in.token == LPAREN ) parArgumentExprs()._1 else operand() :: Nil
993- Apply (t, args)
994- }.setGivenApply()
995-
996992/* -------- IDENTIFIERS AND LITERALS ------------------------------------------- */
997993
998994 /** Accept identifier and return its name as a term name. */
@@ -1028,9 +1024,11 @@ object Parsers {
10281024 def wildcardIdent (): Ident =
10291025 atSpan(accept(USCORE )) { Ident (nme.WILDCARD ) }
10301026
1031- /** Accept identifier acting as a selector on given tree `t`. */
1027+ /** Accept identifier or match clause acting as a selector on given tree `t` */
10321028 def selector (t : Tree ): Tree =
1033- atSpan(startOffset(t), in.offset) { Select (t, ident()) }
1029+ atSpan(startOffset(t), in.offset) {
1030+ if in.token == MATCH then matchClause(t) else Select (t, ident())
1031+ }
10341032
10351033 /** Selectors ::= id { `.' id }
10361034 *
@@ -1737,11 +1735,10 @@ object Parsers {
17371735 * | HkTypeParamClause ‘=>’ Expr
17381736 * | [SimpleExpr `.'] id `=' Expr
17391737 * | SimpleExpr1 ArgumentExprs `=' Expr
1740- * | Expr2
1741- * | [ ‘inline’] Expr2 `match' (`{' CaseClauses `}' | CaseClause)
1738+ * | PostfixExpr [Ascription]
1739+ * | ‘inline’ InfixExpr MatchClause
17421740 * Bindings ::= `(' [Binding {`,' Binding}] `)'
17431741 * Binding ::= (id | `_') [`:' Type]
1744- * Expr2 ::= PostfixExpr [Ascription]
17451742 * Ascription ::= `:' InfixType
17461743 * | `:' Annotation {Annotation}
17471744 * | `:' `_' `*'
@@ -1780,7 +1777,7 @@ object Parsers {
17801777 }
17811778 }
17821779
1783- def expr1 (location : Location .Value = Location .ElseWhere ): Tree = in.token match {
1780+ def expr1 (location : Location .Value = Location .ElseWhere ): Tree = in.token match
17841781 case IF =>
17851782 in.endMarkerScope(IF ) { ifExpr(in.offset, If ) }
17861783 case WHILE =>
@@ -1886,31 +1883,28 @@ object Parsers {
18861883 case IF =>
18871884 ifExpr(start, InlineIf )
18881885 case _ =>
1889- val t = postfixExpr()
1890- if (in.token == MATCH ) matchExpr(t, start, InlineMatch )
1891- else
1892- syntaxErrorOrIncomplete(i " `match` or `if` expected but ${in.token} found " )
1893- t
1886+ postfixExpr() match
1887+ case t @ Match (scrut, cases) =>
1888+ InlineMatch (scrut, cases).withSpan(t.span)
1889+ case t =>
1890+ syntaxError(em " `inline` must be followed by an `if` or a `match` " , start)
1891+ t
18941892 else expr1Rest(postfixExpr(), location)
1895- }
1893+ end expr1
18961894
1897- def expr1Rest (t : Tree , location : Location .Value ): Tree = in.token match {
1895+ def expr1Rest (t : Tree , location : Location .Value ): Tree = in.token match
18981896 case EQUALS =>
1899- t match {
1900- case Ident (_) | Select (_, _) | Apply (_, _) =>
1901- atSpan(startOffset(t), in.skipToken()) { Assign (t, subExpr()) }
1902- case _ =>
1903- t
1904- }
1897+ t match
1898+ case Ident (_) | Select (_, _) | Apply (_, _) =>
1899+ atSpan(startOffset(t), in.skipToken()) { Assign (t, subExpr()) }
1900+ case _ =>
1901+ t
19051902 case COLON =>
19061903 in.nextToken()
1907- val t1 = ascription(t, location)
1908- if (in.token == MATCH ) expr1Rest(t1, location) else t1
1909- case MATCH =>
1910- matchExpr(t, startOffset(t), Match )
1904+ ascription(t, location)
19111905 case _ =>
19121906 t
1913- }
1907+ end expr1Rest
19141908
19151909 def ascription (t : Tree , location : Location .Value ): Tree = atSpan(startOffset(t)) {
19161910 in.token match {
@@ -1951,20 +1945,20 @@ object Parsers {
19511945 mkIf(cond, thenp, elsep)
19521946 }
19531947
1954- /** `match' ( `{' CaseClauses `}' | CaseClause)
1948+ /** MatchClause ::= `match' `{' CaseClauses `}'
19551949 */
1956- def matchExpr (t : Tree , start : Offset , mkMatch : ( Tree , List [ CaseDef ]) => Match ) =
1950+ def matchClause (t : Tree ) : Match =
19571951 in.endMarkerScope(MATCH ) {
1958- atSpan(start, in.skipToken()) {
1959- mkMatch (t, casesExpr( caseClause))
1952+ atSpan(t.span. start, in.skipToken()) {
1953+ Match (t, inBracesOrIndented(caseClauses( caseClause) ))
19601954 }
19611955 }
19621956
1963- /** `match' ( `{' TypeCaseClauses `}' | TypeCaseClause)
1957+ /** `match' `{' TypeCaseClauses `}'
19641958 */
19651959 def matchType (t : Tree ): MatchTypeTree =
19661960 atSpan(t.span.start, accept(MATCH )) {
1967- MatchTypeTree (EmptyTree , t, casesExpr( typeCaseClause))
1961+ MatchTypeTree (EmptyTree , t, inBracesOrIndented(caseClauses( typeCaseClause) ))
19681962 }
19691963
19701964 /** FunParams ::= Bindings
@@ -2032,7 +2026,7 @@ object Parsers {
20322026 /** PostfixExpr ::= InfixExpr [id [nl]]
20332027 * InfixExpr ::= PrefixExpr
20342028 * | InfixExpr id [nl] InfixExpr
2035- * | InfixExpr ‘given’ (InfixExpr | ParArgumentExprs)
2029+ * | InfixExpr MatchClause
20362030 */
20372031 def postfixExpr (): Tree = postfixExprRest(prefixExpr())
20382032
@@ -2063,6 +2057,7 @@ object Parsers {
20632057 * | Path
20642058 * | `(' [ExprsInParens] `)'
20652059 * | SimpleExpr `.' id
2060+ * | SimpleExpr `.' MatchClause
20662061 * | SimpleExpr (TypeArgs | NamedTypeArgs)
20672062 * | SimpleExpr1 ArgumentExprs
20682063 * Quoted ::= ‘'’ ‘{’ Block ‘}’
@@ -2395,10 +2390,6 @@ object Parsers {
23952390 }
23962391 }
23972392
2398- def casesExpr (clause : () => CaseDef ): List [CaseDef ] =
2399- if in.token == CASE then clause() :: Nil
2400- else inBracesOrIndented(caseClauses(clause))
2401-
24022393 /** CaseClauses ::= CaseClause {CaseClause}
24032394 * TypeCaseClauses ::= TypeCaseClause {TypeCaseClause}
24042395 */
0 commit comments