Code Monkey home page Code Monkey logo

expression-parser's Introduction

Build Status Maven Central

Latest releases and snapshots

About

This is a ready to use expression parser and evaluator for Scala powered by parboiled2.

Example:

val result = HEval("4*5+3*2").toInt
// result = 26

It's possible to provide a context with variables to the parser:

val context = Obj.from("user" -> Obj.from("count" -> 10))
val result = HEval("4*user.count+1*2", context).toInt
// result = 42

You also can add your own functions into the context:

val context = new ValueContext(Obj.empty) {
    override def function: PartialFunction[(Identifier, Seq[Value]), Value] = {
        case (Identifier(Seq("pow")), args) => args.head.toBigDecimal.pow(args.tail.head.toInt)
    }
}

val result = HEval("pow(2,8)", context).toInt
// result = 256

TODO

  • HParser function

Built-in operators

+, -, *, / - arithmetical operations

!, =, !=, and, or, xor - boolean operations

% - remainder (modulus) operation

>, <, >=, <= - comparison operations

someObject.someField - evaluate value of 'someField' of 'someObject'

has, has not - tests if collection has (or has not) specified element

like, not like - tests if value matches regexp

++ - adds one collection to another

-- - computes difference between two collections

Built-in functions

case - takes two arguments: index and collection. Returns value placed in collection with specified index

isEmpty - tests whether value is empty

isExists - tests whether value is present in context

length - returns length of collection or string. Boolean argument is a special case: function returns '1' in case of 'true' and '0' in case of 'false'

upper, lower - returns string in upper or lower case

split - takes two arguments: string and separator and returns collection made by splitting of incoming string by specified separator

indexOf - takes two arguments: string and substring. Returns the index within this string of the first occurrence of the specified substring

substr - takes 2 or 3 arguments: string, beginIndex[, endIndex] - returns substring of string

compareIgnoreCase - compares two strings ignoring their case

Context field access

  1. Values of fields of some object should be dot-separated:
user.field.subField
  1. Strings can be written with quotes of apostrophes (later ignores escapes):
'someString\nabcde' // escaping ignored
"someString\nSecondLine" // newline inserted
  1. Numbers should be written as is, without any additional symbols

Other Examples (TODO)

user.isDefined
!user.isDefined
user.isDefined = true 
user.isDefined != true
user.roles has "qa"
user.roles has not "admin"
(user.isDefined = true) and (user.roles has "qa") 
(user.isDefined) and (user.roles has "qa")) 
(!user.isDefined) or (user.roles has "admin")

Download

libraryDependencies += "com.hypertino" %% "expression-parser" % "0.3.0"

Releases published to Maven Central for Scala 2.11 - 2.13 JVM & JS (user %%% for Scala.js enabled projects)

Snapshots live in Sonatype repository, include it additionally:

resolvers ++= Seq(
  Resolver.sonatypeRepo("public")
)

License

library is available under the BSD 3-Clause License

expression-parser's People

Contributors

aolenev avatar maqdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

expression-parser's Issues

Scala3 support version?

Given that it has been a while since anything was done on this project I wonder if it is still active and if there will be a build for Scala3 available? And with that ongoing support for stuff that pop up?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.