Skip to content

Commit

Permalink
Made syntax more tollerant for whitespaces and made the distinction b…
Browse files Browse the repository at this point in the history
…etween string and integer literals
  • Loading branch information
bhermann committed Nov 21, 2019
1 parent df450b5 commit a9e80b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
34 changes: 23 additions & 11 deletions core/src/main/scala/de/upb/cs/swt/delphi/core/ql/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class Syntax(val input : ParserInput) extends Parser {
OrOrElseRule | NotRule
}
def OrOrElseRule = rule {
AndOrElseRule ~ zeroOrMore("||" ~ AndOrElseRule ~> OrExpr)
AndOrElseRule ~ zeroOrMore(Whitespace ~ "||" ~ Whitespace ~ AndOrElseRule ~> OrExpr)
}
def AndOrElseRule = rule {
XorOrElseRule ~ zeroOrMore("&&" ~ XorOrElseRule ~> AndExpr)
XorOrElseRule ~ zeroOrMore(Whitespace ~ "&&" ~ Whitespace ~ XorOrElseRule ~> AndExpr)
}
def XorOrElseRule = rule {
Factor ~ zeroOrMore("%%" ~ Factor ~> XorExpr)
Factor ~ zeroOrMore(Whitespace ~ "%%" ~ Whitespace ~ Factor ~> XorExpr)
}

// Handling parentheses.
Expand All @@ -57,18 +57,30 @@ class Syntax(val input : ParserInput) extends Parser {
EqualRule | NotEqualRule | GreaterThanRule | GreaterOrEqual |
LessThan | LessOrEqual | Like | IsTrue
}
def EqualRule = rule { FieldReferenceRule ~ "=" ~ Literal ~> EqualExpr }
def NotEqualRule = rule { FieldReferenceRule ~ "!=" ~ Literal ~> NotEqualExpr }
def GreaterThanRule = rule { FieldReferenceRule ~ ">" ~ Literal ~> GreaterThanExpr }
def GreaterOrEqual = rule { FieldReferenceRule ~ ">=" ~ Literal ~> GreaterOrEqualExpr }
def LessThan = rule { FieldReferenceRule ~ "<" ~ Literal ~> LessThanExpr }
def LessOrEqual = rule { FieldReferenceRule ~ "<=" ~ Literal ~> LessOrEqualExpr }
def Like = rule { FieldReferenceRule ~ "%" ~ Literal ~> LikeExpr }
def EqualRule = rule { FieldReferenceRule ~ Whitespace ~ "=" ~ Whitespace ~ Literal ~> EqualExpr }
def NotEqualRule = rule { FieldReferenceRule ~ Whitespace ~ "!=" ~ Whitespace ~ Literal ~> NotEqualExpr }
def GreaterThanRule = rule { FieldReferenceRule ~ Whitespace ~ ">" ~ Whitespace ~ IntegerLiteral ~> GreaterThanExpr }
def GreaterOrEqual = rule { FieldReferenceRule ~ Whitespace ~ ">=" ~ Whitespace ~ IntegerLiteral ~> GreaterOrEqualExpr }
def LessThan = rule { FieldReferenceRule ~ Whitespace ~ "<" ~ Whitespace ~ IntegerLiteral ~> LessThanExpr }
def LessOrEqual = rule { FieldReferenceRule ~ Whitespace ~ "<=" ~ Whitespace ~ IntegerLiteral ~> LessOrEqualExpr }
def Like = rule { FieldReferenceRule ~ Whitespace ~ "%" ~ Whitespace ~ StringLiteral ~> LikeExpr }
def IsTrue = rule { FieldReferenceRule ~> IsTrueExpr }

// Literals
def FieldReferenceRule = rule { "[" ~ capture(oneOrMore(CharPredicate.AlphaNum ++ '-' ++ ' ' ++ '_' ++ '(' ++ ':' ++')')) ~ "]" ~> FieldReference }
def Literal = rule { capture(oneOrMore(CharPredicate.AlphaNum)) ~> (_.toString) }

def IntegerLiteral = rule { capture(oneOrMore(CharPredicate.Digit)) }
def StringLiteral = rule { '"' ~ capture(oneOrMore(CharPredicate.AlphaNum)) ~ '"'}

def Literal = rule { (IntegerLiteral | StringLiteral ) ~> (_.toString) }

def Whitespace = rule {
zeroOrMore(anyOf(" \n \r"))
}

def OneOrMoreWhitespace = rule {
oneOrMore(anyOf(" \n \r"))
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import scala.util.{Failure, Success}
class SyntaxTest extends FlatSpec with Matchers {

"Syntax.singularConditionWithOperator" should "be valid" in {
val parseResult = new Syntax("[Filter1]=abc").QueryRule.run()
val parseResult = new Syntax("[Filter1]=\"abc\"").QueryRule.run()
parseResult shouldBe a [Success[_]]
parseResult match {
case Success(ast) => {
Expand Down Expand Up @@ -165,7 +165,7 @@ class SyntaxTest extends FlatSpec with Matchers {
}

"Syntax.notConditionComplex" should "be valid" in {
val parseResult = new Syntax("!!([Filter1])&&!([Filter2]<=0||!([Filter3]&&![Filter4]%abc))").QueryRule.run()
val parseResult = new Syntax("!!([Filter1])&&!([Filter2]<=0||!([Filter3]&&![Filter4]%\"abc\"))").QueryRule.run()
parseResult shouldBe a [Success[_]]
parseResult match {
case Success(ast) => {
Expand Down

0 comments on commit a9e80b1

Please sign in to comment.