Home > Archive > AWK > February 2007 > Parsing Nested Curly Brackets
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Parsing Nested Curly Brackets
|
|
| Alfred 2007-02-04, 6:57 pm |
| Let's say I want to convert:
Utils {
test1 {
}
test2 {
}
}
Bunnies {
}
into:
Utils$$test1 {
}
Utils$$test2 {
}
Bunnies {
}
What's the awk way to do this? (Note I added the silly "Bunnies {}" in
just to see if your example answer will not munge up any following
text.)
| |
| Janis Papanagnou 2007-02-04, 6:57 pm |
| Alfred wrote:
> Let's say I want to convert:
>
> Utils {
> test1 {
> }
> test2 {
> }
> }
> Bunnies {
> }
>
> into:
>
> Utils$$test1 {
> }
> Utils$$test2 {
> }
> Bunnies {
> }
>
> What's the awk way to do this?
Use of recursive functions, as in other programming languages.
Janis
> (Note I added the silly "Bunnies {}" in
> just to see if your example answer will not munge up any following
> text.)
>
| |
| Alfred 2007-02-04, 6:57 pm |
| > Use of recursive functions, as in other programming languages.
>
> Janis
Oh, I made a mistake, this example was wrong. I want to write an awk
script to translate:
namespace Utils {
function test1 {
}
function test2 {
}
}
function Bunnies {
}
into:
function Utils$$test1 {
}
function Utils$$test2 {
}
function Bunnies {
}
So essentially the pseudocode would sort of be like:
1. Look for keyword 'namespace' and store in a variable the keyword
that follows it --> Utils. Store this in $name.
2. Start copying text from the point of the first { that follows
namespace until the last mismatched } is found, and store this in a
variable $temp.
3. Iterate through all the text in $temp, looking for first keyword
that follows 'function', and prepend $name + '$$' + keyword into
$temp.
4. Repeat steps 1-3 until the end of the file.
5. Now go through the file again and remove all 'namespace <keyword>
{' and their ending '}'. The ending } is found by looking for the
first mismatched }.
6. Return the result.
| |
| Ed Morton 2007-02-05, 3:57 am |
| Alfred wrote:
>
>
> Oh, I made a mistake, this example was wrong. I want to write an awk
> script to translate:
>
> namespace Utils {
> function test1 {
> }
> function test2 {
> }
> }
> function Bunnies {
> }
>
> into:
>
> function Utils$$test1 {
> }
> function Utils$$test2 {
> }
> function Bunnies {
> }
>
> So essentially the pseudocode would sort of be like:
>
> 1. Look for keyword 'namespace' and store in a variable the keyword
> that follows it --> Utils. Store this in $name.
> 2. Start copying text from the point of the first { that follows
> namespace until the last mismatched } is found, and store this in a
> variable $temp.
> 3. Iterate through all the text in $temp, looking for first keyword
> that follows 'function', and prepend $name + '$$' + keyword into
> $temp.
> 4. Repeat steps 1-3 until the end of the file.
> 5. Now go through the file again and remove all 'namespace <keyword>
> {' and their ending '}'. The ending } is found by looking for the
> first mismatched }.
> 6. Return the result.
awk '/namespace/{ns=$2} /function/,/}/{ if (/function/) $2=ns"$$"$2;
print}' file
Ed.
| |
| Alfred 2007-02-05, 3:57 am |
| > awk '/namespace/{ns=$2} /function/,/}/{ if (/function/) $2=ns"$$"$2;
> print}' file
>
> Ed Morton
Dude that's like freaking awesome!!! It works fantastic. This is one
to put in my KB for sure and use on other projects.
| |
| Alfred 2007-02-05, 3:57 am |
| On Feb 5, 12:48 am, "Alfred" <9...@myway.com> wrote:
>
>
> Dude that's like freaking awesome!!! It works fantastic. This is one
> to put in my KB for sure and use on other projects.
Hey, Ed, thanks. I must be tired, though, because I typed that last
statement too fast. When I run your example, the "function Bunnies"
gets converted into "function Utils$$Bunnies" and I'm needing to
prevent that. I appreciate your taking the time to look into this,
though.
Perhaps this is not humanly possible to do. Perhaps I need to make the
conversion from:
library Utils {
function test1() {
}
function test2() {
}
} Utils;
function Main() {
}
into
function Utils$$test1() {
}
function Utils$$test2() {
}
function Main() {
}
BTW, -- what am I doing? I'm trying to create a kind of pre-processor
to Javascript that adds a few extra features and functions, taking as
many shortcuts as I can. In the above example, I can permit a call
like Utils.test1() inside main, translate it in the pre-processor to
Utils$$test1(), and therefore give a kind of grouping structure to
Javascript functions.
I'm doing this because yacc and lex seem human adverse when I pour
over mountains of docs about it on the web. I sort of understand sed
and awk, though.
| |
| Ed Morton 2007-02-05, 7:57 am |
| Alfred wrote:
> On Feb 5, 12:48 am, "Alfred" <9...@myway.com> wrote:
>
>
>
> Hey, Ed, thanks. I must be tired, though, because I typed that last
> statement too fast. When I run your example, the "function Bunnies"
> gets converted into "function Utils$$Bunnies" and I'm needing to
> prevent that.
awk '/function/,/}/{ if (/function/) $2=ns$2; print;
next}/namespace/{ns=$2"$$"}/}/{ns=""}' file
Ed.
|
|
|
|
|