Class: Janeway::Parser
- Inherits:
-
Object
- Object
- Janeway::Parser
- Includes:
- Functions
- Defined in:
- lib/janeway/parser.rb
Overview
Transform a list of tokens into an Abstract Syntax Tree
Defined Under Namespace
Classes: Error
Constant Summary collapse
- UNARY_OPERATORS =
%w[! -].freeze
- BINARY_OPERATORS =
%w[== != > < >= <= ,].freeze
- LOGICAL_OPERATORS =
%w[&& ||].freeze
- LOWEST_PRECEDENCE =
0- PREFIX_PRECEDENCE =
7- OPERATOR_PRECEDENCE =
{ ',' => 0, '||' => 1, '&&' => 2, '==' => 3, '!=' => 3, '>' => 4, '<' => 4, '>=' => 4, '<=' => 4, '(' => 8, }.freeze
Instance Attribute Summary collapse
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(tokens, jsonpath) ⇒ Parser
constructor
A new instance of Parser.
-
#parse ⇒ AST::Query
Parse the token list and create an Abstract Syntax Tree.
Methods included from Functions
#parse_function_count, #parse_function_length, #parse_function_match, #parse_function_parameter, #parse_function_search, #parse_function_value, #translate_iregex_to_ruby_regex
Constructor Details
#initialize(tokens, jsonpath) ⇒ Parser
Returns a new instance of Parser.
46 47 48 49 50 |
# File 'lib/janeway/parser.rb', line 46 def initialize(tokens, jsonpath) @tokens = tokens @next_p = 0 @jsonpath = jsonpath end |
Instance Attribute Details
#tokens ⇒ Object
Returns the value of attribute tokens.
11 12 13 |
# File 'lib/janeway/parser.rb', line 11 def tokens @tokens end |
Class Method Details
Instance Method Details
#parse ⇒ AST::Query
Parse the token list and create an Abstract Syntax Tree
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/janeway/parser.rb', line 54 def parse consume raise err('JsonPath queries must start with root identifier "$"') unless current.type == :root root_node = parse_expr_recursively consume unless current.type == :eof remaining = tokens[@next_p..].map(&:lexeme).join raise err("Unrecognized expressions after query: #{remaining}") end # Freeze so this can be used in ractors AST::Query.new(root_node, @jsonpath).freeze end |