aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/xkeyboard-config/tests/ruby/xkbparser.rb
blob: 4b15df3d26d8e797d6df72695b7a6607ab0388bf (plain)
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
183
184
185
#
# $Id$
#
# Commont parsing classes for symbols/inet
# The parsing is simplified, based on regex - it is NOT a real parser for very
# complex XKB format
#

require "utils.rb"

class Symbols < Hash

  #
  # Constructor
  #
  def initialize
    @includedSyms = Array.new
  end

  # Write-only property, parent list of symbols definitions
  def symbols_list=(symbolsList)
    @symbolsList = symbolsList
  end

  # Whether this set of symbols is hidden or not
  def hidden?
    @hidden
  end

  def hidden=(h)
    @hidden = h
  end

  #
  # Add "dependency" - the symbols referenced using the "include" statement.
  #
  def add_included(other)
    @includedSyms.push(other)
  end

  alias get_original []
  alias keys_original keys

  #
  # Get the symbol, trying first own definitions, then walking through all 
  # dependenies
  #
  def [](symName)
    own = self.get_original(symName)
    if own.nil?
      @includedSyms.find_all do | symsName |
        syms = @symbolsList[symsName]
        his = syms[symName]
        if !his.nil?
          own = his
          break
        end
      end
    end
    own
  end

  #
  # All keys - including the ones specified in the included sections
  #
  def keys()
    @includedSyms.inject(keys_original) do | rv, symsName |
      syms = @symbolsList[symsName]
      rv | syms.keys
    end
  end

  # Size of all keys
  def length()
    keys().length()
  end

  #
  # Size - takes into account overlapping key definitions
  #
  def size()
    keys.size()
  end

  #
  # Create a hash including all elements of this hash which are not in the
  # other hash, use symbols + and * for marking the elements which existed in
  # the original hash (+ if not existed)
  #
  def -(other)
    diff = self.class.new
    self.find_all do | key, value | 
      existing = other[key]
      if existing != value
        diff[key] = [ value, existing.nil? ? '+' : '' ]
      end 
    end
    diff
  end


  def to_s
    s = "{\n"
    # First output included syms
    @includedSyms.find_all do | symsName |
       s += "  include \"inet(#{symsName})\"\n"
    end
    # Then - own definitions
    self.find_all do | key, value |
       s += "  key #{key} { [ #{value} ] };\n"
    end
    s + "}";
  end

end

class SymbolsList < Hash

  #
  # Add new xkb_symbols
  #
  def add_symbols (symbolsName, hidden)
    newSyms = Symbols.new
    newSyms.symbols_list = self
    newSyms.hidden = hidden
    self[symbolsName] = newSyms
  end

  def to_s
    s = "// Autogenerated\n\n"
    self.find_all do | symbols, mapping |
      s += "partial alphanumeric_keys\nxkb_symbols \"#{symbols}\" #{mapping};\n\n" 
    end
    s
  end

  def match_symbols(new_symbols,limit)
    matching = Hash.new
    find_all do | symbols, mapping |
      diff = new_symbols - mapping
      if diff.size <= limit
        matching[symbols] = diff
      end
    end
    matching
  end

  def merge()
    everything = NonuniqueCountingHash.new
    find_all do | symsName, syms |
      syms.find_all do | symName, keycode |
        everything[symName] = keycode
      end
    end
    everything
  end

end

class Parser

  def parse (fileName)
    allSyms = SymbolsList.new;
    currentSyms = nil
    hidden = false
    File.open(fileName) do | file |
      file.each_line do | line |
        line.scan(/xkb_symbols\s+"(\w+)"/) do | symsName |
          currentSyms = allSyms.add_symbols(symsName[0], hidden)
        end
        line.scan(/^\s*key\s*<(\w+)>\s*\{\s*\[\s*(\w+)/) do | keycode, keysym |
          currentSyms[keycode] = keysym
        end
        line.scan(/^partial\s+(hidden\s+)?alphanumeric_keys/) do | h |
          hidden = !h[0].nil?
        end
        line.scan(/^\s*include\s+"inet\((\w+)\)"/) do | otherPart |
          currentSyms.add_included(otherPart[0])
        end
      end
    end
    allSyms
  end

end