summaryrefslogtreecommitdiff
path: root/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala
blob: be6f17188f1991f22abfab7a1249bcf354737081 (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
package torimatomeru
package syntax

import org.parboiled2._

trait Literals extends StringLiterals { self: ScalaSyntax =>

  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("true" | "false") }

  def MultilineComment: Rule0 = rule { "/*" ~ zeroOrMore(MultilineComment | !"*/" ~ ANY) ~ "*/" }
  def Comment: Rule0 = rule {
    MultilineComment |
      "//" ~ zeroOrMore(!NewlineS ~ ANY) ~ (NewlineS | EOI)
  }

  def Literal = rule {
    (capture(optional("-")) ~ (FloatingPointLiteral | IntegerLiteral) ~> ((sign: String, number) => sign + number)) |
      BooleanLiteral |
      CharacterLiteral |
      StringLiteral |
      SymbolLiteral |
      capture("null")
  }
}

/**
 * Placed the string defintions in this trait to isolate them, because they are overly complex.
 */
private[syntax] trait StringLiterals { self: Literals with ScalaSyntax =>

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

  def SymbolLiteral = rule { ''' ~ capture(PlainId) }

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

  def MultiLineChars = rule { zeroOrMore(optional('"') ~ optional('"') ~ noneOf("\"")) }
  def StringLiteral = rule {
    ("\"\"\"" ~ capture(MultiLineChars) ~ capture("\"\"\"" ~ zeroOrMore('"')) ~> ((multilineChars: String, quotes) => multilineChars + quotes.dropRight(3))) |
      ('"' ~ 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
  }
}