Sorted it out in AWK I think (thanks for pointing me towards it, worked out how to use it after a couple of mins):
Code:
awk 'BEGIN{LVL=0; INFN=0} /function/ {INFN=1; if(LVL==1) sub(/function/, "(&");} /{/ {LVL++} /}/ {LVL--; if(LVL==1) sub(/}/, "&)"); if(LVL==1) INFN=0;}1' $file If you spot any problems with this please let me know. [EDIT] Spotted a problem, corrected below. Seems to have worked, thanks guys. Code:
#! /bin/bash |
Odd. Both of the scripts you presented does not seem to be working.
Code:
awk 'BEGIN{LVL=0; INFN=0} /function/ {INFN=1; if(LVL==1) sub(/function/, "(&");} /{/ {LVL++} /}/ {LVL--; if(LVL==1) sub(/}/, "&)"); if(LVL==1) INFN=0;}1' ... |
Quote:
Script: Code:
#! /bin/bash Code:
/** Code:
/** |
Whatever you put between the // is what will be looked for, so if you had:
Code:
someName: function someName... } Code:
awk 'BEGIN{RS=""}/^someName:/{sub(/function/,"(&");$NF=$NF ")"}1' file |
Quote:
|
@gfarrell Ok the code seems to work fine only that it still doesn't work on lines that contains multiple }'s. Before I really had an idea that it could also be done in awk but this was really the limit that I was expecting.
@grail There's a problem in the RS="" method if a section contains blank lines within. |
Quote:
(Not that I know how to fix it (or really need to now)). The other problem I got was in comment doc-blocks but it was largely not a problem. |
So using the data you provided, the following worked but needed to exceptions:
Code:
awk 'BEGIN{RS="";ORS="\n\n"}/: function/{sub(/function/,"(&");sub(/},$/,"}),")}1' file 1. The input although it has some lines that appear blank they actually contain whitespace. So I had to remove those through vim prior to running 2. The enhance function is not terminated the same as the others, ie it does not finish with }, and so would need to be changed manually. I will see if I can come up with a more full solution tomorrow as it's 2am and I am tired :( |
Just ran yours to and found that it supplies an extra round closing bracket at the end of your $UI function
|
Quote:
|
Ok here are just sad some points. I'm sorry if I have to tell you these.
I already had same problem with my own purpose but I just gave it up and started to think about letting it get solved in other languages... like perl or parrot. Awk really have its limits. It's not only about counting the total braces then making a deduction in every recursion. It's also about knowing if the braces are just part of an ordinary string or not or part of ... etc. Also, what if there are 4 braces in a line, 3 of them is part of the current function but the 4th is part of the container block holding the function. How can you tell that it's part of the container block since you're only in the context of the function? Probably this can still be solved but that would only mean imitating a real language parser. Still doing that in awk IMO is really no longer practical. e.g. reading single chars and not phrases or lines since you can't tell when do compound statements or blocks ends or separates.. etc. P.S. Maybe using another script that's similar to HTML TIDY for awk scripts then using your methods will do the trick. |
Quote:
I hope you manage to work out the problems you were encountering but for me it's done its job. |
Ok I am really going this time, but I did run your several times and was unable to get it to not print the extra bracket :(
This is a little untidy due to tiredness but seems to work :) You can take any bits that help you ;) Code:
awk '/: function/{sub(/function/,"(function");f=1} |
Well, this conversation really moved beyond me while I was away. Time to bow out, I think.
Quote:
Time to learn more about perl, I guess. :) |
Code:
awk -vRS="}" '/myFunction:/{ |
All times are GMT -5. The time now is 08:13 PM. |