Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ rules = [
LeakingImplicitClassVal
NoAutoTupling
NoValInForComprehension
RemoveUnused
]

# find more at https://scalacenter.github.io/scalafix/docs/rules/community-rules.html
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ lazy val sharedSettings_scalac = Seq(
"-unchecked",
"-deprecation",
"-feature",
"-Xfatal-warnings",
// "-Xfatal-warnings",
"-Wconf:cat=deprecation:i",
"-language:existentials",
"-Wunused:imports",
Expand Down
12 changes: 12 additions & 0 deletions diesel/shared/src/main/scala/diesel/AstHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ object AstHelpers {
}
}

def assertAllAsts(
dsl: Dsl,
verbalizer: Option[Verbalizer] = None,
axiom: Option[Axiom[_]] = None,
navigatorFactory: Result => Navigator = Navigator(_)
)(s: String)(f: Seq[GenericTree] => Unit): Unit = {
assertAsts(dsl, verbalizer, axiom, navigatorFactory)(s) { n: Navigator =>
val as = Seq.from(n.toIterator)
f(as)
}
}

def withAst[T](
dsl: Dsl,
verbalizer: Option[Verbalizer] = None,
Expand Down
18 changes: 18 additions & 0 deletions diesel/shared/src/test/scala/diesel/DslTestFunSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ abstract class DslTestFunSuite extends FunSuite {
}
}

protected def assertAllAsts(text: String)(expected: => Seq[Ast]): Unit = {
withAllAsts(text) { asts =>
assertEquals(expected, asts)
}
}

protected def assertMarkers(text: String)(expected: => Seq[Marker]): Unit = {
withTree(text) { tree =>
assertEquals(tree.markers, expected)
Expand All @@ -54,6 +60,12 @@ abstract class DslTestFunSuite extends FunSuite {
}
}

protected def withAllAsts(text: String)(f: Seq[Ast] => Unit): Unit = {
withAllTrees(text) { trees =>
f(trees.map(ast))
}
}

protected def withAsts(text: String)(f: Navigator => Unit): Unit = {
AstHelpers.assertAsts(dsl, axiom = axiom)(text) { navigator =>
f(navigator)
Expand All @@ -72,6 +84,12 @@ abstract class DslTestFunSuite extends FunSuite {
}
}

protected def withAllTrees(text: String)(f: Seq[GenericTree] => Unit): Unit = {
AstHelpers.assertAllAsts(dsl, verbalizer = verbalizer, axiom = axiom)(text) { trees =>
f(trees)
}
}

// IntellliJ hint
// replace: testAst\(("[^"]*")\) \{([^\}]+)
// by: test($1) { assertAst($1) { $2 }
Expand Down
99 changes: 99 additions & 0 deletions diesel/shared/src/test/scala/diesel/PrecedenceMappedTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2018 The Diesel Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package diesel

import diesel.Dsl.{Axiom, Syntax}
import diesel.samples.calc.Ast._
import diesel.samples.calc.MathBase

object MyDslWithMappedPrecedence extends MathBase {
case class Div(d1: Expr, d2: Expr) extends Expr
case class Abs(d: Expr) extends Expr

val div: Syntax[Expr] = syntax(number)(
number ~ ("/".leftAssoc(13) map { case (_, t) => t }) ~ number map {
case (c, (n1, plus, n2)) =>
c.setTokenStyle(plus, KeywordStyle)
Div(n1, n2)
}
)

val abs: Syntax[Expr] = syntax(number)(
"abs".rightAssoc(1) ~ number map {
case (_, (_, n)) =>
Abs(n)
}
)

val expr: Axiom[Expr] = axiom(number)

}

class PrecedenceMappedTest extends DslTestFunSuite {
import MyDslWithMappedPrecedence.{Div, Abs}

type Ast = Expr
override def dsl = MyDslWithMappedPrecedence

test("unmapped precedence") {
assertAst("1 + 2 + 3") {
Add(
Add(
Value(1),
Value(2)
),
Value(3)
)
}
}

test("mapped precedence is lost") {
// mapping a token does not propagate its precedence
// we will find ways to not even allow that situation via API
assertAllAsts("1 / 2 / 3") {
Seq(
Div(Div(Value(1), Value(2)), Value(3)),
Div(Value(1), Div(Value(2), Value(3)))
)
}
}

test("mixed precedence") {
assertAst("abs 1 + 2") {
Abs(
Add(
Value(1),
Value(2)
)
)
}
}

test("nested precedence") {
assertAst("abs abs 1 + 2") {
Abs(
Abs(
Add(
Value(1),
Value(2)
)
)
)
}
}

}