Module: Janeway

Defined in:
lib/janeway.rb,
lib/janeway/error.rb,
lib/janeway/lexer.rb,
lib/janeway/token.rb,
lib/janeway/parser.rb,
lib/janeway/version.rb,
lib/janeway/ast/null.rb,
lib/janeway/ast/error.rb,
lib/janeway/ast/query.rb,
lib/janeway/functions.rb,
lib/janeway/ast/number.rb,
lib/janeway/ast/boolean.rb,
lib/janeway/ast/helpers.rb,
lib/janeway/interpreter.rb,
lib/janeway/ast/function.rb,
lib/janeway/ast/selector.rb,
lib/janeway/ast/root_node.rb,
lib/janeway/ast/expression.rb,
lib/janeway/ast/identifier.rb,
lib/janeway/ast/string_type.rb,
lib/janeway/functions/count.rb,
lib/janeway/functions/match.rb,
lib/janeway/functions/value.rb,
lib/janeway/ast/current_node.rb,
lib/janeway/functions/length.rb,
lib/janeway/functions/search.rb,
lib/janeway/ast/child_segment.rb,
lib/janeway/ast/name_selector.rb,
lib/janeway/ast/index_selector.rb,
lib/janeway/ast/unary_operator.rb,
lib/janeway/ast/binary_operator.rb,
lib/janeway/ast/filter_selector.rb,
lib/janeway/ast/wildcard_selector.rb,
lib/janeway/ast/descendant_segment.rb,
lib/janeway/ast/array_slice_selector.rb

Overview

Defined Under Namespace

Modules: AST, Functions Classes: Error, Interpreter, Lexer, Parser, Token

Constant Summary collapse

OPERATORS =
{
  and: '&&',
  array_slice_separator: ':',
  child_end: ']',
  child_start: '[',
  current_node: '@',
  descendants: '..',
  dot: '.',
  equal: '==',
  filter: '?',
  greater_than: '>',
  greater_than_or_equal: '>=',
  group_end: ')',
  group_start: '(',
  less_than: '<',
  less_than_or_equal: '<=',
  minus: '-',
  not: '!',
  not_equal: '!=',
  or: '||',
  root: '$',
  union: ',',
  wildcard: '*',
}.freeze
ONE_CHAR_LEX =
OPERATORS.values.select { |lexeme| lexeme.size == 1 }.freeze
TWO_CHAR_LEX =
OPERATORS.values.select { |lexeme| lexeme.size == 2 }.freeze
TWO_CHAR_LEX_FIRST =
TWO_CHAR_LEX.map { |lexeme| lexeme[0] }.freeze
ONE_OR_TWO_CHAR_LEX =
ONE_CHAR_LEX & TWO_CHAR_LEX.map { |str| str[0] }.freeze
WHITESPACE =
" \t\n\r"
KEYWORD =
%w[true false null].freeze
FUNCTIONS =
%w[length count match search value].freeze
ALPHABET =

faster to check membership in a string than an array of char (benchmarked ruby 3.1.2)

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
DIGITS =
'0123456789'
NAME_FIRST =

chars that may be used as the first letter of member-name-shorthand

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'
VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.compile(query) ⇒ Janeway::AST::Query

Compile a JsonPath query into an Abstract Syntax Tree.

This can be used and re-used later on multiple inputs.

Parameters:

  • query (String)

    jsonpath query

Returns:



30
31
32
# File 'lib/janeway.rb', line 30

def self.compile(query)
  Janeway::Parser.parse(query)
end

.find_all(query, input) ⇒ Array

Apply a JsonPath query to the input, and return the result.

Parameters:

  • query (String)

    jsonpath query

  • input (Object)

    ruby object to be searched

Returns:

  • (Array)

    all matched objects



19
20
21
22
# File 'lib/janeway.rb', line 19

def self.find_all(query, input)
  ast = compile(query)
  Janeway::Interpreter.new(ast).interpret(input)
end