From a923a4bd1e9273bda4272fe14c78309801a37e20 Mon Sep 17 00:00:00 2001 From: svu Date: Sun, 22 Oct 2006 12:31:59 +0000 Subject: [PATCH] some Ruby stuff, just my little toys --- tests/ruby/README | 3 + tests/ruby/find_match.rb | 30 ++++++++ tests/ruby/xkbparser.rb | 150 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tests/ruby/README create mode 100644 tests/ruby/find_match.rb create mode 100644 tests/ruby/xkbparser.rb diff --git a/tests/ruby/README b/tests/ruby/README new file mode 100644 index 0000000..b1198b2 --- /dev/null +++ b/tests/ruby/README @@ -0,0 +1,3 @@ +This is just some stuff to play with symbols/inet file, trying to analize it. +Only maintainers might be interested. It is written in Ruby - but it will +never be actually used in xkeyboard-config distribution. diff --git a/tests/ruby/find_match.rb b/tests/ruby/find_match.rb new file mode 100644 index 0000000..4a80691 --- /dev/null +++ b/tests/ruby/find_match.rb @@ -0,0 +1,30 @@ +require "xkbparser.rb" + +basedir = "../.." + +parser = Parser.new + +allSyms = parser.parse("#{basedir}/symbols/inet") + +newSyms = parser.parse(ARGV[0]) +limit = ARGV[1].to_i + +newSyms.find_all do | key, value | + + if value.hidden? + next + end + + puts "Existing xkb_symbols matching #{key}: " + + sorted = allSyms.match_symbols(value,limit).sort_by do | symsName, diff | + sprintf "%03d_%s", diff.size, symsName + end + + sorted.find_all do | symsName, diff | + puts " #{symsName} (difference #{diff.size})-> #{diff}" + end + +end + + diff --git a/tests/ruby/xkbparser.rb b/tests/ruby/xkbparser.rb new file mode 100644 index 0000000..27eb258 --- /dev/null +++ b/tests/ruby/xkbparser.rb @@ -0,0 +1,150 @@ +# +# $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 +# + +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 [] + + # + # 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 + + # + # 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 + +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 -- 2.32.0.93.g670b81a890