7 import xml.etree.ElementTree
10 def get_text(root, i):
13 text = root.text if i == 0 else root[i - 1].tail
14 return '' if text is None else text
16 def set_text(root, i, text):
24 root[i - 1].tail = text
30 for i in range(len(root))
31 for j in [get_text(root, i), to_text(root[i])]
33 [get_text(root, len(root))]
36 def c_to_python(context, text):
38 lex_yy.yy_buffer_stack = [lex_yy.YYBufferState()]
41 root = y_tab.yyparse(t_def.AST.TranslationUnit)
43 root.translate_translation_unit(context)
44 return ''.join(context.lines)
46 root = xml.etree.ElementTree.parse(sys.stdin).getroot()
48 context = t_def.Context()
51 with open('a.c', 'w') as fout:
53 if i.tag == 'AST_Section1_Prologue' or i.tag == 'AST_BracedCode':
55 assert node.tag == 'AST_Text'
58 # we won't put code on same line as %{, though it's legal as input
62 '{0:s}\n'.format(get_text(i, 0).rstrip())
65 i.tag == 'AST_Section1_InitialAction' or
66 i.tag == 'AST_Section1Or2_CodeProps'
69 assert node.tag == 'AST_Text'
72 elif i.tag == 'AST_Production_Action':
74 assert node.tag == 'AST_Text'
77 elif i.tag == 'AST_Section3':
86 get_text(i, 0).lstrip()
88 for j in range(1, len(i)):
92 '\n\n{0:s}\n\n'.format(get_text(i, j).strip())
97 get_text(i, len(i)).lstrip()
99 elif i.tag == 'AST_Section2':
103 get_text(i, 0).lstrip()
105 for j in range(1, len(i)):
109 '\n\n{0:s}'.format(get_text(i, j).lstrip())
114 get_text(i, len(i)).lstrip()
116 elif i.tag == 'AST_Section2_Rules':
120 get_text(i, 0).lstrip()
122 # deal with <AST_SymbolRef><AST_ID>...</AST_ID> :</AST_SymbolRef>
123 assert i[0].tag == 'AST_SymbolRef'
127 '\n ' + get_text(i[0], 1).strip()
132 get_text(i, 1).strip()
134 for j in range(2, len(i) + 1):
138 '\n {0:s}'.format(get_text(i, j).strip())
140 elif i.tag == 'AST_Production':
141 for j in range(len(i)):
146 '\n {0:s}'.format(get_text(i, j).lstrip())
148 i[j].tag == 'AST_Production_Action' or
149 (j > 0 and i[j - 1].tag == 'AST_Production_Action')
151 ' {0:s}'.format(get_text(i, j).lstrip())
157 get_text(i, len(i)).lstrip()
162 #assert len(node) == 0
163 #text = get_text(node, 0)
166 lines = [i.rstrip() for i in text.split('\n')]
167 while len(lines) and len(lines[-1]) == 0:
169 while len(lines) and len(lines[0]) == 0:
174 (temp[:10] == '#include <' and temp[-3:] == '.h>') or
175 (temp[:10] == '#include "' and temp[-3:] == '.h"')
184 @@@ IMPORT END\n'''.format(
185 temp[10:-3].replace('/', '.'),
190 fout.write(line + '\n')
193 actions.append((node, indent, initial))
197 'gcc{0:s} -E a.c >a.i'.format(
198 ''.join([' "{0:s}"'.format(i) for i in sys.argv[1:]])
201 with open('a.i') as fin:
202 for node, indent, initial in actions:
204 line = fin.readline()
205 while line != '@@@\n':
209 (line == '\n' and len(lines) and lines[-1] == '\n')
212 elif line[:11] == '@@@ IMPORT(' and line[-2:] == ')\n':
213 # make the importing look like a function call in the C code:
214 #lines.append('import("{0:s}");\n'.format(line[11:-2]))
215 line = fin.readline()
216 while line != '@@@ IMPORT END\n':
218 line = fin.readline()
221 line = fin.readline()
222 text = ''.join(lines)
225 context.indent = indent
226 text = c_to_python(context, text)
228 context.indent = indent
231 'void a(void) {0:s}'.format(text) # already has braces and \n
233 assert text[:len(indent) + 9] == '{0:s}def a():\n'.format(indent)
234 text = '{{\n{0:s}{1:s}}}'.format(text[len(indent) + 9:], indent)
236 set_text(node, 0, text)
238 xml.etree.ElementTree.ElementTree(root).write(
240 encoding = 'unicode' # strangely does not seem to default to this