Fexpr

A Crystal port of ganigeorgiev/fexpr, a filter expression parser that generates AST structures from user input for safe creation of SQL, Elasticsearch, and other queries.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      fexpr:
        github: rubyattack3r/fexpr
  2. Run shards install

Usage

require "fexpr"

Basic Example

# Input: "id > 1"
result = Fexpr.parse("id > 1")
# => [ExpressionGroup.new(
#   item: Expression.new(
#     left: Token.new(type: TokenType::Identifier, literal: "id"),
#     operation: SignOperation::SignGt,
#     right: Token.new(type: TokenType::Number, literal: "1")
#   ),
#   join: JoinOperation::JoinAnd
# )]

Supported Operators

Token Types

Numbers

Number tokens can be integer or decimal numbers:

"123"    # Integer
"10.50"  # Decimal
"-14"    # Negative

Identifiers

Identifiers start with a letter, _, @ or # and can contain letters, digits, . or ::

"id"                     # Simple identifier
"a.b.c"                  # Nested field
"field123"               # With numbers
"@request.method"        # With @ prefix
"author.name:length"     # With modifier

Quoted Text

Text can be wrapped in single or double quotes:

"'Lorem ipsum dolor 123!'"        # Single quotes
"\"escaped \\\"word\\\"\""        # Escaped quotes
"\"mixed 'quotes' are fine\""     # Mixed quotes

Comments

Single line comments start with // and are ignored by the parser:

"// This is a comment"

Contributing

  1. Fork it (https://github.com/rubyattack3r/fexpr/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

Contributors