From 82c5d1965e0c7e6a953edce03029a049a74e1081 Mon Sep 17 00:00:00 2001 From: Aurelio Jargas Date: Fri, 30 Aug 2019 00:15:00 +0200 Subject: [PATCH] Add support for partial comments Scan all commands and move comments to the previous command, if necessary. Details in the docstring. --- sedsed.py | 55 +++++- test/dumpdebug/comment.gnu.sed | 26 --- test/dumpdebug/comment.sed | 42 ----- test/dumpdebug/tex2xml.gnu.sed | 5 - test/html/comment.gnu.sed.html | 8 +- test/html/comment.sed.html | 54 ++---- test/html/tex2xml.gnu.sed.html | 15 +- test/indent/comment.gnu.sed | 8 +- test/indent/comment.sed | 54 ++---- test/indent/tex2xml.gnu.sed | 15 +- test/token/comment.gnu.sed.txt | 52 +----- test/token/comment.sed.txt | 308 +-------------------------------- 12 files changed, 106 insertions(+), 536 deletions(-) diff --git a/sedsed.py b/sedsed.py index 532a8f0..c9a623b 100755 --- a/sedsed.py +++ b/sedsed.py @@ -1066,7 +1066,60 @@ def set_address(gsed_data, sedsed_data, prefix='addr1'): return ret -ZZ = parse(sedscript) +def fix_partial_comments(commands): + """ + Scan all commands and move comments to the previous command, if necessary. + + If there's only one command in the line, and a comment at the end, being it + preceded by a ';' or not, this comment will be "tied" to the command. + Examples: + + /foo/ d # remove foo + /bar/ d ;# remove bar + + In both cases, the comment will be moved to the 'comment' field of + the respective 'd' command. The --indent output will be: + + /foo/ d ;# remove foo + /bar/ d ;# remove bar + """ + headers = commands[0] + data = commands[1:] + + accept_comment = sedcmds['solo'] + sedcmds['block'] + sedcmds['jump'] + sedcmds['multi'] + + fake = { 'linenr': 0 } + data.insert(0, fake) # because of i-2 + data.append(fake) # because of i+1 + + # i==0 skip: it's the fake + # i==1 skip: first command (nothing previous to append) + # i==2 good: first possible partial comment + # last skip: it's the fake + i = 2 + while i < len(data) - 1: + # Move solo comment into previous command as partial comment when... + if (data[i]['id'] == '#' + # ...previous command accepts comments + and data[i - 1]['id'] in accept_comment + + # ...there's only *one* previous command in the same source line + and data[i]['linenr'] != data[i - 2]['linenr'] + and data[i]['linenr'] == data[i - 1]['linenr'] + and data[i]['linenr'] != data[i + 1]['linenr']): + + # Move solo comment to previous command + data[i - 1]['comment'] = data[i]['comment'] + del data[i] + # Since we're removing, 'i' won't be incremented + else: + i += 1 + + return [headers] + data[1:-1] # remove fakes + +# Parse the script and process/fix the resulting data. +# ZZ is sedsed's internal data structure for a sed script. +ZZ = fix_partial_comments(parse(sedscript)) # Now the ZZ list is full. diff --git a/test/dumpdebug/comment.gnu.sed b/test/dumpdebug/comment.gnu.sed index 50952eb..5639022 100644 --- a/test/dumpdebug/comment.gnu.sed +++ b/test/dumpdebug/comment.gnu.sed @@ -14,34 +14,8 @@ COMM:/foo/ { #-------------------------------------------------- /foo/ { -# c1 - i\ -COMM:c\\\\N1 -#-------------------------------------------------- -c\ -1 # foo;bar #; - s/^/PATT:/ - l - s/^PATT:// - x - s/^/HOLD:/ - l - s/^HOLD:// - x - i\ -COMM:p -#-------------------------------------------------- -p - s/^/PATT:/ - l - s/^PATT:// - x - s/^/HOLD:/ - l - s/^HOLD:// - x i\ COMM:p #-------------------------------------------------- diff --git a/test/dumpdebug/comment.sed b/test/dumpdebug/comment.sed index 875cdfa..2b4ac7e 100644 --- a/test/dumpdebug/comment.sed +++ b/test/dumpdebug/comment.sed @@ -13,40 +13,12 @@ COMM:/foo/ { #-------------------------------------------------- /foo/ { -# c1 - i\ -COMM:c\\\\N1 - allowed without -#-------------------------------------------------- -c\ -1 - allowed without # foo;bar #; - s/^/PATT:/ - l - s/^PATT:// - x - s/^/HOLD:/ - l - s/^HOLD:// - x - i\ -COMM:p -#-------------------------------------------------- -p -#p1 - s/^/PATT:/ - l - s/^PATT:// - x - s/^/HOLD:/ - l - s/^HOLD:// - x i\ COMM:p #-------------------------------------------------- p -# p2 s/^/PATT:/ l s/^PATT:// @@ -59,7 +31,6 @@ p COMM:p #-------------------------------------------------- p -### p3 s/^/PATT:/ l s/^PATT:// @@ -72,7 +43,6 @@ p COMM:p #-------------------------------------------------- p -# p4 s/^/PATT:/ l s/^PATT:// @@ -97,7 +67,6 @@ p COMM:p #-------------------------------------------------- p -# p5 s/^/PATT:/ l s/^PATT:// @@ -110,7 +79,6 @@ p COMM:s/a/b/ #-------------------------------------------------- s/a/b/ -#s1 s/^/PATT:/ l s/^PATT:// @@ -123,7 +91,6 @@ s/a/b/ COMM:s/a/b/ #-------------------------------------------------- s/a/b/ -# s2 s/^/PATT:/ l s/^PATT:// @@ -136,7 +103,6 @@ s/a/b/ COMM:s/a/b/gp #-------------------------------------------------- s/a/b/gp -#s3 s/^/PATT:/ l s/^PATT:// @@ -149,7 +115,6 @@ s/a/b/gp COMM:s/a/b/gp #-------------------------------------------------- s/a/b/gp -# s4 s/^/PATT:/ l s/^PATT:// @@ -162,27 +127,22 @@ s/a/b/gp COMM::foo #-------------------------------------------------- :foo -#l1 i\ COMM::foo #-------------------------------------------------- :foo -# l2 i\ COMM:b foo #-------------------------------------------------- b foo -#b1 i\ COMM:b foo #-------------------------------------------------- b foo -# b2 i\ COMM:b #-------------------------------------------------- b -#b3 s/^/PATT:/ l s/^PATT:// @@ -195,7 +155,6 @@ b COMM:b #-------------------------------------------------- b -# b4 s/^/PATT:/ l s/^PATT:// @@ -208,7 +167,6 @@ b COMM:} #-------------------------------------------------- } -# c2 # s/^/PATT:/ l diff --git a/test/dumpdebug/tex2xml.gnu.sed b/test/dumpdebug/tex2xml.gnu.sed index 2802a75..cfc3d5b 100644 --- a/test/dumpdebug/tex2xml.gnu.sed +++ b/test/dumpdebug/tex2xml.gnu.sed @@ -119,7 +119,6 @@ s,\( *\)\([^|<>}{ ]*\){,\1\ COMM:H #-------------------------------------------------- H -# append to holdspace s/^/PATT:/ l s/^PATT:// @@ -132,7 +131,6 @@ H COMM:s,\\n\\([^\\n]*\\)\\n,<\\1>, #-------------------------------------------------- s,\n\([^\n]*\)\n,<\1>, -# generate XML tag # Holdspace: ..\tagN \n text \n newtag \n text # We only want oldtags + newtag s/^/PATT:/ @@ -255,7 +253,6 @@ x COMM:s,\\n[^\\n]*$,, #-------------------------------------------------- s,\n[^\n]*$,, -# delete tag from holdspace s/^/PATT:/ l s/^PATT:// @@ -280,12 +277,10 @@ x COMM:/^[^}]*{/ b open #-------------------------------------------------- /^[^}]*{/ b open -# if next bracket is an open one i\ COMM:/}/ b close #-------------------------------------------------- /}/ b close -# another one? i\ COMM:} #-------------------------------------------------- diff --git a/test/html/comment.gnu.sed.html b/test/html/comment.gnu.sed.html index 574c3f1..99384e2 100644 --- a/test/html/comment.gnu.sed.html +++ b/test/html/comment.gnu.sed.html @@ -8,17 +8,13 @@ ### foo #foo # -/foo/ {;# c1 - # c1 - c\ -1 +/foo/ {;# c1;c1 # foo;bar #; p;#p1 p;# p2 p;### p3 - p;# p4 - p + p;# p4;p4 p;# p5 s/a/b/;#s1 s/a/b/;# s2 diff --git a/test/html/comment.sed.html b/test/html/comment.sed.html index d71b6b1..6a46636 100644 --- a/test/html/comment.sed.html +++ b/test/html/comment.sed.html @@ -7,45 +7,25 @@ ### foo #foo # -/foo/ {;# c1 - # c1 - c\ -1 - allowed without +/foo/ {;# c1;c1 - allowed without ; # foo;bar #; - p - #p1 - p - # p2 - p - ### p3 - p - # p4 - p - p - # p5 - s/a/b/ - #s1 - s/a/b/ - # s2 - s/a/b/gp - #s3 - s/a/b/gp - # s4 - :foo - #l1 - :foo - # l2 - b foo - #b1 - b foo - # b2 - b - #b3 - b - # b4 -} -# c2 + p;#p1 + p;# p2 + p;### p3 + p;# p4;p4 + p;# p5 + s/a/b/;#s1 + s/a/b/;# s2 + s/a/b/gp;#s3 + s/a/b/gp;# s4 + :foo;#l1 + :foo;# l2 + b foo;#b1 + b foo;# b2 + b ;#b3 + b ;# b4 +} ;# c2 # ### colorized by sedsed, a debugger and code formatter for sed scripts diff --git a/test/html/tex2xml.gnu.sed.html b/test/html/tex2xml.gnu.sed.html index cd12f21..ea43e9b 100644 --- a/test/html/tex2xml.gnu.sed.html +++ b/test/html/tex2xml.gnu.sed.html @@ -46,10 +46,8 @@ , # Isolate tag # Patternspace: text \n newtag \n text - H - # append to holdspace - s,\n\([^\n]*\)\n,<\1>, - # generate XML tag + H;# append to holdspace + s,\n\([^\n]*\)\n,<\1>,;# generate XML tag # Holdspace: ..\tagN \n text \n newtag \n text # We only want oldtags + newtag @@ -72,14 +70,11 @@ G s,\n\n\n\([^\n]*\)\n.*\n\([^\n]*\)$,</\2>\1, x - s,\n[^\n]*$,, - # delete tag from holdspace + s,\n[^\n]*$,,;# delete tag from holdspace x - /^[^}]*{/ b open - # if next bracket is an open one - /}/ b close - # another one? + /^[^}]*{/ b open;# if next bracket is an open one + /}/ b close;# another one? } :unescape diff --git a/test/indent/comment.gnu.sed b/test/indent/comment.gnu.sed index 71641f0..08ffdd0 100644 --- a/test/indent/comment.gnu.sed +++ b/test/indent/comment.gnu.sed @@ -2,17 +2,13 @@ ### foo #foo # -/foo/ { ;# c1 - # c1 - c\ -1 +/foo/ { ;# c1;c1 # foo;bar #; p ;#p1 p ;# p2 p ;### p3 - p ;# p4 - p + p ;# p4;p4 p ;# p5 s/a/b/ ;#s1 s/a/b/ ;# s2 diff --git a/test/indent/comment.sed b/test/indent/comment.sed index ba81354..bff93af 100644 --- a/test/indent/comment.sed +++ b/test/indent/comment.sed @@ -1,43 +1,23 @@ ### foo #foo # -/foo/ { ;# c1 - # c1 - c\ -1 - allowed without +/foo/ { ;# c1;c1 - allowed without ; # foo;bar #; - p - #p1 - p - # p2 - p - ### p3 - p - # p4 - p - p - # p5 - s/a/b/ - #s1 - s/a/b/ - # s2 - s/a/b/gp - #s3 - s/a/b/gp - # s4 - :foo - #l1 - :foo - # l2 - b foo - #b1 - b foo - # b2 - b - #b3 - b - # b4 -} -# c2 + p ;#p1 + p ;# p2 + p ;### p3 + p ;# p4;p4 + p ;# p5 + s/a/b/ ;#s1 + s/a/b/ ;# s2 + s/a/b/gp ;#s3 + s/a/b/gp ;# s4 + :foo ;#l1 + :foo ;# l2 + b foo ;#b1 + b foo ;# b2 + b ;#b3 + b ;# b4 +} ;# c2 # diff --git a/test/indent/tex2xml.gnu.sed b/test/indent/tex2xml.gnu.sed index 6b01cc9..df3d1cc 100644 --- a/test/indent/tex2xml.gnu.sed +++ b/test/indent/tex2xml.gnu.sed @@ -40,10 +40,8 @@ s,\\},&cbrace;,g , # Isolate tag # Patternspace: text \n newtag \n text - H - # append to holdspace - s,\n\([^\n]*\)\n,<\1>, - # generate XML tag + H ;# append to holdspace + s,\n\([^\n]*\)\n,<\1>, ;# generate XML tag # Holdspace: ..\tagN \n text \n newtag \n text # We only want oldtags + newtag @@ -66,14 +64,11 @@ s,\\},&cbrace;,g G s,\n\n\n\([^\n]*\)\n.*\n\([^\n]*\)$,\1, x - s,\n[^\n]*$,, - # delete tag from holdspace + s,\n[^\n]*$,, ;# delete tag from holdspace x - /^[^}]*{/ b open - # if next bracket is an open one - /}/ b close - # another one? + /^[^}]*{/ b open ;# if next bracket is an open one + /}/ b close ;# another one? } :unescape diff --git a/test/token/comment.gnu.sed.txt b/test/token/comment.gnu.sed.txt index 24b2fe8..c0f3635 100644 --- a/test/token/comment.gnu.sed.txt +++ b/test/token/comment.gnu.sed.txt @@ -76,39 +76,7 @@ replace: flag: extrainfo: - comment:# c1 - - linenr:5 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: - extrainfo: - comment:# c1 - - linenr:5 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:c - content:\@#linesep#@1 - delimiter: - pattern: - replace: - flag: - extrainfo: - comment: + comment:# c1;c1 linenr:6 addr1: @@ -204,23 +172,7 @@ replace: flag: extrainfo: - comment:# p4 - - linenr:11 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:p - content: - delimiter: - pattern: - replace: - flag: - extrainfo: - comment: + comment:# p4;p4 linenr:12 addr1: diff --git a/test/token/comment.sed.txt b/test/token/comment.sed.txt index a4c6f67..a0b87c4 100644 --- a/test/token/comment.sed.txt +++ b/test/token/comment.sed.txt @@ -60,39 +60,7 @@ replace: flag: extrainfo: - comment:# c1 - - linenr:4 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: - extrainfo: - comment:# c1 - - linenr:4 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:c - content:\@#linesep#@1 - allowed without - delimiter: - pattern: - replace: - flag: - extrainfo: - comment: + comment:# c1;c1 - allowed without ; linenr:5 addr1: @@ -139,22 +107,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:7 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#p1 @@ -171,22 +123,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:8 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# p2 @@ -203,22 +139,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:9 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:### p3 @@ -236,39 +156,7 @@ replace: flag: extrainfo: - comment: - - linenr:10 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: - extrainfo: - comment:# p4 - - linenr:10 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:p - content: - delimiter: - pattern: - replace: - flag: - extrainfo: - comment: + comment:# p4;p4 linenr:11 addr1: @@ -283,22 +171,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:11 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# p5 @@ -315,22 +187,6 @@ pattern:a replace:b flag: - extrainfo: - comment: - - linenr:12 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#s1 @@ -347,22 +203,6 @@ pattern:a replace:b flag: - extrainfo: - comment: - - linenr:13 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# s2 @@ -379,22 +219,6 @@ pattern:a replace:b flag:gp - extrainfo: - comment: - - linenr:14 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#s3 @@ -411,22 +235,6 @@ pattern:a replace:b flag:gp - extrainfo: - comment: - - linenr:15 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# s4 @@ -443,22 +251,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:16 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#l1 @@ -475,22 +267,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:17 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# l2 @@ -507,22 +283,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:18 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#b1 @@ -539,22 +299,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:19 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# b2 @@ -571,22 +315,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:20 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:#b3 @@ -603,22 +331,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:21 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# b4 @@ -635,22 +347,6 @@ pattern: replace: flag: - extrainfo: - comment: - - linenr:22 - addr1: - addr1flag: - addr2: - addr2flag: - lastaddr: - modifier: - id:# - content: - delimiter: - pattern: - replace: - flag: extrainfo: comment:# c2