Class: AndExpression

Inherits:
CompoundExpression show all
Defined in:
lib/glark/match/and.rb

Overview

Evaluates both expressions, and is satisfied when both return true.

Instance Attribute Summary

Attributes inherited from CompoundExpression

#ops

Attributes inherited from Expression

#matches

Instance Method Summary collapse

Methods inherited from CompoundExpression

#==, #reset_file, #start_position

Methods inherited from Expression

#add_match, #process, #reset_file, #start_position, #to_s

Constructor Details

#initialize(dist, op1, op2) ⇒ AndExpression

Returns a new instance of AndExpression.



10
11
12
13
# File 'lib/glark/match/and.rb', line 10

def initialize dist, op1, op2
  @dist = dist
  super op1, op2
end

Instance Method Details

#end_positionObject



55
56
57
# File 'lib/glark/match/and.rb', line 55

def end_position
  @ops.collect { |op| op.end_position }.max
end

#evaluate(line, lnum, file, formatter) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/glark/match/and.rb', line 59

def evaluate line, lnum, file, formatter
  if match? line, lnum, file, formatter
    @match_line_number = lnum
    true
  else
    false
  end
end

#explain(level = 0) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/glark/match/and.rb', line 68

def explain level = 0
  str = ""
  if @dist == 0
    str += " " * level + "on the same line:\n"
  elsif @dist.kind_of?(Float) && @dist.infinite?
    str += " " * level + "in the same file:\n"
  else 
    lnstr = @dist == 1 ? "line" : "lines"
    str += " " * level + "within #{@dist} #{lnstr} of each other:\n"
  end
  str += @ops[0].explain(level + 4)
  str += " " * level + "and\n"
  str += @ops[1].explain(level + 4)
  str
end

#inspectObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/glark/match/and.rb', line 19

def inspect
  str = "("+ @ops[0].to_s
  if @dist == 0
    str += " same line as "
  elsif @dist.kind_of?(Float) && @dist.infinite?
    str += " same file as "
  else 
    str += " within " + @dist.to_s + " lines of "
  end
  str += @ops[1].to_s + ")"
  str
end

#match?(line, lnum, file, formatter) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/glark/match/and.rb', line 32

def match? line, lnum, file, formatter
  matches = (0 ... @ops.length).select do |oi|
    @ops[oi].evaluate line, lnum, file, formatter
  end

  matches.each do |mi|
    oidx  = (1 + mi) % @ops.length
    other = @ops[oidx]
    if match_within_distance other, lnum
      # search for the maximum match within the distance limit
      other.matches.each do |m|
        if lnum - m <= @dist
          @last_start = m
          return true
        end
      end
      return false
    end
  end

  false
end

#match_within_distance(op, lnum) ⇒ Object



15
16
17
# File 'lib/glark/match/and.rb', line 15

def match_within_distance op, lnum
  op.matches.size > 0 and (op.matches[-1] - lnum <= @dist)
end