diff options
-rw-r--r-- | scalatexApi/src/main/scala/scalatex/ScalatexParser.scala | 37 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala (renamed from scalatexApi/src/test/scala/torimatomeru/ScalaSyntax.scala) | 0 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala | 30 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala | 22 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala | 58 | ||||
-rw-r--r-- | scalatexApi/src/test/scala/scalatex/Main.scala | 44 |
6 files changed, 154 insertions, 37 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/ScalatexParser.scala b/scalatexApi/src/main/scala/scalatex/ScalatexParser.scala new file mode 100644 index 0000000..406f4cd --- /dev/null +++ b/scalatexApi/src/main/scala/scalatex/ScalatexParser.scala @@ -0,0 +1,37 @@ +package scalatex + +import org.parboiled2._ +import torimatomeru.ScalaSyntax + +trait Ast{ + def offset: Int +} +object Ast{ + case class Block(parts: Seq[Block.Sub], offset: Int = 0) extends Chain.Sub + object Block{ + trait Sub + case class Text(txt: String, offset: Int = 0) extends Block.Sub + } + case class Code(code: String, offset: Int = 0) + case class Chain(lhs: Code, parts: Seq[Chain.Sub], offset: Int = 0) extends Block.Sub + object Chain{ + trait Sub + case class Prop(str: String, offset: Int = 0) extends Sub + case class Args(str: String, offset: Int = 0) extends Sub + } +} +class ScalatexParser(input: ParserInput) extends ScalaSyntax(input) { + def TextNot(chars: String) = rule { capture(oneOrMore(noneOf(chars) | "@@")) ~> (x => Ast.Block.Text(x.replace("@@", "@"))) } + def Text = TextNot("@") + def Code = rule { + "@" ~ capture(Id | BlockExpr | ('(' ~ optional(Exprs) ~ ')')) ~> (Ast.Code(_)) + } + def ScalaChain = rule { Code ~ zeroOrMore(Extension) ~> (Ast.Chain(_, _)) } + def Extension: Rule1[Ast.Chain.Sub] = rule { + (capture(('.' ~ Id) ~ optional(TypeArgs)) ~> (Ast.Chain.Prop(_))) | + (capture(!BlockExpr ~ ArgumentExprs) ~> (Ast.Chain.Args(_))) | + TBlock + } + def TBlock = rule{ '{' ~ Body ~ '}' } + def Body = rule{ zeroOrMore(TextNot("@}") | ScalaChain) ~> (x => Ast.Block(x)) } +} diff --git a/scalatexApi/src/test/scala/torimatomeru/ScalaSyntax.scala b/scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala index 5bbd0af..5bbd0af 100644 --- a/scalatexApi/src/test/scala/torimatomeru/ScalaSyntax.scala +++ b/scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala b/scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala new file mode 100644 index 0000000..4240ea9 --- /dev/null +++ b/scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala @@ -0,0 +1,30 @@ +package torimatomeru +package syntax + +import org.parboiled2._ + +trait Basic { self: ScalaSyntax => + + def UnicodeExcape = rule { "\\u" ~ 4.times(HexDigit) } + + + //Numbers and digits + def HexDigit = rule { Digit | "a" - "f" | "A" - "Z" } + def Digit = rule { "0" | NonZeroDigit } + def NonZeroDigit = rule { "1" - "9" } + def HexNumeral = rule { "0x" ~ oneOrMore(HexDigit) } + def DecimalNumeral = rule(oneOrMore(Digit)) + def ExponentPart = rule { anyOf("Ee") ~ optional(anyOf("+-")) ~ oneOrMore(Digit) } + def FloatType = rule { anyOf("FfDd") } + + def Parentheses = rule { "(" | ")" | "[" | "]" | "{" | "}" } + def DelimiterChar = rule { "'" | "\"" | "." | ";" | "," } + + def WhitespaceChar = rule { "\u0020" | "\u0009" } + def Newline = rule { "\r\n" | "\n" } + def Semi = rule { ';' | oneOrMore(NewlineS) } + def OperatorChar = rule { anyOf("""!#$%&*+-/:<=>?@\^|~""") | CharPredicate.from(c => c.getType match { case Character.OTHER_SYMBOL | Character.MATH_SYMBOL => true; case _ => false}) } + def Letter = rule { Upper | Lower | CharPredicate.from(c => c.isLetter | c.isDigit) } + def Lower = rule { "a" - "z" | "$" | "_" | CharPredicate.from(_.isLower) } + def Upper = rule { "A" - "Z" | CharPredicate.from(_.isUpper) } +} diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala b/scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala new file mode 100644 index 0000000..9c39577 --- /dev/null +++ b/scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala @@ -0,0 +1,22 @@ +package torimatomeru +package syntax + +import org.parboiled2._ + +trait Identifiers { self: Parser with Basic => + + def Operator = rule(oneOrMore(OperatorChar)) + + def VarId = rule { !(Keywords ~ (WhitespaceChar | Newline | "//" | "/*")) ~ Lower ~ IdRest } + def PlainId = rule { Upper ~ IdRest | VarId | !(Keywords ~ (WhitespaceChar | Newline | "//" | "/*")) ~ Operator } + def Id = rule { PlainId | ("`" ~ oneOrMore(noneOf("`")) ~ "`") } + def IdRest = rule { zeroOrMore(Letter | Digit) ~ optional("_" ~ Operator) } + + + def Keywords = rule { + "abstract" | "case" | "catch" | "class" | "def" | "do" | "else" | "extends" | "false" | "finally" | "final" | "finally" | "forSome" | "for" | "if" | + "implicit" | "import" | "lazy" | "match" | "new" | "null" | "object" | "override" | "package" | "private" | "protected" | "return" | + "sealed" | "super" | "this" | "throw" | "trait" | "try" | "true" | "type" | "val" | "var" | "while" | "with" | "yield" | "_" | + ":" | ";" | "=>" | "=" | "<-" | "<:" | "<%" | ">:" | "#" | "@" | "\u21d2" | "\u2190" + } +} diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala b/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala new file mode 100644 index 0000000..be6f171 --- /dev/null +++ b/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala @@ -0,0 +1,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 + } +} diff --git a/scalatexApi/src/test/scala/scalatex/Main.scala b/scalatexApi/src/test/scala/scalatex/Main.scala index 968e34c..fa0d213 100644 --- a/scalatexApi/src/test/scala/scalatex/Main.scala +++ b/scalatexApi/src/test/scala/scalatex/Main.scala @@ -30,7 +30,12 @@ object Main extends utest.TestSuite{ 'Block{ * - check("{i am a cow}", _.TBlock.run(), Block(Seq(Block.Text("i am a cow")))) * - check("{i @am a @cow}", _.TBlock.run(), - Block(Seq(Block.Text("i "), Chain(Code("am"),Seq()), Block.Text(" a "), Chain(Code("cow"),Seq()))) + Block(Seq( + Block.Text("i "), + Chain(Code("am"),Seq()), + Block.Text(" a "), + Chain(Code("cow"),Seq()) + )) ) } 'Chain{ @@ -46,43 +51,8 @@ object Main extends utest.TestSuite{ )) ) } - - } - def p(input: String) = { - new ScalatexParser(input) - } -} -trait Ast{ - def offset: Int -} -object Ast{ - case class Code(code: String, offset: Int = 0) extends Ast - case class Block(parts: Seq[Block.Sub], offset: Int = 0) extends Chain.Sub - object Block{ - trait Sub - case class Text(txt: String, offset: Int = 0) extends Block.Sub } - case class Chain(lhs: Code, parts: Seq[Chain.Sub], offset: Int = 0) extends Block.Sub - object Chain{ - trait Sub extends Ast - case class Prop(str: String, offset: Int = 0) extends Sub - case class Args(str: String, offset: Int = 0) extends Sub - } -} -class ScalatexParser(input: ParserInput) extends ScalaSyntax(input) { - def TextNot(chars: String) = rule { capture(oneOrMore(noneOf(chars) | "@@")) ~> (x => Ast.Block.Text(x.replace("@@", "@"))) } - def Text = TextNot("@") - def Code = rule { - "@" ~ capture(Id | BlockExpr | ('(' ~ optional(Exprs) ~ ')')) ~> (Ast.Code(_)) - } - def ScalaChain = rule { Code ~ zeroOrMore(Extension) ~> (Ast.Chain(_, _)) } - def Extension: Rule1[Ast.Chain.Sub] = rule { - (capture(('.' ~ Id) ~ optional(TypeArgs)) ~> (Ast.Chain.Prop(_))) | - (capture(!BlockExpr ~ ArgumentExprs) ~> (Ast.Chain.Args(_))) | - TBlock - } - def TBlock = rule{ '{' ~ Body ~ '}' } - def Body = rule{ zeroOrMore(TextNot("@}") | ScalaChain) ~> (x => Ast.Block(x)) } + } |