From 430d4e5c74896c561d47451b1ee10a754d8d649e Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Fri, 18 Jan 2019 10:13:58 +1100 Subject: [PATCH] Fine tune whitespace, which node tags are translated, addition of braces, etc --- l_to_python.py | 45 ++++++++++++++++++++++++++++++++++----------- scan_to_l.py | 2 +- tests/parse-gram.y | 19 +++++++++++++++++++ y_to_python.py | 21 ++++----------------- 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/l_to_python.py b/l_to_python.py index c6edbef..8dd8e44 100755 --- a/l_to_python.py +++ b/l_to_python.py @@ -38,11 +38,6 @@ root = xml.etree.ElementTree.parse( ).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: @@ -50,8 +45,11 @@ 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' @@ -71,8 +69,25 @@ with open('a.c', 'w') as fout: 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 @@ -80,7 +95,15 @@ with open('a.c', 'w') as fout: 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], @@ -206,7 +229,7 @@ with open('a.i') as fin: 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( diff --git a/scan_to_l.py b/scan_to_l.py index f35d0dc..4486277 100755 --- a/scan_to_l.py +++ b/scan_to_l.py @@ -3,7 +3,7 @@ import element import sys -root = element.deserialize(sys.stdin()) +root = element.deserialize(sys.stdin) def replace_in_action(i): if i.tag == 'AST_Section2_Rule_Action' and len(i): diff --git a/tests/parse-gram.y b/tests/parse-gram.y index 3e9a67a..eeef2f3 100644 --- a/tests/parse-gram.y +++ b/tests/parse-gram.y @@ -104,6 +104,10 @@ 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)); \ @@ -116,6 +120,7 @@ ++gram_piece0; \ ++gram_piece3; \ } while (0) +#endif /* Nick extra rules for element groups */ int nested_rhs = 0; @@ -1148,3 +1153,17 @@ current_lhs (symbol *sym, location loc, named_ref *ref) 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 diff --git a/y_to_python.py b/y_to_python.py index 97e50bc..78d5494 100755 --- a/y_to_python.py +++ b/y_to_python.py @@ -8,17 +8,6 @@ import sys 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()] @@ -38,11 +27,6 @@ root = xml.etree.ElementTree.parse( ).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: @@ -58,7 +42,10 @@ 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 = '' -- 2.34.1