Class: Antlr4::Runtime::VocabularyImpl

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

Constant Summary collapse

EMPTY_VOCABULARY =
VocabularyImpl.new(@@empty_names, @@empty_names, @@empty_names)
@@empty_names =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vocabulary

#max_token_type

Constructor Details

#initialize(literal_names, symbolic_names, display_names = nil) ⇒ VocabularyImpl



6
7
8
9
10
11
12
# File 'lib/antlr4/runtime/vocabulary_impl.rb', line 6

def initialize(literal_names, symbolic_names, display_names = nil)
  @literal_names = !literal_names.nil? ? literal_names : @@empty_names
  @symbolic_names = !symbolic_names.nil? ? symbolic_names : @@empty_names
  @display_names = !display_names.nil? ? display_names : @@empty_names
  # See note here on -1 part: https:#github.com/antlr/antlr4/pull/1146
  @max_token_type = [@display_names.length, @literal_names.length, @symbolic_names.length].max - 1
end

Class Method Details

.from_token_names(token_names) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/antlr4/runtime/vocabulary_impl.rb', line 16

def self.from_token_names(token_names)
  return EMPTY_VOCABULARY if token_names.nil? || token_names.empty?

  @literal_names = Array.new(token_names)
  @symbolic_names = Array.new(token_names)
  i = 0
  while i < token_names.length
    token_name = token_names[i]
    if token_name.nil?
      i += 1
      next
    end

    unless token_name.empty?
      first_char = token_name[0]
      if first_char == '\''
        @symbolic_names[i] = nil
        i += 1
        next
      end
    end

    # wasn't a literal or symbolic name
    @literal_names[i] = nil
    @symbolic_names[i] = nil
    i += 1
  end

  VocabularyImpl.new(@literal_names, @symbolic_names, token_names)
end

Instance Method Details

#display_name(token_type) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/antlr4/runtime/vocabulary_impl.rb', line 65

def display_name(token_type)
  if token_type >= 0 && token_type < @display_names.length
    disp_name = @display_names[token_type]
    return disp_name unless disp_name.nil?
  end

  lit_name = literal_name(token_type)
  return lit_name unless lit_name.nil?

  sym_name = symbolic_name(token_type)
  return sym_name unless sym_name.nil?

  Integer.to_s(token_type)
end

#literal_name(token_type) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/antlr4/runtime/vocabulary_impl.rb', line 47

def literal_name(token_type)
  if token_type >= 0 && token_type < @literal_names.length
    return @literal_names[token_type]
  end

  nil
end

#symbolic_name(token_type) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/antlr4/runtime/vocabulary_impl.rb', line 55

def symbolic_name(token_type)
  if token_type >= 0 && token_type < @symbolic_names.length
    return @symbolic_names[token_type]
  end

  return 'EOF' if token_type == Token::EOF

  token_type.to_s
end