I can't get StringTemplate to indent - indentation

I have the following StringTemplate group
group RPInstr;
before(firstStat) ::= <<
<{<[beforeEnteringInstr(),firstStat]; anchor, separator="\n">}; anchor>
>>
beforeEnteringInstr() ::= "before();"
I am trying to make the first statement of a method to align with the instrumentation code ("before();").
However, what I am getting right now is something like this:
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
What is the proper way to indent before(); and the next statement so I get the following?
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
Thanks in advance.
PS: the options that I am using in my grammar are the following
options {output=template; rewrite=true; backtrack=true; memoize=true;}

the expr in before(firstStat) is not indented. No reason for ST to indent then.

Related

Can someone please explain what the line for(;Q.size();) does?

I am trying to understand a source code and i cannot figure out how the line for(;Q.size();) is meant to work. Could someone please simplify it for me ?
A for statement consists of three parts, separated by semicolons:
an init-statement
a condition
an iteration_expression
A for loop is equivalent to this code:
{
init_statement 
while ( condition ) { 
statement 
iteration_expression ; 
}
}
The init-statement and iteration_expression can be empty, but the semicolons between them are still required.
In your example, for(;Q.size();) would thus be equivalent to:
{
while ( Q.size() ) { 
statement 
}
}
Look at it this way:
for(<do nothing>;Q.size();<do nothing>) {//do something}
Now read the definition of the for loop and see that it fits perfectly.
As mentioned by others, essentially this becomes equivalent to while(Q.size())
It's a for loop which doesn't care about an incrementing index variable. As Blaze pointed out it's equivalent to a while loop.
for(;Q.size();)
{
// do something while Q is not empty
}
or equivalently
while(Q.size())
{
// do something while Q is not empty
}

Want to remove a function from multiple files using Notepad++

I have this 3 functions, looking exactly like this in multiple files, and using Notepad++, Find in Files/Replace in Files:
FirstLine
{
//something here
}
SecondLine
{
//something here
}
ThirthLine
{
//something here
}
I want to remove the second function, the result should be like this:
FirstLine
{
//something here
}
ThirthLine
{
//something here
}
I have tried on regex101.com many times this is the last one i tried:
^.*(SecondLine).*\{.*\}.*$
just doesn't work, :(
This is what I ended up to use:
SecondLine[^\{]*\{[^\}]*\}\r*\n*
Notepad++ threw an Invalid regular expression error before I escaped braces. It seems closing braces should be escaped in a N++ regex to mean a literal } character.
Thank you all for the help!

Generated getters and setters code format

Is there a way to change format how netbeans 8.1 generate code? Instead of having
void setSomething1(bool something1)
{
something1_ = something1;
}
bool getSomething1() const
{
return something1_;
}
I would like to have this code
void setSomething1(bool something1) { something1_ = something1; }
bool getSomething1() const { return something1_; }
and also the set/get prefix could change or disappear
void setSomething1(bool something1) { something1_ = something1; }
bool something1() const { return something1_; }
netbeans only allow me to change braces position, same line or new line, but for this short functions i would like to compress it to one line only. With many setters and getters, the code starts to be confusing.
If netbeans itself cant do it, would it be possible via regular expression or some other workaroud?
Other (long body) functions will remain in the format. There it makes the code better to read.
retType name(args)
{
body;
}
This regex will satisfy the first requirement (Demo):
([\w ]+\s+[gs]et\w+\([\w ]*\)\s*(?:const)?)\s+\{\s+(.*)\s+\}
With $1 { $2 } as replacement
Then this one (note the trailing space):
get(\w+)\(
With $1 (trailing space aslo) as replacement

std::regex_error exception thrown at runtime

Given that this code works:
regex r1{ "fish"s };
smatch m1;
if (regex_search("I love fish and chips"s, m1, r1))
cout << m1[0] << endl;
I believe that VS2015 supports regular expressions. However, initialization of this regular expression object:
regex r{ R"(\d{2,3}(-\d\d) { 2 })" };
throws a std::regex_error exception. What's wrong with the initialization?
So, yeah, as mentioned in the comments:
(\d{2,3}(-\d\d) { 2 })
should be
(\d{2,3}(-\d\d){2})
otherwise the {2} relates to the space instead of the (-\d\d), and other weird things might possibly happen as well…
You have a typo in your regex. Change this:
regex r{ R"(\d{2,3}(-\d\d) { 2 })" };
To:
regex r{ R"(\d{2,3}(-\d\d){2})" };

What does the comma operator do?

What does the following code do in C/C++?
if (blah(), 5) {
//do something
}
Comma operator is applied and the value 5 is used to determine the conditional's true/false.
It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression.
Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious.
If the comma operator is not overloaded, the code is similar to this:
blah();
if (5) {
// do something
}
If the comma operator is overloaded, the result will be based on that function.
#include <iostream>
#include <string>
using namespace std;
string blah()
{
return "blah";
}
bool operator,(const string& key, const int& val) {
return false;
}
int main (int argc, char * const argv[]) {
if (blah(), 5) {
cout << "if block";
} else {
cout << "else block";
}
return 0;
}
(edited to show comma operator overloading scenario. thanks to David Pierre for commenting on this)
I know one thing that this kind of code should do: it should get the coder fired. I would be quite a bit afraid to work next to someone who writes like this.
In the pathological case, it depends on what the comma operator does...
class PlaceHolder
{
};
PlaceHolder Blah() { return PlaceHolder(); }
bool operator,(PlaceHolder, int) { return false; }
if (Blah(), 5)
{
cout << "This will never run.";
}
I would say that depends on blah().
On a more broad answer. The comma operator (non overloaded) resolves as in, execute the first part and return the second part.
So if you have (foo(),bar()) Both functions will be executed, but the value of the expression evaluates to bar() (and the type of the expression as well).
While I won't say there are fair usages for that, is usually considered a bit hard to read code. Mainly because not many languages shares such constructs. So As a personal rule of thumb I avoid it unless I am adding code to a preexistent expression and don't want to change completely its format.
Example: I have a Macro (not discussing if you should use macros or not, sometimes its not even you that wrote it)
FIND_SOMETHING(X) (x>2) ? find_fruits(x) : find_houses(x)
And I usually use it in assignments like my_possession = FIND_SOMETHING(34);
Now I want to add log to it for debuggin purposes but I cannot change the find functions,. I could do :
FIND_SOMETHING(X) (x>2)? (LOG("looking for fruits"),find_fruits(x)):(LOG("looking for houses"),find_houses(x))
I use sometimes constructs like this for debugging purposes. When I force the if close to be true regardless of the return value of blah.
It's obvious that it should never appear in production code.
The following was written assuming it is C code, either in a C file or within a C block of a C++ file:
It is a pointless if. It will call blah(), however the result of blah() is not considered by if at all. The only thing being considered is 5, thus the if will always evaluate to true. IOW you could write this code as
blah();
// do something
without any if at all.