Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3618 from kitbellew/numlit
ScalametaParser: parse numbers with unary applied
- Loading branch information
Showing
6 changed files
with
137 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Unary.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package scala.meta.internal.trees | ||
|
||
import scala.meta.tokens.Token | ||
|
||
private[meta] sealed trait Unary { | ||
def op: String | ||
} | ||
|
||
private[meta] object Unary { | ||
|
||
private val numericOpMap = Seq[Numeric](Plus, Minus, Tilde).map(x => x.op -> x).toMap | ||
val opMap = numericOpMap ++ Seq[Unary](Not).map(x => x.op -> x) | ||
|
||
def unapply(token: Token.Ident): Option[(String, Unary)] = { | ||
val op = token.text | ||
opMap.get(op).map(op -> _) | ||
} | ||
|
||
sealed trait Numeric extends Unary { | ||
def apply(value: BigInt): BigInt | ||
// could return None if not applicable (such as `~`) | ||
def apply(value: BigDecimal): Option[BigDecimal] | ||
} | ||
|
||
object Numeric { | ||
def unapply(token: Token.Ident): Option[Numeric] = | ||
numericOpMap.get(token.text) | ||
} | ||
|
||
sealed trait Logical extends Unary { | ||
def apply(value: Boolean): Boolean | ||
} | ||
|
||
case object Noop extends Numeric { | ||
val op = "" | ||
def apply(value: BigInt): BigInt = value | ||
def apply(value: BigDecimal): Option[BigDecimal] = Some(value) | ||
} | ||
|
||
case object Plus extends Numeric { | ||
val op = "+" | ||
def apply(value: BigInt): BigInt = value | ||
def apply(value: BigDecimal): Option[BigDecimal] = Some(value) | ||
} | ||
|
||
case object Minus extends Numeric { | ||
val op = "-" | ||
def apply(value: BigInt): BigInt = -value | ||
def apply(value: BigDecimal): Option[BigDecimal] = Some(-value) | ||
} | ||
|
||
case object Tilde extends Numeric { | ||
val op = "~" | ||
def apply(value: BigInt): BigInt = ~value | ||
def apply(value: BigDecimal): Option[BigDecimal] = None | ||
} | ||
|
||
case object Not extends Logical { | ||
val op = "!" | ||
def apply(value: Boolean): Boolean = !value | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters