summaryrefslogtreecommitdiff
path: root/scalaParser/src/main/scala/scalaParser/syntax/Literals.scala
diff options
context:
space:
mode:
authorlihaoyi <haoyi.sg@gmail.com>2014-11-24 02:48:13 -0800
committerlihaoyi <haoyi.sg@gmail.com>2014-11-24 02:48:13 -0800
commit6829fb683e1e0ab3a14272a756af63a1a40ebfd7 (patch)
treeb716835039cf23356d083fb4a97ca4475afe1a90 /scalaParser/src/main/scala/scalaParser/syntax/Literals.scala
parentc6e266f8d0f8d8ce948ddf6b8539e28606e9b009 (diff)
downloadhands-on-scala-js-6829fb683e1e0ab3a14272a756af63a1a40ebfd7.tar.gz
hands-on-scala-js-6829fb683e1e0ab3a14272a756af63a1a40ebfd7.tar.bz2
hands-on-scala-js-6829fb683e1e0ab3a14272a756af63a1a40ebfd7.zip
finish move
Diffstat (limited to 'scalaParser/src/main/scala/scalaParser/syntax/Literals.scala')
-rw-r--r--scalaParser/src/main/scala/scalaParser/syntax/Literals.scala57
1 files changed, 57 insertions, 0 deletions
diff --git a/scalaParser/src/main/scala/scalaParser/syntax/Literals.scala b/scalaParser/src/main/scala/scalaParser/syntax/Literals.scala
new file mode 100644
index 0000000..b8342e2
--- /dev/null
+++ b/scalaParser/src/main/scala/scalaParser/syntax/Literals.scala
@@ -0,0 +1,57 @@
+package scalaParser
+package syntax
+import acyclic.file
+import org.parboiled2._
+
+trait Literals { self: Parser with Basic with Identifiers =>
+ object Literals{
+ import Basic._
+ def FloatingPointLiteral = rule {
+ capture(
+ "." ~ oneOrMore(Digit) ~ optional(ExponentPart) ~ optional(FloatType) |
+ oneOrMore(Digit) ~ (
+ "." ~ oneOrMore(Digit) ~ optional(ExponentPart) ~ optional(FloatType) |
+ ExponentPart ~ optional(FloatType) |
+ optional(ExponentPart) ~ FloatType))
+ }
+
+ def IntegerLiteral = rule { capture((DecimalNumeral | HexNumeral) ~ optional(anyOf("Ll"))) }
+
+ def BooleanLiteral = rule { capture(Key.W("true") | Key.W("false")) }
+
+ def MultilineComment: Rule0 = rule { "/*" ~ zeroOrMore(MultilineComment | !"*/" ~ ANY) ~ "*/" }
+ def Comment: Rule0 = rule {
+ MultilineComment |
+ "//" ~ zeroOrMore(!Basic.Newline ~ ANY) ~ &(Basic.Newline | EOI)
+ }
+
+ def Literal = rule {
+ (capture(optional("-")) ~ (FloatingPointLiteral | IntegerLiteral) ~> ((sign: String, number) => sign + number)) |
+ BooleanLiteral |
+ CharacterLiteral |
+ StringLiteral |
+ SymbolLiteral |
+ capture(Key.W("null") ~ !(Basic.Letter | Basic.Digit))
+ }
+
+
+ def EscapedChars = rule { '\\' ~ anyOf("rnt\\\"") }
+
+ // Note that symbols can take on the same values as keywords!
+ def SymbolLiteral = rule { ''' ~ capture(Identifiers.PlainId | Identifiers.Keywords) }
+
+ def CharacterLiteral = rule { ''' ~ capture(UnicodeExcape | EscapedChars | !'\\' ~ CharPredicate.from(isPrintableChar)) ~ ''' }
+
+ def MultiLineChars = rule { zeroOrMore(optional('"') ~ optional('"') ~ noneOf("\"")) }
+ def StringLiteral = rule {
+ (optional(Identifiers.Id) ~ "\"\"\"" ~ capture(MultiLineChars) ~ capture("\"\"\"" ~ zeroOrMore('"')) ~> ((multilineChars: String, quotes) => multilineChars + quotes.dropRight(3))) |
+ (optional(Identifiers.Id) ~ '"' ~ capture(zeroOrMore("\\\"" | noneOf("\n\""))) ~ '"')
+ }
+
+ def isPrintableChar(c: Char): Boolean = {
+ val block = Character.UnicodeBlock.of(c)
+ !Character.isISOControl(c) && !Character.isSurrogate(c) && block != null && block != Character.UnicodeBlock.SPECIALS
+ }
+ }
+}
+