Created
April 9, 2014 15:32
-
-
Save adilakhter/10283628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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