Skip to content

MiroslavCsonka/lamep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LAMEP

TODO: Write a gem description

Installation

Add this line to your application's Gemfile:

gem 'lamep'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lamep

Usage

Basic Usage

You can use lamep for:

  1. Parsing expression into array of tokens:
# Splits expression into single tokens
tokens = TokenParser.new.parse('material = wood && (price > 180 || price <= 250)')

Variable tokens will be array of tokens:

%w{material = wood && ( price > 180 || price <= 250 )}
  1. Converting tokens into postfix notation:
# Converts tokens into postfix notation (also known as RPN)
postfix = ShuntingYard.new(tokens).posfix

Variable postfix will be array of tokens in postfix notation:

%w{material wood = price 180 > price 250 <= || &&}
  1. Building Abstract Syntax Tree:
# Builds tree from postfix
ast = AbstractSyntaxTreeBuilder.new(postfix).build_tree

Variable ast will be containing such structure: ruby And.new( Equal.new('material', 'wood'), Or.new( GreaterThan.new('price', '180'), LessThanEqual.new('price', '250') ) ) 4. Building SQL WHERE clause:

# Builds SQL WHERE clause
sql_where = ast.to_sql

Variable sql_where will be string:

'((material = wood) AND ((price > 180) OR (price <= 250)))'

Supported operators

At this point we support following list of operators and their SQL conversion:

operator description
= Equal
!= Not Equal
> Greater Than
>= Greater Than Equal
< Less Than
<= Less Than Equal
&& Logical AND
|| Logical OR
- Unary Minus

The rest of passed expression is considered as an operand i.e. either variable or a number.

Adding your own operators

TODO:

Contributing

  1. Fork it ( https://github.com/forex-kaiz/lamep/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

Logical and mathematical expression parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages