forked from simos/keyboardlayouteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_tree.py
70 lines (55 loc) · 1.82 KB
/
print_tree.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Version 0.7
import sys
import pdb
import antlr3
from XKBGrammarLexer import XKBGrammarLexer, LAYOUT, SYMBOLS, MAPTYPE, MAPNAME, MAPOPTIONS, MAPMATERIAL, TOKEN_INCLUDE, TOKEN_NAME, TOKEN_KEY_TYPE, TOKEN_KEY, VALUE, KEYCODE, KEYCODEX
from XKBGrammarParser import XKBGrammarParser
from XKBGrammarWalker import XKBGrammarWalker
# Helper function to iterate through all children of a given type
def getChildrenByType(tree, type_value):
for i in range(tree.getChildCount()):
child = tree.getChild(i)
if child.getType() == type_value:
yield child
# Helper function to iterate through all children of a given type
def getChildrenListByType(tree, type_value):
list = []
for i in range(tree.getChildCount()):
child = tree.getChild(i)
if child.getType() == type_value:
list.append(child)
return list
xkbfilename = "gr"
if len(sys.argv) > 1:
xkbfilename = sys.argv[1]
try:
xkbfile = open(xkbfilename, 'r')
except OSError:
print "Could not open file ", xkbfilename, ". Aborting..."
sys.exit(-1)
xkbfile.close
# char_stream = antlr3.ANTLRFileStream(xkbfilename, encoding='utf-8')
char_stream = antlr3.ANTLRFileStream(xkbfilename)
lexer = XKBGrammarLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = XKBGrammarParser(tokens)
result = parser.layout()
print "XXXXXXXXXXXXXXXXXXXXXXX", xkbfilename
print "tree =", result.tree.toStringTree()
nodes = antlr3.tree.CommonTreeNodeStream(result.tree)
nodes.setTokenStream(tokens)
walker = XKBGrammarWalker(nodes)
# walker.layout()
MAX = 10
TABS = "\t\t\t\t\t\t\t\t\t\t"
def print_tree(node, depth):
if depth >= MAX:
return
for n in node.getChildren():
print TABS[:depth], "===", n.getText(), "==="
print_tree(n, depth + 1)
print result.tree.getChild(0).getText()
print
print_tree(result.tree, 0)