fix newline
[enigma2.git] / lib / actions / parseactions.py
1 # takes a header file, outputs action ids
2
3 import token, tokenize, os, sys, string
4
5 def filter(g):
6         while 1:
7                 t = g.next()
8                 if t[1] == "/*":
9                         while g.next()[1] != "*/":
10                                 pass
11                         continue
12                 if t[1] == "//":
13                         while g.next()[1] != "\n":
14                                 pass
15                         continue
16                 
17                 if t[1] != "\n":
18 #                       print t
19                         yield t[1]
20
21 def do_file(f, mode):
22         tokens = filter(tokenize.generate_tokens(open(f, 'r').readline))
23         
24         sys.stderr.write("parsing %s\n" % f)
25         
26         state = 0
27         
28         classstate = 0
29         
30         firsthit = 1
31         
32         while 1:
33                 try:
34                         t = tokens.next()
35                 except:
36                         break
37                 
38                 if t == "class":
39                         classname = tokens.next()
40                         classstate = state
41                 
42                 if t == "{":
43                         state = state + 1
44                 
45                 if t == "}":
46                         state = state - 1
47                 
48                 if t == "enum" and state == classstate + 1:
49                         actionname = tokens.next()
50                         
51                         if actionname[-7:] == "Actions":
52                                 if tokens.next() != "{":
53                                         try:
54                                                 print classname
55                                         except:
56                                                 pass
57                                 
58                                         try:
59                                                 print actionname
60                                         except:
61                                                 pass
62                                 
63                                         raise "action enum must be simple."
64                         
65                                 counter = 0
66                         
67                                 while 1:
68         
69                                         t = tokens.next()
70                                         
71                                         if t == "=":
72                                                 tokens.next()
73                                                 t = tokens.next()
74         
75                                         if t == "}":
76                                                 break
77                                         
78                                         if counter:
79                                                 if t != ",":
80                                                         raise "no comma"
81                                                 t = tokens.next()
82                                 
83                                         if firsthit:
84
85                                                 if mode == "include":
86                                                         # hack hack hack!!
87                                                         print "#include <lib" + f[2:] + ">"
88                                                 else:
89                                                         print "\t// " + f
90
91                                                 firsthit = 0
92
93                                         if mode == "parse":
94                                                 print "{\"" + actionname + "\", \"" + t + "\", " + string.join((classname, t), "::") + "},"
95
96
97                                         counter = counter + 1
98
99 mode = sys.argv[1]
100
101 if mode == "parse":
102         print """
103         /* generated by parseactions.py - do not modify! */
104 struct eActionList
105 {
106         const char *m_context, *m_action;
107         int m_id;
108 } actions[]={"""
109
110 for x in sys.argv[2:]:
111         do_file(x, mode)
112
113 if mode == "parse":
114         print "};"