).getroot()
context = ast.Context()
-#context.translate_identifier['BEGIN'] = 'self.BEGIN'
-#context.translate_identifier['yylval'] = 'ref_data.yylval'
-#context.translate_identifier['yytext'] = 'self.yytext'
-#context.translate_identifier['yy_pop_state'] = 'self.yy_pop_state'
-#context.translate_identifier['yy_push_state'] = 'self.yy_push_state'
actions = []
with open('a.c', 'w') as fout:
if i.tag == 'AST_Section1Or2_CodeBlock':
node = i[0]
assert node.tag == 'AST_Text'
- indent += ' '
- initial = parent.tag == 'AST_Section1'
+ if parent.tag == 'AST_Section1':
+ indent += ' '
+ initial = True
+ else:
+ initial = False
elif i.tag == 'AST_Section2_Rule_Action':
if len(i) == 0: # continued actions
assert parent.tag == 'AST_Section2_Rule'
else:
child_indent = indent
if i.tag == 'AST':
- for j in range(1, len(i) + 1):
- element.set_text(i, j, element.get_text(i, j).rstrip() + '\n')
+ element.set_text(
+ i,
+ 0,
+ element.get_text(i, 0).lstrip()
+ )
+ for j in range(1, len(i)):
+ element.set_text(
+ i,
+ j,
+ '{0:s}\n{1:s}'.format(
+ element.get_text(i, j).strip(),
+ '\n' if j == 2 else ''
+ )
+ )
+ element.set_text(
+ i,
+ len(i),
+ element.get_text(i, len(i)).lstrip()
+ )
elif i.tag == 'AST_Section1' or i.tag == 'AST_Section2':
# kludge, concatenate single line codeblocks to see overall meaning,
# hopefully we can make the scanner do this itself in Python version
while j < len(i):
if i[j].tag == 'AST_Section1Or2_CodeBlock':
k = j + 1
- while k < len(i) and i[k].tag == 'AST_Section1Or2_CodeBlock':
+ pre_delimiter = element.get_text(i[j], 0)
+ post_delimiter = element.get_text(i[j], 1)
+ while (
+ k < len(i) and
+ len(element.get_text(i, k).strip()) == 0 and
+ i[k].tag == 'AST_Section1Or2_CodeBlock' and
+ element.get_text(i[k], 0) == pre_delimiter and
+ element.get_text(i[k], 1) == post_delimiter
+ ):
k += 1
element.set_text(
i[k - 1][0],
2,
element.get_text(parent, 2).rstrip('\t ') + ' '
)
- text = '{{\n{0:s}{1:s}}}\n'.format(text, indent)
+ text = '{{\n{0:s}{1:s}}}\n'.format(text, indent)
element.set_text(node, 0, text)
xml.etree.ElementTree.ElementTree(root).write(
int gram_piece2, gram_piece3;
void gram_piece_insert(int n, const void *str);
+#if 1 /* don't use the macros for C to python translation */
+ static void insert_before(int n, const void *str);
+ static void insert_after(int n, const void *str);
+#else
#define insert_before(n, str) \
do { \
gram_piece_insert(gram_piece2 + (n) * 2, (str)); \
++gram_piece0; \
++gram_piece3; \
} while (0)
+#endif
/* Nick extra rules for element groups */
int nested_rhs = 0;
free (current_lhs_named_ref);
current_lhs_named_ref = ref;
}
+
+#if 1 /* don't use the macros for C to python translation */
+static void insert_before(int n, const void *str) {
+ gram_piece_insert(gram_piece2 + n * 2, str);
+ ++gram_piece0;
+ ++gram_piece3;
+}
+
+static void insert_after(int n, const void *str) {
+ gram_piece_insert(gram_piece2 + n * 2 + 1, str);
+ ++gram_piece0;
+ ++gram_piece3;
+}
+#endif
import xml.etree.ElementTree
import y_tab
-def my_rstrip(text, indent):
- i = len(text)
- while i > 0 and text[i - 1] == '}':
- i -= 1
- assert i > 0
- while text[i - 1] != '{':
- i -= 1
- assert i > 0
- i -= 1
- return text[:i].rstrip('\t ') + indent + text[i:]
-
def c_to_python(context, text):
lex_yy.yyin = None
lex_yy.yy_buffer_stack = [lex_yy.YYBufferState()]
).getroot()
context = ast.Context()
-#context.translate_identifier['BEGIN'] = 'self.BEGIN'
-#context.translate_identifier['yylval'] = 'ref_data.yylval'
-#context.translate_identifier['yytext'] = 'self.yytext'
-#context.translate_identifier['yy_pop_state'] = 'self.yy_pop_state'
-#context.translate_identifier['yy_push_state'] = 'self.yy_push_state'
actions = []
with open('a.c', 'w') as fout:
0,
'{0:s}\n'.format(element.get_text(i, 0).rstrip())
)
- elif i.tag == 'AST_Section1Or2_CodeProps':
+ elif (
+ i.tag == 'AST_Section1_InitialAction' or
+ i.tag == 'AST_Section1Or2_CodeProps'
+ ):
node = i[0]
assert node.tag == 'AST_Text'
indent = ''