summaryrefslogtreecommitdiff
path: root/scalaParser/src/main/scala/scalaParser/syntax/Literals.scala
blob: e6dd54d059a5c6f78748fcbddd8ed7b964602b87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package scalaParser
package syntax
import acyclic.file
import org.parboiled2._

trait Literals { self: Parser with Basic with Identifiers =>
  def Block: Rule0
  def WL: Rule0
  object Literals{
    import Basic._
    def FloatingPointLiteral = rule {

      "." ~ oneOrMore(Digit) ~ optional(ExponentPart) ~ optional(FloatType) |
      oneOrMore(Digit) ~ (
        "." ~ oneOrMore(Digit) ~ optional(ExponentPart) ~ optional(FloatType) |
        ExponentPart ~ optional(FloatType) |
        optional(ExponentPart) ~ FloatType
      )
    }

    def IntegerLiteral = rule { (HexNumeral | DecimalNumeral) ~ optional(anyOf("Ll")) }

    def BooleanLiteral = rule { 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 {
      (optional("-") ~ (FloatingPointLiteral | IntegerLiteral)) |
      BooleanLiteral |
      CharacterLiteral |
      StringLiteral |
      SymbolLiteral |
      (Key.W("null") ~ !(Basic.Letter | Basic.Digit))
    }

    def EscapedChars = rule { '\\' ~ anyOf("btnfr'\\\"") }

    // Note that symbols can take on the same values as keywords!
    def SymbolLiteral = rule { ''' ~ (Identifiers.PlainId | Identifiers.Keywords) }

    def CharacterLiteral = rule {
      ''' ~ (UnicodeExcape | EscapedChars | !'\\' ~ CharPredicate.from(isPrintableChar)) ~ '''
    }

    def MultiLineChars = rule {
      zeroOrMore(Interpolation | optional('"') ~ optional('"') ~ noneOf("\""))
    }
    def pr(s: String) = rule { run(println(s"LOGGING $cursor: $s")) }
    def Interpolation = rule{
      "$" ~ Identifiers.PlainIdNoDollar | "${" ~ Block ~ WL ~ "}" | "$$"
    }
    def StringLiteral = rule {
      (Identifiers.Id ~ "\"\"\"" ~ MultiLineChars ~ ("\"\"\"" ~ zeroOrMore('"'))) |
      (Identifiers.Id ~ '"' ~ zeroOrMore(Interpolation | "\\\"" | "\\\\" | noneOf("\n\"")) ~ '"') |
      ("\"\"\"" ~ MultiLineChars ~ ("\"\"\"" ~ zeroOrMore('"'))) |
      ('"' ~ 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
    }
  }
}