Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Created April 9, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilakhter/10283628 to your computer and use it in GitHub Desktop.
Save adilakhter/10283628 to your computer and use it in GitHub Desktop.
lazy val expression: PackratParser[Expression] =
expression ~ ("add" ~> termExpression) ^^ {case l ~ r => PlusExp(l, r)} |
expression ~ ("sub" ~> termExpression) ^^ {case l ~ r => MinusExp(l, r)} |
termExpression
lazy val termExpression: PackratParser[Expression] =
termExpression ~ ("mul" ~> factor) ^^ {case a ~ b => MultiplyExp(a, b)} |
termExpression ~ ("div" ~> factor) ^^ {case a ~ b => DivideExp(a, b)} |
termExpression ~ ("mod" ~> factor) ^^ {case a ~ b => ModExp(a, b)} |
factor
lazy val factor: PackratParser[Expression] =
("not" ~> factor) ^^ NotExp |
factor ~ ("(" ~> expressionList <~ ")") ^^ {case id ~ param => CallExp(id, param)} |
number |
boolean |
string |
idn |
"(" ~> predicate <~ ")"
lazy val expressionList: PackratParser[Seq[Expression]] = repsep(predicate, ",")
lazy val propertyList: PackratParser[Seq[Property]] = repsep(idn, ",")
lazy val idn: PackratParser[Property] = ident ^^ Property
lazy val number: PackratParser[Number] = floatingPointNumber ^^ Number
lazy val string: PackratParser[StringLiteral] = ("\'" + """([^"\p{Cntrl}\\]|\\[\\/bfnrt]|\\u[a-fA-F0-9]{4})*""" + "\'").r ^^ StringLiteral | stringLiteral ^^ StringLiteral
lazy val boolean: PackratParser[Expression] = "true" ^^^ TrueExpr() | "false" ^^^ FalseExpr()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment