Permalink
Browse files

Uniform fixity prefix operators only have a single syntactic interpre…

…tation, and thus should be ignored by precedence filtering
  • Loading branch information...
1 parent 33deea3 commit 255577d732959457bd1dbae6e6827e7c4331a9db @djspiewak committed Jun 29, 2012
Showing with 36 additions and 0 deletions.
  1. +9 −0 src/main/scala/com/codecommit/gll/ast/Filters.scala
  2. +27 −0 src/test/scala/FilterSpecs.scala
@@ -66,6 +66,15 @@ trait Filters {
leftCheck && rightCheck
}
+ case un: UnaryNode => {
+ un.child match {
+ case un2: UnaryNode if un2.isPrefix == un.isPrefix =>
+ true
+
+ case _ => fallback
+ }
+ }
+
case _ => fallback
}
} else {
@@ -202,6 +202,25 @@ object FilterSpecs extends Specification with ScalaCheck with RegexParsers {
}
}
+ "ignore relative precedence of unary operations of the same fixity" in {
+ lazy val expr: Parser[Expr] = (
+ "-" ~> expr ^^ Neg
+ | "~" ~> expr ^^ Comp2
+ | "(" ~> expr <~ ")"
+ | num ^^ IntLit
+ ) filter prec('add, 'neg, 'comp2)
+
+ expr("-~1") must beLike {
+ case Stream(Success(Neg(Comp2(IntLit(1))), LineStream())) => true
+ case _ => false
+ }
+
+ expr("~-1") must beLike {
+ case Stream(Success(Comp2(Neg(IntLit(1))), LineStream())) => true
+ case _ => false
+ }
+ }
+
"disambiguate non-uniform fixity unary operations with precedence" in {
lazy val expr: Parser[Expr] = (
"-" ~> expr ^^ Neg
@@ -273,6 +292,14 @@ object FilterSpecs extends Specification with ScalaCheck with RegexParsers {
val solve = -child.solve
}
+ case class Comp2(child: Expr) extends Expr with UnaryNode {
+ val label = 'comp2
+
+ val isPrefix = true
+
+ val solve = ~child.solve
+ }
+
case class Comp(child: Expr) extends Expr with UnaryNode {
val label = 'comp

0 comments on commit 255577d

Please sign in to comment.