diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-11-16 18:53:21 -0800 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-11-16 18:53:21 -0800 |
commit | 001bc7ed3dd1f92be14802406897e7304ac1f2de (patch) | |
tree | 092785256d15d1fb28617ef5f257630f9e6e0126 /scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala | |
parent | 85cf9e1849b452da681d11ead314f46ba8be9512 (diff) | |
download | hands-on-scala-js-001bc7ed3dd1f92be14802406897e7304ac1f2de.tar.gz hands-on-scala-js-001bc7ed3dd1f92be14802406897e7304ac1f2de.tar.bz2 hands-on-scala-js-001bc7ed3dd1f92be14802406897e7304ac1f2de.zip |
Bunch of moving things into namespaces, `Basic`, `Identifiers` and `Literals` don't have their guts spilling everywhere
Diffstat (limited to 'scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala')
-rw-r--r-- | scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala | 98 |
1 files changed, 48 insertions, 50 deletions
diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala b/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala index be6f171..bef65e6 100644 --- a/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala +++ b/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala @@ -3,56 +3,54 @@ 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") +trait Literals { self: ScalaSyntax => + 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("true" | "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("null") + } + + + def EscapedChars = rule { '\\' ~ anyOf("rnt\\\"") } + + def SymbolLiteral = rule { ''' ~ capture(Identifiers.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 + } } } -/** - * 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 - } -} |