Class: Antlr4::Runtime::SemanticContext::AND

Inherits:
Operator show all
Defined in:
lib/antlr4/runtime/semantic_context.rb

Constant Summary

Constants inherited from Antlr4::Runtime::SemanticContext

NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Antlr4::Runtime::SemanticContext

and, filter_precedence_predicates, or

Constructor Details

#initialize(a, b) ⇒ AND

Returns a new instance of AND.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/antlr4/runtime/semantic_context.rb', line 98

def initialize(a, b)
  operands = Set.new
  if a.is_a? AND
    operands.add_all(a.opnds)
  else
    operands.add(a)
  end
  if b.is_a? AND
    operands.add_all(b.opnds)
  else
    operands.add(b)
  end
  precedence_predicates = filter_precedence_predicates(operands)
  unless precedence_predicates.empty?
    # interested in the transition with the lowest precedence
    reduced = precedence_predicates.min
    operands.add(reduced)
  end

  @opnds = operands.to_a
end

Instance Attribute Details

#opndsObject

Returns the value of attribute opnds.



96
97
98
# File 'lib/antlr4/runtime/semantic_context.rb', line 96

def opnds
  @opnds
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
# File 'lib/antlr4/runtime/semantic_context.rb', line 120

def eql?(other)
  return true if self == other
  return false unless other.is_a? AND

  @opnds.eql?(other.opnds)
end

#eval(parser, parser_call_stack) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/antlr4/runtime/semantic_context.rb', line 139

def eval(parser, parser_call_stack)
  i = 0
  while i < @opnds.length
    opnd = @opnds[i]
    return false unless opnd.eval(parser, parser_call_stack)
    i += 1
  end
  true
end

#eval_precedence(parser, parser_call_stack) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/antlr4/runtime/semantic_context.rb', line 149

def eval_precedence(parser, parser_call_stack)
  differs = false
  operands = []
  i = 0
  while i < @opnds.length
    context = @opnds[i]
    evaluated = context.eval_precedence(parser, parser_call_stack)
    differs |= (evaluated != context)
    if evaluated == null
      # The AND context is false if any element is false
      return nil
    elsif evaluated != NONE
      # Reduce the result by skipping true elements
      operands.add(evaluated)
    end
    i += 1
  end

  return self unless differs

  if operands.empty?
    # all elements were true, so the AND context is true
    return NONE
  end

  result = operands[0]
  i = 1
  while i < operands.length
    result = SemanticContext.and(result, operands.get(i))
    i += 1
  end

  result
end

#hashObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/antlr4/runtime/semantic_context.rb', line 127

def hash
  hash_code = RumourHash.calculate(@opnds, AND.hash)
  unless @_hash2.nil?
    if hash_code == @_hash2
      puts 'Same hash_code for SemanticContext_2'
    else
      puts 'Different hash_code for SemanticContext_2'
    end
  end
  @_hash2 = hash_code
end

#to_sObject



184
185
186
# File 'lib/antlr4/runtime/semantic_context.rb', line 184

def to_s
  @opnds.join('&&')
end