Class: Antlr4::Runtime::DFASerializer

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

Direct Known Subclasses

LexerDFASerializer

Instance Method Summary collapse

Instance Method Details

#edge_label(i) ⇒ Object


40
41
42
# File 'lib/antlr4/runtime/dfa_serializer.rb', line 40

def edge_label(i)
  @vocabulary.display_name(i - 1)
end

#init_from_token_names(dfa, token_names) ⇒ Object


4
5
6
# File 'lib/antlr4/runtime/dfa_serializer.rb', line 4

def init_from_token_names(dfa, token_names)
  init_from_vocabulary(dfa, VocabularyImpl.from_token_names(token_names))
end

#init_from_vocabulary(dfa, vocabulary) ⇒ Object


8
9
10
11
# File 'lib/antlr4/runtime/dfa_serializer.rb', line 8

def init_from_vocabulary(dfa, vocabulary)
  @dfa = dfa
  @vocabulary = vocabulary
end

#state_string(s) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/antlr4/runtime/dfa_serializer.rb', line 44

def state_string(s)
  n = s.state_number
  base_state_str = (s.is_accept_state ? ':' : '') << 's' << n.to_s << (s.requires_full_context ? '^' : '')
  if s.is_accept_state
    if !s.predicates.nil?
      preds = ''
      s.predicates.each do |p|
        preds << p.to_s
      end

      return base_state_str << '=>' << preds
    else
      return base_state_str << '=>' << @vocabulary.symbolic_name(s.prediction)
    end
  end
  base_state_str
end

#to_sObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/antlr4/runtime/dfa_serializer.rb', line 13

def to_s
  return nil if @dfa.s0.nil?

  buf = ''
  states = @dfa.get_states
  states.each do |s|
    n = 0
    n = s.edges.length unless s.edges.nil?
    i = 0
    while i < n
      t = s.edges[i]
      if !t.nil? && t.state_number != Integer::MAX
        buf << state_string(s)
        label = edge_label(i)
        buf << '-' << label << '->' << state_string(t) << '\n'
      end
      i += 1
    end
  end

  output = buf
  return '' if output.empty?

  # return Utils.sortLinesInString(output)
  output
end