Skip to content

Instantly share code, notes, and snippets.

@systay
Created January 19, 2012 20:25
Show Gist options
  • Save systay/1642357 to your computer and use it in GitHub Desktop.
Save systay/1642357 to your computer and use it in GitHub Desktop.
def term: Parser[Expression] = factor ~ rep("*" ~ factor | "/" ~ factor) ^^ {
case head ~ rest => {
var result = head
rest.foreach {
case "*" ~ f => result = Multiply(result,f)
case "/" ~ f => result = Divide(result,f)
}
result
}
}
def expression: Parser[Expression] = term ~ rep( "+" ~ term | "-" ~ term) ^^ {
case head ~ rest => {
var result = head
rest.foreach {
case "+" ~ f => result = Add(result, f)
case "-" ~ f => result = Subtract(result, f)
}
result
}
}
def factor: Parser[Expression] =
( ignoreCase("true") ^^ (x => Literal(true))
| ignoreCase("false") ^^ (x => Literal(false))
| extract
| function
| identity~>parens(expression | entity)~>failure("unknown function")
| coalesceFunc
| nullableProperty
| property
| string ^^ (x => Literal(x))
| number ^^ (x => Literal(x.toDouble))
| parameter
| entity
| parens(expression)
| failure("illegal start of value") )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment