ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wonder if you have a large string in Perl, for example this:
Code:
stuff
keyword morestuff { blah{
blah{}
}blah
} test
end
Is it possible to make it extract everything from the beginning of the sequence "keyword" until the matching brace to the first left brace after "keyword"?
For example, it should extract this from the above example:
Code:
keyword morestuff { blah{
blah{}
}blah
}
And is is possible to pass this section to a function, where it is processed, and then to replace it with the function's returned string?
I wonder if you have a large string in Perl, for example this:
Code:
stuff
keyword morestuff { blah{
blah{}
}blah
} test
end
Is it possible to make it extract everything from the beginning of the sequence "keyword" until the matching brace to the first left brace after "keyword"?
For example, it should extract this from the above example:
Code:
keyword morestuff { blah{
blah{}
}blah
}
And is is possible to pass this section to a function, where it is processed, and then to replace it with the function's returned string?
I once wrote a simple elegant parser specifically for nested pairs of {} ; I hope the aboveText::Balanced is a better solution. If not, I'll describe my idea.
No, it's for a silly little project I am doing that adds classes and polymorphism tc C using a simple Perl search-and-replace script. It actually kind of works now but the syntax is really terrible and I thought that using curly braces as the block delimiter and semicolons insted of newlines as separators would make it fit in much nicer.
Basically I want the syntax to be:
Code:
class ClassName SuperClass1 SuperClass2 ... {
int var;
char str;
void method();
int anotherMethod(int a) {
return a + this.var;
}
}
No, it's for a silly little project I am doing that adds classes and polymorphism tc C using a simple Perl search-and-replace script. It actually kind of works now but the syntax is really terrible and I thought that using curly braces as the block delimiter and semicolons insted of newlines as separators would make it fit in much nicer.
Basically I want the syntax to be:
Code:
class ClassName SuperClass1 SuperClass2 ... {
int var;
char str;
void method();
int anotherMethod(int a) {
return a + this.var;
}
}
Oh, it looks like you're in trouble: think of the following:
Code:
char *s = "a nasty string with ; inside";
- what will your parser do with the ';' inside ? Or it won't delve into strings ? I.e. are you writing a partial parser ?
But still:
Code:
char *s = "a nasty string with { ... } inside";
I.e. the robust parser should:
get rid of comments (and there should be a possibility to restore them);
temporarily get rid of strings
.
My point is that to make a half-hearted parser robust one should make it more than just half hearted .
... I have already described here in part my PerlPreProcessor - have a look at it. It can help you a lot with text substitution, it can cope with metadata, unlike C++ template it is stateful, i.e. one can pass data between templates - because it's all in Perl.
And no new language is invented - in your case it will be pure "C" + pure Perl. The only new entity is simple reserved comments - like
This piece of code tells which functions to import from Text::Balanced. You most likely will need 'extract_bracketed', and maybe 'extract_codeblock' - the latter one may help dealing with "nasty" strings.
Just copy the above piece - it will import all the functions, you may need some of them later.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.