Module: CommandSearch::Optimizer
- Defined in:
- lib/command_search/optimizer.rb
Class Method Summary collapse
- .ands_and_ors!(ast) ⇒ Object
- .denest_parens!(ast, parent_type = :root) ⇒ Object
- .negate_negate!(ast) ⇒ Object
- .optimize(ast) ⇒ Object
- .remove_empty_strings!(ast) ⇒ Object
Class Method Details
.ands_and_ors!(ast) ⇒ Object
| 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # File 'lib/command_search/optimizer.rb', line 5 def ands_and_ors!(ast) ast.map! do |node| next node unless node[:nest_type] == :paren || node[:nest_type] == :pipe ands_and_ors!(node[:value]) next node[:value].first if node[:value].length < 2 node[:value].map! do |kid| next kid[:value] if kid[:nest_type] == :pipe kid end node[:value].flatten! node[:value].uniq! node end end | 
.denest_parens!(ast, parent_type = :root) ⇒ Object
| 33 34 35 36 37 38 39 40 41 42 43 44 | # File 'lib/command_search/optimizer.rb', line 33 def denest_parens!(ast, parent_type = :root) ast.map! do |node| next node unless node[:nest_type] denest_parens!(node[:value], node[:nest_type]) # valid_self && (valid_parent || valid_child) if node[:nest_type] == :paren && (parent_type != :pipe || node[:value].count < 2) next node[:value] end node end ast.flatten! end | 
.negate_negate!(ast) ⇒ Object
| 20 21 22 23 24 25 26 27 28 29 30 31 | # File 'lib/command_search/optimizer.rb', line 20 def negate_negate!(ast) ast.map! do |node| next node unless node[:nest_type] negate_negate!(node[:value]) next [] if node[:value] == [] type = node[:nest_type] child_type = node[:value].first[:nest_type] next node unless type == :minus && child_type == :minus node[:value].first[:value] end ast.flatten! end | 
.optimize(ast) ⇒ Object
| 53 54 55 56 57 58 59 60 | # File 'lib/command_search/optimizer.rb', line 53 def optimize(ast) denest_parens!(ast) remove_empty_strings!(ast) negate_negate!(ast) ands_and_ors!(ast) ast.uniq! ast end | 
.remove_empty_strings!(ast) ⇒ Object
| 46 47 48 49 50 51 | # File 'lib/command_search/optimizer.rb', line 46 def remove_empty_strings!(ast) ast.reject! do |node| remove_empty_strings!(node[:value]) if node[:nest_type] node[:type] == :quoted_str && node[:value] == '' end end |