Skip to content

Commit

Permalink
Add support for partial comments
Browse files Browse the repository at this point in the history
Scan all commands and move comments to the previous command,
if necessary. Details in the docstring.
  • Loading branch information
aureliojargas committed Nov 14, 2019
1 parent 961624b commit 82c5d19
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 536 deletions.
55 changes: 54 additions & 1 deletion sedsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 0 additions & 26 deletions test/dumpdebug/comment.gnu.sed
Original file line number Diff line number Diff line change
Expand Up @@ -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
#--------------------------------------------------
Expand Down
42 changes: 0 additions & 42 deletions test/dumpdebug/comment.sed
Original file line number Diff line number Diff line change
Expand Up @@ -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://
Expand All @@ -59,7 +31,6 @@ p
COMM:p
#--------------------------------------------------
p
### p3
s/^/PATT:/
l
s/^PATT://
Expand All @@ -72,7 +43,6 @@ p
COMM:p
#--------------------------------------------------
p
# p4
s/^/PATT:/
l
s/^PATT://
Expand All @@ -97,7 +67,6 @@ p
COMM:p
#--------------------------------------------------
p
# p5
s/^/PATT:/
l
s/^PATT://
Expand All @@ -110,7 +79,6 @@ p
COMM:s/a/b/
#--------------------------------------------------
s/a/b/
#s1
s/^/PATT:/
l
s/^PATT://
Expand All @@ -123,7 +91,6 @@ s/a/b/
COMM:s/a/b/
#--------------------------------------------------
s/a/b/
# s2
s/^/PATT:/
l
s/^PATT://
Expand All @@ -136,7 +103,6 @@ s/a/b/
COMM:s/a/b/gp
#--------------------------------------------------
s/a/b/gp
#s3
s/^/PATT:/
l
s/^PATT://
Expand All @@ -149,7 +115,6 @@ s/a/b/gp
COMM:s/a/b/gp
#--------------------------------------------------
s/a/b/gp
# s4
s/^/PATT:/
l
s/^PATT://
Expand All @@ -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://
Expand All @@ -195,7 +155,6 @@ b
COMM:b
#--------------------------------------------------
b
# b4
s/^/PATT:/
l
s/^PATT://
Expand All @@ -208,7 +167,6 @@ b
COMM:}
#--------------------------------------------------
}
# c2
#
s/^/PATT:/
l
Expand Down
5 changes: 0 additions & 5 deletions test/dumpdebug/tex2xml.gnu.sed
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ s,\( *\)\([^|<>}{ ]*\){,\1\
COMM:H
#--------------------------------------------------
H
# append to holdspace
s/^/PATT:/
l
s/^PATT://
Expand All @@ -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:/
Expand Down Expand Up @@ -255,7 +253,6 @@ x
COMM:s,\\n[^\\n]*$,,
#--------------------------------------------------
s,\n[^\n]*$,,
# delete tag from holdspace
s/^/PATT:/
l
s/^PATT://
Expand All @@ -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:}
#--------------------------------------------------
Expand Down
8 changes: 2 additions & 6 deletions test/html/comment.gnu.sed.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
<font color="#00ffff"><b>### foo</b></font>
<font color="#00ffff"><b>#foo</b></font>
<font color="#00ffff"><b>#</b></font>
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>foo</b></font><font color="#ff6060"><b>/</b></font> <font color="#ff6060"><b>{</b></font>;# c1
<font color="#00ffff"><b># c1</b></font>
<font color="#ffff00"><b>c</b></font><font color="#00ff00"><b>\
</b></font>1
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>foo</b></font><font color="#ff6060"><b>/</b></font> <font color="#ff6060"><b>{</b></font>;# c1;c1
<font color="#00ffff"><b># foo;bar</b></font>
<font color="#00ffff"><b>#;</b></font>
<font color="#ffff00"><b>p</b></font>;#p1
<font color="#ffff00"><b>p</b></font>;# p2
<font color="#ffff00"><b>p</b></font>;### p3
<font color="#ffff00"><b>p</b></font>;# p4
<font color="#ffff00"><b>p</b></font>
<font color="#ffff00"><b>p</b></font>;# p4;p4
<font color="#ffff00"><b>p</b></font>;# p5
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>;#s1
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>;# s2
Expand Down
54 changes: 17 additions & 37 deletions test/html/comment.sed.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,25 @@
<font color="#00ffff"><b>### foo</b></font>
<font color="#00ffff"><b>#foo</b></font>
<font color="#00ffff"><b>#</b></font>
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>foo</b></font><font color="#ff6060"><b>/</b></font> <font color="#ff6060"><b>{</b></font>;# c1
<font color="#00ffff"><b># c1</b></font>
<font color="#ffff00"><b>c</b></font><font color="#00ff00"><b>\
</b></font>1 - allowed without
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>foo</b></font><font color="#ff6060"><b>/</b></font> <font color="#ff6060"><b>{</b></font>;# c1;c1 - allowed without ;
<font color="#00ffff"><b># foo;bar</b></font>
<font color="#00ffff"><b>#;</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#00ffff"><b>#p1</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#00ffff"><b># p2</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#00ffff"><b>### p3</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#00ffff"><b># p4</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#ffff00"><b>p</b></font>
<font color="#00ffff"><b># p5</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>
<font color="#00ffff"><b>#s1</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>
<font color="#00ffff"><b># s2</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font><font color="#00ff00"><b>gp</b></font>
<font color="#00ffff"><b>#s3</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font><font color="#00ff00"><b>gp</b></font>
<font color="#00ffff"><b># s4</b></font>
<font color="#ff00ff"><b>:</b></font><font color="#ff00ff"><b><a name="foo">foo</a></b></font>
<font color="#00ffff"><b>#l1</b></font>
<font color="#ff00ff"><b>:</b></font><font color="#ff00ff"><b><a name="foo">foo</a></b></font>
<font color="#00ffff"><b># l2</b></font>
<font color="#ffff00"><b>b</b></font> <a href="#foo">foo</a>
<font color="#00ffff"><b>#b1</b></font>
<font color="#ffff00"><b>b</b></font> <a href="#foo">foo</a>
<font color="#00ffff"><b># b2</b></font>
<font color="#ffff00"><b>b</b></font>
<font color="#00ffff"><b>#b3</b></font>
<font color="#ffff00"><b>b</b></font>
<font color="#00ffff"><b># b4</b></font>
<font color="#ff6060"><b>}</b></font>
<font color="#00ffff"><b># c2</b></font>
<font color="#ffff00"><b>p</b></font>;#p1
<font color="#ffff00"><b>p</b></font>;# p2
<font color="#ffff00"><b>p</b></font>;### p3
<font color="#ffff00"><b>p</b></font>;# p4;p4
<font color="#ffff00"><b>p</b></font>;# p5
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>;#s1
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font>;# s2
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font><font color="#00ff00"><b>gp</b></font>;#s3
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>a</b></font><font color="#ff6060"><b>/</b></font>b<font color="#ff6060"><b>/</b></font><font color="#00ff00"><b>gp</b></font>;# s4
<font color="#ff00ff"><b>:</b></font><font color="#ff00ff"><b><a name="foo">foo</a></b></font>;#l1
<font color="#ff00ff"><b>:</b></font><font color="#ff00ff"><b><a name="foo">foo</a></b></font>;# l2
<font color="#ffff00"><b>b</b></font> <a href="#foo">foo</a>;#b1
<font color="#ffff00"><b>b</b></font> <a href="#foo">foo</a>;# b2
<font color="#ffff00"><b>b</b></font> ;#b3
<font color="#ffff00"><b>b</b></font> ;# b4
<font color="#ff6060"><b>}</b></font> ;# c2
<font color="#00ffff"><b>#</b></font>

<font color="#00ffff"><b>### colorized by <a href="https://aurelio.net/projects/sedsed/">sedsed</a>, a debugger and code formatter for sed scripts</b></font>
Expand Down
15 changes: 5 additions & 10 deletions test/html/tex2xml.gnu.sed.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@
</b></font><font color="#ff6060"><b>,</b></font>
<font color="#00ffff"><b># Isolate tag</b></font>
<font color="#00ffff"><b># Patternspace: text \n newtag \n text</b></font>
<font color="#ffff00"><b>H</b></font>
<font color="#00ffff"><b># append to holdspace</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>,</b></font><font color="#8080ff"><b>\n\<font color="#ff00ff"><b>(</b></font>[^\n]*\)\n</b></font><font color="#ff6060"><b>,</b></font>&lt;<font color="#00ff00"><b>\1</b></font>&gt;<font color="#ff6060"><b>,</b></font>
<font color="#00ffff"><b># generate XML tag</b></font>
<font color="#ffff00"><b>H</b></font>;# append to holdspace
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>,</b></font><font color="#8080ff"><b>\n\<font color="#ff00ff"><b>(</b></font>[^\n]*\)\n</b></font><font color="#ff6060"><b>,</b></font>&lt;<font color="#00ff00"><b>\1</b></font>&gt;<font color="#ff6060"><b>,</b></font>;# generate XML tag

<font color="#00ffff"><b># Holdspace: ..\tagN \n text \n newtag \n text</b></font>
<font color="#00ffff"><b># We only want oldtags + newtag</b></font>
Expand All @@ -72,14 +70,11 @@
<font color="#ffff00"><b>G</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>,</b></font><font color="#8080ff"><b>\n\n\n\<font color="#ff00ff"><b>(</b></font>[^\n]*\)\n.*\n\<font color="#ff00ff"><b>(</b></font>[^\n]*\)$</b></font><font color="#ff6060"><b>,</b></font>&lt;/<font color="#00ff00"><b>\2</b></font>&gt;<font color="#00ff00"><b>\1</b></font><font color="#ff6060"><b>,</b></font>
<font color="#ffff00"><b>x</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>,</b></font><font color="#8080ff"><b>\n[^\n]*$</b></font><font color="#ff6060"><b>,</b></font><font color="#ff6060"><b>,</b></font>
<font color="#00ffff"><b># delete tag from holdspace</b></font>
<font color="#ffff00"><b>s</b></font><font color="#ff6060"><b>,</b></font><font color="#8080ff"><b>\n[^\n]*$</b></font><font color="#ff6060"><b>,</b></font><font color="#ff6060"><b>,</b></font>;# delete tag from holdspace
<font color="#ffff00"><b>x</b></font>

<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>^[^}]*{</b></font><font color="#ff6060"><b>/</b></font> <font color="#ffff00"><b>b</b></font> <a href="#open">open</a>
<font color="#00ffff"><b># if next bracket is an open one</b></font>
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>}</b></font><font color="#ff6060"><b>/</b></font> <font color="#ffff00"><b>b</b></font> <a href="#close">close</a>
<font color="#00ffff"><b># another one?</b></font>
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>^[^}]*{</b></font><font color="#ff6060"><b>/</b></font> <font color="#ffff00"><b>b</b></font> <a href="#open">open</a>;# if next bracket is an open one
<font color="#ff6060"><b>/</b></font><font color="#8080ff"><b>}</b></font><font color="#ff6060"><b>/</b></font> <font color="#ffff00"><b>b</b></font> <a href="#close">close</a>;# another one?
<font color="#ff6060"><b>}</b></font>

<font color="#ff00ff"><b>:</b></font><font color="#ff00ff"><b><a name="unescape">unescape</a></b></font>
Expand Down
Loading

0 comments on commit 82c5d19

Please sign in to comment.