Class: HumanQL::QueryGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/human-ql/query_generator.rb

Overview

Generate a Human Query Language String from an abstract syntax tree (AST). This allows query simplification (e.g. via TreeNormalizer) and re-writing queries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ QueryGenerator

Construct given options which are interpreted as attribute names to set.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/human-ql/query_generator.rb', line 80

def initialize( opts = {} )
  @and = ' and '.freeze
  @or = ' or '.freeze
  @not = '-'.freeze
  @space = ' '.freeze
  @lquote = @rquote = '"'.freeze
  @colon = ':'.freeze
  @lparen = '('.freeze
  @rparen = ')'.freeze
  @default_op = nil
  @precedence = nil

  opts.each do |name,val|
    send( name.to_s + '=', val )
  end
end

Instance Attribute Details

#andObject

The AND operator (if not default). Default: ‘ and ’



26
27
28
# File 'lib/human-ql/query_generator.rb', line 26

def and
  @and
end

#colonObject

COLON character used a prefix delimiter. Default: ‘:’



50
51
52
# File 'lib/human-ql/query_generator.rb', line 50

def colon
  @colon
end

#default_opObject

The default operator (:and or :or). If set, will output a :space instead of the operator. Default: nil



63
64
65
# File 'lib/human-ql/query_generator.rb', line 63

def default_op
  @default_op
end

#lparenObject

Left parenthesis character. Default: ‘(’



54
55
56
# File 'lib/human-ql/query_generator.rb', line 54

def lparen
  @lparen
end

#lquoteObject

Left quote character for phrases. Default: ‘“’



42
43
44
# File 'lib/human-ql/query_generator.rb', line 42

def lquote
  @lquote
end

#notObject

The NOT operator. Default: ‘-’



34
35
36
# File 'lib/human-ql/query_generator.rb', line 34

def not
  @not
end

#orObject

The OR operator (if not default). Default: ‘ or ’



30
31
32
# File 'lib/human-ql/query_generator.rb', line 30

def or
  @or
end

#precedenceObject

Hash of operators to precedence integer values, as per QueryParser#precedence. If set, outputs parentheses only when precedence dictates that it is necessary. Default: nil



69
70
71
# File 'lib/human-ql/query_generator.rb', line 69

def precedence
  @precedence
end

#rparenObject

Right parenthesis character. Default: ‘)’



58
59
60
# File 'lib/human-ql/query_generator.rb', line 58

def rparen
  @rparen
end

#rquoteObject

Right quote character for phrases. Default: ‘“’



46
47
48
# File 'lib/human-ql/query_generator.rb', line 46

def rquote
  @rquote
end

#spaceObject

SPACE delimiter. Default: ‘ ’



38
39
40
# File 'lib/human-ql/query_generator.rb', line 38

def space
  @space
end

Instance Method Details

#generate(node) ⇒ Object

Given the root node of the AST, return a String in Human Query Language syntax.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/human-ql/query_generator.rb', line 99

def generate( node )
  op,*args = node
  if ! node.is_a?( Array )
    op
  elsif args.empty?
    nil
  else
    case op
    when :and
      terms_join( args, :and )
    when :or
      terms_join( args, :or )
    when :not
      @not + pwrap_gen( args[0], op )
    when :phrase
      @lquote + args.join( @space ) + @rquote
    when String
      op + @colon + pwrap_gen( args[0], op )
    else
      raise "Unsupported op: #{node.inspect}"
    end
  end
end

#parser=(p) ⇒ Object

Set #default_op and #precedence from the given QueryParser, as a convenience.



73
74
75
76
# File 'lib/human-ql/query_generator.rb', line 73

def parser=( p )
  @default_op = p.default_op
  @precedence = p.precedence
end