Class: Antlr4::Runtime::PredictionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/prediction_context.rb

Constant Summary collapse

INITIAL_HASH =
1
EMPTY_RETURN_STATE =
Integer::MAX
@@global_node_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cached_hash_code) ⇒ PredictionContext

Returns a new instance of PredictionContext.



13
14
15
16
17
# File 'lib/antlr4/runtime/prediction_context.rb', line 13

def initialize(cached_hash_code)
  @id = @@global_node_count
  @@global_node_count += 1
  @cached_hash_code = cached_hash_code
end

Instance Attribute Details

#cached_hash_codeObject

Returns the value of attribute cached_hash_code.



11
12
13
# File 'lib/antlr4/runtime/prediction_context.rb', line 11

def cached_hash_code
  @cached_hash_code
end

Instance Method Details

#empty_path?Boolean

since EMPTY_RETURN_STATE can only appear in the last position, we check last one

Returns:

  • (Boolean)


19
20
21
# File 'lib/antlr4/runtime/prediction_context.rb', line 19

def empty_path? # since EMPTY_RETURN_STATE can only appear in the last position, we check last one
  get_return_state(size - 1) == EMPTY_RETURN_STATE
end

#hashObject



23
24
25
# File 'lib/antlr4/runtime/prediction_context.rb', line 23

def hash
  @cached_hash_code
end

#to_s_recog(_recog) ⇒ Object



27
28
29
# File 'lib/antlr4/runtime/prediction_context.rb', line 27

def to_s_recog(_recog)
  to_s
end

#to_strings(recognizer, current_state) ⇒ Object



31
32
33
# File 'lib/antlr4/runtime/prediction_context.rb', line 31

def to_strings(recognizer, current_state)
  to_strings3(recognizer, EMPTY, current_state)
end

#to_strings3(_recognizer, _stop, _current_state) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/antlr4/runtime/prediction_context.rb', line 35

def to_strings3(_recognizer, _stop, _current_state)
  result = []

  while to_strings3_inner result

  end

  result
end

#to_strings3_inner(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/antlr4/runtime/prediction_context.rb', line 45

def to_strings3_inner(result)
  perm = 0
  while perm
    offset = 0
    last = true
    p = self
    state_number = current_state
    local_buffer = ''
    local_buffer << '['
    while !p.empty? && p != stop
      index = 0
      unless p.empty?
        bits = 1
        bits += 1 while (1 << bits) < p.size

        mask = (1 << bits) - 1
        index = (perm >> offset) & mask
        last &= index >= p.size - 1
        return true if index >= p.size

        offset += bits
      end

      if !recognizer.nil?
        if local_buffer.length > 1
          # first char is '[', if more than that this isn't the first rule
          local_buffer << ' '
        end

        atn = recognizer.getATN
        s = atn.states.get(state_number)
        ruleName = recognizer.rule_names[s.rule_index]
        local_buffer << ruleName
      elsif p.get_return_state(index) != EMPTY_RETURN_STATE
        unless p.empty?
          if local_buffer.length > 1
            # first char is '[', if more than that this isn't the first rule
            local_buffer << ' '
          end

          local_buffer << p.get_return_state(index)
        end
      end
      state_number = p.get_return_state(index)
      p = p.getParent(index)
    end
    local_buffer << ']'
    result.push(local_buffer.to_s)

    break if last

    perm += 1
  end
  false
end