Class: DFASerializer

Inherits:
Object show all
Defined in:
lib/antlr4/dfa/DFASerializer.rb

Overview

A DFA walker that knows how to dump them to serialized strings.

Direct Known Subclasses

LexerDFASerializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dfa, tokenNames = nil) ⇒ DFASerializer

Returns a new instance of DFASerializer.



6
7
8
9
# File 'lib/antlr4/dfa/DFASerializer.rb', line 6

def initialize(dfa, tokenNames=nil)
    @dfa = dfa
    @tokenNames = tokenNames
end

Instance Attribute Details

#dfaObject

Returns the value of attribute dfa.



5
6
7
# File 'lib/antlr4/dfa/DFASerializer.rb', line 5

def dfa
  @dfa
end

#tokenNamesObject

Returns the value of attribute tokenNames.



5
6
7
# File 'lib/antlr4/dfa/DFASerializer.rb', line 5

def tokenNames
  @tokenNames
end

Instance Method Details

#getEdgeLabel(i) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/antlr4/dfa/DFASerializer.rb', line 41

def getEdgeLabel(i)
    return "EOF" if i==0
    if not self.tokenNames.nil? then
        return self.tokenNames[i-1]
    else
        return (i-1).to_s
    end
end

#getStateString(s) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/antlr4/dfa/DFASerializer.rb', line 50

def getStateString(s)
#        s_acceptState = nil
#        s_acceptState = ":" if s.isAcceptState 
    s_requireContext = nil
    s_requireContext = "^" if s.requiresFullContext 
    baseStateStr = "s#{s.stateNumber}#{s_requireContext}"
    if s.isAcceptState then
        if not s.predicates.nil? then 
            return ":#{baseStateStr}=>#{s.predicates}"
        else
            return ":#{baseStateStr}=>#{s.prediction}"
        end
    else
        return baseStateStr
    end
end

#to_sObject



11
12
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
39
# File 'lib/antlr4/dfa/DFASerializer.rb', line 11

def to_s
    return nil if self.dfa.s0.nil? 
    StringIO.open do |buf|
        self.dfa.sortedStates().each do |s|
            n = 0
            if not s.edges.nil? 
                n = s.edges.length
            end
            for i in 0..(n-1) do 
                t = s.edges[i]
                if not t.nil? and t.stateNumber != 0x7FFFFFFF then
                    buf.write(self.getStateString(s))
                    label = self.getEdgeLabel(i)
                    buf.write("-")
                    buf.write(label)
                    buf.write("->")
                    buf.write(self.getStateString(t))
                    buf.write("\n")
                end
            end
        end
        output = buf.string()
        if output.length == 0
            return nil
        else
            return output
        end
    end
end