summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-11-04 21:26:40 -0800
committerLi Haoyi <haoyi@dropbox.com>2014-11-04 21:26:40 -0800
commit938c026bef8d4ce10848831479944642ac6da853 (patch)
treeb7a3326195325fcae6bcae08151ee349d02e534f
parentbc675b3e40882bdd8ddbb97a4a717a5672f4980f (diff)
downloadhands-on-scala-js-938c026bef8d4ce10848831479944642ac6da853.tar.gz
hands-on-scala-js-938c026bef8d4ce10848831479944642ac6da853.tar.bz2
hands-on-scala-js-938c026bef8d4ce10848831479944642ac6da853.zip
Various combinations of if-else work
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Parser.scala29
-rw-r--r--scalatexApi/src/test/scala/scalatex/ParserTests.scala34
2 files changed, 49 insertions, 14 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
index 3a5fb97..3b75d54 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
@@ -55,30 +55,35 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
def LoneScalaChain: Rule2[Ast.Block.Text, Ast.Chain] = rule {
(capture(Indent) ~> (Ast.Block.Text(_))) ~
ScalaChain ~
- test(cursorNextIndent() > indent) ~
- runSubParser(new Parser(_, cursorNextIndent()).Body) ~> {
+ IndentBlock ~> {
(chain: Ast.Chain, body: Ast.Block) => chain.copy(parts = chain.parts :+ body)
}
}
-
+ def IndentBlock = rule{
+ test(cursorNextIndent() > indent) ~
+ runSubParser(new Parser(_, cursorNextIndent()).Body)
+ }
+ def IfHead = rule{ "@" ~ capture("if" ~ "(" ~ Expr ~ ")") }
+ def IfElse1 = rule{
+ IfHead ~ BraceBlock ~ optional("else" ~ (BraceBlock | IndentBlock))
+ }
+ def IfElse2 = rule{
+ IfHead ~ IndentBlock ~ optional(Indent ~ "@else" ~ (BraceBlock | IndentBlock))
+ }
def IfElse = rule{
- "@" ~ capture("if" ~ "(" ~ Expr ~ ")") ~ TBlock ~ optional("else" ~ TBlock) ~>{
- Ast.Block.IfElse(_, _, _)
- }
+ (IfElse1 | IfElse2) ~> (Ast.Block.IfElse(_, _, _))
}
def ForLoop = rule{
"@" ~
capture("for" ~ ('(' ~ Enumerators ~ ')' | '{' ~ Enumerators ~ '}')) ~
- TBlock ~> (Ast.Block.For(_, _))
+ BraceBlock ~> (Ast.Block.For(_, _))
}
def LoneForLoop = rule{
"@" ~
capture("for" ~ ('(' ~ Enumerators ~ ')' | '{' ~ Enumerators ~ '}')) ~
- test(cursorNextIndent() > indent) ~
- runSubParser(new Parser(_, cursorNextIndent()).Body) ~>
+ IndentBlock ~>
(Ast.Block.For(_, _))
}
-
def ScalaChain = rule {
Code ~ zeroOrMore(Extension) ~> {Ast.Chain(_, _)}
}
@@ -86,7 +91,7 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
('.' ~ capture(Id) ~> (Ast.Chain.Prop(_))) |
(capture(TypeArgs2) ~> (Ast.Chain.TypeArgs(_))) |
(capture(ArgumentExprs2) ~> (Ast.Chain.Args(_))) |
- TBlock
+ BraceBlock
}
def Ws = Whitespace
// clones of the version in ScalaSyntax, but without tailing whitespace or newlines
@@ -95,7 +100,7 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
'(' ~ Ws ~ (optional(Exprs ~ ',' ~ Ws) ~ PostfixExpr ~ ':' ~ Ws ~ '_' ~ Ws ~ '*' ~ Ws | optional(Exprs)) ~ ')'
}
def BlockExpr2: Rule0 = rule { '{' ~ Ws ~ (CaseClauses | Block) ~ '}' }
- def TBlock: Rule1[Ast.Block] = rule{ '{' ~ Body ~ '}' }
+ def BraceBlock: Rule1[Ast.Block] = rule{ '{' ~ Body ~ '}' }
def BodyItem: Rule1[Seq[Ast.Block.Sub]] = rule{
LoneScalaChain ~> (Seq(_, _)) |
diff --git a/scalatexApi/src/test/scala/scalatex/ParserTests.scala b/scalatexApi/src/test/scala/scalatex/ParserTests.scala
index c2f35cc..c012328 100644
--- a/scalatexApi/src/test/scala/scalatex/ParserTests.scala
+++ b/scalatexApi/src/test/scala/scalatex/ParserTests.scala
@@ -119,8 +119,8 @@ object ParserTests 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(),
+ * - check("{i am a cow}", _.BraceBlock.run(), Block(Seq(Block.Text("i am a cow"))))
+ * - check("{i @am a @cow}", _.BraceBlock.run(),
Block(Seq(
Block.Text("i "),
Chain("am",Seq()),
@@ -170,6 +170,36 @@ object ParserTests extends utest.TestSuite{
_.IfElse.run(),
IfElse("if(true)", Block(Seq(Text("lol"))), Some(Block(Seq(Text(" omg ")))))
)
+ 'ifBlock- check(
+ """@if(true)
+ | omg""".stripMargin,
+ _.IfElse.run(),
+ IfElse("if(true)", Block(Seq(Text("\n "), Text("omg"))), None)
+ )
+ 'ifBlockElseBlock - check(
+ """@if(true)
+ | omg
+ |@else
+ | wtf""".stripMargin,
+ _.IfElse.run(),
+ IfElse(
+ "if(true)",
+ Block(Seq(Text("\n "), Text("omg"))),
+ Some(Block(Seq(Text("\n "), Text("wtf"))))
+ )
+ )
+ 'ifElseBlock - check(
+ """@if(true){
+ | omg
+ |}else
+ | wtf""".stripMargin,
+ _.IfElse.run(),
+ IfElse(
+ "if(true)",
+ Block(Seq(Text("\n "), Text("omg"), Text("\n"))),
+ Some(Block(Seq(Text("\n "), Text("wtf"))))
+ )
+ )
}
'Body{
'indents - check(